soundor-dsp
Implements real-time-safe audio processing in Rust for Soundor plugins.
pnpm dlx skills add soundor/skills --skill soundor-dspWhen an agent uses it
Implements real-time-safe audio processing in Rust for Soundor plugins. Covers filters, gain, distortion, delays, synthesis, metering and FFT analysis with parameter smoothing, any sample rate and block size, numerical stability and allocation-free processing, using FunDSP and RustFFT where they help. Use when writing or changing lib/src/processor.rs or code it calls, adding a DSP dependency, fixing clicks, distortion, NaNs or CPU spikes, or writing numerical tests of audio output.
Instructions
The processor in lib/src/processor.rs runs on the audio thread, inside a
deadline the host sets: a 128-sample block at 48 kHz must be done in well
under 2.7 ms, every time, while the DAW runs other plugins. Late output is a
dropout the user hears. Correct and predictable both matter; fast on
average does not.
Confirm the processor API first
Take the API from the project, not from memory. Run npm run gen, then read
the generated SDK in .soundor/generated/rust/soundor/src/ (prelude.rs,
context.rs, testing.rs, and the generated parameters), the crate's
existing lib/src/processor.rs and lib/tests/, and any SKILL.md that
installed Soundor packages bundle
(find node_modules/soundor node_modules/@soundor -name SKILL.md; none
ship yet). The documentation is at https://soundor.dev/docs/rust-processor
(Markdown: add .md). In Soundor 0.21 the shape is:
use soundor::prelude::*;
#[derive(Default)]
pub struct PluginProcessor { /* DSP state */ }
impl Processor for PluginProcessor {
fn prepare(&mut self, ctx: &PrepareContext) { /* allocate, design */ }
fn process(&mut self, ctx: &mut ProcessContext) { /* every block */ }
fn reset(&mut self) { /* clear history, keep capacity */ }
}ctx.params.<id> holds each parameter, typed, once per block;
ctx.audio.channels() yields each channel's samples in place;
ctx.streams, ctx.publish, ctx.state, ctx.resources and ctx.midi
connect the rest.
The real-time rules
process() and reset() must never:
- allocate or free heap memory (including growing a
Vec,collect(),format!,to_string(),Box::new, cloning aVec, or dropping the lastArcorBoxof anything); - lock a mutex or
RwLock, wait on a channel, sleep, spawn or join a thread; - do file or network I/O, log or print (
println!,dbg!,logmacros); - run unbounded loops or retries, or work whose cost depends on input rather than the block size;
- build FFT plans, DSP graphs, filter tables or other expensive setup;
- panic: index out of bounds,
unwrap()onNone, integer overflow in debug builds, division by zero in integer code.
Rust's memory safety does not make code real-time safe: safe Rust allocates
and locks freely. Do all preparation in prepare(), which runs off the
audio thread and may run again at any time.
Real-time safety lists hidden allocations,
safe patterns, and how to verify.
Correct audio
- Sample rate: compute every coefficient, time constant and delay
length from
ctx.sample_rateinprepare(); never hard-code 44.1 or 48 kHz. - Block size: any number of frames up to the prepared maximum, zero included. State carries across blocks; output must not depend on how the host splits the audio.
- Channels: process each channel the layout gives, with its own state; never assume stereo.
- Smoothing: parameters arrive once per block; ramp gains and smooth filter settings so automation does not click.
- Numerics: keep feedback stable, flush or avoid denormals, never let NaN or infinity reach the output, and limit aliasing from nonlinearities.
Audio correctness covers each of these, plus latency, reset and CPU cost.
Libraries
Write simple DSP (gain, mixing, panning, a one-pole smoother, soft clipping) directly in Rust. For established algorithms, use established implementations rather than inventing them. The recommended crates are:
- FunDSP (https://docs.rs/fundsp/): filters, oscillators, envelopes, effects and signal graphs. Using FunDSP.
- RustFFT (https://docs.rs/rustfft/): FFTs for spectrum analysis and frequency-domain processing. Using RustFFT.
Both are MIT OR Apache-2.0, pure Rust, and build for native targets and
wasm32-unknown-unknown (Soundor Prism ships both in its VST3 and web
builds). Neither is required. Before adding any other DSP crate, check its
license, its default features, that it builds for
wasm32-unknown-unknown if the plugin has a web build, and that the calls
you make on the audio thread do not allocate or lock: test it, do not
assume it.
Test the numbers
Every DSP change gets tests that check output values with Soundor's test
harness (soundor::testing::TestProcessor), run by npm run test:rust:
gain and frequency response measured with sines, impulse responses,
silence in and out, extreme parameter values, several sample rates and
block sizes, independent channels, finite output, and zero allocations
with CountingAllocator. Testing DSP has
recipes and tolerances.
Further reading
DSP references lists the sources to consult for algorithms and formulas: link to them and follow them rather than deriving formulas from memory.
Before you finish
-
prepare()allocates everythingprocess()uses;process()andreset()contain none of the forbidden operations above, including in the dependencies they call. - A
CountingAllocatortest exercises every processing path (parameter changes, bypass, each mode) and asserts zero allocations. - Numerical tests pass at 44.1, 48 and 96 kHz and at block sizes 1, odd sizes and the maximum.
- Output stays finite for silence, full-scale input, and every parameter's extremes.
-
cargo clippy --all-targets --manifest-path lib/Cargo.toml -- -D warningsandcargo fmt --check --manifest-path lib/Cargo.tomlpass. - If the plugin has a web build,
npm run build:webstill succeeds.
References
The skill reads these when a task needs them: