diff --git a/crates/engine/src/normalize.rs b/crates/engine/src/normalize.rs index d4cae6c..1635cc7 100644 --- a/crates/engine/src/normalize.rs +++ b/crates/engine/src/normalize.rs @@ -534,9 +534,13 @@ fn settle( mix: bool, fps: f64, end: u32, -) -> Result<(Loudness, bool), String> { +) -> Result<(Loudness, bool, bool), String> { let plans: Vec = lanes.iter().map(|&l| plan_of(s, l)).collect(); let mut gain = loudness::gain_to_target(first, lufs, tp); + if gain.abs() < 0.01 { + let over = if first.true_peak_dbtp.is_finite() { first.true_peak_dbtp - tp } else { 0.0 }; + return Ok((*first, over >= -PEAK_TOLERANCE_DB && (lufs - first.integrated_lufs).abs() > TOLERANCE_LU, false)); + } set_plans(s, &plans, gain); let probe_of = |s: &PlaybackSession| -> Result { match (mix, lanes.first()) { @@ -571,7 +575,7 @@ fn settle( set_plans(s, &plans, gain); after = probe_of(s)?; } - Ok((after, cut)) + Ok((after, cut, true)) } /// Loudness-normalizes the timeline to `lufs` (integrated) with the true peak held under `tp` @@ -622,11 +626,12 @@ pub fn normalize(s: &mut PlaybackSession, scope: &Scope, lufs: f64, tp: f64) -> match scope { Scope::Mix => { let before = mix_before.as_ref().expect("a mix is measured"); - let (after, cut) = settle(s, &lanes, before, lufs, tp, true, fps, end)?; - let afters = lanes - .iter() - .map(|&l| measure_probe(s, &Probe::Lane(l), fps, end)) - .collect::, _>>()?; + let (after, cut, moved) = settle(s, &lanes, before, lufs, tp, true, fps, end)?; + let afters = if !moved { + befores.clone() + } else { + lanes.iter().map(|&l| measure_probe(s, &Probe::Lane(l), fps, end)).collect::, _>>()? + }; let reached = (lufs - after.integrated_lufs).abs() <= TOLERANCE_LU + 1e-6; Ok((afters, Some(after), reached, cut)) } @@ -638,7 +643,7 @@ pub fn normalize(s: &mut PlaybackSession, scope: &Scope, lufs: f64, tp: f64) -> afters.push(befores[i]); continue; } - let (after, cut) = settle(s, &[l], &befores[i], lufs, tp, false, fps, end)?; + let (after, cut, _) = settle(s, &[l], &befores[i], lufs, tp, false, fps, end)?; reached &= (lufs - after.integrated_lufs).abs() <= TOLERANCE_LU + 1e-6; held |= cut; afters.push(after); diff --git a/crates/mcp/src/tools/hear.rs b/crates/mcp/src/tools/hear.rs index cca64e3..c411113 100644 --- a/crates/mcp/src/tools/hear.rs +++ b/crates/mcp/src/tools/hear.rs @@ -2810,12 +2810,12 @@ fn settle( limited: bool, mut ceiling: f64, mut gain: f64, + unmoved: Option, ) -> Result { - let mut clamped = set_plans(s, plans, gain); - // The result is measured, not predicted: a limiter, a lane gain limit or the - // true peak moves the sound by less than the gain asks. Corrections follow - // while the level misses or the peaks pass the ceiling. - let mut after = measure_target(s, &json!({}), raw)?.loud; + let (mut clamped, mut after) = match unmoved.filter(|_| gain.abs() < 0.01) { + Some(l) => (Vec::new(), l), + None => (set_plans(s, plans, gain), measure_target(s, &json!({}), raw)?.loud), + }; let max_passes = pass_cap(limited); let tol = peak_tolerance(limited); let mut passes = 0; @@ -3018,6 +3018,7 @@ fn normalize_trial( hold: f64, limited: bool, delta: f64, + played: Loudness, ) -> Result { let mut gain = delta; // The limiter's detector is the meter's own true-peak method, so its ceiling is the target: the @@ -3033,7 +3034,7 @@ fn normalize_trial( gain = lufs - r.loud.integrated_lufs; } } - settle(s, raw, plans, lufs, hold, limited, hold, gain) + settle(s, raw, plans, lufs, hold, limited, hold, gain, (!limited).then_some(played)) } /// The past tense a reply uses for a change that was made, or the conditional for a dry run, which @@ -3125,7 +3126,7 @@ fn run_normalize_each(srv: &mut Server, args: &Value) -> Reply { let settled: Result)>, String> = each .iter() .map(|e| -> Result<(Settled, f64, Option), String> { - let st = settle(s, &e.name, std::slice::from_ref(&e.plan), lufs, tp, false, tp, e.delta)?; + let st = settle(s, &e.name, std::slice::from_ref(&e.plan), lufs, tp, false, tp, e.delta, Some(e.played))?; let applied = total_moved(s, &e.plan); Ok((st, applied, spill_of(s, &e.plan))) }) @@ -3380,7 +3381,7 @@ fn run_normalize(srv: &mut Server, args: &Value) -> Reply { // does not fall with gain the way the prediction assumes (the clip is what the peak is made of), // so a linear walk down chases it, while the limiter, ahead of the clip, holds it linearly. let limited = limiter_in_the_way(limiter_asked, tp_binds, played.true_peak_dbtp, hold); - let trial = normalize_trial(s, raw, &plans, lufs, hold, limited, delta); + let trial = normalize_trial(s, raw, &plans, lufs, hold, limited, delta, played); let changed = s.history_pushes() > start; let lim = s.limiter(); // The lane gain each lane really holds after the change: the clamp can hold a lane back.