weirdnet-audio-plugin 0.3.0

Audio plugins for Weirdnet: the Plugin trait, typed parameters, a registry, chain processing with a content-hash cache key, and the built-ins gain, levels and frequencies.
Documentation
use crate::{ParamSpec, ParamValues, Plugin, PluginError};
use weirdnet_media::waveform::Waveform;

/// `weirdnet.gain`: multiplies the samples by a gain in decibels, optionally inverting the polarity.
///
/// | Parameter | Range | Default |
/// | --- | --- | --- |
/// | `gain_db` | -60 to +24 dB | 0 |
/// | `invert` | on or off | off |
///
/// Zero dB with no invert returns the input's samples unchanged (shared, not copied).
pub struct Gain;

static PARAMS: [ParamSpec; 2] = [ParamSpec::float("gain_db", -60.0, 24.0, 0.0, "dB"), ParamSpec::boolean("invert", false)];

impl Plugin for Gain {
    fn id(&self) -> &'static str {
        "weirdnet.gain"
    }

    fn params(&self) -> &[ParamSpec] {
        &PARAMS
    }

    fn process(&self, input: &Waveform, values: &ParamValues) -> Result<Waveform, PluginError> {
        let gain = weirdnet_dsp::db_to_linear(values.float("gain_db") as f32);
        let gain = if values.bool("invert") { -gain } else { gain };
        Ok(input.gain_linear(gain)?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Registry;

    fn run(input: &Waveform, v: ParamValues) -> Result<Waveform, PluginError> {
        Registry::with_builtins().process("weirdnet.gain", input, &v)
    }

    #[test]
    fn default_is_the_same_samples() {
        let w = Waveform::tone(48_000, 440.0, 100, 0.5).unwrap();
        let out = run(&w, ParamValues::new()).unwrap();
        assert_eq!(out, w);
        assert!(std::ptr::eq(out.samples().as_ptr(), w.samples().as_ptr()), "shared, not copied");
    }

    #[test]
    fn six_db_doubles_and_minus_six_halves() {
        let w = Waveform::new(48_000, 1, vec![0.25, -0.5, 0.0]).unwrap();
        let up = run(&w, ParamValues::new().with_float("gain_db", 6.0206)).unwrap();
        assert!(up.samples().iter().zip([0.5, -1.0, 0.0]).all(|(a, b)| (a - b).abs() < 1e-4), "{:?}", up.samples());
        let down = run(&w, ParamValues::new().with_float("gain_db", -6.0206)).unwrap();
        assert!(down.samples().iter().zip([0.125, -0.25, 0.0]).all(|(a, b)| (a - b).abs() < 1e-4));
    }

    #[test]
    fn invert_flips_the_sign_and_twice_is_identity() {
        let w = Waveform::new(48_000, 2, vec![0.25, -0.5, 1.0, 0.0]).unwrap();
        let inv = run(&w, ParamValues::new().with_bool("invert", true)).unwrap();
        assert_eq!(inv.samples(), &[-0.25, 0.5, -1.0, -0.0]);
        assert_eq!(run(&inv, ParamValues::new().with_bool("invert", true)).unwrap().samples(), w.samples());
    }

    #[test]
    fn limits_are_accepted_and_beyond_them_rejected() {
        let w = Waveform::new(48_000, 1, vec![0.001]).unwrap();
        assert!(run(&w, ParamValues::new().with_float("gain_db", -60.0)).is_ok());
        assert!(run(&w, ParamValues::new().with_float("gain_db", 24.0)).is_ok());
        assert!(matches!(run(&w, ParamValues::new().with_float("gain_db", 24.5)), Err(PluginError::InvalidParam { .. })));
    }

    #[test]
    fn keeps_format_and_length_and_handles_empty() {
        let w = Waveform::silence(44_100, 2, 0).unwrap();
        let out = run(&w, ParamValues::new().with_float("gain_db", 3.0)).unwrap();
        assert_eq!((out.sample_rate(), out.channels(), out.frames()), (44_100, 2, 0));
    }
}