@maruaudio/dsp/math/quantizer is a bit-depth staircase read through a sampled curve. It reproduces the public "crush" web preview, a 2048-point bitcrushCurve fed to a WaveShaperNode with no oversampling, rather than the ideal closed-form staircase. The curve stores round((x + 1) / step) × step − 1 at f32, with step = 2 / (2^bits − 1), and each sample is the Web Audio curve-application formula: the input maps to v = 0.5 × (x + 1) × 2047, neighbouring curve points are interpolated linearly in f64, and the result is stored to f32.
Parameters
| Path | Setter | Range as coded | Default | Unit |
|---|---|---|---|---|
bits | setQuantizerBits | quantizerDepthFor: rounded, then 1 to 16 | 4 | bits |
Lifecycle
initQuantizerNode(sampleRate) allocates the 2048-point curve and a one-slot f32 buffer and fills the curve for 4 bits. setQuantizerBits rebuilds the curve only when the clamped depth changes; run it at control rate. quantizerNodeTick(state, x) reads one sample. resetQuantizerNode only zeroes the rounding slot, since the curve depends on the parameter alone.
When to use it
Bit-depth reduction that must match the shipped web preview at the same setting, on any target. Rate reduction, held samples and boost are not part of it; they belong to @maru/distortion-bitcrusher.
Limits
- Mono sample tick. No smoothing: a depth change applies to the next sample.
- Inputs below −1 return
curve[0]and inputs at or above +1 returncurve[2047]; a NaN input returns NaN. - At 11 bits and above the levels outnumber the curve points, so the output is the interpolated curve, not 2^bits distinct levels.
- No oversampling or rate reduction, so high-frequency quantization products may alias (the demo note says so).