diff --git a/README.md b/README.md index da0710c964c..1d130345e90 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ This repository holds Node.js samples used throughout [cloud.google.com](). * [Google Cloud Functions](#google-cloud-functions) * [Google Cloud Logging](#google-cloud-logging) * [Google Cloud Pub/Sub](#google-cloud-pubsub) +* [Google Cloud Speech](#google-cloud-speech) * [Google Cloud Storage](#google-cloud-storage) * [Google Cloud Vision](#google-cloud-vision) * [Google Prediction API](#google-prediction-api) @@ -103,6 +104,10 @@ __Other Examples__ - Subscriber/Publisher sample - [Source code][pubsub_subscriber_1] | [Documentation][pubsub_subscriber_2] - IAM sample - [Source code][pubsub_iam_1] | [Documentation][pubsub_iam_2] +## Google Cloud Speech + +- Recognition sample - [Source code][speech_1] | [Documentation][speech_2] + ## Google Cloud Storage - Auth sample - [Source code][storage_1] | [Documentation][storage_2] @@ -309,6 +314,9 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma [pubsub_iam_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/pubsub/iam.js [pubsub_iam_2]: https://cloud.google.com/pubsub/access_control +[speech_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/speech/recognize.js +[speech_2]: https://cloud.google.com/speech + [storage_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/storage/authSample.js [storage_2]: https://cloud.google.com/storage/docs/authentication#acd-examples diff --git a/package.json b/package.json index 3e62c5a3301..5281ff185f2 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "report-html": "nyc report --reporter=html", "deps_gce": "cd computeengine; npm i; cd ../", "deps_bigquery": "cd bigquery; npm i; cd ../", + "deps_speech": "cd speech; npm i; cd ../", "deps_datastore": "cd datastore; npm i; cd ../", "deps_pubsub": "cd pubsub; npm i; cd ../", "deps_monitoring": "cd monitoring; npm i; cd ../", @@ -42,7 +43,7 @@ "deps_vision": "cd vision; npm i; cd ../", "deps_functions": "cd functions/uuid; npm i; cd ../..", "pretest_geddy": "cd appengine/geddy; npm i geddy; GEDDY_SECRET=config/secrets.json; [[ -f $GEDDY_SECRET ]] || echo '{}' > $GEDDY_SECRET && node node_modules/.bin/geddy gen secret; cd ../..;", - "pretest": "npm run deps_vision; npm run deps_gce; npm run deps_bigquery; npm run deps_datastore; npm run deps_monitoring; npm run deps_storage; npm run deps_pubsub; npm run deps_prediction; npm run deps_logging; npm run deps_functions; npm run pretest_geddy", + "pretest": "npm run deps_vision; npm run deps_gce; npm run deps_bigquery; npm run deps_datastore; npm run deps_monitoring; npm run deps_storage; npm run deps_pubsub; npm run deps_prediction; npm run deps_logging; npm run deps_functions; npm run deps_speech; npm run pretest_geddy;", "test": "npm run jshint && npm run cover" }, "ava": { diff --git a/speech/README.md b/speech/README.md new file mode 100644 index 00000000000..9cd2ce7e1d5 --- /dev/null +++ b/speech/README.md @@ -0,0 +1,23 @@ +## Cloud Speech API samples + +These samples require two environment variables to be set: + +- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can +download one from your Google project's "credentials" page. +- `GCLOUD_PROJECT` - ID of your Google project. + +See [gcloud-node authentication][auth] for more details. + +[auth]: https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication + +## Run a sample + +Install dependencies first: + + npm install + +### Recognition sample + +Execute the sample: + + node recognize "/path/to/audio.file" diff --git a/speech/package.json b/speech/package.json new file mode 100644 index 00000000000..51a0432f6bc --- /dev/null +++ b/speech/package.json @@ -0,0 +1,20 @@ +{ + "name": "nodejs-docs-samples-speech", + "description": "Node.js samples for Google Cloud Speech API.", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "author": "Google Inc.", + "engines": { + "node": ">=0.10.x" + }, + "scripts": { + "recognize": "node recognize" + }, + "dependencies": { + "googleapis": "^6.1.0" + }, + "devDependencies": { + "async": "^1.5.0" + } +} diff --git a/speech/recognize.js b/speech/recognize.js new file mode 100644 index 00000000000..4113f590397 --- /dev/null +++ b/speech/recognize.js @@ -0,0 +1,127 @@ +// Copyright 2015-2016, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +// [START import_libraries] +var google = require('googleapis'); +var async = require('async'); +var fs = require('fs'); +// [END import_libraries] + +// Url to discovery doc file +// [START discovery_doc] +var url = 'https://speech.googleapis.com/$discovery/rest'; +// [END discovery_doc] + +// [START authenticating] +function getSpeechService(callback) { + // Acquire credentials + google.auth.getApplicationDefault(function (err, authClient) { + if (err) { + return callback(err); + } + + // The createScopedRequired method returns true when running on GAE or a + // local developer machine. In that case, the desired scopes must be passed + // in manually. When the code is running in GCE or a Managed VM, the scopes + // are pulled from the GCE metadata server. + // See https://cloud.google.com/compute/docs/authentication for more + // information. + if (authClient.createScopedRequired && authClient.createScopedRequired()) { + // Scopes can be specified either as an array or as a single, + // space-delimited string. + authClient = authClient.createScoped([ + 'https://www.googleapis.com/auth/cloud-platform' + ]); + } + + // Load the speach service using acquired credentials + console.log('Loading speech service...'); + google.discoverAPI({ + url: url, + version: 'v1', + auth: authClient + }, function (err, speechService) { + if (err) { + return callback(err); + } + callback(null, speechService, authClient); + }); + }); +} +// [END authenticating] + +// [START construct_request] +function prepareRequest(inputFile, callback) { + fs.readFile(inputFile, function (err, audioFile) { + if (err) { + return callback(err); + } + console.log('Got audio file!'); + var encoded = new Buffer(audioFile).toString('base64'); + var payload = { + initialRequest: { + encoding: 'LINEAR16', + sampleRate: 16000 + }, + audioRequest: { + content: encoded + } + }; + return callback(null, payload); + }); +} +// [END construct_request] + +// [START send_request] +function main(inputFile, callback) { + var requestPayload; + + async.waterfall([ + function (cb) { + prepareRequest(inputFile, cb); + }, + function (payload, cb) { + requestPayload = payload; + getSpeechService(cb); + }, + function (speechService, authClient, cb) { + console.log('Analyzing speech...'); + speechService.speech.recognize({ + auth: authClient, + resource: requestPayload + }, cb); + } + ], function (err, result) { + if (err) { + return callback(err); + } + console.log('result:', JSON.stringify(result, null, 2)); + callback(null, result); + }); +} +// [END send_request] + +exports.main = main; + +// [START run_application] +if (module === require.main) { + if (process.argv.length < 3) { + console.log('Usage: node recognize '); + process.exit(); + } + var inputFile = process.argv[2]; + main(inputFile, console.log); +} +// [END run_application] \ No newline at end of file diff --git a/speech/resources/audio.raw b/speech/resources/audio.raw new file mode 100644 index 00000000000..5ebf79d3c9c Binary files /dev/null and b/speech/resources/audio.raw differ diff --git a/speech/resources/audio2.raw b/speech/resources/audio2.raw new file mode 100644 index 00000000000..35413b78817 Binary files /dev/null and b/speech/resources/audio2.raw differ diff --git a/speech/resources/quit.raw b/speech/resources/quit.raw new file mode 100644 index 00000000000..a01dfc45a59 Binary files /dev/null and b/speech/resources/quit.raw differ diff --git a/test/speech/recognize.test.js b/test/speech/recognize.test.js new file mode 100644 index 00000000000..e23d0e60177 --- /dev/null +++ b/test/speech/recognize.test.js @@ -0,0 +1,32 @@ +// Copyright 2016, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +var test = require('ava'); +var path = require('path'); +var recognizeExample = require('../../speech/recognize'); + +test.cb('should list entries', function (t) { + recognizeExample.main( + path.resolve('../../speech/resources/audio.raw'), + function (err, result) { + t.ifError(err); + t.ok(result); + t.ok(result.responses); + t.is(result.responses.length, 1); + t.ok(result.responses[0].results); + t.end(); + } + ); +}); diff --git a/vision/out.png b/vision/out.png new file mode 100644 index 00000000000..5d33ca48492 --- /dev/null +++ b/vision/out.png @@ -0,0 +1 @@ +testfoobar \ No newline at end of file