DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
dspark::ADSREnvelope< T > Class Template Reference

Classic ADSR envelope generator with exponential curves. More...

#include <EnvelopeGenerator.h>

Public Types

enum class  State {
  Idle , Attack , Decay , Sustain ,
  Release
}
 

Public Member Functions

 ADSREnvelope () noexcept
 
void prepare (double sampleRate) noexcept
 Prepares the envelope for the given sample rate.
 
void prepare (const AudioSpec &spec) noexcept
 Prepares from AudioSpec (unified API).
 
void setAttack (T ms) noexcept
 Sets attack time in milliseconds (0 -> 1).
 
void setDecay (T ms) noexcept
 Sets decay time in milliseconds (1 -> sustain).
 
void setSustain (T level) noexcept
 Sets sustain level (0.0 to 1.0).
 
void setRelease (T ms) noexcept
 Sets release time in milliseconds (full scale -> silence).
 
void setParameters (T attackMs, T decayMs, T sustain, T releaseMs) noexcept
 Sets all ADSR parameters at once.
 
void setCurvature (T curve) noexcept
 Sets the curvature of all exponential stages at once.
 
void setCurvature (T attackCurve, T decayCurve, T releaseCurve) noexcept
 Sets independent curvatures per stage.
 
void noteOn () noexcept
 Triggers the attack phase (note on). Legato: continues from the current value.
 
void noteOff () noexcept
 Triggers the release phase (note off).
 
void reset () noexcept
 Resets the envelope to idle with zero output.
 
getNextValue () noexcept
 Returns the next envelope value and advances the state machine.
 
void processBlock (T *output, int numSamples) noexcept
 Fills a buffer with envelope values.
 
getCurrentValue () const noexcept
 Returns the current envelope value.
 
State getState () const noexcept
 Returns the current state.
 
bool isActive () const noexcept
 Returns true if the envelope is actively producing output.
 

Detailed Description

template<FloatType T>
class dspark::ADSREnvelope< T >

Classic ADSR envelope generator with exponential curves.

Each stage runs an asymptotic RC-style one-pole toward a target that overshoots the stage endpoint, so the stage completes in finite time with the exact duration set by its parameter:

  • Attack: 0 -> 1 in attackMs (from a retrigger it starts at the current value and arrives earlier, click-free legato).
  • Decay: 1 -> sustain in decayMs for any sustain level (the overshoot scales with the stage depth, matching the self-similar RC discharge).
  • Release: calibrated for full scale -> silence in releaseMs. Like an analog RC release (fixed dB-per-second slope), releasing from a lower sustain reaches silence proportionally sooner.

Curvature shapes each stage: high values are strongly exponential, 0.1 is near-linear. Stage durations stay exact across the whole curvature range.

State machine: Idle -> Attack -> Decay -> Sustain -> Release -> Idle. The recursion state is double regardless of T (the framework rule for recursive state): in float, very long stages stall short of their target when the per-sample increment rounds to zero.

Threading is owner-managed: call setters, triggers and processing from the owning (audio) thread, the pattern of a synth voice. A default-constructed envelope is functional at 48 kHz; prepare() adapts it to the real rate.

Template Parameters
TOutput type (float or double). Constrained to IEEE 754 floats.

Definition at line 56 of file EnvelopeGenerator.h.

Member Enumeration Documentation

◆ State

template<FloatType T>
enum class dspark::ADSREnvelope::State
strong
Enumerator
Idle 
Attack 
Decay 
Sustain 
Release 

Definition at line 59 of file EnvelopeGenerator.h.

Constructor & Destructor Documentation

◆ ADSREnvelope()

template<FloatType T>
dspark::ADSREnvelope< T >::ADSREnvelope ( )
inlinenoexcept

Definition at line 61 of file EnvelopeGenerator.h.

Member Function Documentation

◆ getCurrentValue()

template<FloatType T>
T dspark::ADSREnvelope< T >::getCurrentValue ( ) const
inlinenoexcept

Returns the current envelope value.

Definition at line 328 of file EnvelopeGenerator.h.

◆ getNextValue()

template<FloatType T>
T dspark::ADSREnvelope< T >::getNextValue ( )
inlinenoexcept

Returns the next envelope value and advances the state machine.

Returns
Envelope value in [0, 1].

Definition at line 167 of file EnvelopeGenerator.h.

◆ getState()

template<FloatType T>
State dspark::ADSREnvelope< T >::getState ( ) const
inlinenoexcept

Returns the current state.

Definition at line 331 of file EnvelopeGenerator.h.

◆ isActive()

template<FloatType T>
bool dspark::ADSREnvelope< T >::isActive ( ) const
inlinenoexcept

Returns true if the envelope is actively producing output.

Definition at line 334 of file EnvelopeGenerator.h.

◆ noteOff()

template<FloatType T>
void dspark::ADSREnvelope< T >::noteOff ( )
inlinenoexcept

Triggers the release phase (note off).

Definition at line 148 of file EnvelopeGenerator.h.

◆ noteOn()

template<FloatType T>
void dspark::ADSREnvelope< T >::noteOn ( )
inlinenoexcept

Triggers the attack phase (note on). Legato: continues from the current value.

Definition at line 142 of file EnvelopeGenerator.h.

◆ prepare() [1/2]

template<FloatType T>
void dspark::ADSREnvelope< T >::prepare ( const AudioSpec spec)
inlinenoexcept

Prepares from AudioSpec (unified API).

Parameters
specAudio specification containing the sample rate.

Definition at line 78 of file EnvelopeGenerator.h.

◆ prepare() [2/2]

template<FloatType T>
void dspark::ADSREnvelope< T >::prepare ( double  sampleRate)
inlinenoexcept

Prepares the envelope for the given sample rate.

Parameters
sampleRateThe operating sample rate in Hz.

Definition at line 67 of file EnvelopeGenerator.h.

◆ processBlock()

template<FloatType T>
void dspark::ADSREnvelope< T >::processBlock ( T *  output,
int  numSamples 
)
inlinenoexcept

Fills a buffer with envelope values.

Runs each stage as a tight loop with its constants hoisted (no state dispatch per sample). Sample-exact match with getNextValue().

Parameters
outputBuffer to fill with envelope values.
numSamplesNumber of samples to generate.

Definition at line 227 of file EnvelopeGenerator.h.

◆ reset()

template<FloatType T>
void dspark::ADSREnvelope< T >::reset ( )
inlinenoexcept

Resets the envelope to idle with zero output.

Definition at line 155 of file EnvelopeGenerator.h.

◆ setAttack()

template<FloatType T>
void dspark::ADSREnvelope< T >::setAttack ( ms)
inlinenoexcept

Sets attack time in milliseconds (0 -> 1).

Definition at line 83 of file EnvelopeGenerator.h.

◆ setCurvature() [1/2]

template<FloatType T>
void dspark::ADSREnvelope< T >::setCurvature ( attackCurve,
decayCurve,
releaseCurve 
)
inlinenoexcept

Sets independent curvatures per stage.

Classic analog envelopes shape each stage differently (near-linear attack, strongly exponential release). Higher values = more curved.

Parameters
attackCurveAttack curvature (0.1 to 10.0; low = near-linear).
decayCurveDecay curvature.
releaseCurveRelease curvature.

Definition at line 131 of file EnvelopeGenerator.h.

◆ setCurvature() [2/2]

template<FloatType T>
void dspark::ADSREnvelope< T >::setCurvature ( curve)
inlinenoexcept

Sets the curvature of all exponential stages at once.

Parameters
curveCurvature factor (default: 3.0, range: 0.1 to 10.0).

Definition at line 114 of file EnvelopeGenerator.h.

◆ setDecay()

template<FloatType T>
void dspark::ADSREnvelope< T >::setDecay ( ms)
inlinenoexcept

Sets decay time in milliseconds (1 -> sustain).

Definition at line 86 of file EnvelopeGenerator.h.

◆ setParameters()

template<FloatType T>
void dspark::ADSREnvelope< T >::setParameters ( attackMs,
decayMs,
sustain,
releaseMs 
)
inlinenoexcept

Sets all ADSR parameters at once.

Parameters
attackMsAttack time in ms.
decayMsDecay time in ms.
sustainSustain level (0-1).
releaseMsRelease time in ms.

Definition at line 101 of file EnvelopeGenerator.h.

◆ setRelease()

template<FloatType T>
void dspark::ADSREnvelope< T >::setRelease ( ms)
inlinenoexcept

Sets release time in milliseconds (full scale -> silence).

Definition at line 92 of file EnvelopeGenerator.h.

◆ setSustain()

template<FloatType T>
void dspark::ADSREnvelope< T >::setSustain ( level)
inlinenoexcept

Sets sustain level (0.0 to 1.0).

Definition at line 89 of file EnvelopeGenerator.h.


The documentation for this class was generated from the following file: