removed one loop vector allocation. Implemented a bit smarter

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

View File

@ -44,10 +44,14 @@ impl SimpleClipDetector {
rayon::spawn( rayon::spawn(
move || { move || {
let mut streammeta: Option<Arc<StreamMetaData>> = None; let mut streammeta: Option<Arc<StreamMetaData>> = None;
let mut mins = vec![];
let mut maxs = vec![];
loop { 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) => { InStreamMsg::StreamStarted(meta) => {
mins.resize(meta.nchannels(), 0.);
maxs.resize(meta.nchannels(), 0.);
streammeta = Some(meta); streammeta = Some(meta);
} }
InStreamMsg::InStreamData(dat) => { InStreamMsg::InStreamData(dat) => {
@ -55,37 +59,39 @@ impl SimpleClipDetector {
.as_ref() .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 // Update channel maximum values
.columns() flt.columns()
.into_iter() .into_iter()
.map(|col| max(col)) .zip(maxs.iter_mut())
.collect::<Vec<_>>(); .for_each(|(coli, maxi)| *maxi = max(coli));
let mins = flt // Update channel minimum values
.columns() flt.columns()
.into_iter() .into_iter()
.map(|col| min(col)) .zip(mins.iter_mut())
.collect::<Vec<_>>(); .for_each(|(coli, mini)| *mini = min(coli));
maxs.into_iter().zip(mins).zip(&meta.channelInfo).for_each( // Compare minima and maxima against clip limits
|((max, min), ch)| { maxs.iter()
.zip(mins.iter())
.zip(&meta.channelInfo)
.for_each(|((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 {
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 { if *min <= min_for_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;
} }
}, });
);
} }
// Ignore other stream messages // Ignore other stream messages
_ => {} _ => {}