| 383 | } |
| 384 | |
| 385 | function streamingMicRecognize(encoding, sampleRateHertz, languageCode) { |
| 386 | // [START speech_transcribe_streaming_mic] |
| 387 | const recorder = require('node-record-lpcm16'); |
| 388 | |
| 389 | // Imports the Google Cloud client library |
| 390 | const speech = require('@google-cloud/speech'); |
| 391 | |
| 392 | // Creates a client |
| 393 | const client = new speech.SpeechClient(); |
| 394 | |
| 395 | /** |
| 396 | * TODO(developer): Uncomment the following lines before running the sample. |
| 397 | */ |
| 398 | // const encoding = 'Encoding of the audio file, e.g. LINEAR16'; |
| 399 | // const sampleRateHertz = 16000; |
| 400 | // const languageCode = 'BCP-47 language code, e.g. en-US'; |
| 401 | |
| 402 | const request = { |
| 403 | config: { |
| 404 | encoding: encoding, |
| 405 | sampleRateHertz: sampleRateHertz, |
| 406 | languageCode: languageCode, |
| 407 | }, |
| 408 | interimResults: false, // If you want interim results, set this to true |
| 409 | }; |
| 410 | |
| 411 | // Create a recognize stream |
| 412 | const recognizeStream = client |
| 413 | .streamingRecognize(request) |
| 414 | .on('error', console.error) |
| 415 | .on('data', data => |
| 416 | process.stdout.write( |
| 417 | data.results[0] && data.results[0].alternatives[0] |
| 418 | ? `Transcription: ${data.results[0].alternatives[0].transcript}\n` |
| 419 | : '\n\nReached transcription time limit, press Ctrl+C\n' |
| 420 | ) |
| 421 | ); |
| 422 | |
| 423 | // Start recording and send the microphone input to the Speech API. |
| 424 | // Ensure SoX is installed, see https://www.npmjs.com/package/node-record-lpcm16#dependencies |
| 425 | recorder |
| 426 | .record({ |
| 427 | sampleRateHertz: sampleRateHertz, |
| 428 | threshold: 0, |
| 429 | // Other options, see https://www.npmjs.com/package/node-record-lpcm16#options |
| 430 | verbose: false, |
| 431 | recordProgram: 'rec', // Try also "arecord" or "sox" |
| 432 | silence: '10.0', |
| 433 | }) |
| 434 | .stream() |
| 435 | .on('error', console.error) |
| 436 | .pipe(recognizeStream); |
| 437 | |
| 438 | console.log('Listening, press Ctrl+C to stop.'); |
| 439 | // [END speech_transcribe_streaming_mic] |
| 440 | } |
| 441 | |
| 442 | async function syncRecognizeModelSelection( |