重新取样音频缓冲从44100到16000 [英] resample audio buffer from 44100 to 16000

查看:3276
本文介绍了重新取样音频缓冲从44100到16000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据URI的格式,那么我现在转换这个数据-URI到缓冲区中,我需要在新的采样率这个缓冲区的数据的音频数据,目前的音频数据是44.1,我需要在16kHz的数据,如果我使用RecordRTC API,如果我记录低采样率后来,我被扭曲的音频语音音频记录的声音,所以我没有得到如何我重新取样音频缓冲,

I have audio data in format of data-uri, then I converted this data-uri into a buffer now I need this buffer data in new samplerate, currently audio data is in 44.1khz and I need data in 16khz, and If I recorded the audio using RecordRTC API and if I record audio in low sample rate then I got distorted audio voice, So I am not getting how to resample my audio buffer,

如果你们任何人对此任何想法,那么请帮助我。

If any of you any idea regarding this then please help me out.

在此先感谢:)

推荐答案

您可以使用OfflineAudioContext做重采样,但你需要你的数据-URI先转换为ArrayBuffer。该解决方案可在浏览器中,而不是在服务器上,因为它是更好地在网络上发送低质量的音频(较低的采样率),比发送大量数据,并重新取样的服务器上。

You can use an OfflineAudioContext to do the resampling, but you need to convert your data-uri to an ArrayBuffer first. This solution works in the browser, not on the server, as it's better to send lower quality audio (lower sample rate) on the network, than send a lot of data and resample on the server.

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

Let's assume that's done and your data is in a Float32Array called yourBufferAt44100. If you have integers instead of float, convert them like so:

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();

这篇关于重新取样音频缓冲从44100到16000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆