| 273 | } |
| 274 | |
| 275 | async function asyncRecognizeGCSWords( |
| 276 | gcsUri, |
| 277 | encoding, |
| 278 | sampleRateHertz, |
| 279 | languageCode |
| 280 | ) { |
| 281 | // [START speech_transcribe_async_word_time_offsets_gcs] |
| 282 | // Imports the Google Cloud client library |
| 283 | const speech = require('@google-cloud/speech'); |
| 284 | |
| 285 | // Creates a client |
| 286 | const client = new speech.SpeechClient(); |
| 287 | |
| 288 | /** |
| 289 | * TODO(developer): Uncomment the following lines before running the sample. |
| 290 | */ |
| 291 | // const gcsUri = 'gs://my-bucket/audio.raw'; |
| 292 | // const encoding = 'Encoding of the audio file, e.g. LINEAR16'; |
| 293 | // const sampleRateHertz = 16000; |
| 294 | // const languageCode = 'BCP-47 language code, e.g. en-US'; |
| 295 | |
| 296 | const config = { |
| 297 | enableWordTimeOffsets: true, |
| 298 | encoding: encoding, |
| 299 | sampleRateHertz: sampleRateHertz, |
| 300 | languageCode: languageCode, |
| 301 | }; |
| 302 | |
| 303 | const audio = { |
| 304 | uri: gcsUri, |
| 305 | }; |
| 306 | |
| 307 | const request = { |
| 308 | config: config, |
| 309 | audio: audio, |
| 310 | }; |
| 311 | |
| 312 | // Detects speech in the audio file. This creates a recognition job that you |
| 313 | // can wait for now, or get its result later. |
| 314 | const [operation] = await client.longRunningRecognize(request); |
| 315 | |
| 316 | // Get a Promise representation of the final result of the job |
| 317 | const [response] = await operation.promise(); |
| 318 | response.results.forEach(result => { |
| 319 | console.log(`Transcription: ${result.alternatives[0].transcript}`); |
| 320 | result.alternatives[0].words.forEach(wordInfo => { |
| 321 | // NOTE: If you have a time offset exceeding 2^32 seconds, use the |
| 322 | // wordInfo.{x}Time.seconds.high to calculate seconds. |
| 323 | const startSecs = |
| 324 | `${wordInfo.startTime.seconds}` + |
| 325 | '.' + |
| 326 | wordInfo.startTime.nanos / 100000000; |
| 327 | const endSecs = |
| 328 | `${wordInfo.endTime.seconds}` + |
| 329 | '.' + |
| 330 | wordInfo.endTime.nanos / 100000000; |
| 331 | console.log(`Word: ${wordInfo.word}`); |
| 332 | console.log(`\t ${startSecs} secs - ${endSecs} secs`); |