Skip to content

Instantly share code, notes, and snippets.

@cf
Created March 23, 2015 01:30
Show Gist options
  • Save cf/78aa9c67ac7820f2f810 to your computer and use it in GitHub Desktop.
Save cf/78aa9c67ac7820f2f810 to your computer and use it in GitHub Desktop.
var WAVE={};
(function(){
function str16(x){return (x>15)?x.toString(16):"0"+x.toString(16);}
function shortToStr(x){return "%"+str16(x&0xff)+"%"+str16((x>>>8)&0xff);}
function intToStr(x){return "%"+str16(x&0xff)+"%"+str16((x>>>8)&0xff)+"%"+str16((x>>>16)&0xff)+"%"+str16((x>>>24)&0xff);}
WAVE.makeWavDecimal=function(channels,sampleFrequency)
{
var str="data:audio/x-wav,%52%49%46%46"+
intToStr(channels.length*2*channels[0].length+0x24)+
"%57%41%56%45%66%6D%74%20"+
intToStr(0x10)+
shortToStr(1)+
shortToStr(channels.length)+
intToStr(sampleFrequency)+
intToStr(channels.length*2*sampleFrequency)+
shortToStr(2*channels.length)+
shortToStr(0x10)+
"%64%61%74%61"+
intToStr(channels.length*2*channels[0].length);
var l=channels[0].length;
for(var i=0;i<l;i++)
{
for(var c=0;c<channels.length;c++)
{
str+=shortToStr(Math.round((channels[c][i]+1)/2*32767.5));
}
}
return str;
};
WAVE.makeWav=function(channels,sampleFrequency)
{
var str="data:audio/x-wav,%52%49%46%46"+
intToStr(channels.length*2*channels[0].length+0x24)+
"%57%41%56%45%66%6D%74%20"+
intToStr(0x10)+
shortToStr(1)+
shortToStr(channels.length)+
intToStr(sampleFrequency)+
intToStr(channels.length*2*sampleFrequency)+
shortToStr(2*channels.length)+
shortToStr(0x10)+
"%64%61%74%61"+
intToStr(channels.length*2*channels[0].length);
var l=channels[0].length;
for(var i=0;i<l;i++)
{
for(var c=0;c<channels.length;c++)
{
str+=shortToStr(channels[c][i]);
}
}
return str;
};
WAVE.makeWavFunc=function(f,duration,sampleFrequency)
{
var nVals=duration*sampleFrequency;
var str="data:audio/x-wav,%52%49%46%46"+
intToStr(2*nVals+0x24)+
"%57%41%56%45%66%6D%74%20"+
intToStr(0x10)+
shortToStr(1)+
shortToStr(1)+
intToStr(sampleFrequency)+
intToStr(2*sampleFrequency)+
shortToStr(2)+
shortToStr(0x10)+
"%64%61%74%61"+
intToStr(2*nVals);
for(var i=0;i<nVals;i++)
{
str+=shortToStr(Math.round(f(i)));
}
return str;
};
})();
function TestIt()
{
var audio=document.createElement("audio");
audio.src=WAVE.makeWavFunc(function(t){return w=t>>9,k=32,m=2048,a=1-t/m%1,d=(14*t*t^t)%m*a,y=[3,3,4.7,2][p=w/k&3]*t/4,h="IQNNNN!!]]!Q!IW]WQNN??!!W]WQNNN?".charCodeAt(w/2&15|p/3<<4)/33*t-t,s=y*.98%80+y%80+(w>>7&&a*((5*t%m*a&128)*(0x53232323>>w/4&1)+(d&127)*(0xa444c444>>w/4&1)*1.5+(d*w&1)+(h%k+h*1.99%k+h*.49%k+h*.97%k-64)*(4-a-a))),s*s>>14?127:s;},60,8000);
document.body.appendChild(audio);
audio.play();
}
TestIt();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment