public
Created

wave loops

  • Download Gist
wave.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
var baudio = require('baudio');
var tau = Math.PI * 2;
 
var b = baudio(function (t) {
if (t > 30) return b.end();
return beep(t) * 0.25 + boop(t) * 0.25 + wub(t) * 0.5;
});
b.record('400.ogg');
 
function beep (t) {
var riff = [ 1, 2/3, 0, 0, 2/3, 1, 0, 0 ];
var note = riff[Math.floor(t % riff.length)];
if (note === 0) return 0;
if (t % 0.5 > 0.25) return 0;
return Math.sin(tau * t * 200 * Math.pow(2, note / 12)) * 0.05
+ Math.sin(tau * t * 400 * Math.pow(2, note / 12)) * 0.01
+ Math.sin(tau * t * 6400 * Math.pow(2, note / 12)) * 0.0625
;
}
 
function boop (t) {
var riff = [ 1, 3 / 2, 5 / 4, 0, 6 / 5, 3 / 2, 5 / 3, 0 ];
var note = riff[Math.floor(t % riff.length)];
if (note === 0) return 0;
return Math.sin(tau * t * 6400 * note) * 0.125
* Math.sin(tau * t * 8)
;
}
 
function wub (t) {
var x = Math.sin(tau * t * 80);
var y = Math.sin(tau * t * 85);
var n = [ 12, 3 ][Math.floor(t / 4 % 2)];
return (x + y) / 2 * Math.sqrt(Math.sin(t * tau * (t % 4 < 3 ? 2 : n)));
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.