@maruaudio/dsp/dynamics/peak-compressor is a feed-forward compressor that reads the instantaneous peak level of each sample. The level in dB (20·log10|x|, floored at −240 dB below 1e−12) goes through a quadratic soft-knee gain computer; the gain reduction in dB is smoothed by a branching one-pole (attack while it rises, release while it falls) and rounded to f32; the output is x·10^((makeup − reduction)/20).
const state = initCompressorNode(48000)
setCompressorRatio(state, 4)
const y = compressorNodeTick(state, x)Parameters
| Setter | Range the setter enforces | Default | Unit |
|---|---|---|---|
setCompressorThreshold | −60 to 0 | −24 | dB |
setCompressorRatio | 1 to 20 | 12 | ratio |
setCompressorKnee | 0 to 40 (full width) | 30 | dB |
setCompressorAttack | 0 to 1; 0 is instantaneous | 0.003 | s |
setCompressorRelease | 0 to 1; 0 is instantaneous | 0.25 | s |
setCompressorMakeup | −24 to +24 | 0 | dB |
compressorGainReductionDb(level, threshold, ratio, knee) is exported and re-clamps its arguments to the same ranges.
Lifecycle
initCompressorNode(sampleRate) → setters → compressorNodeTick → resetCompressorNode, which zeroes the gain reduction and keeps the settings. prepareCompressorNodeSampleRate re-derives both coefficients from the stored times without allocating; the coefficient clamps the rate to 8 to 384 kHz.
When to use it
Evening out a guitar, bass or vocal line where each peak should be caught as it arrives. @maru/dynamics-keyed-compressor adds a sidechain, RMS detection, hold and auto-release.
Limits
- Mono, sample tick, no lookahead or sidechain input.
- Each tick calls
logandpow(throughdbToGain). - Clamps are the kernel node's conditional clamp, not
@maru/math-range.