Max Phillips
Max Phillips

Reputation: 7489

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

I'm trying to send the audio stream of a websocket connection to the Google Speech API. The websocket sends binary messages in 20ms increments. That it sends it in increments leads me to believe that I will somehow have to read and write the data to a local file temporarily to avoid killing the connection to Google. However, this is not ideal.

Is there a way to directly pipe the websocket stream into 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);

Websocket connection:

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.');
    });


});

Upvotes: 4

Views: 2602

Answers (2)

Max Phillips
Max Phillips

Reputation: 7489

This is actually quite simple. So simple that I feel a little sheepish for not seeing it. In accordance to exactly how the code was written in the OP this works perfectly:

else if (message.type === 'binary') {

  //Send to Google Speech API by passing into recognizeStream
  recognizeStream.write(message.binaryData)

}

Upvotes: 1

Kalana Demel
Kalana Demel

Reputation: 3266

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

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

Upvotes: 0

Related Questions