Skip to content

Commit 06985c9

Browse files
fix: use .midpoint() to avoid overflow.
- unlikely, might be possible. extend test coverage too Signed-off-by: Maika Namuo <httpworldview@gmail.com>
1 parent 1dbb141 commit 06985c9

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

‎src/util/audio.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ impl Channel {
3737
match self {
3838
Self::Left => left,
3939
Self::Right => right,
40-
Self::Mid => (left + right) * 0.5,
41-
Self::Side => (left - right) * 0.5,
40+
Self::Mid => left.midpoint(right),
41+
Self::Side => left.midpoint(-right),
4242
Self::None => 0.0,
4343
}
4444
}

‎src/visuals/waveform/processor.rs‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ impl WaveformProcessor {
241241
let bands = [
242242
left,
243243
right,
244-
std::array::from_fn(|band| (left[band] + right[band]) * 0.5),
245-
std::array::from_fn(|band| (left[band] - right[band]) * 0.5),
244+
std::array::from_fn(|band| left[band].midpoint(right[band])),
245+
std::array::from_fn(|band| left[band].midpoint(-right[band])),
246246
];
247247
tracker.process(std::array::from_fn(|channel| {
248248
if finite[channel] {
@@ -486,15 +486,18 @@ mod tests {
486486
.all(f32::is_finite)
487487
);
488488

489-
let mut processor = WaveformProcessor::new(config(RATE, 8));
490-
let update = process(&mut processor, &[f32::MAX, f32::MAX], 2);
491-
assert!(
492-
update
493-
.columns
494-
.iter()
495-
.flatten()
496-
.all(|c| c.min.is_finite() && c.max.is_finite())
497-
);
489+
for (right, channel) in [(f32::MAX, 2), (-f32::MAX, 3)] {
490+
let mut processor = WaveformProcessor::new(config(RATE, 8));
491+
let update = process(&mut processor, &[f32::MAX, right].repeat(128), 2);
492+
for frame in update.columns {
493+
assert!(frame.iter().all(|c| c.min.is_finite() && c.max.is_finite()));
494+
assert_eq!(
495+
(frame[channel].min, frame[channel].max),
496+
(f32::MAX, f32::MAX)
497+
);
498+
assert_eq!(frame[channel], frame[0]);
499+
}
500+
}
498501
}
499502

500503
#[test]

0 commit comments

Comments
 (0)