forge
Explore
Log in

Don't you dare go Hollow

⚡ Loudness of the mix or a lane is measured once per timeline revision: a repeat loudness or normalize on an unchanged timeline, or after an undo back to it, renders nothing

Browse files at this commit

04f9ade52bec0f03d44f9143824e4ef2a4d17228Ahmet Kaan GümüşParent: f9ab8bd
crates/engine/src/normalize.rsmodified+42 -0
@@ -255,12 +255,43 @@ impl Ducked {
255255 /// The sound a lane makes over timeline frames `from..to`, as the engine plays
256256 /// it: clip speeds, equalizers and gaps included, and its duck if it has one.
257257 /// Lane gain is not applied; the caller adds it. `None` when there is no sound.
258+type MeasureKey = (u64, Option<Lane>, u32, u32, u64);
259+
260+static MEASURED: std::sync::Mutex<Vec<(MeasureKey, Option<Loudness>)>> = std::sync::Mutex::new(Vec::new());
261+
262+fn remembered(
263+ key: MeasureKey,
264+ measure: impl FnOnce() -> Result<Option<Loudness>, String>,
265+) -> Result<Option<Loudness>, String> {
266+ if let Some(hit) = MEASURED.lock().ok().and_then(|m| m.iter().find(|(k, _)| *k == key).map(|(_, l)| *l)) {
267+ return Ok(hit);
268+ }
269+ let got = measure()?;
270+ if let Ok(mut m) = MEASURED.lock() {
271+ if m.len() >= 32 {
272+ m.remove(0);
273+ }
274+ m.push((key, got));
275+ }
276+ Ok(got)
277+}
278+
258279 pub fn measure_lane(
259280 s: &PlaybackSession,
260281 lane: Lane,
261282 from: u32,
262283 to: u32,
263284 fps: f64,
285+) -> Result<Option<Loudness>, String> {
286+ remembered((s.revision(), Some(lane), from, to, fps.to_bits()), || measure_lane_now(s, lane, from, to, fps))
287+}
288+
289+fn measure_lane_now(
290+ s: &PlaybackSession,
291+ lane: Lane,
292+ from: u32,
293+ to: u32,
294+ fps: f64,
264295 ) -> Result<Option<Loudness>, String> {
265296 if let Some(mut ducked) = Ducked::open(s, lane, to, fps, false)? {
266297 let from_at = (f64::from(from) / fps * f64::from(ducked.rate)).round() as u64;
@@ -348,6 +379,17 @@ pub fn measure_mix(
348379 to: u32,
349380 fps: f64,
350381 end: u32,
382+) -> Result<Loudness, String> {
383+ remembered((s.revision(), None, from, to.min(end), fps.to_bits()), || measure_mix_now(s, from, to, fps, end).map(Some))?
384+ .ok_or_else(|| "the mix has no sound track".to_string())
385+}
386+
387+fn measure_mix_now(
388+ s: &PlaybackSession,
389+ from: u32,
390+ to: u32,
391+ fps: f64,
392+ end: u32,
351393 ) -> Result<Loudness, String> {
352394 let wav = render_mix(s)?;
353395 let range = (from > 0 || to < end).then(|| {