When trying to implement an ADSR envelop its easy to implement ADS Attack, Decay and Sustain as the timing of all those values are known at the same time. However if attempting to implement the Release part of this envelop I'm running into trouble.
The problem is that I've note on and note off events which are scheduled ahead of time. However AudioParams.linearRampToValueAtTime however only takes two arguments the time that the ramp should end and the value that it should end at.
How does one then produce a ramp that begins at a certain time?
/**
* @param attack {int}
* @param decay {int}
* @param sustain {number} 0-100 percentage of overall level
* @param release {int} time for volume to reach 0
*/
function ADSR(attack, decay, sustain, release) {
this.attack = attack;
this.decay = decay;
this.sustain = sustain;
this.release = release;
function applyTo(audioParam, time) {
audioParam.linearRampToValueAtTime(1, time+attack);
audioParam.linearRampToValueAtTime(this.sustain/100, time+attack+decay);
}
this.applyTo = applyTo;
function applyRelease(audioParam, time, audioNode) {
// here I want to apply the release starting at the time given
// ending at time + this.time
}
return time;
}