Skip to content

Commit 9dd9a91

Browse files
author
Ace Nassri
authored
Update samples to match new Speech API (#342)
Update samples to match new Speech API
1 parent e81d52f commit 9dd9a91

File tree

5 files changed

+70
-37
lines changed

5 files changed

+70
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"@google-cloud/pubsub": "0.8.0",
8484
"@google-cloud/resource": "0.6.0",
8585
"@google-cloud/spanner": "0.1.0",
86-
"@google-cloud/speech": "0.6.0",
86+
"@google-cloud/speech": "0.9.0",
8787
"@google-cloud/storage": "0.7.0",
8888
"@google-cloud/translate": "0.8.0",
8989
"@google-cloud/videointelligence": "https://storage.googleapis.com/videointelligence-alpha/videointelligence-nodejs.tar.gz",

speech/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test": "cd ..; npm run st -- --verbose speech/system-test/*.test.js"
99
},
1010
"dependencies": {
11-
"@google-cloud/speech": "0.6.0",
11+
"@google-cloud/speech": "0.9.0",
1212
"@google-cloud/storage": "0.7.0",
1313
"node-record-lpcm16": "0.2.0",
1414
"yargs": "6.6.0"

speech/quickstart.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ const speechClient = Speech({
3030
// The name of the audio file to transcribe
3131
const fileName = './resources/audio.raw';
3232

33-
// The audio file's encoding and sample rate
33+
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
3434
const options = {
3535
encoding: 'LINEAR16',
36-
sampleRate: 16000
36+
sampleRateHertz: 16000,
37+
languageCode: 'en-US'
3738
};
3839

3940
// Detects speech in the audio file

speech/recognize.js

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
'use strict';
2525

26-
function syncRecognize (filename, encoding, sampleRate) {
26+
function syncRecognize (filename, encoding, sampleRateHertz, languageCode) {
2727
// [START speech_sync_recognize]
2828
// Imports the Google Cloud client library
2929
const Speech = require('@google-cloud/speech');
@@ -37,12 +37,16 @@ function syncRecognize (filename, encoding, sampleRate) {
3737
// The encoding of the audio file, e.g. 'LINEAR16'
3838
// const encoding = 'LINEAR16';
3939

40-
// The sample rate of the audio file, e.g. 16000
41-
// const sampleRate = 16000;
40+
// The sample rate of the audio file in hertz, e.g. 16000
41+
// const sampleRateHertz = 16000;
42+
43+
// The BCP-47 language code to use, e.g. 'en-US'
44+
// const languageCode = 'en-US';
4245

4346
const request = {
4447
encoding: encoding,
45-
sampleRate: sampleRate
48+
sampleRateHertz: sampleRateHertz,
49+
languageCode: languageCode
4650
};
4751

4852
// Detects speech in the audio file
@@ -55,7 +59,7 @@ function syncRecognize (filename, encoding, sampleRate) {
5559
// [END speech_sync_recognize]
5660
}
5761

58-
function syncRecognizeGCS (gcsUri, encoding, sampleRate) {
62+
function syncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
5963
// [START speech_sync_recognize_gcs]
6064
// Imports the Google Cloud client library
6165
const Speech = require('@google-cloud/speech');
@@ -69,12 +73,16 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRate) {
6973
// The encoding of the audio file, e.g. 'LINEAR16'
7074
// const encoding = 'LINEAR16';
7175

72-
// The sample rate of the audio file, e.g. 16000
73-
// const sampleRate = 16000;
76+
// The sample rate of the audio file in hertz, e.g. 16000
77+
// const sampleRateHertz = 16000;
78+
79+
// The BCP-47 language code to use, e.g. 'en-US'
80+
// const languageCode = 'en-US';
7481

7582
const request = {
7683
encoding: encoding,
77-
sampleRate: sampleRate
84+
sampleRateHertz: sampleRateHertz,
85+
languageCode: languageCode
7886
};
7987

8088
// Detects speech in the audio file
@@ -87,7 +95,7 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRate) {
8795
// [END speech_sync_recognize_gcs]
8896
}
8997

90-
function asyncRecognize (filename, encoding, sampleRate) {
98+
function asyncRecognize (filename, encoding, sampleRateHertz, languageCode) {
9199
// [START speech_async_recognize]
92100
// Imports the Google Cloud client library
93101
const Speech = require('@google-cloud/speech');
@@ -101,12 +109,16 @@ function asyncRecognize (filename, encoding, sampleRate) {
101109
// The encoding of the audio file, e.g. 'LINEAR16'
102110
// const encoding = 'LINEAR16';
103111

104-
// The sample rate of the audio file, e.g. 16000
105-
// const sampleRate = 16000;
112+
// The sample rate of the audio file in hertz, e.g. 16000
113+
// const sampleRateHertz = 16000;
114+
115+
// The BCP-47 language code to use, e.g. 'en-US'
116+
// const languageCode = 'en-US';
106117

107118
const request = {
108119
encoding: encoding,
109-
sampleRate: sampleRate
120+
sampleRateHertz: sampleRateHertz,
121+
languageCode: languageCode
110122
};
111123

112124
// Detects speech in the audio file. This creates a recognition job that you
@@ -123,7 +135,7 @@ function asyncRecognize (filename, encoding, sampleRate) {
123135
// [END speech_async_recognize]
124136
}
125137

126-
function asyncRecognizeGCS (gcsUri, encoding, sampleRate) {
138+
function asyncRecognizeGCS (gcsUri, encoding, sampleRateHertz, languageCode) {
127139
// [START speech_async_recognize_gcs]
128140
// Imports the Google Cloud client library
129141
const Speech = require('@google-cloud/speech');
@@ -137,12 +149,16 @@ function asyncRecognizeGCS (gcsUri, encoding, sampleRate) {
137149
// The encoding of the audio file, e.g. 'LINEAR16'
138150
// const encoding = 'LINEAR16';
139151

140-
// The sample rate of the audio file, e.g. 16000
141-
// const sampleRate = 16000;
152+
// The sample rate of the audio file in hertz, e.g. 16000
153+
// const sampleRateHertz = 16000;
154+
155+
// The BCP-47 language code to use, e.g. 'en-US'
156+
// const languageCode = 'en-US';
142157

143158
const request = {
144159
encoding: encoding,
145-
sampleRate: sampleRate
160+
sampleRateHertz: sampleRateHertz,
161+
languageCode: languageCode
146162
};
147163

148164
// Detects speech in the audio file. This creates a recognition job that you
@@ -159,7 +175,7 @@ function asyncRecognizeGCS (gcsUri, encoding, sampleRate) {
159175
// [END speech_async_recognize_gcs]
160176
}
161177

162-
function streamingRecognize (filename, encoding, sampleRate) {
178+
function streamingRecognize (filename, encoding, sampleRateHertz, languageCode) {
163179
// [START speech_streaming_recognize]
164180
const fs = require('fs');
165181

@@ -175,13 +191,17 @@ function streamingRecognize (filename, encoding, sampleRate) {
175191
// The encoding of the audio file, e.g. 'LINEAR16'
176192
// const encoding = 'LINEAR16';
177193

178-
// The sample rate of the audio file, e.g. 16000
179-
// const sampleRate = 16000;
194+
// The sample rate of the audio file in hertz, e.g. 16000
195+
// const sampleRateHertz = 16000;
196+
197+
// The BCP-47 language code to use, e.g. 'en-US'
198+
// const languageCode = 'en-US';
180199

181200
const request = {
182201
config: {
183202
encoding: encoding,
184-
sampleRate: sampleRate
203+
sampleRateHertz: sampleRateHertz,
204+
languageCode: languageCode
185205
}
186206
};
187207

@@ -197,7 +217,7 @@ function streamingRecognize (filename, encoding, sampleRate) {
197217
// [END speech_streaming_recognize]
198218
}
199219

200-
function streamingMicRecognize (encoding, sampleRate) {
220+
function streamingMicRecognize (encoding, sampleRateHertz, languageCode) {
201221
// [START speech_streaming_mic_recognize]
202222
const record = require('node-record-lpcm16');
203223

@@ -210,13 +230,17 @@ function streamingMicRecognize (encoding, sampleRate) {
210230
// The encoding of the audio file, e.g. 'LINEAR16'
211231
// const encoding = 'LINEAR16';
212232

213-
// The sample rate of the audio file, e.g. 16000
214-
// const sampleRate = 16000;
233+
// The sample rate of the audio file in hertz, e.g. 16000
234+
// const sampleRateHertz = 16000;
235+
236+
// The BCP-47 language code to use, e.g. 'en-US'
237+
// const languageCode = 'en-US';
215238

216239
const request = {
217240
config: {
218241
encoding: encoding,
219-
sampleRate: sampleRate
242+
sampleRateHertz: sampleRateHertz,
243+
languageCode: languageCode
220244
}
221245
};
222246

@@ -227,7 +251,7 @@ function streamingMicRecognize (encoding, sampleRate) {
227251

228252
// Start recording and send the microphone input to the Speech API
229253
record.start({
230-
sampleRate: sampleRate,
254+
sampleRateHertz: sampleRateHertz,
231255
threshold: 0
232256
}).pipe(recognizeStream);
233257

@@ -241,37 +265,37 @@ require(`yargs`)
241265
`sync <filename>`,
242266
`Detects speech in a local audio file.`,
243267
{},
244-
(opts) => syncRecognize(opts.filename, opts.encoding, opts.sampleRate)
268+
(opts) => syncRecognize(opts.filename, opts.encoding, opts.sampleRateHertz, opts.languageCode)
245269
)
246270
.command(
247271
`sync-gcs <gcsUri>`,
248272
`Detects speech in an audio file located in a Google Cloud Storage bucket.`,
249273
{},
250-
(opts) => syncRecognizeGCS(opts.gcsUri, opts.encoding, opts.sampleRate)
274+
(opts) => syncRecognizeGCS(opts.gcsUri, opts.encoding, opts.sampleRateHertz, opts.languageCode)
251275
)
252276
.command(
253277
`async <filename>`,
254278
`Creates a job to detect speech in a local audio file, and waits for the job to complete.`,
255279
{},
256-
(opts) => asyncRecognize(opts.filename, opts.encoding, opts.sampleRate)
280+
(opts) => asyncRecognize(opts.filename, opts.encoding, opts.sampleRateHertz, opts.languageCode)
257281
)
258282
.command(
259283
`async-gcs <gcsUri>`,
260284
`Creates a job to detect speech in an audio file located in a Google Cloud Storage bucket, and waits for the job to complete.`,
261285
{},
262-
(opts) => asyncRecognizeGCS(opts.gcsUri, opts.encoding, opts.sampleRate)
286+
(opts) => asyncRecognizeGCS(opts.gcsUri, opts.encoding, opts.sampleRateHertz, opts.languageCode)
263287
)
264288
.command(
265289
`stream <filename>`,
266290
`Detects speech in a local audio file by streaming it to the Speech API.`,
267291
{},
268-
(opts) => streamingRecognize(opts.filename, opts.encoding, opts.sampleRate)
292+
(opts) => streamingRecognize(opts.filename, opts.encoding, opts.sampleRateHertz, opts.languageCode)
269293
)
270294
.command(
271295
`listen`,
272296
`Detects speech in a microphone input stream.`,
273297
{},
274-
(opts) => streamingMicRecognize(opts.encoding, opts.sampleRate)
298+
(opts) => streamingMicRecognize(opts.encoding, opts.sampleRateHertz, opts.languageCode)
275299
)
276300
.options({
277301
encoding: {
@@ -281,12 +305,19 @@ require(`yargs`)
281305
requiresArg: true,
282306
type: 'string'
283307
},
284-
sampleRate: {
308+
sampleRateHertz: {
285309
alias: 'r',
286310
default: 16000,
287311
global: true,
288312
requiresArg: true,
289313
type: 'number'
314+
},
315+
languageCode: {
316+
alias: 'l',
317+
default: 'en-US',
318+
global: true,
319+
requiresArg: true,
320+
type: 'string'
290321
}
291322
})
292323
.example(`node $0 sync ./resources/audio.raw -e LINEAR16 -r 16000`)

speech/system-test/quickstart.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const speech = proxyquire(`@google-cloud/speech`, {})();
2424
const fileName = path.join(__dirname, `../resources/audio.raw`);
2525
const config = {
2626
encoding: `LINEAR16`,
27-
sampleRate: 16000
27+
sampleRateHertz: 16000,
28+
languageCode: `en-US`
2829
};
2930

3031
test.before(stubConsole);

0 commit comments

Comments
 (0)