Skip to content

Instantly share code, notes, and snippets.

@steve-chavez
Last active August 29, 2015 14:05
Show Gist options
  • Save steve-chavez/fd60fcb3e339fec81b59 to your computer and use it in GitHub Desktop.
Save steve-chavez/fd60fcb3e339fec81b59 to your computer and use it in GitHub Desktop.
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var samples = [];
for( var i=0; i<1000; i++){ samples[i] = getRandomInt(-128,127);}
var floats = new Float32Array(samples.length);
samples.forEach(function( sample, i ) {
floats[i] = sample < 0 ? sample / 0x80 : sample / 0x7F;
});
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment