Table of Contents

Class DahdsrEnvelope

Namespace
NAudio.Dsp
Assembly
NAudio.Core.dll

Six-stage DAHDSR (Delay, Attack, Hold, Decay, Sustain, Release) envelope generator. This generalises the four-stage EnvelopeGenerator with the extra Delay and Hold stages that SoundFont and SFZ instruments require, and a seconds-based API (stage durations are set in seconds rather than raw rates). The output is a linear amplitude/modulation multiplier in the range [0, 1].

The attack is convex (an exponential approach toward an over-shoot target), matching the SoundFont volume-envelope attack shape. Decay and release are constant-rate ramps following the SoundFont "time for a 100% change" semantic (SF2.04 §8.1.2): the stage time sets the ramp's rate, and the level the ramp runs to (the sustain level for decay, silence for release) truncates it, so a smaller change completes proportionally sooner. The ramp shape is selectable via DecayReleaseShape: exponential in amplitude (a constant fall in dB — the volume-envelope shape, gens 36/38) or linear in value (the modulation-envelope shape, gens 28/30). Designed for allocation-free real-time use.

public class DahdsrEnvelope
Inheritance
DahdsrEnvelope
Inherited Members

Constructors

DahdsrEnvelope(float)

Creates a DAHDSR envelope for the given sample rate.

public DahdsrEnvelope(float sampleRate)

Parameters

sampleRate float

Sample rate in Hz.

Properties

AttackSeconds

Attack time in seconds (time to rise from 0 to 1).

public float AttackSeconds { get; set; }

Property Value

float

DecayReleaseShape

The shape of the decay and release ramps: exponential in amplitude (constant dB rate — the SF2 volume-envelope shape, the default) or linear in value (the SF2 modulation-envelope shape).

public DahdsrEnvelope.RampShape DecayReleaseShape { get; set; }

Property Value

DahdsrEnvelope.RampShape

DecaySeconds

Decay time in seconds. Per the SoundFont semantic (SF2.04 §8.1.2 gens 36/28) this is the time for a 100% change — full scale down to -100 dB (exponential shape) or to zero (linear shape) — and sets the ramp's rate; the sustain level truncates the ramp, so a high sustain is reached proportionally sooner (e.g. a 2 s decay to a -1 dB sustain completes in 2 s x 1/100 = 20 ms).

public float DecaySeconds { get; set; }

Property Value

float

DelaySeconds

Delay time in seconds (output held at 0 before the attack begins).

public float DelaySeconds { get; set; }

Property Value

float

HoldSeconds

Hold time in seconds (output held at 1 after the attack completes).

public float HoldSeconds { get; set; }

Property Value

float

IsFinished

True once the envelope has completed its release and gone idle.

public bool IsFinished { get; }

Property Value

bool

Output

The most recent output value.

public float Output { get; }

Property Value

float

ReleaseSeconds

Release time in seconds. Like the decay this is the time for a 100% change (SF2.04 §8.1.2 gens 38/30), so it sets the ramp's rate and releasing from below full scale completes proportionally sooner — e.g. releasing from a -20 dB sustain takes (80/100) x ReleaseSeconds with the exponential shape.

public float ReleaseSeconds { get; set; }

Property Value

float

Stage

The current envelope stage.

public DahdsrEnvelope.EnvelopeStage Stage { get; }

Property Value

DahdsrEnvelope.EnvelopeStage

SustainLevel

Sustain level in the range [0, 1].

public float SustainLevel { get; set; }

Property Value

float

Methods

Advance(int)

Advances the envelope by samples samples in one call and returns the new output value, leaving Output, Stage and IsFinished bit-identical to calling Process() that many times. The delay and hold counters are consumed in O(1); the attack/decay/release ramps are stepped per sample within the active stage with the state hoisted to locals — a closed-form Math.Pow skip would drift from the repeated per-sample float multiplies by more than 1 ulp, so exact per-stage stepping is used instead, which still removes the per-sample call and field-traffic overhead. Intended for control-rate consumers that read the envelope once per block.

public float Advance(int samples)

Parameters

samples int

Number of samples to advance by. Must not be negative.

Returns

float

Gate(bool)

Opens (note on) or closes (note off) the gate. Opening restarts the envelope from its delay stage; closing moves to the release stage unless the envelope is already idle/finished.

public void Gate(bool on)

Parameters

on bool

Process()

Advances the envelope by one sample and returns the new output value.

public float Process()

Returns

float

Reset()

Resets the envelope to idle with an output of 0.

public void Reset()