play pcm data by webAudio API play pcm data by webAudio API google-chrome google-chrome

play pcm data by webAudio API


The Web Audio API uses 32-bit signed floats from -1 to 1, so that's what I'm going to (hopefully) show you how to do, rather than 16-bit as you mentioned in the question.

Assuming your array of samples is called samples and are stored as 2's compliment from -128 to 127, I think this should work:

var floats = new Float32Array(samples.length);samples.forEach(function( sample, i ) {  floats[i] = sample < 0 ? sample / 0x80 : sample / 0x7F;});

Then you can do something like this:

var ac = new webkitAudioContext()  , ab = ac.createBuffer(1, floats.length, ac.sampleRate)  , bs = ac.createBufferSource();ab.getChannelData(0).set(floats);bs.buffer = ab;bs.connect(ac.destination);bs.start(0);