我正在使用 Google Cloud Speech 来识别 node 服务器上的流式 audios(来自麦克风)。同时我想将 store 流式 audio 放入一个文件中。由于流式识别是在缓冲区块上操作的,我如何将所有缓冲区和 store 组合为单个 audio 文件?
我使用的实际代码改编自 https://github.com/vin-ni/Google-Cloud-Speech-Node-Socket-Playground,其中服务器端的 audio 编码定义为
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
每次有 audio 流时,它都会通过云 API 发送:
client.on('binaryData', function (data) {
if (recognizeStream !== null) {
recognizeStream.write(data);
}
});
在客户端,缓冲区在 https://github.com/vin-ni/Google-Cloud-Speech-Node-Socket-Playground/blob/master/src/public/js/recorderWorkletProcessor.js 中进行了预处理(下采样为 Int16Array
)。
那么,当流式传输完成时,我可以连接每个缓冲区并保存到服务器端的 pcm
文件吗?还是我需要以复杂的方式合并缓冲区(例如使用 https://github.com/Jam3/audiobuffer-to-wav?
回答1
https://stackoverflow.com/questions/51399121/how-to-save-int16array-buffer-to-wav-file-node-js 有一个这样的答案,它完全解决了我的问题。它使用 wav
库添加标题,然后将 audio 缓冲区写入文件。