Envelope Example

var audio = new maximJs.maxiAudio();

audio.init();

var myCounter = new maximJs.maxiOsc(); //these oscillators will help us count and play sound
var mySwitchableOsc = new maximJs.maxiOsc();//

var CurrentCount;//
var myOscOutput,myCurrentVolume;//

var myEnvelope = new maximJs.maxiEnv();


//Timing is in ms
myEnvelope.setAttack(0);
myEnvelope.setDecay(1);  // Needs to be at least 1
myEnvelope.setSustain(1);
myEnvelope.setRelease(1000);


audio.play = function(){

    //notice that we feed in a value of 1. to create an envelope shape we can apply later.
    myCurrentVolume=myEnvelope.adsr(1.,myEnvelope.trigger);
    
    CurrentCount=Math.round(myCounter.phasor(1, 1, 9));//phasor can take three arguments; frequency, start value and end value.
    
    // You'll notice that these 'if' statements don't require curly braces "{}".
    // This is because there is only one outcome if the statement is true.
    
    if (CurrentCount==1){
     myEnvelope.trigger=1; //trigger the envelope
 }
 else { 
    myEnvelope.trigger=0;//release the envelope to make it fade out only if it's been triggered

}

if (CurrentCount<5){

    myOscOutput=mySwitchableOsc.sawn(CurrentCount*100);
}
    else if (CurrentCount>=5) {//and the 'else' bit.

        myOscOutput=mySwitchableOsc.sinewave(CurrentCount*50);//one osc object can produce whichever waveform you want.
    }
    
    this.output=myOscOutput*myCurrentVolume;//left speaker
}