Skip to main content

generator_algorithm

Macro generator_algorithm 

Source
macro_rules! generator_algorithm {
    (
        $(#[$struct_meta:meta])*
        $vis:vis struct $name:ident<$($generic:ident: $bound:path),+> {
            params: {
                $(
                    $(#[$param_meta:meta])*
                    $param_name:ident : $param_type:ty = $param_default:expr
                ),* $(,)?
            },
            state: {
                $(
                    $(#[$state_meta:meta])*
                    $state_name:ident : $state_type:ty = $state_default:expr
                ),* $(,)?
            },
            generate: $generate:expr
        }
    ) => { ... };
}
Expand description

Macro for creating a generator

ยงExample

use rill_core_dsp::generator_algorithm; use rill_core::math::Transcendental;

generator_algorithm! { /// Sine generator #[derive(Debug, Clone, Copy)] pub struct SineGen<T: Transcendental> { params: { freq: T = T::from_f32(440.0), amp: T = T::from_f32(0.5), }, state: { phase: T = T::ZERO, }, generate: |this| { let output = (this.phase * T::from_f32(2.0 * std::f32::consts::PI)).sin() * this.amp; let phase_inc = this.freq / T::from_f32(this.sample_rate); this.phase = (this.phase + phase_inc) % T::from_f32(1.0); output } } }