Using FunDSP
A reference of the soundor-dsp skill.
soundor-dsp reads this reference when a task needs it. Source on GitHub
FunDSP (docs) is an audio processing and synthesis library: filters, oscillators, envelopes, delays, reverbs, dynamics, and a graph notation to combine them. License: MIT OR Apache-2.0.
Add it
# lib/Cargo.toml
[dependencies]
fundsp = { version = "0.23", default-features = false, features = ["std"] }The default features add files (audio file I/O through Symphonia) and
fft (FFT convolution); a plugin rarely needs either, and both add code
and dependencies to the VST3 and the WebAssembly build. Turn one on only
when you use it. Check the newest release on https://crates.io/crates/fundsp
and read the docs for the version in lib/Cargo.lock: FunDSP's API
changes between 0.x releases. Soundor Prism uses 0.23 with exactly these
features, natively and in its web build.
Imports
Import the items you use. A glob import of fundsp::prelude32::* next to
soundor::prelude::* brings traits with the same method names into scope,
and calls such as .max() stop compiling:
use fundsp::prelude32::{An, AudioNode, FixedSvf, LowpassMode, Setting, lowpass_hz};
use soundor::prelude::*;prelude32 uses f32 for parameters; prelude64 uses f64. Audio
samples are f32 in both.
Pattern: nodes in the processor
Build nodes in prepare(), one per channel, set the sample rate, call
allocate(), and process per sample or per block. This filter was tested
to cut 10 kHz by more than 35 dB at a 1 kHz cutoff at 44.1, 48 and 96 kHz,
with zero allocations while processing:
type Lowpass = An<FixedSvf<f32, LowpassMode<f32>>>;
#[derive(Default)]
pub struct PluginProcessor {
filters: Vec<Lowpass>,
}
impl Processor for PluginProcessor {
fn prepare(&mut self, ctx: &PrepareContext) {
let channels = ctx.layout.outputs().first().copied().unwrap_or(2).max(1);
self.filters = (0..channels)
.map(|_| {
let mut filter = lowpass_hz(ctx.params.cutoff, 0.707);
filter.set_sample_rate(ctx.sample_rate);
filter.allocate(); // preallocate before the audio thread
filter
})
.collect();
}
fn process(&mut self, ctx: &mut ProcessContext) {
for filter in &mut self.filters {
filter.set(Setting::center(ctx.params.cutoff)); // no allocation
}
for (channel, samples) in ctx.audio.channels().enumerate() {
let Some(filter) = self.filters.get_mut(channel) else { continue };
for sample in samples {
*sample = filter.filter_mono(*sample);
}
}
}
fn reset(&mut self) {
for filter in &mut self.filters {
filter.reset();
}
}
}Smooth cutoff before setting it when automation must be click-free.
Pattern: coefficients only
FunDSP's filter designs can be used without its nodes. Prism computes
fundsp::svf::SvfCoefs::{bell, lowshelf, highshelf, lowpass, highpass, notch, bandpass} per band and sample rate, and runs its own state variable
stages, so it controls smoothing and stage counts. Designs are plain
arithmetic: computing them per block is real-time safe.
Graphs and the real-time path
- Static graphs (
AudioNode, combined with>>,|,&) are inlined and mostly stack-allocated; build them inprepare()and callallocate(). NetandSequencerare dynamic and heap-allocated. Their frontend/backend split is designed for a separate control thread that edits the graph; a Soundor processor gets its inputs through parameters, audio state and resources instead, so build theNetinprepare()and change it through settings, not by editing the graph inprocess().shared()/var()atomics are unnecessary for parameters: readctx.paramseach block and pass values in withset()or as inputs.- Wavetable oscillators allocate global tables on first use: create them
in
prepare(), never first inprocess().
Verify with a CountingAllocator test for every node you call from
process(); a node's documentation does not guarantee it is
allocation-free for every setting.
Useful parts
Filters (lowpass_hz, highpass_hz, bell_hz, lowshelf_hz,
highshelf_hz, notch_hz, Moog and resonators), oscillators (sine_hz,
band-limited saw_hz, square_hz), envelopes (adsr_live), dynamics
(limiter), delay/tap, reverb_stereo, oversample, and denormal
helpers. Check each one's exact signature and behavior in the docs for the
locked version.