Skip to content

Commit

Permalink
Add Cloud Speech sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed May 12, 2016
1 parent a3c96e9 commit c112f50
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ../",
Expand All @@ -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": {
Expand Down
23 changes: 23 additions & 0 deletions speech/README.md
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 20 additions & 0 deletions speech/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
127 changes: 127 additions & 0 deletions speech/recognize.js
Original file line number Diff line number Diff line change
@@ -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 <inputFile>');
process.exit();
}
var inputFile = process.argv[2];
main(inputFile, console.log);
}
// [END run_application]
Binary file added speech/resources/audio.raw
Binary file not shown.
Binary file added speech/resources/audio2.raw
Binary file not shown.
Binary file added speech/resources/quit.raw
Binary file not shown.
32 changes: 32 additions & 0 deletions test/speech/recognize.test.js
Original file line number Diff line number Diff line change
@@ -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();
}
);
});
1 change: 1 addition & 0 deletions vision/out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c112f50

Please sign in to comment.