⚡ 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 commit04f9ade52bec0f03d44f9143824e4ef2a4d17228Parent: f9ab8bd1 changed file +42 -0
crates/engine/src/normalize.rsmodified+42 -0
| @@ -255,12 +255,43 @@ impl Ducked { | |||
| 255 | /// The sound a lane makes over timeline frames `from..to`, as the engine plays | 255 | /// The sound a lane makes over timeline frames `from..to`, as the engine plays |
| 256 | /// it: clip speeds, equalizers and gaps included, and its duck if it has one. | 256 | /// it: clip speeds, equalizers and gaps included, and its duck if it has one. |
| 257 | /// Lane gain is not applied; the caller adds it. `None` when there is no sound. | 257 | /// 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 | + | ||
| 258 | pub fn measure_lane( | 279 | pub fn measure_lane( |
| 259 | s: &PlaybackSession, | 280 | s: &PlaybackSession, |
| 260 | lane: Lane, | 281 | lane: Lane, |
| 261 | from: u32, | 282 | from: u32, |
| 262 | to: u32, | 283 | to: u32, |
| 263 | fps: f64, | 284 | 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, | ||
| 264 | ) -> Result<Option<Loudness>, String> { | 295 | ) -> Result<Option<Loudness>, String> { |
| 265 | if let Some(mut ducked) = Ducked::open(s, lane, to, fps, false)? { | 296 | if let Some(mut ducked) = Ducked::open(s, lane, to, fps, false)? { |
| 266 | let from_at = (f64::from(from) / fps * f64::from(ducked.rate)).round() as u64; | 297 | let from_at = (f64::from(from) / fps * f64::from(ducked.rate)).round() as u64; |
| @@ -348,6 +379,17 @@ pub fn measure_mix( | |||
| 348 | to: u32, | 379 | to: u32, |
| 349 | fps: f64, | 380 | fps: f64, |
| 350 | end: u32, | 381 | 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, | ||
| 351 | ) -> Result<Loudness, String> { | 393 | ) -> Result<Loudness, String> { |
| 352 | let wav = render_mix(s)?; | 394 | let wav = render_mix(s)?; |
| 353 | let range = (from > 0 || to < end).then(|| { | 395 | let range = (from > 0 || to < end).then(|| { |