Skip to main content

Module

Trait Module 

Source
pub trait Module: Send {
    type In;
    type Out;

    // Required methods
    fn tick(&mut self, input: Self::In) -> Self::Out;
    fn reset(&mut self);

    // Provided methods
    fn process(&mut self, input: &[Self::In], output: &mut [Self::Out])
       where Self::In: Clone { ... }
    fn set_sample_rate(&mut self, _sample_rate: f64) { ... }
}
Expand description

A signal processing module with typed input and output.

This is the fundamental abstraction for DSP processing in Quiver. Modules are stateful processors that transform input samples to output samples. The associated types In and Out enable compile-time verification of signal shape (arity): a Module<Out = f64> only chains into a Module<In = f64>, a stereo (f64, f64) only into a (f64, f64), and so on. This is structural type safety; it does not check semantic SignalKind (Audio vs CV vs V/Oct) — both are f64 here. Signal-kind compatibility is validated by the graph layer (crate::port / crate::graph).

§Mathematical Model

A module represents a morphism in the category of signals:

M : In → Out

The tick method computes one step of this transformation, potentially updating internal state (like oscillator phase or filter memory).

§Implementing Module

use quiver::prelude::*;

struct Amplifier { gain: f64 }

impl Module for Amplifier {
    type In = f64;
    type Out = f64;

    fn tick(&mut self, input: f64) -> f64 {
        input * self.gain
    }

    fn reset(&mut self) {
        // Amplifier is stateless, nothing to reset
    }
}

let mut amp = Amplifier { gain: 2.0 };
assert_eq!(amp.tick(0.5), 1.0);

§Thread Safety

All modules must be Send to allow audio processing on dedicated threads.

Required Associated Types§

Source

type In

Input signal type (e.g., f64 for mono, (f64, f64) for stereo)

Source

type Out

Output signal type

Required Methods§

Source

fn tick(&mut self, input: Self::In) -> Self::Out

Process a single sample, advancing internal state by one time step.

This is the core DSP function. For a VCO, this updates phase and outputs a waveform sample. For a filter, this processes through the filter stages.

Source

fn reset(&mut self)

Reset internal state to initial conditions.

Called when starting a new note, reinitializing the synth, etc. For oscillators, this typically resets phase. For filters, clears memory.

Provided Methods§

Source

fn process(&mut self, input: &[Self::In], output: &mut [Self::Out])
where Self::In: Clone,

Process a block of samples for efficiency.

Override this method for SIMD optimization or when block processing is more efficient than sample-by-sample. The default implementation simply calls tick in a loop.

Source

fn set_sample_rate(&mut self, _sample_rate: f64)

Notify module of sample rate changes.

Modules with time-dependent behavior (filters, delays, envelopes) should recalculate coefficients here.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<A, B> Module for Chain<A, B>
where A: Module, B: Module<In = A::Out>,

Source§

type In = <A as Module>::In

Source§

type Out = <B as Module>::Out

Source§

impl<A, B> Module for Fanout<A, B>
where A: Module, B: Module<In = A::In>, A::In: Clone,

Source§

type In = <A as Module>::In

Source§

type Out = (<A as Module>::Out, <B as Module>::Out)

Source§

impl<A, B> Module for Parallel<A, B>
where A: Module, B: Module,

Source§

type In = (<A as Module>::In, <B as Module>::In)

Source§

type Out = (<A as Module>::Out, <B as Module>::Out)

Source§

impl<A: Send, B: Send> Module for Swap<A, B>

Source§

impl<F, A, B> Module for Arr<F, A>
where F: Fn(A) -> B + Send, A: Send,

Source§

type In = A

Source§

type Out = B

Source§

impl<G: GraphModule> Module for GraphModuleAdapter<G>

Source§

impl<M, C> Module for First<M, C>
where M: Module, C: Send,

Source§

type In = (<M as Module>::In, C)

Source§

type Out = (<M as Module>::Out, C)

Source§

impl<M, C> Module for Second<M, C>
where M: Module, C: Send,

Source§

type In = (C, <M as Module>::In)

Source§

type Out = (C, <M as Module>::Out)

Source§

impl<M, F, Combined> Module for Feedback<M, F>
where M: Module<In = Combined>, F: Fn(M::Out, M::Out) -> Combined + Send, M::Out: Default + Clone + Send,

Source§

type In = <M as Module>::Out

Source§

type Out = <M as Module>::Out

Source§

impl<M, F, U> Module for Contramap<M, F, U>
where M: Module, F: Fn(U) -> M::In + Send, U: Send,

Source§

type In = U

Source§

type Out = <M as Module>::Out

Source§

impl<M, F, U> Module for Map<M, F>
where M: Module, F: Fn(M::Out) -> U + Send,

Source§

type In = <M as Module>::In

Source§

type Out = U

Source§

impl<T, F> Module for Merge<T, F>
where T: Send, F: Fn(T, T) -> T + Send,

Source§

impl<T: Clone + Send> Module for Constant<T>

Source§

type In = ()

Source§

type Out = T

Source§

impl<T: Clone + Send> Module for Split<T>

Source§

impl<T: Send> Module for Identity<T>

Source§

type In = T

Source§

type Out = T