Skip to content

Instantly share code, notes, and snippets.

@phmongeau
Created October 12, 2014 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phmongeau/803783f4e5d13dc8fd14 to your computer and use it in GitHub Desktop.
Save phmongeau/803783f4e5d13dc8fd14 to your computer and use it in GitHub Desktop.
music thingy
function pulse(freq, offset, t) {
return Math.sin(2 * Math.PI * t * freq)
+ Math.sin(2 * Math.PI * t * (freq + (1* offset)))
+ Math.sin(2 * Math.PI * t * (freq + (2*offset)));
}
function noise() {
return Math.random();
}
function sqr(freq, t) {
return Math.sin(2 * Math.PI * t * freq) > 0 ? 1 : -1;
}
function drum(freq, t, n) {
return sqr(freq, (t + Math.random() * 0.001));
}
function saw(freq, t) {
return (2 * ((freq * t) - floor(t * freq)) - 1);
}
function dsp(t) {
var notes = [100, 148, 10, 350, 10, 10, 100, 150, 5, 200, 10, 600, 1, 600, 200, 50];
var notes2 = [500, 200, 500, 100, 500, 10, 10, 350];
var cmel = (Math.floor(t*0.25)) % 2 ? notes: notes2;
var n = cmel[Math.floor(t*8) % cmel.length];
var d = cmel[(Math.floor(t*4) - 1) % cmel.length];
return 0.3 * pulse(80, 8, t) * sqr(n, t) * Math.sin(2 * Math.PI * t * 8)
+ (t % .25 < .08 ? drum(d/10, t, 1) * 1: 0)
+ sqr(d, t) * .6;
}
@phmongeau
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment