Skip to content

Commit 49b6c77

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

File tree

10 files changed

+209
-1
lines changed

10 files changed

+209
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 23 additions & 0 deletions
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

Lines changed: 20 additions & 0 deletions
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

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

Lines changed: 32 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)