lasprs 0.14.3

Library for Acoustic Signal Processing (Rust edition, with optional Python bindings via pyo3)
Documentation
use crate::filter::{Filter, FilterMethods};
use crate::*;
/// Signal generator config for a certain channel
#[derive(Clone, Debug)]
pub(crate) struct SiggenChannelConfig {
    muted: bool,
    prefilter: Option<Filter>,
    gain: Positive,
    pub DCOffset: Flt,
}
unsafe impl Send for SiggenChannelConfig {}
impl SiggenChannelConfig {
    /// Set new pre-filter that filters the source signal
    pub fn setPreFilter(&mut self, pref: Option<Filter>) {
        self.prefilter = pref;
    }
    /// Set the gain applied to the source signal
    ///
    /// * `g`: Gain value. Can be any float. If set to 0.0, the source is effectively muted. Only
    ///   using (setMute) is a more efficient way to do this.
    pub fn setGain(&mut self, g: Positive) {
        self.gain = g;
    }

    /// Reset signal channel config. Only resets the prefilter state
    pub fn reset(&mut self, _fs: StrictlyPositive) {
        if let Some(f) = &mut self.prefilter {
            f.reset()
        }
    }
    /// Generate new channel configuration using 'arbitrary' initial config: muted false, gain 1.0, DC offset 0.
    /// and no prefilter
    pub fn new() -> SiggenChannelConfig {
        SiggenChannelConfig {
            // Start muted
            muted: true,
            prefilter: None,
            gain: 1.0.try_into().unwrap(),
            DCOffset: 0.0,
        }
    }

    /// Set mute on channel. If true, only DC signal offset is outputed from (SiggenChannelConfig::transform).
    pub fn setMute(&mut self, mute: bool) {
        self.muted = mute;
    }
    /// Generate new signal data, given input source data.
    ///
    /// # Args
    ///
    /// source: Input source signal.
    /// result: Reference of array of float values to be filled with signal data.
    ///
    /// # Details
    ///
    /// - When muted, the DC offset is still applied
    /// - The order of the generation is:
    ///     - If a prefilter is installed, this pre-filter is applied to the source signal.
    ///     - Gain is applied.
    ///     - Offset is applied (thus, no gain is applied to the DC offset).
    ///
    pub fn genSignal(&mut self, source: &[Flt], result: &mut [Flt]) {
        if self.muted {
            result.iter_mut().for_each(|x| {
                *x = 0.0;
            });
        } else {
            if let Some(filter) = &mut self.prefilter {
                filter.filter(source, result);
            } else {
                result.copy_from_slice(source);
            }
        }
        result.iter_mut().for_each(|x| {
            // First apply gain, then offset
            *x *= *self.gain;
            *x += self.DCOffset;
        });
    }
}