我正在尝试对从 audioContext 获得的采样率进行下采样。我相信它会在 44100 进入,我希望它是 11025。我想我可以平均每 3 个样本并以正确的速率播放,但是音高太高,好像我们都在氦气上.
在 11025 个样本处将 float32Array 从 44100 下采样到 int16Array 的正确方法是什么。
var context = new Flash.audioContext();
var audioInput = context.createMediaStreamSource(stream);
var recorder = context.createScriptProcessor(null, 1, 1);
recorder.onaudioprocess = onAudio;
audioInput.connect(recorder);
recorder.connect(context.destination);
var onAudio = function (e) {
var left = e.inputBuffer.getChannelData(0);
bStream.write(Flash.convertFloat32ToInt16(left));
}
var convertFloat32ToInt16 = function(buffer) {
var l = buffer.length;
var point = Math.floor(l/3);
var buf = new Int16Array(point);
for (var x = l; x > 0;) {
var average = (buffer[x] + buffer[x-1] + buffer[x-2]) / 3;
buf[point] = average*0x7FFF;
point -= 1;
x -= 3;
}
return buf.buffer;
}