Created
June 19, 2014 02:57
-
-
Save MattRoelle/63b663a80da7cc79fc56 to your computer and use it in GitHub Desktop.
wavepot.com is my new addiction, livecoding in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var generated = false; | |
var currentNote = false; | |
var bpm = 600; | |
var sequence = [ | |
squareWave(50, 0.1), | |
sineWave(440, 0.4), | |
squareWave(50, 0.1), | |
sineWave(540, 0.4), | |
squareWave(50, 0.1), | |
sineWave(640, 0.4), | |
squareWave(50, 0.1), | |
sineWave(340, 0.4), | |
squareWave(50, 0.1), | |
sineWave(440, 0.4), | |
reset | |
]; | |
function dsp(t) { | |
if (!generated) { | |
sequence.forEach(function(note, i) { | |
setTimeout(function() { | |
currentNote = note; | |
}, i * (60000 / bpm)); // bpm | |
}); | |
generated = true; | |
} | |
if (currentNote) { return currentNote(t); } | |
else { return 0.1 * Math.sin(Math.PI * 2 * 440); } | |
} | |
// Sample generation functions | |
function sineWave(freq, amp) { | |
return function(t) { | |
return amp * Math.sin(2 * Math.PI * t * freq); | |
}; | |
} | |
function squareWave(freq, amp) { | |
return function(t) { | |
return amp * (((new Date()).getMilliseconds() % freq*10000) > (freq*10000)/2) ? 1: 0 | |
} | |
} | |
function reset() { | |
generated = false; | |
currentNote = false; | |
} | |
function rest() { return 0; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment