Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

improve(audio): sum remaining waveform windows in pairs

vedanth98b13533d48b
diff --git a/crates/audio/examples/waveform-tail-benchmark.rs b/crates/audio/examples/waveform-tail-benchmark.rs
new file mode 100644
index 000000000..052a5137a
--- /dev/null
+++ b/crates/audio/examples/waveform-tail-benchmark.rs
@@ -0,0 +1,84 @@
+use cap_audio::waveform_peaks;
+use std::{
+ collections::hash_map::DefaultHasher,
+ error::Error,
+ hash::{Hash, Hasher},
+ hint::black_box,
+ time::Instant,
+};
+
+#[inline(never)]
+fn peaks(blocks: &[&[f32]], channels: u16) -> Vec<f32> {
+ waveform_peaks(blocks.iter().copied(), channels)
+}
+
+fn main() -> Result<(), Box<dyn Error>> {
+ let mut arguments = std::env::args().skip(1);
+ let seconds: usize = arguments.next().unwrap_or_else(|| "600".into()).parse()?;
+ let channels: u16 = arguments.next().unwrap_or_else(|| "1".into()).parse()?;
+ let samples: usize = arguments.next().unwrap_or_else(|| "25".into()).parse()?;
+ if seconds == 0
+ || seconds > 3600
+ || !(1..=2).contains(&channels)
+ || samples == 0
+ || arguments.next().is_some()
+ {
+ return Err("expected seconds (1..=3600), channels (1..=2), samples (>0)".into());
+ }
+ let mut state = 0x1234_5678_u32;
+ let source: Vec<f32> = (0..seconds * 48_000 * usize::from(channels))
+ .map(|_| {
+ state ^= state << 13;
+ state ^= state >> 17;
+ state ^= state << 5;
+ state as f32 / u32::MAX as f32 * 2.0 - 1.0
+ })
+ .collect();
+ let window = 4800 * usize::from(channels);
+ let expected: Vec<f32> = source
+ .chunks(window)
+ .map(|chunk| {
+ let average =
+ chunk.iter().fold(0.0_f32, |sum, value| sum + value.abs()) / chunk.len() as f32;
+ if average > 0.0 {
+ 20.0 * average.log10()
+ } else {
+ -60.0
+ }
+ })
+ .collect();
+ let expected_bits: Vec<_> = expected.iter().map(|value| value.to_bits()).collect();
+ println!("seconds,channels,layout,sample,elapsed_ms,fingerprint");
+ for layout in ["contiguous", "decoded-blocks"] {
+ let owned: Vec<Vec<f32>> = if layout == "decoded-blocks" {
+ source
+ .chunks(32768 * usize::from(channels))
+ .map(<[f32]>::to_vec)
+ .collect()
+ } else {
+ Vec::new()
+ };
+ let blocks: Vec<&[f32]> = if layout == "decoded-blocks" {
+ owned.iter().map(Vec::as_slice).collect()
+ } else {
+ vec![&source]
+ };
+ for sample in 0..samples + 3 {
+ let started = Instant::now();
+ let actual = black_box(peaks(black_box(&blocks), black_box(channels)));
+ let elapsed_ms = started.elapsed().as_secs_f64() * 1000.0;
+ let bits: Vec<_> = actual.iter().map(|value| value.to_bits()).collect();
+ assert_eq!(bits, expected_bits);
+ let mut hash = DefaultHasher::new();
+ bits.hash(&mut hash);
+ if sample >= 3 {
+ println!(
+ "{seconds},{channels},{layout},{},{elapsed_ms:.6},{}",
+ sample - 3,
+ hash.finish()
+ );
+ }
+ }
+ }
+ Ok(())
+}
diff --git a/crates/audio/src/progressive.rs b/crates/audio/src/progressive.rs
index 702524e85..bd44bcefc 100644
--- a/crates/audio/src/progressive.rs
+++ b/crates/audio/src/progressive.rs
@@ -31,6 +31,10 @@ pub fn waveform_peaks<'a>(slices: impl IntoIterator<Item = &'a [f32]>, channels:
let (group, rest) = slice.split_at(block_samples * WAVEFORM_PEAK_LANES);
push_waveform_peak_lanes(&mut peaks, group, block_samples);
slice = rest;
+ } else if count == 0 && slice.len() >= block_samples * 2 {
+ let (group, rest) = slice.split_at(block_samples * 2);
+ push_waveform_peak_pair(&mut peaks, group, block_samples);
+ slice = rest;
} else {
let (window, rest) = slice.split_at((block_samples - count).min(slice.len()));
sum = window.iter().fold(sum, |sum, sample| sum + sample.abs());
@@ -57,6 +61,18 @@ pub fn waveform_peaks<'a>(slices: impl IntoIterator<Item = &'a [f32]>, channels:
peaks
}
+fn push_waveform_peak_pair(peaks: &mut Vec<f32>, group: &[f32], block_samples: usize) {
+ let (first, second) = group.split_at(block_samples);
+ let mut first_sum = 0.0_f32;
+ let mut second_sum = 0.0_f32;
+ for index in 0..block_samples {
+ first_sum += first[index].abs();
+ second_sum += second[index].abs();
+ }
+ peaks.push(first_sum / block_samples as f32);
+ peaks.push(second_sum / block_samples as f32);
+}
+
fn push_waveform_peak_lanes(peaks: &mut Vec<f32>, group: &[f32], block_samples: usize) {
let (first, rest) = group.split_at(block_samples);
let (second, rest) = rest.split_at(block_samples);
@@ -1273,6 +1289,49 @@ mod tests {
}
}
+ #[test]
+ fn waveform_pairs_match_sequential_bits_at_window_and_block_edges() {
+ for channels in [0, 1, 2, 6] {
+ let window = 4_800 * usize::from(channels.max(1));
+ for length in [
+ 2 * window - 1,
+ 2 * window,
+ 2 * window + 1,
+ 3 * window + 7,
+ 6 * window + 17,
+ ] {
+ for nonfinite in [false, true] {
+ let mut source = xorshift_samples(length);
+ if nonfinite {
+ for (index, value) in [
+ (0, -0.0),
+ (window - 1, f32::from_bits(0x7fc0_0123)),
+ (window, f32::INFINITY),
+ (window + 1, f32::NEG_INFINITY),
+ ] {
+ source[index] = value;
+ }
+ }
+ let expected = sequential_waveform_peaks(&source, channels);
+ for block in [
+ 2 * window,
+ 2 * window + 1,
+ 6 * window,
+ 32_768 * usize::from(channels.max(1)),
+ ] {
+ let slices: Vec<_> = source.chunks(block).collect();
+ assert_peak_bits(
+ &waveform_peaks(slices.iter().copied(), channels),
+ &expected,
+ channels,
+ block,
+ );
+ }
+ }
+ }
+ }
+ }
+
#[test]
fn waveform_peaks_match_a_sequential_sum_across_uneven_blocks() {
let samples = xorshift_samples(48_000 * 3 + 1_234);