如何将websocket二进制消息作为流发送到Google Speech API?

时间:2017-03-09 20:11:19

标签: node.js websocket google-speech-api

我尝试将websocket连接的音频流发送到Google Speech API。 websocket以20ms的增量发送二进制消息。它以递增的方式发送它让我相信我会以某种方式暂时读取和写入数据到本地文件,以避免终止与Google的连接。但是,这并不理想。

有没有办法直接将websocket流管道导入recognizeStream

来自文档的Google streamingRecognize示例:

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连接:

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


});

2 个答案:

答案 0 :(得分:0)

最好的解决方案是使用专门的流媒体解决方案而不是自己动手,这将处理所有缓冲区并为您提供适合Google Speech API的稳定流。尝试使用类似的方法,

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

答案 1 :(得分:0)

这实际上非常简单。如此简单,我觉得有点羞怯没有看到它。根据OP中代码的确切编写方式,这非常有效:

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

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

}