I am trying to send a web schedule connection audio stream to the Google Speech API. Websocket sends binary messages in 20 ms increments. The fact that he sends it in steps makes me believe that somehow I will have to temporarily read and write data to a local file in order to avoid killing the Google connection. However, this is not ideal.
Is there a way to directly connect the websocket stream to recognizeStream ?
Google streamingRecognize example from the docs:
const request = { config: { encoding: encoding, sampleRate: sampleRate } }; const recognizeStream = speech.createRecognizeStream(request) .on('error', console.error) .on('data', (data) => process.stdout.write(data.results)); record.start({ sampleRate: sampleRate, threshold: 0 }).pipe(recognizeStream);
Connection to Websocket:
var HttpDispatcher = require('httpdispatcher'); var dispatcher = new HttpDispatcher(); var WebSocketServer = require('websocket').server; var server = http.createServer(handleRequest); var wsServer = new WebSocketServer({ httpServer: server, autoAcceptConnections: true, }); function handleRequest(request, response){ try { //log the request on console console.log(request.url); //Dispatch dispatcher.dispatch(request, response); } catch(err) { console.log(err); } } wsServer.on('connect', function(connection) { console.log((new Date()) + ' Connection accepted' + ' - Protocol Version ' + connection.webSocketVersion); connection.on('message', function(message) { if (message.type === 'utf8') { console.log(message.utf8Data); } else if (message.type === 'binary') { //Send to Google Speech API by passing into recognizeStream } }); connection.on('close', function(reasonCode, description) { console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });
source share