W3C home > Mailing lists > Public > public-audio@w3.org > January to March 2016

Attack, Sustain, Release using linearRampToValueAtTime

From: D <dilots@gmail.com>
Date: Fri, 15 Jan 2016 13:12:37 -0800
Message-ID: <CAO5aZd0rd2VkbUQ2uDoGMzwSzOq+PxWKyUpYg2wFPty9LaVfzg@mail.gmail.com>
To: public-audio@w3.org
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

This archive was generated by hypermail 2.3.1 : Monday, 18 January 2016 10:01:15 UTC