Attack, Sustain, Release using linearRampToValueAtTime

Hello,
I’m trying to implement an Attack-Sustain-Release envelope with WebAudio.
The release part of the envelope does not work properly if the attack
portion of the envelope has not finished yet. I tried using
cancelScheduledValues to stop the attack envelope and snap the gain to its
current position, but it immediately jumps back down to 0 when I do that.
Any idea what I’m missing?

Here’s a snippet of my code:

function ASR_EnvelopeGenerator(context, peak) {
  this.attackTime = 0.1;
  this.sustainLevel = 0.5;
  this.releaseTime = 0.3;
  this.peak = peak;
  this.gain = 0;

  this.trigger = function() {
    var now = context.currentTime;
    this.gain.cancelScheduledValues(now);
    this.gain.setValueAtTime(0, now);
    this.gain.linearRampToValueAtTime(this.peak, now + this.attackTime);
  };

  this.release = function(){
    var now = context.currentTime;
    var currentValue = this.gain.value;
    this.gain.cancelScheduledValues(now);
    this.gain.setValueAtTime(currentValue, now);
    this.gain.exponentialRampToValueAtTime(0.01, now + this.releaseTime);
  }

  this.connect = function(gain) {
    this.gain = gain;
  };
};

I found a similar question from 2 years ago here:
https://lists.w3.org/Archives/Public/public-audio/2013JanMar/0020.html
However, this solution seems to be the same as what I'm doing already.

Cheers,
David
​

Received on Monday, 18 January 2016 10:01:14 UTC