Audio correctness
A reference of the soundor-dsp skill.
soundor-dsp reads this reference when a task needs it. Source on GitHub
Sample-rate independence
Derive everything time- or frequency-based from ctx.sample_rate in
prepare(): filter coefficients, envelope and smoothing coefficients
(exp(-1 / (time_seconds * sample_rate)) for a one-pole), delay lengths in
samples, LFO increments, FFT hop sizes. Clamp frequencies below Nyquist
(Prism holds them under 0.49 × the sample rate). Test at 44.1, 48, 96 and
192 kHz; the same settings must sound the same.
Variable block sizes
ctx.audio.frames()may change every call and may be zero; each channel slice has exactly that length.- Never assume a block is a power of two or a multiple of anything. Work that needs fixed frames (an FFT hop, oversampling) buffers internally.
- A ramp over "one block" lasts a different time at every block size; smooth with time constants in seconds when the duration matters.
- Test: process a signal whole and in irregular blocks
(
[1, 37, 512, 64]); outputs must match within float tolerance.
Channels
Each channel gets its own filter state, delay line and envelope. Size them
from the layout in prepare() (ctx.layout.outputs()). Process main
input in place on the main output; read every input you need before writing
outputs that share storage. Mono and stereo layouts both work if declared.
Parameter smoothing
Values change between blocks in steps. Smooth anything audible:
- gains: a linear ramp across the block, or a one-pole smoother per sample (5 to 20 ms);
- filter cutoff and resonance: smooth the parameter, then recompute coefficients per block, per small sub-block, or per sample for fast sweeps; state variable filters tolerate coefficient changes better than direct-form biquads;
- delay times: smooth the read position and interpolate, or crossfade between taps;
- switches and modes: crossfade a few milliseconds between old and new.
Start smoothers at the current value in prepare() so the first block does
not glide from zero.
Automation and events
Parameters are block-rate. MIDI events carry a sample_offset: split
rendering at each event's offset rather than applying it at the block
start. Do not schedule audio from UI callbacks.
Numerical stability
- Use
f64for recursive filters at low frequencies or high sample rates, coefficient computation, phase accumulators, and long sums;f32is fine for most per-sample arithmetic. - Wrap phase accumulators (
phase -= 1.0when it passes 1) instead of letting them grow. - Keep feedback gains below 1 in magnitude, including at the parameter extremes; saturate inside feedback loops that can exceed it.
- Guard divisions and logs:
log10(x + tiny),max(x, floor).
NaN and infinity
One NaN in a filter's state silences or blows up everything after it, forever. Prevent them at the source (guarded math, stable designs, clamped parameters). As a last line of defence, a processor may check its state after a block and reset it if it is not finite; test with extreme parameters and full-scale noise that outputs stay finite.
Denormals
Very small floats (subnormals) can be many times slower on x86. They
appear in decaying feedback (reverbs, filters, envelopes after silence).
Add a tiny DC offset or noise (around 1e-20) inside feedback paths, or
flush values below a threshold to zero. Measure a long silent tail: CPU
should not rise after the input stops. FunDSP's denormal module has
helpers.
Aliasing and oversampling
Nonlinearities (clipping, saturation, waveshaping) and naive oscillators
create harmonics above Nyquist that fold back. Options, cheapest first:
smoother curves (soft clipping before hard), band-limited oscillators
(PolyBLEP, wavetables), and oversampling the nonlinear stage by 2× to 8×
with good anti-aliasing filters. Oversampling costs CPU and adds latency;
use it when a test or listening shows aliasing, and report the latency it
adds. FunDSP provides an oversample node.
FFT processing
Plan once in prepare(), window frames (Hann for analysis), overlap
correctly for resynthesis (overlap-add with a window pair that sums to a
constant), and scale explicitly: RustFFT does not normalize. A frequency-
domain effect has latency of at least one frame: report it. See
Using RustFFT.
Latency
Lookahead, linear-phase filters, FFT frames and oversampling delay the
output. Declare a fixed audio.latency equal to what the DSP adds, and keep
it constant: dynamic latency is reported to VST3 hosts as zero in Soundor
0.21. Test the measured delay of an impulse against the declared value.
Reset
reset() clears delay lines, filter memory, envelopes, voices and FFT
history, so output after reset depends only on input after it. Test:
process a loud signal, reset, process silence, expect silence (or the
plugin's documented tail).
CPU efficiency
- Hoist work out of the per-sample loop: coefficients per block, not per sample, unless smoothing needs it.
- Avoid
powf,exp,sinper sample where a recurrence or table does. - Prefer contiguous loops over slices the compiler can vectorize.
lib/Cargo.toml's[profile.dev] opt-level = 1keeps debug builds fast enough to run; measure performance in release (cargo test --releasewith a benchmark test marked#[ignore], as Prism does).