| 336 | } |
| 337 | |
| 338 | async function streamingRecognize( |
| 339 | filename, |
| 340 | encoding, |
| 341 | sampleRateHertz, |
| 342 | languageCode |
| 343 | ) { |
| 344 | // [START speech_transcribe_streaming] |
| 345 | const fs = require('fs'); |
| 346 | |
| 347 | // Imports the Google Cloud client library |
| 348 | const speech = require('@google-cloud/speech'); |
| 349 | |
| 350 | // Creates a client |
| 351 | const client = new speech.SpeechClient(); |
| 352 | |
| 353 | /** |
| 354 | * TODO(developer): Uncomment the following lines before running the sample. |
| 355 | */ |
| 356 | // const filename = 'Local path to audio file, e.g. /path/to/audio.raw'; |
| 357 | // const encoding = 'Encoding of the audio file, e.g. LINEAR16'; |
| 358 | // const sampleRateHertz = 16000; |
| 359 | // const languageCode = 'BCP-47 language code, e.g. en-US'; |
| 360 | |
| 361 | const request = { |
| 362 | config: { |
| 363 | encoding: encoding, |
| 364 | sampleRateHertz: sampleRateHertz, |
| 365 | languageCode: languageCode, |
| 366 | }, |
| 367 | interimResults: false, // If you want interim results, set this to true |
| 368 | }; |
| 369 | |
| 370 | // Stream the audio to the Google Cloud Speech API |
| 371 | const recognizeStream = client |
| 372 | .streamingRecognize(request) |
| 373 | .on('error', console.error) |
| 374 | .on('data', data => { |
| 375 | console.log( |
| 376 | `Transcription: ${data.results[0].alternatives[0].transcript}` |
| 377 | ); |
| 378 | }); |
| 379 | |
| 380 | // Stream an audio file from disk to the Speech API, e.g. "./resources/audio.raw" |
| 381 | fs.createReadStream(filename).pipe(recognizeStream); |
| 382 | // [END speech_transcribe_streaming] |
| 383 | } |
| 384 | |
| 385 | function streamingMicRecognize(encoding, sampleRateHertz, languageCode) { |
| 386 | // [START speech_transcribe_streaming_mic] |