Skip to content

Instantly share code, notes, and snippets.

@MattRoelle
Created June 19, 2014 02:57
Show Gist options
  • Save MattRoelle/63b663a80da7cc79fc56 to your computer and use it in GitHub Desktop.
Save MattRoelle/63b663a80da7cc79fc56 to your computer and use it in GitHub Desktop.
wavepot.com is my new addiction, livecoding in javascript
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