Skip to content

Commit e9a963a

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

File tree

10 files changed

+216
-2
lines changed

10 files changed

+216
-2
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

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
// [START app]
17+
// [START import_libraries]
18+
var google = require('googleapis');
19+
var async = require('async');
20+
var fs = require('fs');
21+
// [END import_libraries]
22+
23+
// Url to discovery doc file
24+
// [START discovery_doc]
25+
var url = 'https://speech.googleapis.com/$discovery/rest';
26+
// [END discovery_doc]
27+
28+
// [START authenticating]
29+
function getSpeechService(callback) {
30+
// Acquire credentials
31+
google.auth.getApplicationDefault(function (err, authClient) {
32+
if (err) {
33+
return callback(err);
34+
}
35+
36+
// The createScopedRequired method returns true when running on GAE or a
37+
// local developer machine. In that case, the desired scopes must be passed
38+
// in manually. When the code is running in GCE or a Managed VM, the scopes
39+
// are pulled from the GCE metadata server.
40+
// See https://cloud.google.com/compute/docs/authentication for more
41+
// information.
42+
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
43+
// Scopes can be specified either as an array or as a single,
44+
// space-delimited string.
45+
authClient = authClient.createScoped([
46+
'https://www.googleapis.com/auth/cloud-platform'
47+
]);
48+
}
49+
50+
// Load the speach service using acquired credentials
51+
console.log('Loading speech service...');
52+
google.discoverAPI({
53+
url: url,
54+
version: 'v1',
55+
auth: authClient
56+
}, function (err, speechService) {
57+
if (err) {
58+
return callback(err);
59+
}
60+
callback(null, speechService, authClient);
61+
});
62+
});
63+
}
64+
// [END authenticating]
65+
66+
// [START construct_request]
67+
function prepareRequest(inputFile, callback) {
68+
fs.readFile(inputFile, function (err, audioFile) {
69+
if (err) {
70+
return callback(err);
71+
}
72+
console.log('Got audio file!');
73+
var encoded = new Buffer(audioFile).toString('base64');
74+
var payload = {
75+
initialRequest: {
76+
encoding: 'LINEAR16',
77+
sampleRate: 16000
78+
},
79+
audioRequest: {
80+
content: encoded
81+
}
82+
};
83+
return callback(null, payload);
84+
});
85+
}
86+
// [END construct_request]
87+
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+
// [START send_request]
100+
function sendRequest(speechService, authClient, cb) {
101+
console.log('Analyzing speech...');
102+
speechService.speech.recognize({
103+
auth: authClient,
104+
resource: requestPayload
105+
}, function (err, result) {
106+
if (err) {
107+
return cb(err);
108+
}
109+
console.log('result:', JSON.stringify(result, null, 2));
110+
cb(null, result);
111+
});
112+
}
113+
// [END send_request]
114+
], callback);
115+
}
116+
117+
// [START run_application]
118+
if (module === require.main) {
119+
if (process.argv.length < 3) {
120+
console.log('Usage: node recognize <inputFile>');
121+
process.exit();
122+
}
123+
var inputFile = process.argv[2];
124+
main(inputFile, console.log);
125+
}
126+
// [END run_application]
127+
// [END app]
128+
129+
exports.main = main;

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/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
out.*
1+
out.png
2+
out.*

0 commit comments

Comments
 (0)