How to send websocket binary messages as a stream in the Google Speech API?

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.'); }); }); 
+5
source share
2 answers

It is actually quite simple. It’s so simple that I feel a little shy because I don’t see him. According to how the code was written in OP, this works fine:

 else if (message.type === 'binary') { //Send to Google Speech API by passing into recognizeStream recognizeStream.write(message.binaryData) } 
0
source

The best solution would be to use a specialized streaming solution, rather than doing it yourself, this will process all the buffers and give you a constant stream suitable for the Google Speech API. Try using something like

https://www.npmjs.com/package/websocket-stream

0
source

Source: https://habr.com/ru/post/1265258/


All Articles