Skip to main content

Processor

Trait Processor 

Source
pub trait Processor: Send + 'static {
    type Params: ProcessorParams + Default + Send + Sync + 'static;

    // Required method
    fn process(
        &mut self,
        buffer: &mut [&mut [f32]],
        transport: &Transport,
        params: &Self::Params,
    );

    // Provided methods
    fn set_sample_rate(&mut self, _sample_rate: f32) { ... }
    fn reset(&mut self) { ... }
}
Expand description

Trait for user-implemented DSP processors.

Implement this trait to define custom audio processing logic. All methods must be real-time safe (no allocations, locks, or syscalls).

§Example

use wavecraft_dsp::{ParamRange, ParamSpec, Processor, ProcessorParams, Transport};

#[derive(Default)]
struct MyGainParams {
    level: f32,
}

impl ProcessorParams for MyGainParams {
    fn param_specs() -> &'static [ParamSpec] {
        &[ParamSpec {
            name: "Level",
            id_suffix: "level",
            range: ParamRange::Linear { min: 0.0, max: 2.0 },
            default: 1.0,
            unit: "x",
            group: None,
        }]
    }
}

struct MyGain {
    sample_rate: f32,
}

impl Processor for MyGain {
    type Params = MyGainParams;

    fn process(
        &mut self,
        buffer: &mut [&mut [f32]],
        _transport: &Transport,
        params: &Self::Params,
    ) {
        for channel in buffer.iter_mut() {
            for sample in channel.iter_mut() {
                *sample *= params.level;
            }
        }
    }
}

Required Associated Types§

Source

type Params: ProcessorParams + Default + Send + Sync + 'static

Associated parameter type for this processor.

Use () for processors with no parameters, or define a struct with #[derive(ProcessorParams)].

Required Methods§

Source

fn process( &mut self, buffer: &mut [&mut [f32]], transport: &Transport, params: &Self::Params, )

Process a buffer of audio samples.

The buffer is provided as a slice of mutable slices, one per channel. Modify samples in-place to apply your DSP effect.

§Arguments
  • buffer - Audio channels as [L, R, ...] where each channel is [samples]
  • transport - Playback timing information
  • params - Current parameter values
§Real-Time Safety

This method is called on the audio thread. It MUST be real-time safe:

  • No allocations (Vec::push, String, Box::new)
  • No locks (Mutex, RwLock)
  • No syscalls (file I/O, logging, network)
  • No panics (use debug_assert! only)

Provided Methods§

Source

fn set_sample_rate(&mut self, _sample_rate: f32)

Called when the sample rate changes.

Use this to update internal state that depends on sample rate (e.g., filter coefficients, delay line sizes).

§Arguments
  • sample_rate - New sample rate in Hz (e.g., 44100.0, 48000.0)
§Default

No-op by default. Override if your processor needs sample rate.

Source

fn reset(&mut self)

Reset processor state.

Called when the host stops playback or when the user resets the plugin. Use this to clear delay lines, reset filters, etc.

§Default

No-op by default. Override if your processor maintains state.

Implementors§