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,12 +41,18 @@ impl SimpleClipDetector {
let stopThread2 = stopThread.clone(); let stopThread2 = stopThread.clone();
smgr.addInQueue(tx); smgr.addInQueue(tx);
rayon::spawn(move || loop { rayon::spawn(
move || {
let mut streammeta: Option<Arc<StreamMetaData>> = None; let mut streammeta: Option<Arc<StreamMetaData>> = None;
loop {
if let Ok(msg) = rx.recv_timeout(Duration::from_millis(1500)) { if let Ok(msg) = rx.recv_timeout(Duration::from_millis(1500)) {
match msg { match msg {
InStreamMsg::StreamStarted(meta) => {
streammeta = Some(meta);
}
InStreamMsg::InStreamData(dat) => { InStreamMsg::InStreamData(dat) => {
let meta = streammeta let meta = streammeta
.as_ref()
.expect("If we are here, stream metadata should be available"); .expect("If we are here, stream metadata should be available");
let flt = dat.getFloatData(); let flt = dat.getFloatData();
let maxs = flt let maxs = flt
@ -60,39 +66,37 @@ impl SimpleClipDetector {
.map(|col| min(col)) .map(|col| min(col))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut clip = false;
maxs.into_iter().zip(mins).zip(&meta.channelInfo).for_each( maxs.into_iter().zip(mins).zip(&meta.channelInfo).for_each(
|((max, min), ch)| { |((max, min), ch)| {
let min_for_clip = CLIP_REL_LIMIT * ch.range.0; let min_for_clip = CLIP_REL_LIMIT * ch.range.0;
let max_for_clip = CLIP_REL_LIMIT * ch.range.1; let max_for_clip = CLIP_REL_LIMIT * ch.range.1;
if max >= max_for_clip { if max >= max_for_clip {
clip = true;
}
if min <= min_for_clip {
clip = true;
}
},
);
if clip {
clipstate.store(true, Relaxed); clipstate.store(true, Relaxed);
// We do not have to do anything anymore. The signal // We do not have to do anything anymore. The signal
// has clipped so we do not have to check any new // has clipped so we do not have to check any new
// blocks anymore. // blocks anymore.
return; return;
} }
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
// blocks anymore.
return;
} }
InStreamMsg::StreamStarted(meta) => { },
streammeta = Some(meta); );
} }
// Ignore other stream messages
_ => {} _ => {}
} } // end match msg
}; }; // end if let Ok(msg)
if stopThread.load(Relaxed) { if stopThread.load(Relaxed) {
return; return;
} }
}); } // end of loop
}, // End of rayon closure
);
Self { Self {
clipped: clipstate2, clipped: clipstate2,