我正在尝试通过网络套接字将客户端的音频流发送到NodeJS后端(用于语音到文本处理)。
当我在浏览器中将一个简短的.wav文件加载到AudioBuffer中时,使用Float32Array
方法将音频数据提取为getChannelData()
并通过websocket将结果ArrayBuffer发送到我的服务器进行播放,由于某种原因,音频会叠加很多静态噪声。
我尝试了多种发送音频数据的方法,包括将其作为字符串发送(以防止浮点数舍入),还尝试了多种将接收到的音频数据转换为ReadableStream的方法,所有方法都具有相同的结果。
以下是相关代码:
客户:
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
function loadAudioBuffer() {
return new Promise((resolve, reject) => {
fetch(`/test.wav`)
.then(response => response.arrayBuffer())
.then(arrayBuffer => audioCtx.decodeAudioData(arrayBuffer))
.then(audioBuffer => resolve(audioBuffer));
})
}
(async () => {
let customBuffer = await loadAudioBuffer();
const ws = new WebSocket(`ws://localhost:9999/socket`);
ws.onopen = () => {
ws.send(customBuffer.getChannelData(0).toString());
}
})()
服务器:
const { Readable } = require(`stream`);
const Speaker = require('speaker');
// Create the Speaker instance
const speaker = new Speaker({
channels: 1,
bitDepth: 16, // 16-bit samples
sampleRate: 44100 // 44,100 Hz sample rate
});
const output = new Readable({
read() {}
});
output.pipe(speaker);
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
let floatArray = new Float32Array(message.split(`,`));
let audioBuffer = Buffer.allocUnsafe(floatArray.byteLength);
let offset = 0;
floatArray.map(floatValue => {
audioBuffer.writeFloatLE(floatValue, offset);
offset += 4;
})
output.push(audioBuffer);
});
});
我还尝试通过websocket发送原始ArrayBuffer并将接收到的Buffer立即推到扬声器,这会导致相同的静态干扰。
我在这里缺少明显的东西吗?