Skip to content

Commit bd96cde

Browse files
authored
Bring Speech API samples up to standard. (#362)
1 parent e68a117 commit bd96cde

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

speech/recognize.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ function streamingRecognize (filename, encoding, sampleRateHertz, languageCode)
216216
encoding: encoding,
217217
sampleRateHertz: sampleRateHertz,
218218
languageCode: languageCode
219-
}
219+
},
220+
interimResults: false // If you want interim results, set this to true
220221
};
221222

222223
// Stream the audio to the Google Cloud Speech API
@@ -255,7 +256,8 @@ function streamingMicRecognize (encoding, sampleRateHertz, languageCode) {
255256
encoding: encoding,
256257
sampleRateHertz: sampleRateHertz,
257258
languageCode: languageCode
258-
}
259+
},
260+
interimResults: false // If you want interim results, set this to true
259261
};
260262

261263
// Create a recognize stream
@@ -264,16 +266,23 @@ function streamingMicRecognize (encoding, sampleRateHertz, languageCode) {
264266
.on('data', (data) => process.stdout.write(data.results));
265267

266268
// Start recording and send the microphone input to the Speech API
267-
record.start({
268-
sampleRateHertz: sampleRateHertz,
269-
threshold: 0
270-
}).pipe(recognizeStream);
269+
record
270+
.start({
271+
sampleRateHertz: sampleRateHertz,
272+
threshold: 0,
273+
// Other options, see https://www.npmjs.com/package/node-record-lpcm16#options
274+
verbose: false,
275+
recordProgram: 'rec', // Try also "arecord" or "sox"
276+
silence: '10.0'
277+
})
278+
.on('error', console.error)
279+
.pipe(recognizeStream);
271280

272281
console.log('Listening, press Ctrl+C to stop.');
273282
// [END speech_streaming_mic_recognize]
274283
}
275284

276-
require(`yargs`) // eslint-disable-line
285+
const cli = require(`yargs`)
277286
.demand(1)
278287
.command(
279288
`sync <filename>`,
@@ -342,5 +351,8 @@ require(`yargs`) // eslint-disable-line
342351
.recommendCommands()
343352
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
344353
.help()
345-
.strict()
346-
.argv;
354+
.strict();
355+
356+
if (module === require.main) {
357+
cli.parse(process.argv.slice(2));
358+
}

0 commit comments

Comments
 (0)