From 2efb610caa720f58eda7ea905d360f19606378fb Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - Redu-Sone B.V., ASCEE V.O.F." Date: Sun, 29 Sep 2024 21:17:16 +0200 Subject: [PATCH] Added export of timeweighting to Python code --- src/lib.rs | 1 + src/slm/tw.rs | 30 ++++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8430507..0b6a80e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,6 +62,7 @@ fn lasprs(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_class::()?; m.add_class::()?; + m.add_class::()?; m.add_class::()?; m.add_class::()?; m.add_class::()?; diff --git a/src/slm/tw.rs b/src/slm/tw.rs index 8f0fe87..82b40d4 100644 --- a/src/slm/tw.rs +++ b/src/slm/tw.rs @@ -1,16 +1,18 @@ use crate::config::*; + +use strum::EnumMessage; +use strum_macros::Display; /// Time weighting to use in level detection of Sound Level Meter. -/// +/// // Do the following when Pyo3 0.22 can finally be used combined with rust-numpy: // #[cfg_attr(feature = "python-bindings", pyclass(eq))] // For now: #[cfg_attr(feature = "python-bindings", pyclass)] -#[derive(Clone, Copy, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq, Display)] pub enum TimeWeighting { // I know that the curly braces here are not required and add some // boilerplate, but this is the only way Pyo3 swallows complex enums at the // moment. - /// Slow time weighting ~ 1 s Slow {}, /// Fast time weighting ~ 1/8 s @@ -22,7 +24,7 @@ pub enum TimeWeighting { /// Custom time constant [s] t: Flt, }, - + /// A custom symmetric time weighting CustomAsymmetric { /// Time weighting when level is increasing @@ -38,6 +40,14 @@ impl TimeWeighting { fn __eq__(&self, other: &Self) -> bool { self == other } + fn __str__(&self) -> String { + format!("{self}") + } + #[staticmethod] + fn all_standards() -> Vec { + use TimeWeighting::*; + vec![Slow {}, Fast {}, Impulse {}] + } } impl Default for TimeWeighting { @@ -85,3 +95,15 @@ impl TimeWeighting { } } } + +#[cfg(test)] +mod test { + use crate::slm::TimeWeighting; + + #[test] + fn test_tw() { + println!("Impulse: {}", TimeWeighting::Impulse {}); + println!("Fast : {}", TimeWeighting::Fast {}); + println!("Slow : {}", TimeWeighting::Slow {}); + } +}