Skip to content

Instantly share code, notes, and snippets.

@Marak
Created June 11, 2010 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Marak/435148 to your computer and use it in GitHub Desktop.
Save Marak/435148 to your computer and use it in GitHub Desktop.
/*******************************
JSONloops is a javascript audio sequencer that runs on JSON arrays of sounds
JSONloops are nested arrays that contain: a nested array, a sound, or null
the arrays are nested in the following hierarchy
* SONG
* TRACKS
* MEASURES
* BEATS
a 2 measure song with 4 beats in each measure might be structured as something like this
* SONG
* TRACK1
* MEASURE1
* BEAT
* BEAT
* BEAT
* BEAT
* MEASURE2
* BEAT
* BEAT
* BEAT
* BEAT
* TRACK2
* MEASURE1
* BEAT
* BEAT
* BEAT
* BEAT
* MEASURE2
* BEAT
* BEAT
* BEAT
* BEAT
* TRACK3
* MEASURE1
* BEAT
* BEAT
* BEAT
* BEAT
* MEASURE2
* BEAT
* BEAT
* BEAT
* BEAT
*******************************/
var sys = require('sys');
var play = require('./lib/play');
// measures in song
var measures = 1;
// beats per measure
var beats = 4;
// beats per minute
var bpm = 60;
// the current beat position
var position = 0;
// simple drum kit
var drums = {
kick : './wavs/kick.wav',
snare : './wavs/snare.wav',
tick : './wavs/tick.wav'
};
// simple 4 beat drum loop, notice the empty arrays which represent beats with no note playing
var song = [
[[[drums.kick], [],[drums.kick], [drums.kick]],[[drums.kick], [],[drums.kick], [drums.kick]]],
[[[],[],[],[]],[[],[],[],[]]],
[[[drums.tick],[drums.tick],[drums.tick],[drums.tick]],[[drums.tick],[drums.tick],[drums.tick],[drums.tick]]]
];
var theMix = [];
function json_loop( song ){
// for every measure in the song
for(var m = 0; m < measures; m++){
// for every measure in track
for(var b = 0; b < beats; b++){
// for every track in the song
for(var t = 0; t < song.length; t++){
//play.start(song[t][m][b]);
theMix.push(song[t][m][b]);
//sys.puts(JSON.stringify(song[t][m][b]));
}
}
}
}
function step(steps){
if(!steps.length){
json_loop(song);
step(theMix);
return;
}
setTimeout(function(){
// calculate the node we are going to pull and play now
for(var b = 0; b < beats; b++){
play.start(steps.pop());
}
step(steps);
},60000 / bpm);
}
json_loop(song);
step(theMix);
sys.puts(JSON.stringify(theMix));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment