| 241 | } |
| 242 | |
| 243 | async function streamingDetectIntent( |
| 244 | projectId, |
| 245 | sessionId, |
| 246 | filename, |
| 247 | encoding, |
| 248 | sampleRateHertz, |
| 249 | languageCode |
| 250 | ) { |
| 251 | // [START dialogflow_detect_intent_streaming] |
| 252 | const fs = require('fs'); |
| 253 | const util = require('util'); |
| 254 | const {Transform, pipeline} = require('stream'); |
| 255 | const {struct} = require('pb-util'); |
| 256 | |
| 257 | const pump = util.promisify(pipeline); |
| 258 | // Imports the Dialogflow library |
| 259 | const dialogflow = require('@google-cloud/dialogflow'); |
| 260 | |
| 261 | // Instantiates a session client |
| 262 | const sessionClient = new dialogflow.SessionsClient(); |
| 263 | |
| 264 | // The path to the local file on which to perform speech recognition, e.g. |
| 265 | // /path/to/audio.raw const filename = '/path/to/audio.raw'; |
| 266 | |
| 267 | // The encoding of the audio file, e.g. 'AUDIO_ENCODING_LINEAR_16' |
| 268 | // const encoding = 'AUDIO_ENCODING_LINEAR_16'; |
| 269 | |
| 270 | // The sample rate of the audio file in hertz, e.g. 16000 |
| 271 | // const sampleRateHertz = 16000; |
| 272 | |
| 273 | // The BCP-47 language code to use, e.g. 'en-US' |
| 274 | // const languageCode = 'en-US'; |
| 275 | const sessionPath = sessionClient.projectAgentSessionPath( |
| 276 | projectId, |
| 277 | sessionId |
| 278 | ); |
| 279 | |
| 280 | const initialStreamRequest = { |
| 281 | session: sessionPath, |
| 282 | queryInput: { |
| 283 | audioConfig: { |
| 284 | audioEncoding: encoding, |
| 285 | sampleRateHertz: sampleRateHertz, |
| 286 | languageCode: languageCode, |
| 287 | }, |
| 288 | }, |
| 289 | }; |
| 290 | |
| 291 | // Create a stream for the streaming request. |
| 292 | const detectStream = sessionClient |
| 293 | .streamingDetectIntent() |
| 294 | .on('error', console.error) |
| 295 | .on('data', data => { |
| 296 | if (data.recognitionResult) { |
| 297 | console.log( |
| 298 | `Intermediate transcript: ${data.recognitionResult.transcript}` |
| 299 | ); |
| 300 | } else { |