Skip to content

Instantly share code, notes, and snippets.

@egonelbre
Created April 23, 2011 20:19
Show Gist options
  • Save egonelbre/938938 to your computer and use it in GitHub Desktop.
Save egonelbre/938938 to your computer and use it in GitHub Desktop.
wave generation speed up
freq = 770; len = 10; rate = 44100; vol = 1.0;
start = new Date();
w = '';
phase = 0;
sin = Math.sin;
chr = String.fromCharCode;
total = len*rate;
shift = 2*Math.PI*freq/rate
for(var i=0; i < total; i++){
var v = sin(phase) * vol;
phase += shift;
w += chr( v & 255, (v>>8) & 255 );
}
stop = new Date();
console.log('done 1',stop - start);
function i2b(b,i) { with (String) switch (b) {
case 16: return fromCharCode(i&255,(i>>8)&255);
case 32: return fromCharCode(i&255,(i>>8)&255,(i>>16)&255,(i>>24)&255);
}}
start = new Date();
var w = '';
with (Math)
for (var i = 0; i < len * rate; i++)
w += i2b(16, sin((2*PI)*(i/rate)*freq) * vol);
stop = new Date();
console.log('done 2', stop - start);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment