From 04f9ade52bec0f03d44f9143824e4ef2a4d17228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 10 Nov 2026 07:57:57 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Loudness=20of=20the=20mix=20or=20a?= =?UTF-8?q?=20lane=20is=20measured=20once=20per=20timeline=20revision:=20a?= =?UTF-8?q?=20repeat=20loudness=20or=20normalize=20on=20an=20unchanged=20t?= =?UTF-8?q?imeline,=20or=20after=20an=20undo=20back=20to=20it,=20renders?= =?UTF-8?q?=20nothing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/engine/src/normalize.rs | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) 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(|| {