polysynth

This shows how to use maximilian to build a polyphonic synth.

maximJs.maxiAudio.init();
  // change this.output to an array
  maximJs.maxiAudio.outputIsArray(true, 2);

//This shows how to use maximilian to build a polyphonic synth.
// Chrome doesn't like this one for some reason
var VCO_ArraySize = 6;

//These are the synthesiser bits
//  maxiTools makes it quicker to fill arrays with maxim objects
var VCO1 = maximJs. maxiTools.arrayOfObj(maximJs.maxiOsc, VCO_ArraySize);
var VCO2 = maximJs. maxiTools.arrayOfObj(maximJs.maxiOsc, VCO_ArraySize);
var LFO1 = maximJs. maxiTools.arrayOfObj(maximJs.maxiOsc, VCO_ArraySize);
var LFO2 = maximJs. maxiTools.arrayOfObj(maximJs.maxiOsc, VCO_ArraySize);

var VCF = maximJs. maxiTools.arrayOfObj(maximJs.maxiFilter, VCO_ArraySize);
var ADSR = maximJs. maxiTools.arrayOfObj(maximJs.maxiEnv, VCO_ArraySize);

//and these are some variables we can use to pass stuff around
var VCO1out = [], VCO2out = [], LFO1out = [],LFO2out = [], VCFout = [], ADSRout = [];

var pitch = []; 

for(var i = 0; i < VCO_ArraySize; i++){
    VCO1out.push(0);
    VCO2out.push(0);
    LFO1out.push(0);
    LFO2out.push(0);
    VCFout.push(0);
    ADSRout.push(0);
    pitch.push(0);
}

//This is a bunch of control signals so that we can hear something
var timer = new maximJs.maxiOsc();//this is the metronome
var currentCount = 0,lastCount = 0, voice = 0;//these values are used to check if we have a new beat this sample

var mix;

for (var i=0;i=6) {
            voice=0;
        }

        ADSR[voice].trigger=1;//trigger the envelope from the start
        pitch[voice]=voice+1;
        voice++;
        // lastCount = 0;        
    }
    
    //and this is where we build the synth
    for (var i=0; i<6; i++) {


        ADSRout[i]=ADSR[i].adsr(1.,ADSR[i].trigger);//our ADSR env is passed a constant signal of 1 to generate the transient.
        
        LFO1out[i]=LFO1[i].sinebuf(0.2);//this lfo is a sinewave at 0.2 hz
        
        VCO1out[i]=VCO1[i].pulse(55*pitch[i],0.6);//here's VCO1. it's a pulse wave at 55 hz, with a pulse width of 0.6
        VCO2out[i]=VCO2[i].pulse((110*pitch[i])+LFO1out[i],0.2);//here's VCO2. it's a pulse wave at 110hz with LFO modulation on the frequency, and width of 0.2
        
        
        VCFout[i]=VCF[i].lores((VCO1out[i]+VCO2out[i])*0.5, 250+((pitch[i]+LFO1out[i])*1000), 10);//now we stick the VCO's into the VCF, using the ADSR as the filter cutoff
        
        mix+=VCFout[i]*ADSRout[i]/6;//finally we add the ADSR as an amplitude modulator
        
        
    }
    
    this.output[0]=mix*0.5;//left channel
    this.output[1]=mix*0.5;//right channel
    
    // This just sends note-off messages.
    for (var i=0; i<6; i++) {
        ADSR[i].trigger=0;
    }
}