| 171 | } |
| 172 | |
| 173 | async function asyncRecognize( |
| 174 | filename, |
| 175 | encoding, |
| 176 | sampleRateHertz, |
| 177 | languageCode |
| 178 | ) { |
| 179 | // [START speech_transcribe_async] |
| 180 | // Imports the Google Cloud client library |
| 181 | const speech = require('@google-cloud/speech'); |
| 182 | const fs = require('fs'); |
| 183 | |
| 184 | // Creates a client |
| 185 | const client = new speech.SpeechClient(); |
| 186 | |
| 187 | /** |
| 188 | * TODO(developer): Uncomment the following lines before running the sample. |
| 189 | */ |
| 190 | // const filename = 'Local path to audio file, e.g. /path/to/audio.raw'; |
| 191 | // const encoding = 'Encoding of the audio file, e.g. LINEAR16'; |
| 192 | // const sampleRateHertz = 16000; |
| 193 | // const languageCode = 'BCP-47 language code, e.g. en-US'; |
| 194 | |
| 195 | const config = { |
| 196 | encoding: encoding, |
| 197 | sampleRateHertz: sampleRateHertz, |
| 198 | languageCode: languageCode, |
| 199 | }; |
| 200 | |
| 201 | /** |
| 202 | * Note that transcription is limited to 60 seconds audio. |
| 203 | * Use a GCS file for audio longer than 1 minute. |
| 204 | */ |
| 205 | const audio = { |
| 206 | content: fs.readFileSync(filename).toString('base64'), |
| 207 | }; |
| 208 | |
| 209 | const request = { |
| 210 | config: config, |
| 211 | audio: audio, |
| 212 | }; |
| 213 | |
| 214 | // Detects speech in the audio file. This creates a recognition job that you |
| 215 | // can wait for now, or get its result later. |
| 216 | const [operation] = await client.longRunningRecognize(request); |
| 217 | |
| 218 | // Get a Promise representation of the final result of the job |
| 219 | const [response] = await operation.promise(); |
| 220 | const transcription = response.results |
| 221 | .map(result => result.alternatives[0].transcript) |
| 222 | .join('\n'); |
| 223 | console.log(`Transcription: ${transcription}`); |
| 224 | // [END speech_transcribe_async] |
| 225 | } |
| 226 | |
| 227 | async function asyncRecognizeGCS( |
| 228 | gcsUri, |