ADSREnvelope
A comprehensive ADSR envelope generator with advanced features including anti-click retriggering and configurable AD mode.Template Parameters
Sample type (typically
double or float)Constructor
Name for debugging (only used when
DEBUG_ENV=1 is defined)Callback function to execute when envelope is retriggered (useful for resetting oscillator phase)
If
true, envelope is ADSR. If false, envelope is AD (suitable for drums)Envelope Stages
EStage
Enumeration of envelope stages:Envelope is inactive
Attack phase - rising from 0 to peak
Decay phase - falling from peak to sustain level
Sustain phase - held at sustain level
Release phase - falling from current level to zero
Fast fade to zero before retriggering (3ms)
Fast fade to zero for voice stealing (20ms)
Methods
SetStageTime
Stage to set:
kAttack, kDecay, or kReleaseDuration in milliseconds (clamped to 0.022675 - 60000 ms)
Attack uses linear ramping, while decay and release use exponential curves for more natural-sounding envelopes.
Process
Sustain level for this sample (0.0 to 1.0). Passed as parameter to allow external smoothing.
Start
Overall envelope depth, typically linked to MIDI velocity (0.0 to 1.0)
Factor to scale envelope rates (for key tracking). Values > 1.0 make the envelope faster.
Release
Retrigger
Velocity level for the new note
Time scaling factor for the new note
This method prevents clicks when voices are stolen or notes are retriggered before release completes.
Kill
If
true, instantly reset (may cause clicks). If false, fade out over 20ms.SetSampleRate
Sample rate in samples per second
GetBusy
true if envelope is not idle
GetReleased
true if envelope has been released
GetPrevOutput
SetResetFunc
Callback function or
nullptr to disableSetEndReleaseFunc
Callback function or
nullptr to disableConstants
Duration in milliseconds for soft kill fade-out
Duration in milliseconds for retrigger fade-out
Minimum stage time (1 sample at 44.1kHz)
Maximum stage time (60 seconds)
Threshold value for considering envelope at zero (-120dB)
Threshold value for considering envelope at peak
Usage Examples
Basic ADSR Envelope
AD Envelope for Drums
Envelope with Retrigger and Callbacks
Key Tracking (Velocity Scaling)
Implementation Details
Curve Types
Attack
Linear ramp from 0 to 1 for predictable rise time
Decay
Exponential curve using
expm1() for natural decaySustain
Constant level held until release
Release
Exponential curve matching decay algorithm
Anti-Click Features
- Retrigger Mode: Fast 3ms fade to zero before restarting
- Early Release: 20ms fade when voice is stolen
- Thresholding: Uses -120dB threshold to detect zero
Sustain Level Parameter
The sustain level is passed toProcess() rather than set as a member variable:
- Allows external smoothing of sustain parameter
- Prevents discontinuities when sustain is changed during playback
- Gives more control to the calling code
Code Location
Source:IPlug/Extras/ADSREnvelope.h:1
See Also
- Oscillators - Waveform generators
- LFO - Low frequency modulation
- Smoothers - Parameter smoothing