重新采样audio缓冲区从44100到16000

我有data-uri格式的audio数据,然后我把这个data-uri转换成缓冲区,现在我需要这个缓冲区数据在新的采样率,当前audio数据是在44.1khz,我需要在16khz的数据,如果我loggingaudio使用RecordRTC API,如果我以低采样率loggingaudio,那么我得到了失真的audio声音,所以我没有得到如何重新采样我的audio缓冲区,

如果您有任何关于这方面的想法,那么请帮助我。

提前致谢 :)

您可以使用OfflineAudioContext来进行重新采样,但是您需要首先将您的data-uri转换为ArrayBuffer。 此解决scheme在浏览器中工作,而不是在服务器上,因为在networking上发送质量较低的audio(较低的采样率)要好于在服务器上发送大量数据并重新采样。

假设完成了,你的数据在一个名为yourBufferAt44100的Float32Array中。 如果你有整数而不是浮点数,就像这样转换它们:

for (var i = 0; i < count; i++) { ints[i] = floats[i] * Math.pow(2, 16) / 2; } 

然后:

 /* Get an OfflineAudioContext at 16000Hz (the target sample rate). * `durationInSamples` is the number of audio samples you have. * `channels` is the number of channels (1 for mono, 2 for stereo). */ var o = new OfflineAudioContext(channels, durationInSamples, 16000); /* Get an empty AudioBuffer at 44100Hz */ var b = o.createBuffer(channels, durationInSamples, 44100); /* Copy your data in the AudioBuffer */ for (var channel = 0; i < channels; channels++) { var buf = b.getChannelData(channe); for (var i = 0; i < durationInSamples; i++) { buf[i] = yourBufferAt44100[i]; } } /* Play it from the beginning. */ var source = o.createBufferSource(); source.buffer = b; source.connect(o.destination); source.start(0); o.oncomplete = function(audiobuffer) { /* audiobuffer contains audio resampled at 16000Hz, use * audiobuffer.getChannelData(x) to get an ArrayBuffer for * channel x. */ } /* Start rendering as fast as the machine can. */ o.startRendering(); 
Interesting Posts