diff --git a/crates/ec-aac/tests/sbr_real_library/chunk_a.rs b/crates/ec-aac/tests/sbr_real_library/chunk_a.rs index 48bad545..8f8978af 100644 --- a/crates/ec-aac/tests/sbr_real_library/chunk_a.rs +++ b/crates/ec-aac/tests/sbr_real_library/chunk_a.rs @@ -164,9 +164,14 @@ pub(super) fn full_chain_low_band_matches_own_core() { // sample-for-sample with the full-chain output's rate -- // crude, but a low-pass well below the core Nyquist erases the // stairstep artifacts this introduces. - let up: Vec = core[ch].iter().flat_map(|&s| [s, s]).collect(); + let up = upsample_2x(&core[ch]); let cutoff = f64::from(core_rate) * 0.4; - let ol = lowpass(&full[ch], full_rate, cutoff); + let keep = if full.len() > core.len() { + mono_prefix(&full) + } else { + full[ch].len() + }; + let ol = lowpass(&full[ch][..keep], full_rate, cutoff); let ul = lowpass(&up, full_rate, cutoff); // Escalating search: narrow (`WIDE_LAG_MAX`=20_000, the file's // original bound) first, widen to `SEARCH_LAG_MAX` only if that diff --git a/crates/ec-aac/tests/sbr_real_library/helpers.rs b/crates/ec-aac/tests/sbr_real_library/helpers.rs index e8b0f15d..4c50a910 100644 --- a/crates/ec-aac/tests/sbr_real_library/helpers.rs +++ b/crates/ec-aac/tests/sbr_real_library/helpers.rs @@ -231,11 +231,7 @@ pub(super) fn correlation(a: &[f32], b: &[f32]) -> f64 { /// case the coarse slice picked a noisy lag) gives the reported number. pub(super) fn best_lag_correlation_ex(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64, bool) { const COARSE: usize = 4_096; - let start = ours - .len() - .min(theirs.len()) - .saturating_sub(WINDOW) - .min(ours.len() / 4); + let start = window_start(ours, theirs); let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { let (oa, ob) = if lag >= 0 { (start, start + lag as usize) @@ -365,11 +361,7 @@ pub(super) fn best_lag_correlation_wide(ours: &[f32], theirs: &[f32], lag_max: i /// refinement it replaces, so the resolution is simply not worth trading. pub(super) fn exhaustive_lag_correlation(ours: &[f32], theirs: &[f32], lag_max: i64) -> (i64, f64, bool) { const COARSE: usize = 4_096; - let start = ours - .len() - .min(theirs.len()) - .saturating_sub(WINDOW) - .min(ours.len() / 4); + let start = window_start(ours, theirs); let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { let (oa, ob) = if lag >= 0 { (start, start + lag as usize) @@ -421,11 +413,7 @@ pub(super) fn robust_lag_topk( k: usize, ) -> (i64, f64, bool) { const COARSE: usize = 4_096; - let start = ours - .len() - .min(theirs.len()) - .saturating_sub(WINDOW) - .min(ours.len() / 4); + let start = window_start(ours, theirs); let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> { let (oa, ob) = if lag >= 0 { (start, start + lag as usize) @@ -477,6 +465,40 @@ pub(super) fn robust_lag_topk( best } +pub(super) fn window_start(ours: &[f32], theirs: &[f32]) -> usize { + const CHUNK: usize = 1_024; + const CHUNKS: usize = 4; + let end = ours.len().min(theirs.len()); + let base = end.saturating_sub(WINDOW).min(ours.len() / 4); + let power = |s: &[f32]| { + s.iter().map(|v| f64::from(*v) * f64::from(*v)).sum::() / s.len().max(1) as f64 + }; + let floor = 1e-4 * power(&ours[base..end]).min(power(&theirs[base..end])); + let loud = |p: usize| { + p + CHUNK * CHUNKS <= end + && (p..p + CHUNK * CHUNKS) + .step_by(CHUNK) + .all(|a| power(&ours[a..a + CHUNK]) > floor && power(&theirs[a..a + CHUNK]) > floor) + }; + (base..end).step_by(CHUNK).find(|&p| loud(p)).unwrap_or(base) +} + +pub(super) fn upsample_2x(core: &[f32]) -> Vec { + let mut out = Vec::with_capacity(core.len() * 2); + for (i, &s) in core.iter().enumerate() { + out.push(s); + out.push((s + core.get(i + 1).copied().unwrap_or(s)) * 0.5); + } + out +} + +pub(super) fn mono_prefix(planes: &[Vec]) -> usize { + let first = &planes[0]; + planes.get(1).map_or(first.len(), |second| { + first.iter().zip(second).position(|(a, b)| a != b).unwrap_or(first.len()) + }) +} + /// Signed correlation of `ours` against `theirs` at one fixed `lag`, full /// `WINDOW`-sized, anchored at the SAME `start` offset `best_lag_correlation` /// uses internally -- used to test a hypothesized sign inversion by reusing @@ -486,11 +508,7 @@ pub(super) fn robust_lag_topk( /// match `best_lag_correlation`'s `start`, not sample 0 -- the two windows /// otherwise cover different seconds of audio and are not comparable. pub(super) fn correlation_at_lag(ours: &[f32], theirs: &[f32], lag: i64) -> f64 { - let start = ours - .len() - .min(theirs.len()) - .saturating_sub(WINDOW) - .min(ours.len() / 4); + let start = window_start(ours, theirs); let (oa, ob) = if lag >= 0 { (start, start + lag as usize) } else {