lasprs 0.14.0

Library for Acoustic Signal Processing (Rust edition, with optional Python bindings via pyo3)
//! Physical quantities that are input / output of a daq device. Provides an enumeration for these.
//!

use crate::{StrictlyPositive, config::*};
use serde::{Deserialize, Serialize};
use strum::EnumMessage;
use strum_macros;

/// Physical quantities that are I/O of a Daq device.
#[cfg_attr(
    feature = "python-bindings",
    gen_stub_pyclass_enum,
    pyclass(eq, eq_int, from_py_object)
)]
#[derive(
    PartialEq, Serialize, Deserialize, strum_macros::EnumMessage, Debug, Clone, Copy, Default,
)]
#[allow(dead_code)]
#[repr(u32)]
pub enum Qty {
    /// Number
    #[default]
    #[strum(message = "number", detailed_message = "Unit full scale")]
    Number = 0,
    /// Acoustic pressure
    #[strum(
        message = "acousticpressure",
        detailed_message = "Acoustic Pressure [Pa]"
    )]
    AcousticPressure = 1,
    /// Voltage
    #[strum(message = "voltage", detailed_message = "Voltage [V]")]
    Voltage = 2,

    #[strum(message = "vibration", detailed_message = "Vibration [m/s²]")]
    /// Vibration
    Vib = 3,
}
impl From<u32> for Qty {
    fn from(value: u32) -> Self {
        match value {
            0 => Qty::Number,
            1 => Qty::AcousticPressure,
            2 => Qty::Voltage,
            3 => Qty::Vib,
            _ => panic!("Invalid value for Qty"),
        }
    }
}

#[cfg_attr(feature = "python-bindings", gen_stub_pymethods, pymethods)]
impl Qty {
    #[cfg(feature = "python-bindings")]
    #[staticmethod]
    fn all() -> Vec<Qty> {
        use Qty::*;
        vec![Number, AcousticPressure, Voltage]
    }
    fn __str__(&self) -> String {
        self.get_detailed_message().unwrap().into()
    }

    #[cfg(feature = "python-bindings")]
    #[staticmethod]
    #[pyo3(name = "default")]
    fn default_py() -> Self {
        Self::default()
    }
    /// Return a unit symbol for the current quantity
    pub fn unit_symb(&self) -> String {
        use Qty::*;
        match self {
            Number => "1".into(),
            AcousticPressure => "Pa".into(),
            Voltage => "V".into(),
            Vib => "m/s²".into(),
        }
    }
    /// Reference level for computing dB's
    pub fn level_ref_value(&self) -> StrictlyPositive {
        use Qty::*;
        match self {
            Number => 1.0.try_into().unwrap(),
            AcousticPressure => 2e-5.try_into().unwrap(),
            Voltage => 1.0.try_into().unwrap(),
            Vib => 1.0.try_into().unwrap(),
        }
    }
    /// Level units (dB re..)
    pub fn level_unit(&self) -> String {
        use Qty::*;
        match self {
            Number => "dB re FS".into(),
            AcousticPressure => "dB SPL".into(),
            Voltage => "dBV".into(),
            Vib => "dB re 1 m/s²".into(),
        }
    }
    /// Reference level text:
    /// - "" for number,
    /// - "re 20 µPa" for acoustic pressure,
    /// - "re 1 V" for voltage,
    /// - "re 1 m/s²" for vibration.
    pub fn ref_level_txt(&self) -> String {
        use Qty::*;
        match self {
            Number => "".into(),
            AcousticPressure => "re 20 µPa".into(),
            Voltage => "re 1 V".into(),
            Vib => "re 1 m/s²".into(),
        }
    }
}