Pregunta

For a project, I'm retrieving a live audio stream via WebSockets from a Java server. On the server, I'm processing the samples in 16Bit/8000hz/mono in the form of 8-bit signed byte values (with two bytes making up one sample). On the browser, however, the lowest supported samplerate is 22050 hz. So my idea was to "simply" upsample the existing 8000 to 32000 hz, which is supported and seems to me like an easy calculation.

So far, I've tried linear upsampling and cosine interpolation, but both didn't work. In addition to sounding really distorted, the first one also added some clicking noises. I might also have trouble with the WebAudioAPI in Chrome, but at least the sound is playing and is barely recognizable as what it should be. So I guess no codec- or endianess-problem.

Here's the complete code that gets executed when a binary packet with sound data is received. I'm creating new buffers and buffersources all the time for the sake of simplicity (yeah, no good for performance). data is an ArrayBuffer. First, I'm converting the samples to Float, then I'm upsampling.

//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);
¿Fue útil?

Solución

Finally found a solution for this. The conversion from 16Bit to Float is wrong, it just needs to be

floatBuffer8000[i]=j/32767.0;

Also, feeding the API with a lot of small samples doesn't work well, so you need to buffer some samples and play them together.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top