Skip to content

Instantly share code, notes, and snippets.

@nfriedly
Last active March 3, 2017 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfriedly/e78b5869cf406dddca9d8140fcd64923 to your computer and use it in GitHub Desktop.
Save nfriedly/e78b5869cf406dddca9d8140fcd64923 to your computer and use it in GitHub Desktop.
Extract transaction ID from IBM Watson Node.js SDK Speech to Text RecognizeStream (WebSocket)
'use strict';
const SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
require('dotenv').config({silent: true}); // optional, loads credentials from a .env file
const fs = require('fs');
const speech_to_text = new SpeechToTextV1({
// defaults to env properties if these are unset
// username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
// password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE'
});
// create the stream
const recognizeStream = speech_to_text.createRecognizeStream({
content_type: 'audio/wav'
});
// pipe in some audio
fs.createReadStream(__dirname + '/resources/speech.wav').pipe(recognizeStream);
// notes:
// 1. Requires watson-developer-cloud@2.24.0 or later
// 2. It won't resolve until after you begin sending audio
// 3. This won't work in browsers because WebSocket headers are not exposed in browsers
recognizeStream.getTransactionId()
.then( transId => {
console.log('transaction id', transId);
}).catch( err => {
console.log('error getting transaction id', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment