Skip to content

Commit c112f50

Browse files
committed
Add Cloud Speech sample.
1 parent a3c96e9 commit c112f50

File tree

10 files changed

+213
-1
lines changed

10 files changed

+213
-1
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This repository holds Node.js samples used throughout [cloud.google.com]().
1313
* [Google Cloud Functions](#google-cloud-functions)
1414
* [Google Cloud Logging](#google-cloud-logging)
1515
* [Google Cloud Pub/Sub](#google-cloud-pubsub)
16+
* [Google Cloud Speech](#google-cloud-speech)
1617
* [Google Cloud Storage](#google-cloud-storage)
1718
* [Google Cloud Vision](#google-cloud-vision)
1819
* [Google Prediction API](#google-prediction-api)
@@ -103,6 +104,10 @@ __Other Examples__
103104
- Subscriber/Publisher sample - [Source code][pubsub_subscriber_1] | [Documentation][pubsub_subscriber_2]
104105
- IAM sample - [Source code][pubsub_iam_1] | [Documentation][pubsub_iam_2]
105106

107+
## Google Cloud Speech
108+
109+
- Recognition sample - [Source code][speech_1] | [Documentation][speech_2]
110+
106111
## Google Cloud Storage
107112

108113
- Auth sample - [Source code][storage_1] | [Documentation][storage_2]
@@ -309,6 +314,9 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma
309314
[pubsub_iam_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/pubsub/iam.js
310315
[pubsub_iam_2]: https://cloud.google.com/pubsub/access_control
311316

317+
[speech_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/speech/recognize.js
318+
[speech_2]: https://cloud.google.com/speech
319+
312320
[storage_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/storage/authSample.js
313321
[storage_2]: https://cloud.google.com/storage/docs/authentication#acd-examples
314322

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"report-html": "nyc report --reporter=html",
3434
"deps_gce": "cd computeengine; npm i; cd ../",
3535
"deps_bigquery": "cd bigquery; npm i; cd ../",
36+
"deps_speech": "cd speech; npm i; cd ../",
3637
"deps_datastore": "cd datastore; npm i; cd ../",
3738
"deps_pubsub": "cd pubsub; npm i; cd ../",
3839
"deps_monitoring": "cd monitoring; npm i; cd ../",
@@ -42,7 +43,7 @@
4243
"deps_vision": "cd vision; npm i; cd ../",
4344
"deps_functions": "cd functions/uuid; npm i; cd ../..",
4445
"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 ../..;",
45-
"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",
46+
"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;",
4647
"test": "npm run jshint && npm run cover"
4748
},
4849
"ava": {

speech/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Cloud Speech API samples
2+
3+
These samples require two environment variables to be set:
4+
5+
- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can
6+
download one from your Google project's "credentials" page.
7+
- `GCLOUD_PROJECT` - ID of your Google project.
8+
9+
See [gcloud-node authentication][auth] for more details.
10+
11+
[auth]: https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
12+
13+
## Run a sample
14+
15+
Install dependencies first:
16+
17+
npm install
18+
19+
### Recognition sample
20+
21+
Execute the sample:
22+
23+
node recognize "/path/to/audio.file"

speech/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "nodejs-docs-samples-speech",
3+
"description": "Node.js samples for Google Cloud Speech API.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"engines": {
9+
"node": ">=0.10.x"
10+
},
11+
"scripts": {
12+
"recognize": "node recognize"
13+
},
14+
"dependencies": {
15+
"googleapis": "^6.1.0"
16+
},
17+
"devDependencies": {
18+
"async": "^1.5.0"
19+
}
20+
}

speech/recognize.js

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START import_libraries]
17+
var google = require('googleapis');
18+
var async = require('async');
19+
var fs = require('fs');
20+
// [END import_libraries]
21+
22+
// Url to discovery doc file
23+
// [START discovery_doc]
24+
var url = 'https://speech.googleapis.com/$discovery/rest';
25+
// [END discovery_doc]
26+
27+
// [START authenticating]
28+
function getSpeechService(callback) {
29+
// Acquire credentials
30+
google.auth.getApplicationDefault(function (err, authClient) {
31+
if (err) {
32+
return callback(err);
33+
}
34+
35+
// The createScopedRequired method returns true when running on GAE or a
36+
// local developer machine. In that case, the desired scopes must be passed
37+
// in manually. When the code is running in GCE or a Managed VM, the scopes
38+
// are pulled from the GCE metadata server.
39+
// See https://cloud.google.com/compute/docs/authentication for more
40+
// information.
41+
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
42+
// Scopes can be specified either as an array or as a single,
43+
// space-delimited string.
44+
authClient = authClient.createScoped([
45+
'https://www.googleapis.com/auth/cloud-platform'
46+
]);
47+
}
48+
49+
// Load the speach service using acquired credentials
50+
console.log('Loading speech service...');
51+
google.discoverAPI({
52+
url: url,
53+
version: 'v1',
54+
auth: authClient
55+
}, function (err, speechService) {
56+
if (err) {
57+
return callback(err);
58+
}
59+
callback(null, speechService, authClient);
60+
});
61+
});
62+
}
63+
// [END authenticating]
64+
65+
// [START construct_request]
66+
function prepareRequest(inputFile, callback) {
67+
fs.readFile(inputFile, function (err, audioFile) {
68+
if (err) {
69+
return callback(err);
70+
}
71+
console.log('Got audio file!');
72+
var encoded = new Buffer(audioFile).toString('base64');
73+
var payload = {
74+
initialRequest: {
75+
encoding: 'LINEAR16',
76+
sampleRate: 16000
77+
},
78+
audioRequest: {
79+
content: encoded
80+
}
81+
};
82+
return callback(null, payload);
83+
});
84+
}
85+
// [END construct_request]
86+
87+
// [START send_request]
88+
function main(inputFile, callback) {
89+
var requestPayload;
90+
91+
async.waterfall([
92+
function (cb) {
93+
prepareRequest(inputFile, cb);
94+
},
95+
function (payload, cb) {
96+
requestPayload = payload;
97+
getSpeechService(cb);
98+
},
99+
function (speechService, authClient, cb) {
100+
console.log('Analyzing speech...');
101+
speechService.speech.recognize({
102+
auth: authClient,
103+
resource: requestPayload
104+
}, cb);
105+
}
106+
], function (err, result) {
107+
if (err) {
108+
return callback(err);
109+
}
110+
console.log('result:', JSON.stringify(result, null, 2));
111+
callback(null, result);
112+
});
113+
}
114+
// [END send_request]
115+
116+
exports.main = main;
117+
118+
// [START run_application]
119+
if (module === require.main) {
120+
if (process.argv.length < 3) {
121+
console.log('Usage: node recognize <inputFile>');
122+
process.exit();
123+
}
124+
var inputFile = process.argv[2];
125+
main(inputFile, console.log);
126+
}
127+
// [END run_application]

speech/resources/audio.raw

56.6 KB
Binary file not shown.

speech/resources/audio2.raw

156 KB
Binary file not shown.

speech/resources/quit.raw

160 KB
Binary file not shown.

test/speech/recognize.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var test = require('ava');
17+
var path = require('path');
18+
var recognizeExample = require('../../speech/recognize');
19+
20+
test.cb('should list entries', function (t) {
21+
recognizeExample.main(
22+
path.resolve('../../speech/resources/audio.raw'),
23+
function (err, result) {
24+
t.ifError(err);
25+
t.ok(result);
26+
t.ok(result.responses);
27+
t.is(result.responses.length, 1);
28+
t.ok(result.responses[0].results);
29+
t.end();
30+
}
31+
);
32+
});

vision/out.png

+1
Loading

0 commit comments

Comments
 (0)