0
我試圖將語音轉換爲節點服務器中使用AudioContext在瀏覽器中進行語音錄製的文本。我可以通過binaryType:arraybuffer的WebSocket連接將int16Array緩衝區(記錄數據)發送到我的節點服務器。NodeJS將Int16Array二進制緩衝區轉換爲Google語音API的LINEAR16編碼原始流
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 ...>
現在我想解析或轉換成可讀的流,這樣我可以管谷歌語音recognizeStream。
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)
一些想法:http://stackoverflow.com/questions/16038705/how-to-wrap-a-buffer-as-a -stream2-readable-stream –
謝謝,@MichaelTallino我用streamifier&stream試過了,但沒有運氣。 –