Basic ADSR envelope generator for web audio. A demo is running here.
- The release stage exists as a separate
GainNode, so the envelope doesn't need to keep track of its output gain internally.
- Uses the voltage idea from mmckegg/adsr.
npm install --save envelope-generator
;let context = ;let osc = context;let gain = context;let env = contextattackTime: 01decayTime: 3sustainLevel: 04releaseTime: 01;env;var startAt = contextcurrentTime;var releaseAt = startAt + 05;oscstartstartAt;envstartstartAt;env;let stopAt = env;osc;env;
The constructor accepts two arguments: an AudioContext and a settings object. All settings are optional, but you will probably want to set at least
...Timeproperties are in seconds
releaseCurveproperties default to
- attackCurve, decayCurve, and releaseCurve override their respective curves
- Passing an
initialValueCurvewill determine the shape of the curve usually covered by the attack and decay sections of an envelope, overriding any other curve values.
- Passing a
releaseValueCurvewill determine the shape of the release, overriding any other release curve value.
- Both initialValueCurve and releaseValueCurve are expected to be normalized, i.e. not extending outside of the bounds [0, 1]. This is relatively intuitive for the
initialValueCurve, but ensure that your
releaseValueCurvealso starts at a value of 1 to avoid any jumps in the sound. The reason for this is that these two curves are applied in series.
- The sampleRate property applies to initialValueCurve and releaseValueCurve, allowing them to be expressed in a sampleRate different from that of the context.
let context = ;let settings =curve: "linear"attackCurve: "linear"decayCurve: "linear"releaseCurve: "linear"initialValueCurve: Float32ArrayreleaseValueCurve: Float32ArraysampleRate: 44100delayTime: 0startLevel: 0maxLevel: 1attackTime: 01holdTime: 0decayTime: 0sustainLevel: 05releaseTime: 1;let env = context settings
connect method should be attached directly to
let osc = context;let gainNode = context;let env = context settings;env;
start method triggers the attack and decay stages of the envelope:
let osc = context;let gainNode = context;let env = context settings;env;oscstartcontextcurrentTime;envstartcontextcurrentTime;
release method triggers the release stage of the envelope:
let osc = context;let gainNode = context;let env = context settings;env;oscstartcontextcurrentTime;envstartcontextcurrentTime;// Release the envelope after 1 secondenv;
Releasing the envelope isn't the same as stopping the sound source. Once release has been called,
getReleaseCompleteTime() will return the time that the envelope finishes its release stage. If this is an amp envelope, and the startLevel (i.e., where the envelope will release to) is 0,
getReleaseCompleteTime() is when your sound source is guaranteed to be silent and can be stopped:
let osc = context;let gainNode = context;let env = context settings;env;oscstartcontextcurrentTime;envstartcontextcurrentTime;env;// Stop the oscillator once the envelope has completed.osc;
Because they are generating a signal, envelopes need to be stopped as well as released. This should coincide with when the actual sound source is stopped.
let osc = context;let gainNode = context;let env = context settings;env;oscstartcontextcurrentTime;envstartcontextcurrentTime;env;// Stop the oscillator once the envelope has completed.let stopAt = env;osc;env;