Note that there are some explanatory texts on larger screens.

plurals
  1. POUpsampling Audio PCM-data in Javascript with WebAudioApi
    primarykey
    data
    text
    <p>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. </p> <p>So far, I've tried <a href="https://stackoverflow.com/questions/5407452/pcm-algorithm-for-upsampling">linear upsampling</a> and <a href="http://paulbourke.net/miscellaneous/interpolation/" rel="nofollow noreferrer">cosine interpolation</a>, 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.</p> <p>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). <code>data</code> is an ArrayBuffer. First, I'm converting the samples to <a href="https://stackoverflow.com/questions/6010708/html5-web-audio-api-porting-from-javax-sound-and-getting-distortion">Float</a>, then I'm upsampling.</p> <pre><code>//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 =&gt; Float for(var i=0,j=null;i&lt;160;i++){ j=bufferView.getInt16(i*2,false); floatBuffer8000[i]=(j&gt;0)?j/32767:j/-32767; } //convert 8000 =&gt; 32000 var point1,point2,point3,point4,mu=0.2,mu2=(1-Math.cos(mu*Math.PI))/2; for(var i=0,j=0;i&lt;160;i++){ //index for dst buffer j=i*4; //the points to interpolate between point1=floatBuffer8000[i]; point2=(i&lt;159)?floatBuffer8000[i+1]:point1; point3=(i&lt;158)?floatBuffer8000[i+2]:point1; point4=(i&lt;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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload