diff --git a/crates/engine/src/normalize.rs b/crates/engine/src/normalize.rs index 1635cc7..6ecdc81 100644 --- a/crates/engine/src/normalize.rs +++ b/crates/engine/src/normalize.rs @@ -255,12 +255,43 @@ impl Ducked { /// The sound a lane makes over timeline frames `from..to`, as the engine plays /// it: clip speeds, equalizers and gaps included, and its duck if it has one. /// Lane gain is not applied; the caller adds it. `None` when there is no sound. +type MeasureKey = (u64, Option, u32, u32, u64); + +static MEASURED: std::sync::Mutex)>> = std::sync::Mutex::new(Vec::new()); + +fn remembered( + key: MeasureKey, + measure: impl FnOnce() -> Result, String>, +) -> Result, String> { + if let Some(hit) = MEASURED.lock().ok().and_then(|m| m.iter().find(|(k, _)| *k == key).map(|(_, l)| *l)) { + return Ok(hit); + } + let got = measure()?; + if let Ok(mut m) = MEASURED.lock() { + if m.len() >= 32 { + m.remove(0); + } + m.push((key, got)); + } + Ok(got) +} + pub fn measure_lane( s: &PlaybackSession, lane: Lane, from: u32, to: u32, fps: f64, +) -> Result, String> { + remembered((s.revision(), Some(lane), from, to, fps.to_bits()), || measure_lane_now(s, lane, from, to, fps)) +} + +fn measure_lane_now( + s: &PlaybackSession, + lane: Lane, + from: u32, + to: u32, + fps: f64, ) -> Result, String> { if let Some(mut ducked) = Ducked::open(s, lane, to, fps, false)? { let from_at = (f64::from(from) / fps * f64::from(ducked.rate)).round() as u64; @@ -348,6 +379,17 @@ pub fn measure_mix( to: u32, fps: f64, end: u32, +) -> Result { + remembered((s.revision(), None, from, to.min(end), fps.to_bits()), || measure_mix_now(s, from, to, fps, end).map(Some))? + .ok_or_else(|| "the mix has no sound track".to_string()) +} + +fn measure_mix_now( + s: &PlaybackSession, + from: u32, + to: u32, + fps: f64, + end: u32, ) -> Result { let wav = render_mix(s)?; let range = (from > 0 || to < end).then(|| {