PPM: Increased clip indication time to 4 secs. Moved the thread creation to method startThread.

This commit is contained in:
Anne de Jong 2024-10-03 11:45:10 +02:00
parent 51e4ea8272
commit 89677a8320

View File

@ -1,6 +1,6 @@
use crate::daq::InStreamMsg; use crate::daq::InStreamMsg;
use crate::math::maxabs; use crate::math::maxabs;
use crate::slm::{self, SLMSettingsBuilder, SLM}; use crate::slm::{self, SLMSettingsBuilder, TimeWeighting, SLM};
use crate::{config::*, FreqWeighting, StandardFilterDescriptor}; use crate::{config::*, FreqWeighting, StandardFilterDescriptor};
use crate::{daq::StreamMgr, Dcol}; use crate::{daq::StreamMgr, Dcol};
use crossbeam::channel::{unbounded, Receiver, Sender}; use crossbeam::channel::{unbounded, Receiver, Sender};
@ -17,7 +17,7 @@ use std::time::{Duration, Instant};
const ALMOST_CLIPPED_REL_AMP: Flt = 0.95; const ALMOST_CLIPPED_REL_AMP: Flt = 0.95;
/// If clipping occured, this is the time it keeps saying 'signal is clipped' /// If clipping occured, this is the time it keeps saying 'signal is clipped'
const CLIP_INDICATOR_WAIT_S: Duration = Duration::from_secs(2); const CLIP_INDICATOR_WAIT_S: Duration = Duration::from_secs(4);
/// If the signal level falls below this value, we indicate that the signal level is low. /// If the signal level falls below this value, we indicate that the signal level is low.
const LEVEL_THRESHOLD_FOR_LOW_LEVEL: Flt = -50.; const LEVEL_THRESHOLD_FOR_LOW_LEVEL: Flt = -50.;
@ -42,10 +42,23 @@ impl PPM {
pub fn new(mgr: &mut StreamMgr) -> Self { pub fn new(mgr: &mut StreamMgr) -> Self {
let (sender, rxmsg) = unbounded(); let (sender, rxmsg) = unbounded();
let (tx, rxstream) = unbounded(); let (tx, rxstream) = unbounded();
// Add queue sender part of queue to stream manager
mgr.addInQueue(tx); mgr.addInQueue(tx);
let status2: SharedPPMStatus = Arc::new(Mutex::new(vec![])); // Shared status object
let status = status2.clone(); let status: SharedPPMStatus = Arc::new(Mutex::new(vec![]));
// Start the thread that calculates PPM and clip values
Self::startThread(status.clone(), rxstream, rxmsg);
PPM { status, sender }
}
fn startThread(
status: SharedPPMStatus,
rxstream: Receiver<InStreamMsg>,
rxmsg: Receiver<PPMMessage>,
) {
rayon::spawn(move || { rayon::spawn(move || {
let mut slms: Vec<SLM> = vec![]; let mut slms: Vec<SLM> = vec![];
@ -120,6 +133,7 @@ impl PPM {
.fs(meta.samplerate) .fs(meta.samplerate)
.freqWeighting(FreqWeighting::Z) .freqWeighting(FreqWeighting::Z)
.Lref(1.0) .Lref(1.0)
.timeWeighting(TimeWeighting::Impulse {})
.filterDescriptors([ .filterDescriptors([
StandardFilterDescriptor::Overall().unwrap() StandardFilterDescriptor::Overall().unwrap()
]) ])
@ -132,7 +146,7 @@ impl PPM {
s.push(PPMChannelStatus { s.push(PPMChannelStatus {
clip: ClipState::LowLevels, clip: ClipState::LowLevels,
level: -300., level: -300.,
clip_time: None clip_time: None,
}); });
}); });
} }
@ -159,24 +173,18 @@ impl PPM {
} }
} }
}); });
PPM {
status: status2,
sender,
}
} }
/// Return array of instantaneous PPM level per channel
pub fn getLevels(&self) -> Dcol {
Dcol::from_iter(self.status.lock().iter().map(|s| s.level))
}
/// Reset clip state. Used to quickly restore the clipping state. /// Reset clip state. Used to quickly restore the clipping state.
pub fn resetClip(&self) { pub fn resetClip(&self) {
self.sender.send(PPMMessage::ResetClip).unwrap(); self.sender.send(PPMMessage::ResetClip).unwrap();
} }
fn getClipState(&self) -> Vec<ClipState> { /// Returns the current state: levels and clip state
self.status.lock().iter().map(|s| s.clip).collect() pub fn getState(&self) -> (Dcol, Vec<ClipState>) {
let status = self.status.lock();
let levels = Dcol::from_iter(status.iter().map(|s| s.level));
let clips = status.iter().map(|s| s.clip).collect();
(levels, clips)
} }
} }
impl Drop for PPM { impl Drop for PPM {
@ -228,14 +236,11 @@ impl PPM {
Self::new(smgr) Self::new(smgr)
} }
#[pyo3(name = "getLevels")] #[pyo3(name = "getState")]
fn getLevels_py<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<Flt>> { fn getState_py<'py>(&self, py: Python<'py>) -> (Bound<'py, PyArray1<Flt>>, Vec<ClipState>) {
self.getLevels().to_pyarray_bound(py) let (levels, clips) = self.getState();
} let levels = levels.to_pyarray_bound(py);
(levels, clips)
#[pyo3(name = "getClipState")]
fn getClipState_py(&self) -> Vec<ClipState> {
self.getClipState()
} }
#[pyo3(name = "resetClip")] #[pyo3(name = "resetClip")]