/* * ByteWarrior library for playing and exporting bytebeat tunes in your browser * by Multiversum * Usage: new ByteWarrior(pattern_string, sampleRate, duration), then call play() and pause() methods * Pattern string is mandatory, you can omit all other parameters, then their defaults will be: * sample rate - 8000 Hz, duration - 16 sec * pattern_string accepts a JS expression from single 't' parameter * The following functions can be specified without Math: sin, cos, tan, floor, ceil * Example: * var bb = new ByteWarrior("((t<<1)^((t<<18)+(t>>7)&t>>12))|t>>(4-(1^7&(t>>19)))|t>>7"); bb.play() * Export to WAV is done with accessing wavedata parameter: * var bb = new ByteWarrior(pattern,8000,120); * var wavDataURI = bb.wavedata; * This library is public domain. Have fun! */ ByteWarrior = function(pstring, sampleRate, duration) { this.rate = sampleRate||8000; this.duration = duration||16; this.pattern = eval("(function(t){return "+pstring.replace(/sin|cos|tan|floor|ceil/g,function(s){return "Math."+s})+"})"); this.audio = new Audio(); this.toWav = function(){ var nsamples = this.duration*this.rate, i2b=function(v){return [0xFF&v,0xFF&(v>>8),0xFF&(v>>16),0xFF&(v>>24)]}, wave=[].concat([82,73,70,70],i2b(44+nsamples), [87,65,86,69,102,109,116,32,16,0,0,0,1,0,1,0],i2b(this.rate),i2b(this.rate), [1,0,8,0,100,97,116,97],i2b(nsamples)); for(var t=0;t