using samples to make a drum machine
var audio = new maximJs.maxiAudio();
audio.init();
var kick = new maximJs.maxiSample();
var snare = new maximJs.maxiSample();
var hat = new maximJs.maxiSample();
var timer = new maximJs.maxiOsc();
var currentCount,lastCount,playHead = 0;
var kickhit = [1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1]; //This is the sequence for the kick
var snarehit=[0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0];//This is the sequence for the snare
var hathit=[0,0,1,0, 0,0,1,0, 0,0,1,0, 0,0,1,1];//This is the sequence for the snare
var kicktrigger,snaretrigger, hattrigger;
var sampleOut;
audio.loadSample('audio/kick1.wav', kick);
audio.loadSample('audio/snare1.wav', snare);
audio.loadSample('audio/hat12.wav', hat);
audio.play = function(){
// if(kick.isReady() && snare.isReady()){
currentCount=Math.floor(timer.phasor(8));//this sets up a metronome that ticks 8 times a second
if (lastCount!=currentCount) {//if we have a new timer int this sample, play the sound
kicktrigger=kickhit[playHead%16];//get the value out of the array for the kick
snaretrigger=snarehit[playHead%16];//same for the snare
hattrigger=hathit[playHead%16];
playHead++;//iterate the playhead
lastCount=0;//reset the metrotest
}
if (kicktrigger==1) {//if the sequence has a 1 in it
kick.trigger();//reset the playback position of the sample to 0 (the beginning)
}
if (snaretrigger==1) {
snare.trigger();//likewise for the snare
}
if (hattrigger==1) {
hat.trigger();//likewise for the snare
}
sampleOut=hat.playOnce()*0.2+kick.playOnce()+snare.playOnce();//just play the files. No looping.
this.output=sampleOut;//left channel
kicktrigger = 0;//set trigger to 0 at the end of each sample to guarantee retriggering.
snaretrigger = 0;
hattrigger = 0;
}