public
Last active

beep boop computer music beats

  • Download Gist
beat.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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.