Re-implemented simpleclip, debugged

This commit is contained in:
Anne de Jong 2024-11-02 00:22:07 +01:00
parent f58c6f8839
commit 36de394a2e

View File

@ -5,7 +5,7 @@ use crate::math::max;
use crate::math::maxabs; use crate::math::maxabs;
use crate::math::min; use crate::math::min;
use crate::Flt; use crate::Flt;
use crossbeam::channel::bounded; use crossbeam::channel::unbounded;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::sync::atomic::Ordering::Relaxed; use std::sync::atomic::Ordering::Relaxed;
use std::{ use std::{
@ -15,7 +15,7 @@ use std::{
/// If signal is below / above the range times the value below, we indicate that /// If signal is below / above the range times the value below, we indicate that
/// the signal has clipped. /// the signal has clipped.
const CLIP_REL_LIMIT: Flt = 0.999; const CLIP_REL_LIMIT: Flt = 0.99;
/// Very simple clip detector. Used to detect cliping in a recording. Stores one /// Very simple clip detector. Used to detect cliping in a recording. Stores one
/// clip value if just something happened between time of new and moment of drop(). /// clip value if just something happened between time of new and moment of drop().
@ -32,7 +32,7 @@ impl SimpleClipDetector {
/// ///
/// - `smgr` - see [StreamMgr] /// - `smgr` - see [StreamMgr]
pub fn new(smgr: &mut StreamMgr) -> Self { pub fn new(smgr: &mut StreamMgr) -> Self {
let (tx, rx) = bounded(0); let (tx, rx) = unbounded();
let clipstate = Arc::new(AtomicBool::new(false)); let clipstate = Arc::new(AtomicBool::new(false));
let stopThread = Arc::new(AtomicBool::new(false)); let stopThread = Arc::new(AtomicBool::new(false));
@ -41,58 +41,62 @@ impl SimpleClipDetector {
let stopThread2 = stopThread.clone(); let stopThread2 = stopThread.clone();
smgr.addInQueue(tx); smgr.addInQueue(tx);
rayon::spawn(move || loop { rayon::spawn(
let mut streammeta: Option<Arc<StreamMetaData>> = None; move || {
if let Ok(msg) = rx.recv_timeout(Duration::from_millis(1500)) { let mut streammeta: Option<Arc<StreamMetaData>> = None;
match msg { loop {
InStreamMsg::InStreamData(dat) => { if let Ok(msg) = rx.recv_timeout(Duration::from_millis(1500)) {
let meta = streammeta match msg {
.expect("If we are here, stream metadata should be available"); InStreamMsg::StreamStarted(meta) => {
let flt = dat.getFloatData(); streammeta = Some(meta);
let maxs = flt }
.columns() InStreamMsg::InStreamData(dat) => {
.into_iter() let meta = streammeta
.map(|col| max(col)) .as_ref()
.collect::<Vec<_>>(); .expect("If we are here, stream metadata should be available");
let mins = flt let flt = dat.getFloatData();
.columns() let maxs = flt
.into_iter() .columns()
.map(|col| min(col)) .into_iter()
.collect::<Vec<_>>(); .map(|col| max(col))
.collect::<Vec<_>>();
let mins = flt
.columns()
.into_iter()
.map(|col| min(col))
.collect::<Vec<_>>();
let mut clip = false; maxs.into_iter().zip(mins).zip(&meta.channelInfo).for_each(
|((max, min), ch)| {
maxs.into_iter().zip(mins).zip(&meta.channelInfo).for_each( let min_for_clip = CLIP_REL_LIMIT * ch.range.0;
|((max, min), ch)| { let max_for_clip = CLIP_REL_LIMIT * ch.range.1;
let min_for_clip = CLIP_REL_LIMIT * ch.range.0; if max >= max_for_clip {
let max_for_clip = CLIP_REL_LIMIT * ch.range.1; clipstate.store(true, Relaxed);
if max >= max_for_clip { // We do not have to do anything anymore. The signal
clip = true; // has clipped so we do not have to check any new
} // blocks anymore.
if min <= min_for_clip { return;
clip = true; }
} if min <= min_for_clip {
}, clipstate.store(true, Relaxed);
); // We do not have to do anything anymore. The signal
// has clipped so we do not have to check any new
if clip { // blocks anymore.
clipstate.store(true, Relaxed); return;
// We do not have to do anything anymore. The signal }
// has clipped so we do not have to check any new },
// blocks anymore. );
return; }
} // Ignore other stream messages
_ => {}
} // end match msg
}; // end if let Ok(msg)
if stopThread.load(Relaxed) {
return;
} }
InStreamMsg::StreamStarted(meta) => { } // end of loop
streammeta = Some(meta); }, // End of rayon closure
} );
_ => {}
}
};
if stopThread.load(Relaxed) {
return;
}
});
Self { Self {
clipped: clipstate2, clipped: clipstate2,