Skip to content

EZ Web Audio / Envelope

Class: Envelope

Defined in: envelope.ts:60

ADSR Envelope class for managing amplitude envelope scheduling.

The envelope controls how a sound's amplitude evolves over time:

  • Attack: Ramp from 0 to peak (1.0)
  • Decay: Ramp from peak to sustain level
  • Sustain: Hold at sustain level until release() called
  • Release: Exponential decay to silence

Supports clickless retriggering: when a note is retriggered while the envelope is still active, it picks up from the current value instead of jumping to zero, preventing audible clicks.

Example

typescript
const envelope = new Envelope({
  attackTime: 0.05,
  decayTime: 0.1,
  sustainLevel: 0.7,
  releaseTime: 0.3
})

// Apply on note start
envelope.applyTo(gainNode.gain, audioContext.currentTime)

// Release on note end
envelope.release(gainNode.gain, audioContext.currentTime)

Constructors

Constructor

new Envelope(options): Envelope

Defined in: envelope.ts:87

Creates a new Envelope with the specified ADSR parameters.

Parameters

options

EnvelopeOptions = {}

ADSR configuration options

Returns

Envelope

Properties

attackTime

readonly attackTime: number

Defined in: envelope.ts:62

Duration in seconds to ramp from 0 to peak (1.0)


decayTime

readonly decayTime: number

Defined in: envelope.ts:65

Duration in seconds to ramp from peak to sustain level


releaseTime

readonly releaseTime: number

Defined in: envelope.ts:71

Duration in seconds for release to silence


sustainLevel

readonly sustainLevel: number

Defined in: envelope.ts:68

Amplitude level (0-1) held during sustain phase

Accessors

isActive

Get Signature

get isActive(): boolean

Defined in: envelope.ts:97

Whether the envelope is currently active (between applyTo and release).

Returns

boolean

Methods

applyTo()

applyTo(gainParam, startTime): void

Defined in: envelope.ts:165

Applies the attack-decay-sustain phases to an AudioParam.

Schedules:

  1. setValueAtTime(startValue, startTime) - Start from current value (0 for first trigger)
  2. linearRampToValueAtTime(1, startTime + attackTime) - Attack to peak
  3. linearRampToValueAtTime(sustainLevel, startTime + attackTime + decayTime) - Decay to sustain

If retriggering (envelope already active), cancels scheduled values and starts the attack from the current estimated value to prevent clicks.

Parameters

gainParam

AudioParam

The AudioParam to schedule the envelope on (typically gainNode.gain)

startTime

number

The audio context time to start the envelope

Returns

void


estimateCurrentValue()

estimateCurrentValue(currentTime): number

Defined in: envelope.ts:110

Estimates the current envelope value at a given time.

Used for retriggering to determine where to pick up from. Returns 0 if envelope is not active.

Parameters

currentTime

number

The time to estimate the value at

Returns

number

The estimated envelope value (0-1)


release()

release(gainParam, startTime): void

Defined in: envelope.ts:213

Applies the release phase to an AudioParam.

Uses setTargetAtTime for smooth exponential decay to zero. The time constant is calculated as releaseTime/5, which gives approximately 99% completion within releaseTime seconds.

Parameters

gainParam

AudioParam

The AudioParam to schedule the release on

startTime

number

The audio context time to start the release phase

Returns

void