23
23
24
24
'use strict' ;
25
25
26
- function syncRecognize ( filename , encoding , sampleRate ) {
26
+ function syncRecognize ( filename , encoding , sampleRateHertz , languageCode ) {
27
27
// [START speech_sync_recognize]
28
28
// Imports the Google Cloud client library
29
29
const Speech = require ( '@google-cloud/speech' ) ;
@@ -37,12 +37,16 @@ function syncRecognize (filename, encoding, sampleRate) {
37
37
// The encoding of the audio file, e.g. 'LINEAR16'
38
38
// const encoding = 'LINEAR16';
39
39
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';
42
45
43
46
const request = {
44
47
encoding : encoding ,
45
- sampleRate : sampleRate
48
+ sampleRateHertz : sampleRateHertz ,
49
+ languageCode : languageCode
46
50
} ;
47
51
48
52
// Detects speech in the audio file
@@ -55,7 +59,7 @@ function syncRecognize (filename, encoding, sampleRate) {
55
59
// [END speech_sync_recognize]
56
60
}
57
61
58
- function syncRecognizeGCS ( gcsUri , encoding , sampleRate ) {
62
+ function syncRecognizeGCS ( gcsUri , encoding , sampleRateHertz , languageCode ) {
59
63
// [START speech_sync_recognize_gcs]
60
64
// Imports the Google Cloud client library
61
65
const Speech = require ( '@google-cloud/speech' ) ;
@@ -69,12 +73,16 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRate) {
69
73
// The encoding of the audio file, e.g. 'LINEAR16'
70
74
// const encoding = 'LINEAR16';
71
75
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';
74
81
75
82
const request = {
76
83
encoding : encoding ,
77
- sampleRate : sampleRate
84
+ sampleRateHertz : sampleRateHertz ,
85
+ languageCode : languageCode
78
86
} ;
79
87
80
88
// Detects speech in the audio file
@@ -87,7 +95,7 @@ function syncRecognizeGCS (gcsUri, encoding, sampleRate) {
87
95
// [END speech_sync_recognize_gcs]
88
96
}
89
97
90
- function asyncRecognize ( filename , encoding , sampleRate ) {
98
+ function asyncRecognize ( filename , encoding , sampleRateHertz , languageCode ) {
91
99
// [START speech_async_recognize]
92
100
// Imports the Google Cloud client library
93
101
const Speech = require ( '@google-cloud/speech' ) ;
@@ -101,12 +109,16 @@ function asyncRecognize (filename, encoding, sampleRate) {
101
109
// The encoding of the audio file, e.g. 'LINEAR16'
102
110
// const encoding = 'LINEAR16';
103
111
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';
106
117
107
118
const request = {
108
119
encoding : encoding ,
109
- sampleRate : sampleRate
120
+ sampleRateHertz : sampleRateHertz ,
121
+ languageCode : languageCode
110
122
} ;
111
123
112
124
// Detects speech in the audio file. This creates a recognition job that you
@@ -123,7 +135,7 @@ function asyncRecognize (filename, encoding, sampleRate) {
123
135
// [END speech_async_recognize]
124
136
}
125
137
126
- function asyncRecognizeGCS ( gcsUri , encoding , sampleRate ) {
138
+ function asyncRecognizeGCS ( gcsUri , encoding , sampleRateHertz , languageCode ) {
127
139
// [START speech_async_recognize_gcs]
128
140
// Imports the Google Cloud client library
129
141
const Speech = require ( '@google-cloud/speech' ) ;
@@ -137,12 +149,16 @@ function asyncRecognizeGCS (gcsUri, encoding, sampleRate) {
137
149
// The encoding of the audio file, e.g. 'LINEAR16'
138
150
// const encoding = 'LINEAR16';
139
151
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';
142
157
143
158
const request = {
144
159
encoding : encoding ,
145
- sampleRate : sampleRate
160
+ sampleRateHertz : sampleRateHertz ,
161
+ languageCode : languageCode
146
162
} ;
147
163
148
164
// Detects speech in the audio file. This creates a recognition job that you
@@ -159,7 +175,7 @@ function asyncRecognizeGCS (gcsUri, encoding, sampleRate) {
159
175
// [END speech_async_recognize_gcs]
160
176
}
161
177
162
- function streamingRecognize ( filename , encoding , sampleRate ) {
178
+ function streamingRecognize ( filename , encoding , sampleRateHertz , languageCode ) {
163
179
// [START speech_streaming_recognize]
164
180
const fs = require ( 'fs' ) ;
165
181
@@ -175,13 +191,17 @@ function streamingRecognize (filename, encoding, sampleRate) {
175
191
// The encoding of the audio file, e.g. 'LINEAR16'
176
192
// const encoding = 'LINEAR16';
177
193
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';
180
199
181
200
const request = {
182
201
config : {
183
202
encoding : encoding ,
184
- sampleRate : sampleRate
203
+ sampleRateHertz : sampleRateHertz ,
204
+ languageCode : languageCode
185
205
}
186
206
} ;
187
207
@@ -197,7 +217,7 @@ function streamingRecognize (filename, encoding, sampleRate) {
197
217
// [END speech_streaming_recognize]
198
218
}
199
219
200
- function streamingMicRecognize ( encoding , sampleRate ) {
220
+ function streamingMicRecognize ( encoding , sampleRateHertz , languageCode ) {
201
221
// [START speech_streaming_mic_recognize]
202
222
const record = require ( 'node-record-lpcm16' ) ;
203
223
@@ -210,13 +230,17 @@ function streamingMicRecognize (encoding, sampleRate) {
210
230
// The encoding of the audio file, e.g. 'LINEAR16'
211
231
// const encoding = 'LINEAR16';
212
232
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';
215
238
216
239
const request = {
217
240
config : {
218
241
encoding : encoding ,
219
- sampleRate : sampleRate
242
+ sampleRateHertz : sampleRateHertz ,
243
+ languageCode : languageCode
220
244
}
221
245
} ;
222
246
@@ -227,7 +251,7 @@ function streamingMicRecognize (encoding, sampleRate) {
227
251
228
252
// Start recording and send the microphone input to the Speech API
229
253
record . start ( {
230
- sampleRate : sampleRate ,
254
+ sampleRateHertz : sampleRateHertz ,
231
255
threshold : 0
232
256
} ) . pipe ( recognizeStream ) ;
233
257
@@ -241,37 +265,37 @@ require(`yargs`)
241
265
`sync <filename>` ,
242
266
`Detects speech in a local audio file.` ,
243
267
{ } ,
244
- ( opts ) => syncRecognize ( opts . filename , opts . encoding , opts . sampleRate )
268
+ ( opts ) => syncRecognize ( opts . filename , opts . encoding , opts . sampleRateHertz , opts . languageCode )
245
269
)
246
270
. command (
247
271
`sync-gcs <gcsUri>` ,
248
272
`Detects speech in an audio file located in a Google Cloud Storage bucket.` ,
249
273
{ } ,
250
- ( opts ) => syncRecognizeGCS ( opts . gcsUri , opts . encoding , opts . sampleRate )
274
+ ( opts ) => syncRecognizeGCS ( opts . gcsUri , opts . encoding , opts . sampleRateHertz , opts . languageCode )
251
275
)
252
276
. command (
253
277
`async <filename>` ,
254
278
`Creates a job to detect speech in a local audio file, and waits for the job to complete.` ,
255
279
{ } ,
256
- ( opts ) => asyncRecognize ( opts . filename , opts . encoding , opts . sampleRate )
280
+ ( opts ) => asyncRecognize ( opts . filename , opts . encoding , opts . sampleRateHertz , opts . languageCode )
257
281
)
258
282
. command (
259
283
`async-gcs <gcsUri>` ,
260
284
`Creates a job to detect speech in an audio file located in a Google Cloud Storage bucket, and waits for the job to complete.` ,
261
285
{ } ,
262
- ( opts ) => asyncRecognizeGCS ( opts . gcsUri , opts . encoding , opts . sampleRate )
286
+ ( opts ) => asyncRecognizeGCS ( opts . gcsUri , opts . encoding , opts . sampleRateHertz , opts . languageCode )
263
287
)
264
288
. command (
265
289
`stream <filename>` ,
266
290
`Detects speech in a local audio file by streaming it to the Speech API.` ,
267
291
{ } ,
268
- ( opts ) => streamingRecognize ( opts . filename , opts . encoding , opts . sampleRate )
292
+ ( opts ) => streamingRecognize ( opts . filename , opts . encoding , opts . sampleRateHertz , opts . languageCode )
269
293
)
270
294
. command (
271
295
`listen` ,
272
296
`Detects speech in a microphone input stream.` ,
273
297
{ } ,
274
- ( opts ) => streamingMicRecognize ( opts . encoding , opts . sampleRate )
298
+ ( opts ) => streamingMicRecognize ( opts . encoding , opts . sampleRateHertz , opts . languageCode )
275
299
)
276
300
. options ( {
277
301
encoding : {
@@ -281,12 +305,19 @@ require(`yargs`)
281
305
requiresArg : true ,
282
306
type : 'string'
283
307
} ,
284
- sampleRate : {
308
+ sampleRateHertz : {
285
309
alias : 'r' ,
286
310
default : 16000 ,
287
311
global : true ,
288
312
requiresArg : true ,
289
313
type : 'number'
314
+ } ,
315
+ languageCode : {
316
+ alias : 'l' ,
317
+ default : 'en-US' ,
318
+ global : true ,
319
+ requiresArg : true ,
320
+ type : 'string'
290
321
}
291
322
} )
292
323
. example ( `node $0 sync ./resources/audio.raw -e LINEAR16 -r 16000` )
0 commit comments