Skip to content

@substack /beat.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
beep boop computer music beats
var baudio = require('baudio');
var b = baudio({ rate : 44100, channels : 1 });
var duration = Number(process.argv[2]) || Number.MAX_VALUE;
b.addChannel(function (t) {
if (t >= duration) b.end();
return baseline(t) * 8;
});
b.addChannel(function (t) {
return baseline(t) + Math.sqrt(melody(t));
});
b.push(1, function (t) {
var x = 14;
if (t % 16 < x) return 0;
var f = [ 4, 3.5, 2.5, 2, 5, 1, 0, 0 ][Math.floor((t - x) * 4 % 8)];
var res = Math.sin(2 * Math.PI * t * 400 * f)
+ Math.sin(2 * Math.PI * t * 400 * (f + 2 * Math.sin(t / 4)))
;
return res * 0.5;
});
var melody = (function () {
var notes = [
0, 0, 0, 0,
0, 0, 0, 0,
5, 3, 3.5, 3.5,
0, 0, 0, 0,
0, 0, 0, 0,
5, 3, 2.5, 2.5,
0, 0, 0, 0,
0, 0, 0, 0,
5, 5, 0, 0,
3, 3, 0, 0,
5, 3, 3.5, 3.5,
3, 3, 2.5, 2.5,
3, 3, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
];
return function (t) {
var n = notes[Math.floor((t * 4) % notes.length)];
if (n === 0) return 0;
var f = 200 * Math.pow(2, 1 + n / 5);
return Math.sin(2 * Math.PI * t * f);
};
})();
function baseline (t) {
var f = [ 40, 50, 20, 60 ][Math.floor((t * 4) % 4)];
var d = [ 16, 8, 10, 12 ][Math.floor((t * 4) % 4)] / 8;
var a = Math.sin(2 * Math.PI * t * f)
var b = Math.sin(2 * Math.PI * t * (f + d));
return Math.min(1, Math.max(-1, a + b));
}
if (process.stdout.isTTY) b.play()
else b.pipe(process.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.