Skip to content

EZ Web Audio / CompressorEffect

Class: CompressorEffect ​

Defined in: packages/core/src/effects/compressor-effect.ts:50

CompressorEffect - Dynamics compression using the native DynamicsCompressorNode.

Reduces the dynamic range of audio by attenuating signals above the threshold. All parameters map 1:1 to the DynamicsCompressorNode API.

Extends BaseEffect for shared wet/dry mixing, bypass, and rampTo() functionality.

Example ​

typescript
import { createCompressor, createSound } from 'ez-web-audio'

const sound = await createSound('drums.mp3')
const comp = createCompressor({
  threshold: -24,
  ratio: 4,
  knee: 30,
  attack: 0.003,
  release: 0.25
})
sound.addEffect(comp)
sound.play()

// Check gain reduction
console.log(comp.reduction) // -6.5 (dB)

Extends ​

Constructors ​

Constructor ​

new CompressorEffect(audioContext, options): CompressorEffect

Defined in: packages/core/src/effects/compressor-effect.ts:61

Parameters ​

audioContext ​

AudioContext

options ​

CompressorOptions = {}

Returns ​

CompressorEffect

Overrides ​

BaseEffect.constructor

Accessors ​

attack ​

Get Signature ​

get attack(): number

Defined in: packages/core/src/effects/compressor-effect.ts:122

Attack time in seconds

Returns ​

number

Set Signature ​

set attack(v): void

Defined in: packages/core/src/effects/compressor-effect.ts:126

Parameters ​
v ​

number

Returns ​

void


bypass ​

Get Signature ​

get bypass(): boolean

Defined in: packages/core/src/effects/base-effect.ts:95

When true, signal bypasses the effect entirely (100% dry).

Returns ​

boolean

Set Signature ​

set bypass(v): void

Defined in: packages/core/src/effects/base-effect.ts:99

When true, effect is bypassed (passthrough)

Parameters ​
v ​

boolean

Returns ​

void

When true, effect is bypassed (passthrough)

Inherited from ​

BaseEffect.bypass


input ​

Get Signature ​

get input(): AudioNode

Defined in: packages/core/src/effects/base-effect.ts:83

The input AudioNode (receives signal from chain)

Returns ​

AudioNode

The input AudioNode that receives signal from the chain

Inherited from ​

BaseEffect.input


knee ​

Get Signature ​

get knee(): number

Defined in: packages/core/src/effects/compressor-effect.ts:111

Knee width in dB

Returns ​

number

Set Signature ​

set knee(v): void

Defined in: packages/core/src/effects/compressor-effect.ts:115

Parameters ​
v ​

number

Returns ​

void


mix ​

Get Signature ​

get mix(): number

Defined in: packages/core/src/effects/base-effect.ts:108

Wet/dry mix: 0 = fully dry (no effect), 1 = fully wet (all through effect). Uses equal-power crossfade for natural mixing.

Returns ​

number

Set Signature ​

set mix(v): void

Defined in: packages/core/src/effects/base-effect.ts:112

Wet/dry mix: 0 = fully dry (no effect), 1 = fully wet (full effect)

Parameters ​
v ​

number

Returns ​

void

Wet/dry mix: 0 = fully dry (no effect), 1 = fully wet (full effect)

Inherited from ​

BaseEffect.mix


output ​

Get Signature ​

get output(): AudioNode

Defined in: packages/core/src/effects/base-effect.ts:88

The output AudioNode (sends signal to next in chain)

Returns ​

AudioNode

The output AudioNode that sends signal to the next in chain

Inherited from ​

BaseEffect.output


ratio ​

Get Signature ​

get ratio(): number

Defined in: packages/core/src/effects/compressor-effect.ts:100

Compression ratio (e.g., 4 = 4:1)

Returns ​

number

Set Signature ​

set ratio(v): void

Defined in: packages/core/src/effects/compressor-effect.ts:104

Parameters ​
v ​

number

Returns ​

void


reduction ​

Get Signature ​

get reduction(): number

Defined in: packages/core/src/effects/compressor-effect.ts:144

Current gain reduction in dB (read-only). Useful for metering

Returns ​

number


release ​

Get Signature ​

get release(): number

Defined in: packages/core/src/effects/compressor-effect.ts:133

Release time in seconds

Returns ​

number

Set Signature ​

set release(v): void

Defined in: packages/core/src/effects/compressor-effect.ts:137

Parameters ​
v ​

number

Returns ​

void


threshold ​

Get Signature ​

get threshold(): number

Defined in: packages/core/src/effects/compressor-effect.ts:89

Threshold in dB above which compression starts

Returns ​

number

Set Signature ​

set threshold(v): void

Defined in: packages/core/src/effects/compressor-effect.ts:93

Parameters ​
v ​

number

Returns ​

void

Methods ​

_clearListeners() ​

protected _clearListeners(): void

Defined in: packages/core/src/events/typed-event-emitter.ts:192

Remove every listener registered through this emitter (via addEventListener(), on(), or once()), regardless of how many or what event type they're bound to.

Native EventTarget has no removeAllListeners(). This works around that by aborting a shared AbortSignal threaded through every addEventListener() call this class makes, then swapping in a fresh AbortController so the instance can keep accepting new listeners afterward (e.g. if it's reused before being garbage collected).

Subclasses call this from their dispose() alongside neutering dispatchEvent — the neuter stops future emits, this stops stale listener closures from being retained/invoked at all.

Returns ​

void

Inherited from ​

BaseEffect._clearListeners


addEventListener() ​

Call Signature ​

addEventListener<K>(type, listener, options?): void

Defined in: packages/core/src/events/typed-event-emitter.ts:44

Add a typed event listener for known event types. Overloaded to provide type safety for known event types while remaining compatible with the native EventTarget API.

Type Parameters ​
K ​

K extends "dispose"

Parameters ​
type ​

K

The event type (key of TMap)

listener ​

(event) => void

Typed event handler

options? ​

Standard addEventListener options

boolean | AddEventListenerOptions

Returns ​

void

Inherited from ​

BaseEffect.addEventListener

Call Signature ​

addEventListener(type, listener, options?): void

Defined in: packages/core/src/events/typed-event-emitter.ts:49

Add a typed event listener for known event types. Overloaded to provide type safety for known event types while remaining compatible with the native EventTarget API.

Parameters ​
type ​

string

The event type (key of TMap)

listener ​

Typed event handler

EventListenerOrEventListenerObject | null

options? ​

Standard addEventListener options

boolean | AddEventListenerOptions

Returns ​

void

Inherited from ​

BaseEffect.addEventListener


dispose() ​

dispose(): void

Defined in: packages/core/src/effects/compressor-effect.ts:148

Disconnect all internal audio nodes and emit a 'dispose' event.

Subclass disposal contract: Subclasses that create additional audio nodes (e.g., BiquadFilterNode, DynamicsCompressorNode, WaveShaperNode) MUST override dispose() to disconnect those nodes before calling super.dispose().

Returns ​

void

Example ​

typescript
public override dispose(): void {
  try { this.myNode.disconnect() } catch { /* already disconnected */ }
  super.dispose()
}

Idempotent — safe to call multiple times.

Overrides ​

BaseEffect.dispose


emit() ​

protected emit<K>(type, detail): void

Defined in: packages/core/src/events/typed-event-emitter.ts:95

Emit a typed event with the given detail.

Creates a CustomEvent with the provided detail and dispatches it on this target. Subclasses call this internally to fire lifecycle events.

Type Parameters ​

K ​

K extends "dispose"

Parameters ​

type ​

K

The event type to emit (key of TMap)

detail ​

BaseEffectEventMap[K]["detail"]

The event detail object (typed by TMap)

Returns ​

void

Inherited from ​

BaseEffect.emit


getAudioContext() ​

getAudioContext(): AudioContext

Defined in: packages/core/src/effects/base-effect.ts:121

Get the AudioContext used by this effect. Useful for external tools (e.g., LFO) that need the context.

Returns ​

AudioContext

Inherited from ​

BaseEffect.getAudioContext


getAudioParam() ​

protected getAudioParam(name): AudioParam | null

Defined in: packages/core/src/effects/compressor-effect.ts:156

Map a parameter name to its underlying AudioParam. Subclasses implement this to expose their effect-specific parameters.

Parameters ​

name ​

string

The parameter name

Returns ​

AudioParam | null

The AudioParam, or null if not recognized

Overrides ​

BaseEffect.getAudioParam


getParam() ​

getParam(name): AudioParam | null

Defined in: packages/core/src/effects/base-effect.ts:134

Get a named AudioParam from this effect for external modulation.

Delegates to the subclass's getAudioParam() implementation. Returns null if the parameter name is not recognized.

Parameters ​

name ​

string

The parameter name (e.g., 'frequency', 'time', 'feedback')

Returns ​

AudioParam | null

The AudioParam, or null if not recognized

Inherited from ​

BaseEffect.getParam


getUnrampableParams() ​

protected getUnrampableParams(): readonly string[]

Defined in: packages/core/src/effects/base-effect.ts:243

Names of documented effect properties that are NOT backed by a single AudioParam (e.g. WaveShaper curve swaps, multi-node comb/allpass networks) and therefore cannot be smoothly ramped via rampTo(). rampTo() warns instead of silently no-op-ing when called with one of these names, so callers don't mistake "no-op" for "it worked."

Default: none. Subclasses override where applicable.

Returns ​

readonly string[]

Inherited from ​

BaseEffect.getUnrampableParams


off() ​

off<K>(type, listener): this

Defined in: packages/core/src/events/typed-event-emitter.ts:167

Unsubscribe from an event.

Note: Due to native EventTarget limitations, you must provide the same listener function reference that was used when subscribing.

Type Parameters ​

K ​

K extends "dispose"

Parameters ​

type ​

K

The event type to unsubscribe from

listener ​

(event) => void

The event handler function to remove

Returns ​

this

this for chaining

Example ​

typescript
const handler = (e) => console.log(e.detail)
emitter.on('play', handler)
// later...
emitter.off('play', handler)

Inherited from ​

BaseEffect.off


on() ​

on<K>(type, listener): this

Defined in: packages/core/src/events/typed-event-emitter.ts:116

Subscribe to one or more events. Supports chaining.

Type Parameters ​

K ​

K extends "dispose"

Parameters ​

type ​

The event type(s) to subscribe to (key or array of keys of TMap)

K | K[]

listener ​

(event) => void

The event handler function

Returns ​

this

this for chaining

Example ​

typescript
emitter.on('play', handlePlay).on('stop', handleStop)
emitter.on(['play', 'stop'], handleBoth)

Inherited from ​

BaseEffect.on


once() ​

once<K>(type, listener): this

Defined in: packages/core/src/events/typed-event-emitter.ts:141

Subscribe to an event once. Handler is removed after first invocation.

Type Parameters ​

K ​

K extends "dispose"

Parameters ​

type ​

K

The event type to subscribe to

listener ​

(event) => void

The event handler function

Returns ​

this

this for chaining

Example ​

typescript
emitter.once('end', () => console.log('Finished'))

Inherited from ​

BaseEffect.once


onParamRamped() ​

protected onParamRamped(param, value): number

Defined in: packages/core/src/effects/compressor-effect.ts:173

ramp-setter-desync fix: rampTo() re-applies the same clamp each property setter uses and updates the shadow field, so getters stay honest after a ramp and the AudioParam can never receive an out-of-range value the setter would have rejected.

Parameters ​

param ​

string

value ​

number

Returns ​

number

Overrides ​

BaseEffect.onParamRamped


rampTo() ​

rampTo(param, value, duration): void

Defined in: packages/core/src/effects/base-effect.ts:164

Smoothly ramp a parameter to a target value over a duration.

Uses AudioParam.setTargetAtTime for glitch-free transitions. If the parameter name is not recognized, this is a no-op (a console.warn is emitted if the name is a real, documented effect property that just isn't backed by a single AudioParam — see getUnrampableParams).

Single write path: every ramped write is funneled through onParamRamped, the same hook subclasses use to keep their shadow getters honest. This guarantees effect.someParam reflects the ramp target immediately after calling rampTo(), and that the exact same validation/clamping a synchronous property-set would apply is also applied to ramped writes — the AudioParam and the getter can never diverge from each other.

Parameters ​

param ​

string

Parameter name (e.g., 'frequency', 'time', 'feedback')

value ​

number

Target value

duration ​

number

Ramp duration in seconds

Returns ​

void

Example ​

typescript
delay.rampTo('time', 0.5, 2)      // Ramp delay time to 0.5s over 2 seconds
filter.rampTo('frequency', 800, 1) // Ramp cutoff to 800Hz over 1 second

Inherited from ​

BaseEffect.rampTo


removeEventListener() ​

Call Signature ​

removeEventListener<K>(type, listener, options?): void

Defined in: packages/core/src/events/typed-event-emitter.ts:70

Remove a typed event listener for known event types. Overloaded to provide type safety for known event types while remaining compatible with the native EventTarget API.

Type Parameters ​
K ​

K extends "dispose"

Parameters ​
type ​

K

The event type (key of TMap)

listener ​

(event) => void

Typed event handler to remove

options? ​

Standard removeEventListener options

boolean | EventListenerOptions

Returns ​

void

Inherited from ​

BaseEffect.removeEventListener

Call Signature ​

removeEventListener(type, listener, options?): void

Defined in: packages/core/src/events/typed-event-emitter.ts:75

Remove a typed event listener for known event types. Overloaded to provide type safety for known event types while remaining compatible with the native EventTarget API.

Parameters ​
type ​

string

The event type (key of TMap)

listener ​

Typed event handler to remove

EventListenerOrEventListenerObject | null

options? ​

Standard removeEventListener options

boolean | EventListenerOptions

Returns ​

void

Inherited from ​

BaseEffect.removeEventListener