Testing DSP
A reference of the soundor-dsp skill.
soundor-dsp reads this reference when a task needs it. Source on GitHub
Tests run the real processor in plain Rust with the SDK's harness,
soundor::testing::TestProcessor, without a host, browser or engine. Check
the harness's methods in .soundor/generated/rust/soundor/src/testing.rs
for the installed version.
npm run test:rust # soundor gen, then cargo test
cargo test --manifest-path lib/Cargo.toml <filter>The harness
use soundor::testing::TestProcessor;
use my_plugin::PluginProcessor;
let mut processor = TestProcessor::<PluginProcessor>::new()
.sample_rate(48_000.0)
.block_size(256) // the prepared maximum; process() splits into blocks
.channels(2) // a layout the config declares
.parameter("cutoff", 1000.0) // by id, plain value
.build(); // constructs and prepares
let output = processor.process(&[left, right]); // Vec<Vec<f32>> per channel
processor.parameters_mut().cutoff = 3000.0; // typed, snake_case fields
processor.prepare(96_000.0, 512); // reprepare
processor.reset();Also: state_mut() for audio: true state, transport_mut(),
process_buses for sidechains and extra outputs, allocations() with the
counting allocator.
Helpers worth writing once
fn sine(hz: f64, rate: f64, frames: usize) -> Vec<f32> {
(0..frames)
.map(|i| (0.5 * (std::f64::consts::TAU * hz * i as f64 / rate).sin()) as f32)
.collect()
}
fn rms(x: &[f32]) -> f64 {
(x.iter().map(|&s| f64::from(s).powi(2)).sum::<f64>() / x.len() as f64).sqrt()
}
fn db(ratio: f64) -> f64 {
20.0 * ratio.log10()
}Measure after the transient: skip the first part of the output (half a second of signal, measure the second half) so filters and smoothers have settled.
What to test
| Behavior | Test |
|---|---|
| Gain | a sine in, RMS out / RMS in, within 0.01 to 0.1 dB of the expected gain |
| Frequency response | sines at several frequencies (passband, cutoff, stopband) against the design: −3 dB at a Butterworth cutoff, the bell's gain at its centre |
| Impulse response | an impulse in: the first samples match the design; the response decays (finite, not growing) |
| Silence | zeros in, zeros (or below 1e-6) out after any tail; no DC from nowhere |
| Extremes | every parameter at min and max, full-scale noise in: output finite (is_finite()), bounded |
| Sample rates | the same tests at 44.1, 48, 96 (and 192) kHz give the same result in Hz and dB |
| Block sizes | process whole and in blocks of [1, 7, 64, 333, max]: outputs equal within 1e-6 |
| Channels | a signal in the left channel only: the right output stays silent (unless the effect mixes) |
| Smoothing | a parameter step: no sample-to-sample jump above a bound; settles to the target in the expected time |
| Reset | loud input, reset(), silence in: silence out |
| Latency | an impulse comes out exactly audio.latency.samples later |
| Allocation-free | CountingAllocator, every path, allocations() == 0 |
Tolerances
- Compare floats with tolerances, never
==, except for exact operations (a gain of 1, a pure delay). - Pick the tolerance from what is audible and what the math can promise: 0.01 dB for gains, 0.1 to 0.5 dB near filter cutoffs (sampled measurements and bilinear warping), 1e-6 absolute for sample-by-sample equality across block sizes.
- When comparing native output with the WebAssembly build, transcendental functions differ slightly: allow around 1e-6 absolute, and assert that the signal is not silent, so silence cannot pass as a match.
Reference values
Compute expected values from the design (a cookbook formula, an analog
prototype, the design function you used) in the test, or from a fixture
generated once and checked into lib/tests/fixtures/. Do not copy the
processor's own output into the test as the expectation without checking
it independently: that only proves it does not change.