Instantly share code, notes, and snippets.

substack/wave.js Created May 3, 2013

What would you like to do?
wave loops
 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))); }