Table of Contents

Class AdsrSampleProvider

Namespace
NAudio.Wave.SampleProviders
Assembly
NAudio.Core.dll

Applies an ADSR (attack / decay / sustain / release) amplitude envelope to a source sample provider. Wraps EnvelopeGenerator, exposing its stage times in seconds and its sustain level, and multiplies the source by the envelope each sample.

public class AdsrSampleProvider : ISampleProvider
Inheritance
AdsrSampleProvider
Implements
Inherited Members
Extension Methods

Remarks

The envelope holds at SustainLevel once the attack and decay have completed, until Stop() is called to enter the release phase. Once the release finishes the envelope is idle, Read(Span<float>) returns 0 (so the provider self-evicts from a MixingSampleProvider), and EnvelopeCompleted is raised.

Constructors

AdsrSampleProvider(ISampleProvider, bool)

Creates a new AdsrSampleProvider with default values (fast attack, no decay, full sustain, 0.3s release).

public AdsrSampleProvider(ISampleProvider source, bool startGateOpen = true)

Parameters

source ISampleProvider

The source to apply the envelope to. Any channel count is supported.

startGateOpen bool

When true (the default) the envelope's gate opens immediately, so the note begins its attack as soon as playback starts — matching the previous behaviour of this class. Set to false to arm the provider without starting it and trigger the note later with Start(); note that while idle Read(Span<float>) returns 0, so do not initialise a player directly on an un-started provider and treat that as end-of-stream.

Properties

AttackSeconds

Attack time in seconds (time for the envelope to rise from silence to full level).

public float AttackSeconds { get; set; }

Property Value

float

DecaySeconds

Decay time in seconds (time for the envelope to fall from full level to the sustain level).

public float DecaySeconds { get; set; }

Property Value

float

ReleaseSeconds

Release time in seconds (time for the envelope to fall from the sustain level to silence after Stop()).

public float ReleaseSeconds { get; set; }

Property Value

float

State

The current envelope phase.

public EnvelopeGenerator.EnvelopeState State { get; }

Property Value

EnvelopeGenerator.EnvelopeState

SustainLevel

Sustain level in the range 0..1 (the level held after the decay until release).

public float SustainLevel { get; set; }

Property Value

float

WaveFormat

The output WaveFormat

public WaveFormat WaveFormat { get; }

Property Value

WaveFormat

Methods

Read(Span<float>)

Reads audio from this sample provider, applying the envelope.

public int Read(Span<float> buffer)

Parameters

buffer Span<float>

Returns

int

Start()

Triggers note-on, entering the attack phase. Can be used to retrigger a note that has already been released.

public void Start()

Stop()

Triggers note-off, entering the release phase.

public void Stop()

Events

EnvelopeCompleted

Raised once when the envelope finishes (returns to Idle after the release completes). Fires on the thread that calls Read(Span<float>) (i.e. the audio thread during playback), in the same manner as MixerInputEnded.

public event EventHandler EnvelopeCompleted

Event Type

EventHandler