【问题标题】:Upsampling Audio PCM-data in Javascript with WebAudioApi使用 WebAudioApi 在 Javascript 中对音频 PCM 数据进行上采样
【发布时间】:2012-12-17 21:21:35
【问题描述】:

对于一个项目,我通过 WebSockets 从 Java 服务器检索实时音频流。在服务器上,我以 8 位有符号字节值的形式处理 16Bit/8000hz/mono 的样本(两个字节组成一个样本)。但是,在浏览器上,支持的最低采样率为 22050 Hz。所以我的想法是“简单地”对现有的 8000 到 32000 赫兹进行上采样,这是受支持的,在我看来是一个简单的计算。

到目前为止,我已经尝试过linear upsamplingcosine interpolation,但都没有成功。除了听起来确实失真之外,第一个还添加了一些咔哒声。我在 Chrome 中的 WebAudioAPI 可能也有问题,但至少声音正在播放并且几乎无法识别它应该是什么。所以我猜没有编解码器或字节序问题。

这是接收到带有声音数据的二进制数据包时执行的完整代码。为了简单起见,我一直在创建新的缓冲区和缓冲区源(是的,对性能没有好处)。 data 是一个 ArrayBuffer。首先,我将样本转换为Float,然后进行上采样。

//endianess-aware buffer view
var bufferView=new DataView(data),
//the audio buffer to set for output
buffer=_audioContext.createBuffer(1,640,32000),
//reference to underlying buffer array
buf=buffer.getChannelData(0),
floatBuffer8000=new Float32Array(160);


//16Bit => Float
for(var i=0,j=null;i<160;i++){
    j=bufferView.getInt16(i*2,false);
    floatBuffer8000[i]=(j>0)?j/32767:j/-32767;
}   

//convert 8000 => 32000
var point1,point2,point3,point4,mu=0.2,mu2=(1-Math.cos(mu*Math.PI))/2;
for(var i=0,j=0;i<160;i++){
    //index for dst buffer
    j=i*4;

    //the points to interpolate between
    point1=floatBuffer8000[i];
    point2=(i<159)?floatBuffer8000[i+1]:point1;
    point3=(i<158)?floatBuffer8000[i+2]:point1;
    point4=(i<157)?floatBuffer8000[i+3]:point1;


    //interpolate
    point2=(point1*(1-mu2)+point2*mu2);
    point3=(point2*(1-mu2)+point3*mu2);
    point4=(point3*(1-mu2)+point4*mu2);

    //put data into buffer
    buf[j]=point1;
    buf[j+1]=point2;
    buf[j+2]=point3;
    buf[j+3]=point4;
}

//playback
var node=_audioContext.createBufferSource(0);
node.buffer=buffer;
node.connect(_audioContext.destination);
node.noteOn(_audioContext.currentTime);

【问题讨论】:

    标签: javascript pcm web-audio-api resampling


    【解决方案1】:

    终于找到了解决办法。 16Bit到Float的转换是错误的,只需要是

    floatBuffer8000[i]=j/32767.0;
    

    另外,向 API 提供大量小样本效果不佳,因此您需要缓冲一些样本并将它们一起播放。

    【讨论】:

    • 如何将任何一种采样率转换为另一种?不仅8000到32000
    猜你喜欢
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 2013-08-04
    • 2020-05-19
    • 1970-01-01
    • 2016-09-07
    相关资源
    最近更新 更多