voice-filter 0.1.2

Live voice filter for PipeWire: a virtual microphone that removes the echo of the speakers and background noise (DeepFilterNet3) and enhances the voice
use ::std::sync::Arc;

use ::anyhow::{Result, ensure};

use crate::atomic::AtomicF32;

/// A setting of a stage that the control interface changes while the stage runs in the audio
/// path: the stage reads [`Param::get`] at the start of every frame and applies a change with it.
/// Clones share the value.
#[derive(Clone, Debug)]
pub struct Param {
    /// Unique within its stage, for the control interface.
    pub name: &'static str,
    /// For the app.
    pub title: &'static str,
    /// Shown after the value, e.g. `dB`, `Hz` or `:1`.
    pub unit: &'static str,
    pub min: f32,
    pub max: f32,
    /// The step the app offers.
    pub step: f32,
    pub default: f32,
    value: Arc<AtomicF32>,
}

impl Param {
    pub fn new(
        name: &'static str,
        title: &'static str,
        unit: &'static str,
        (min, max, step): (f32, f32, f32),
        default: f32,
    ) -> Self {
        Self {
            name,
            title,
            unit,
            min,
            max,
            step,
            default,
            value: Arc::new(AtomicF32::new(default)),
        }
    }

    /// The parameter `name` of `stage` among the parameters of a chain, as
    /// [`crate::chain::Chain::params`] gives them.
    pub fn find<'a>(
        params: &'a [(&'static str, Param)],
        stage: &str,
        name: &str,
    ) -> Option<&'a Param> {
        params
            .iter()
            .find(|(of, param)| *of == stage && param.name == name)
            .map(|(_, param)| param)
    }

    pub fn get(&self) -> f32 {
        self.value.load()
    }

    /// Fails outside `min..=max` (and for NaN), the value stays as it is then.
    pub fn set(&self, value: f32) -> Result<()> {
        ensure!(
            (self.min..=self.max).contains(&value),
            "{} must be {} to {} {}, not {value}",
            self.name,
            self.min,
            self.max,
            self.unit,
        );
        self.value.store(value);
        Ok(())
    }
}