Added export of timeweighting to Python code

This commit is contained in:
Anne de Jong 2024-09-29 21:17:16 +02:00
parent 65df1c82f6
commit 2efb610caa
2 changed files with 27 additions and 4 deletions

View File

@ -62,6 +62,7 @@ fn lasprs(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<ps::FreqWeighting>()?;
m.add_class::<slm::SLMSettings>()?;
m.add_class::<slm::SLM>()?;
m.add_class::<slm::TimeWeighting>()?;
m.add_class::<ps::WindowType>()?;
m.add_class::<ps::Overlap>()?;
m.add_class::<ps::ApsMode>()?;

View File

@ -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<TimeWeighting> {
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 {});
}
}