我尝试将语音转换为节点服务器中的文本,其中使用AudioContext在浏览器中进行语音录制。我能通过binaryType:arraybuffer的WebSocket连接将int16Array缓冲区(记录的数据)发送到我的节点服务器。
this.processor.onaudioprocess = (e) => {
// this.processAudio(e)
for (
var float32Array = e.inputBuffer.getChannelData(0) || new Float32Array(this.bufferSize),
len = float32Array.length,
int16Array = new Int16Array(len);
len--;)
int16Array[len] = 32767 * Math.min(1, float32Array[len]);
this.socket.send(int16Array.buffer);
};
在服务器中,数据以
的形式接收<Buffer 66 6f 6f ...>
现在我想解析或转换为可读的流,以便我可以通过Google语音识别流。
function processAudioBuffer(int16ArrayBuffer) {
console.log("Received stream :", int16ArrayBuffer, typeof
recognizeStreams[userId]);
const recognizer = getGoogleSpeechStreamRecognizer();
if (recognizer) {
/* HERE I NEED SOMETHING WHICH MAKES MY BUFFER COMPATIBLE WITH GOOGLE SPEECH API */
// tried with streamifier but no luck
// streamifier.createReadStream(int16ArrayBuffer).pipe(recognizer);
// also tried with Record which is used in google-cloud-node-samples to record stream from connected mic device, but no luck
var file = new Record({
path: `${userId}.raw`,
encoding: 'arraybuffer',
contents: int16ArrayBuffer
});
file.pipe(recognizer);
} else {
console.log('user stream is not yet created');
}
}
识别器会抛出以下错误:
Error: write after end
at writeAfterEnd (/Users/demo/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:222:12)
at Writable.write (/Users/demo/node_modules/duplexify/node_modules/readable-stream/lib/_stream_writable.js:262:20)
at Duplexify.end (/Users/demo/node_modules/duplexify/index.js:223:18)
at Record.pipe (/Users/demo/node_modules/record/index.js:70:14)
at processAudioBuffer (/Users/demo/app.js:87:10)
at WebSocket.incoming (/Users/demo/app.js:104:7)
at emitTwo (events.js:106:13)
at WebSocket.emit (events.js:191:7)
at Receiver._receiver.onmessage (/Users/demo/node_modules/ws/lib/WebSocket.js:146:54)
at Receiver.dataMessage (/Users/demo/node_modules/ws/lib/Receiver.js:380:14)
答案 0 :(得分:2)
解决了!!!我们可以将缓冲区直接写入使用GoogleSpeech创建的recognizerStream,如下所示:
const recognizer = getGoogleSpeechStreamRecognizer();
recognizer.write(int16ArrayBuffer)