Skip to Content
Soundor Skills

Using RustFFT

A reference of the soundor-dsp skill.

soundor-dsp reads this reference when a task needs it. Source on GitHub

RustFFT (docs) computes FFTs of any size in pure Rust, with SIMD on x86 and ARM. License: MIT OR Apache-2.0.

# lib/Cargo.toml
[dependencies]
rustfft = "6.4"

Default features enable AVX, SSE and Neon, selected at run time. On wasm32-unknown-unknown it runs without SIMD unless the wasm_simd feature is enabled, which needs a runtime with fixed-width SIMD; leave it off unless a measurement shows the web build needs it.

Plan in prepare, process with scratch

Planning allocates and caches twiddle factors; process() without scratch allocates every call. So, as Soundor Prism's analyzer does:

use std::sync::Arc;

use rustfft::num_complex::Complex;
use rustfft::{Fft, FftPlanner};

const FFT_SIZE: usize = 1024;

#[derive(Default)]
pub struct PluginProcessor {
    fft: Option<Arc<dyn Fft<f32>>>,
    frame: Vec<Complex<f32>>,
    scratch: Vec<Complex<f32>>,
}

impl Processor for PluginProcessor {
    fn prepare(&mut self, _ctx: &PrepareContext) {
        let fft = FftPlanner::new().plan_fft_forward(FFT_SIZE);
        self.scratch = vec![Complex::default(); fft.get_inplace_scratch_len()];
        self.frame = vec![Complex::default(); FFT_SIZE];
        self.fft = Some(fft);
    }

    fn process(&mut self, ctx: &mut ProcessContext) {
        // … fill self.frame with the windowed input …
        if let Some(fft) = &self.fft {
            fft.process_with_scratch(&mut self.frame, &mut self.scratch);
        }
        // … read magnitudes, publish through ctx.streams …
    }
}
  • Plan every size the plugin offers in prepare() and keep the plans (Prism plans four sizes and allocates one scratch buffer of the largest scratch length). Never replace a plan in process(): dropping the old Arc frees memory on the audio thread.
  • Size scratch with get_inplace_scratch_len() (or get_outofplace_scratch_len() for process_outofplace_with_scratch).

Scaling and windows

  • RustFFT does not normalize: a forward then inverse transform multiplies by the size N. Divide explicitly.
  • Window analysis frames (Hann is a good default) and correct for the window's gain: a full-scale sine reads 0 dBFS when the magnitude is scaled by 2 / sum(window).
  • Real input fills only the real parts; bins above N / 2 mirror those below it. Use bins 0..=N/2.
  • For spectra on a log-frequency display, average the bins each display band covers, with bands computed in prepare() for the sample rate.

Analysis versus resynthesis

  • Analysis (meters, spectrum displays): hop by a fixed number of samples kept in a ring buffer, transform every hop, publish the result through a buffer<float32> stream written with buffer_mut() and commit(). The audio passes through unchanged, so there is no latency.
  • Resynthesis (spectral effects, FFT convolution, linear-phase EQ): overlap-add with a window pair that sums to a constant, and declare the frame latency in audio.latency. This is substantially harder; follow a documented method (see the DSP references) and test it against a time-domain reference.

Test

  • Transform a sine at an exact bin frequency: the peak is in that bin, at the expected level, and leakage elsewhere is below the window's sidelobe level.
  • Count allocations across a hop boundary: process() with enough frames to trigger a transform, then assert zero.

On this page