Last active
June 16, 2017 09:57
-
-
Save Qix-/7c131c6eb83bef7c44ef6faaf1eab106 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// it's a thousand degrees and I can't sleep, it's thursday, | |
// i'm feeling saucy and so I decided to make a song out of math. | |
// | |
// super, super high def audio (176,400 Hz = HDCD) | |
// | |
// to run: | |
// - install ffmpeg (`brew install ffmpeg` or `apt install ffmpeg`) | |
// - run (without backticks) `node bouncy-egyptian-song.js | ffplay -f f64le -ar 176400 -` | |
// ^ | |
// this hyphen is required! > > ^ | |
S = Math.sin; | |
N = Math.random; | |
function bouncyEgyptianSong(t) { | |
return Math.min(1,(1/17)*(((S(t)+1)/4+0.5)*S(t*[1000,0,1000,0,1400,1800,0,900][Math.floor(t*8)%8])+(N()*0.15*(1-Math.pow((t*8)%1,-1)))+(S(t*390)*(1-Math.pow((t*8)%1,-1))*[4,0,0,1,4,0,1,0.5][Math.floor(t*8)%8])+(S(t*5200)*[0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0][Math.floor(t*16)%16]*([1,0][Math.floor(t*16%2)]))+(S(t*5140)*Math.sin(t*16)%16*0.2)+(N()*(Math.floor(t*4%2)==1?5:0)*Math.pow(1-((t*4)%1),10))+((n=>Math.round(S(t*(2600+n)))+Math.round(S(t*(2640+n))))([0,0,0,100,400,0,0,100,0,0,-440,0,-540,0,-750,0][Math.floor(t*4)%16])*(1-Math.pow(t*4%1,9))*1.2*[1,0.2,0,0.05][Math.floor(t*32)%4]*(0.25+Math.floor(t*8%4)))+(S(t*[480,250,250,250,250,250,450,250][Math.floor(t*8)%8])*3*(1-Math.pow(t*8%1,2))))) | |
} | |
bufferSize = 48000; //samples | |
rate = 176400; //sample rate | |
buffer = new Buffer(bufferSize * 8); | |
sampleLength = 1000 / rate / 1000; | |
time = 0.0; | |
function tick() { | |
for (i = 0; i < bufferSize; i++) { | |
offset = i * 8; | |
buffer.writeDoubleLE(bouncyEgyptianSong(time)||0.0, offset); | |
time += sampleLength; | |
} | |
process.stdout.write(buffer); | |
} | |
setInterval(tick, 240); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment