Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(verify): identify damaged Instant audio markers by their timing pattern

Richie McIlroy8a1b5c3e3710
diff --git a/crates/verify/src/audio_code.rs b/crates/verify/src/audio_code.rs
index 3a1aacc28..7e498c076 100644
--- a/crates/verify/src/audio_code.rs
+++ b/crates/verify/src/audio_code.rs
@@ -297,6 +297,44 @@ pub fn locate_markers(
out
}
+/// Identifies markers whose chirp was found but whose payload did not
+/// decode, by their timing pattern: the schedule's per-index jitter makes
+/// the gaps between markers unique, so one constant shift maps the chirps
+/// onto the expected times. Returns `(index, chirp time)` pairs, or `None`
+/// unless a single shift places at least three chirps within `tolerance`
+/// of an expected marker and no other shift places as many.
+pub fn identify_by_schedule(
+ expected: &[(u32, f64)],
+ chirps: &[f64],
+ tolerance: f64,
+) -> Option<Vec<(u32, f64)>> {
+ let pairs = |shift: f64| -> Vec<(u32, f64)> {
+ chirps
+ .iter()
+ .filter_map(|&c| {
+ expected
+ .iter()
+ .find(|&&(_, t)| (c - shift - t).abs() <= tolerance)
+ .map(|&(index, _)| (index, c))
+ })
+ .collect()
+ };
+ let mut scored: Vec<(usize, f64)> = Vec::new();
+ for &c in chirps {
+ for &(_, t) in expected {
+ let shift = c - t;
+ if scored.iter().any(|&(_, s)| (s - shift).abs() <= tolerance) {
+ continue;
+ }
+ scored.push((pairs(shift).len(), shift));
+ }
+ }
+ scored.sort_by(|a, b| b.0.cmp(&a.0));
+ let &(best, shift) = scored.first()?;
+ let runner_up = scored.get(1).map_or(0, |s| s.0);
+ (best >= 3 && runner_up < best).then(|| pairs(shift))
+}
+
fn refine_peak(corr: &[f64], at: usize) -> f64 {
if at == 0 || at + 1 >= corr.len() {
return at as f64;
@@ -413,6 +451,32 @@ mod tests {
}
}
+ #[test]
+ fn schedule_pattern_identifies_undecodable_markers_without_aliasing() {
+ let schedule = standard_schedule(AudioSource::Mic, 0.1, 5.9, 0.5);
+ let expected: Vec<(u32, f64)> = schedule
+ .iter()
+ .skip(1)
+ .map(|p| (p.index, p.time_secs))
+ .collect();
+ for shift in [-0.519, 0.0, 0.5, 1.0, 0.0145] {
+ let chirps: Vec<f64> = schedule.iter().map(|p| p.time_secs + shift).collect();
+ let found = identify_by_schedule(&expected, &chirps, 0.002).expect("identified");
+ assert!(
+ found.len() >= expected.len() - 2,
+ "shift {shift}: {found:?}"
+ );
+ for (index, time) in found {
+ let truth = schedule.iter().find(|p| p.index == index).unwrap();
+ assert!(
+ (time - shift - truth.time_secs).abs() < 1e-9,
+ "shift {shift}"
+ );
+ }
+ }
+ assert!(identify_by_schedule(&expected, &[1.0, 2.0], 0.002).is_none());
+ }
+
#[test]
fn crc_rejects_flipped_payloads() {
for index in [0, 1, 77, MAX_AUDIO_INDEX] {
diff --git a/crates/verify/tests/export.rs b/crates/verify/tests/export.rs
index 23850c57d..49097d561 100644
--- a/crates/verify/tests/export.rs
+++ b/crates/verify/tests/export.rs
@@ -577,6 +577,28 @@ fn instant_case(spec: cap_verify::pipeline::InstantSpec) -> Option<String> {
.collect::<Vec<_>>()
));
}
+ // Chirps whose payload was damaged can still be identified by the
+ // schedule's timing pattern, so a large offset stays measurable.
+ let chirps: Vec<f64> = found
+ .iter()
+ .filter(|f| f.index.is_none())
+ .map(|f| f.time_secs + audio.start_secs)
+ .collect();
+ if let Some(pattern) = audio_code::identify_by_schedule(&expected, &chirps, 0.002) {
+ let mut added = 0;
+ for (index, time) in pattern {
+ if absent.contains(&index)
+ && let Some(&(_, t)) = expected.iter().find(|e| e.0 == index)
+ {
+ offsets.push(time - t);
+ added += 1;
+ }
+ }
+ case.note(format!(
+ "{}: {added} markers without a decodable identity identified by their timing pattern",
+ source.name()
+ ));
+ }
if offsets.is_empty() {
case.unobserved(
"sync",