Min/max peak extraction off the audio decode path
Browse files at this commit57bd15091afc6243359965c24f66c19fd6792284Parent: 3adbaf7- feat
- min/max peak extraction off the audio decode path
2 changed files +121 -0
- crates/engine/src/lib.rs +1 -0
- crates/engine/src/waveform.rs +120 -0
crates/engine/src/lib.rsmodified+1 -0
| @@ -12,6 +12,7 @@ pub mod hw; | ||
| 12 | 12 | pub mod mux; |
| 13 | 13 | pub mod playback; |
| 14 | 14 | pub mod project; |
| 15 | +pub mod waveform; | |
| 15 | 16 | |
| 16 | 17 | pub use audio::{AacPacket, AacTrackParams, AudioChunk, AudioMeta, AudioProbe, AudioSession}; |
| 17 | 18 | pub use clock::PlaybackClock; |
crates/engine/src/waveform.rsadded+120 -0
| @@ -0,0 +1,120 @@ | ||
| 1 | +//! Waveform peaks for drawing a clip's audio: min/max per time bucket, taken | |
| 2 | +//! off the same read-only decode path playback uses. Path in, peaks out — no | |
| 3 | +//! project model, so a lane rework cannot reach it. | |
| 4 | + | |
| 5 | +use std::path::Path; | |
| 6 | + | |
| 7 | +use crate::AudioSession; | |
| 8 | + | |
| 9 | +/// `(min, max)` of every sample in each `1 / buckets_per_sec` window of `path`'s | |
| 10 | +/// audio, from media time 0 (priming already trimmed by the decoder). Channels | |
| 11 | +/// are folded together: one envelope per clip, not per channel. | |
| 12 | +/// | |
| 13 | +/// `Ok(None)` for a file with no audio track — a silent source is valid, not a | |
| 14 | +/// failure. Values are clamped to `[-1.0, 1.0]`, and a bucket's pair always | |
| 15 | +/// straddles zero, so silence draws as a flat line. | |
| 16 | +/// | |
| 17 | +/// Decoding the whole file runs at ~1700x realtime, but it is still linear in | |
| 18 | +/// source length: callers cache the result per source, never per repaint. | |
| 19 | +pub fn peaks( | |
| 20 | + path: impl AsRef<Path>, | |
| 21 | + buckets_per_sec: u32, | |
| 22 | +) -> crate::Result<Option<Vec<(f32, f32)>>> { | |
| 23 | + let Some((meta, rx)) = AudioSession::open(path)? else { | |
| 24 | + return Ok(None); | |
| 25 | + }; | |
| 26 | + // Kept fractional: 44100 / 30 is not whole, and rounding it would drift a | |
| 27 | + // bucket every few seconds over a long source. | |
| 28 | + let per_bucket = f64::from(meta.sample_rate) / f64::from(buckets_per_sec.max(1)); | |
| 29 | + let channels = (meta.channels as usize).max(1); | |
| 30 | + let mut peaks: Vec<(f32, f32)> = Vec::new(); | |
| 31 | + | |
| 32 | + for chunk in rx { | |
| 33 | + for (frame, values) in chunk.samples.chunks(channels).enumerate() { | |
| 34 | + let bucket = ((chunk.start_sample + frame as u64) as f64 / per_bucket) as usize; | |
| 35 | + if bucket >= peaks.len() { | |
| 36 | + peaks.resize(bucket + 1, (0.0, 0.0)); | |
| 37 | + } | |
| 38 | + let slot = &mut peaks[bucket]; | |
| 39 | + for &v in values { | |
| 40 | + let v = v.clamp(-1.0, 1.0); | |
| 41 | + slot.0 = slot.0.min(v); | |
| 42 | + slot.1 = slot.1.max(v); | |
| 43 | + } | |
| 44 | + } | |
| 45 | + } | |
| 46 | + Ok(Some(peaks)) | |
| 47 | +} | |
| 48 | + | |
| 49 | +#[cfg(test)] | |
| 50 | +mod tests { | |
| 51 | + use std::path::PathBuf; | |
| 52 | + | |
| 53 | + use super::peaks; | |
| 54 | + | |
| 55 | + const BPS: u32 = 10; | |
| 56 | + | |
| 57 | + fn asset(name: &str) -> PathBuf { | |
| 58 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 59 | + .join("../../assets") | |
| 60 | + .join(name) | |
| 61 | + } | |
| 62 | + | |
| 63 | + #[test] | |
| 64 | + fn peaks_follow_the_1hz_volume_pulse() { | |
| 65 | + let peaks = peaks(asset("test_av.mp4"), BPS) | |
| 66 | + .expect("open") | |
| 67 | + .expect("test_av.mp4 has an audio track"); | |
| 68 | + // 5 s of source; the container's tail padding may spill one bucket. | |
| 69 | + let want = 5 * BPS as usize; | |
| 70 | + assert!( | |
| 71 | + peaks.len().abs_diff(want) <= 1, | |
| 72 | + "{} buckets, want {want} +/-1", | |
| 73 | + peaks.len() | |
| 74 | + ); | |
| 75 | + | |
| 76 | + for (i, &(lo, hi)) in peaks.iter().enumerate() { | |
| 77 | + assert!( | |
| 78 | + (-1.0..=0.0).contains(&lo) && (0.0..=1.0).contains(&hi), | |
| 79 | + "bucket {i} is ({lo}, {hi})" | |
| 80 | + ); | |
| 81 | + } | |
| 82 | + | |
| 83 | + // The fixture's envelope is 0.5 + 0.5*sin(2*PI*t): a full silence every | |
| 84 | + // second at t = 0.75, a full-scale peak at t = 0.25. So every second's | |
| 85 | + // quietest bucket must be the 8th (t in [0.7, 0.8)) — a bucketing that | |
| 86 | + // was off by a scale factor or dropped chunk positions would not line | |
| 87 | + // the dips up second after second. | |
| 88 | + for second in 0..5 { | |
| 89 | + let band = &peaks[second * BPS as usize..][..BPS as usize]; | |
| 90 | + let level = |&(lo, hi): &(f32, f32)| hi - lo; | |
| 91 | + let quietest = (0..band.len()) | |
| 92 | + .min_by(|&a, &b| level(&band[a]).total_cmp(&level(&band[b]))) | |
| 93 | + .expect("non-empty band"); | |
| 94 | + assert!( | |
| 95 | + (6..=8).contains(&quietest), | |
| 96 | + "second {second}: dip at bucket {quietest}, want 7 +/-1" | |
| 97 | + ); | |
| 98 | + // Depth as a ratio, not an absolute: the fixture's sines sit around | |
| 99 | + // an eighth of full scale, and that is the encoder's business. | |
| 100 | + let (dip, loudest) = ( | |
| 101 | + level(&band[quietest]), | |
| 102 | + band.iter().map(level).fold(0.0, f32::max), | |
| 103 | + ); | |
| 104 | + assert!(loudest > 0.05, "second {second}: peak level only {loudest}"); | |
| 105 | + assert!( | |
| 106 | + dip < 0.1 * loudest, | |
| 107 | + "second {second}: dip {dip} against peak {loudest}, want near silence" | |
| 108 | + ); | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 112 | + #[test] | |
| 113 | + fn video_only_source_has_no_peaks() { | |
| 114 | + assert!( | |
| 115 | + peaks(asset("test_baseline.mp4"), BPS) | |
| 116 | + .expect("open") | |
| 117 | + .is_none() | |
| 118 | + ); | |
| 119 | + } | |
| 120 | +} |