From 8cebf23e05137e6dd12033661bc5882a96458333 Mon Sep 17 00:00:00 2001 From: Lawrence Date: Wed, 2 Dec 2026 15:29:45 -0800 Subject: [PATCH] feat: audio clocks carry on across a microphone drop, on one run-wide synthetic clock (P-09, D701) --- crates/strobe-app/src/app.rs | 3 + crates/strobe-app/src/audio.rs | 182 ++++++++++++++++++++++++++++++--- docs/IMPLEMENTATION_BACKLOG.md | 2 +- 3 files changed, 174 insertions(+), 13 deletions(-) diff --git a/crates/strobe-app/src/app.rs b/crates/strobe-app/src/app.rs index 40f4467b..7d4affa4 100644 --- a/crates/strobe-app/src/app.rs +++ b/crates/strobe-app/src/app.rs @@ -5441,6 +5441,9 @@ impl Running { } else { dt }; + // One run clock for the synthetic track, whatever the scenes' + // own clocks do (D701). + self.audio.advance(heard_dt); if self.song.step(heard_dt, &mut self.audio) { if self.stack.show.is_some() { self.run_show(Show::song_changed); diff --git a/crates/strobe-app/src/audio.rs b/crates/strobe-app/src/audio.rs index b3a12aaf..ff3a0715 100644 --- a/crates/strobe-app/src/audio.rs +++ b/crates/strobe-app/src/audio.rs @@ -79,6 +79,19 @@ type Started = Result; /// The result of listing devices on its own thread. type Listed = Result, String>; +/// The audio clocks that carry on across a change of source (D701, P-09), +/// and whether each counts beats, whose offset is kept whole. +const CLOCKS: [(&str, bool); 8] = [ + ("syn_Time", false), + ("syn_BassTime", false), + ("syn_MidTime", false), + ("syn_MidHighTime", false), + ("syn_HighTime", false), + ("syn_CurvedTime", false), + ("syn_BeatTime", true), + ("syn_BPMTwitcher", true), +]; + /// The app's audio input and its settings. pub struct AudioInput { /// Whether capture was asked for at all (`--audio synthetic` says no). @@ -100,6 +113,19 @@ pub struct AudioInput { pushed_gain: [f32; 2], /// The frame handed to the renderer; its vectors are sized once. frame: AudioFrame, + /// Seconds of the run, advanced once a frame ([`Self::advance`]): the + /// synthetic track is evaluated here, not at each caller's scene time, + /// so every scene on synthetic audio hears the same moment (D701). + run_time: f64, + /// Where each clock in [`CLOCKS`] sits in the scalars. + clock_slots: [Option; CLOCKS.len()], + /// Added to each clock so it carries on across a change of source + /// (D701, P-09): taken at the switch, kept for the rest of the run. + clock_offsets: [f32; CLOCKS.len()], + /// Each clock as last shown, offset included. + clock_shown: [f32; CLOCKS.len()], + /// Which source the last frame came from. + produced: Option, trail_seconds: f32, bpm_index: Option, source: Source, @@ -270,6 +296,11 @@ impl AudioInput { meta_gain: [1.0, 1.0], pushed_gain: [1.0, 1.0], frame: AudioFrame::silent(layout), + run_time: 0.0, + clock_slots: CLOCKS.map(|(name, _)| SCALARS.iter().position(|n| *n == name)), + clock_offsets: [0.0; CLOCKS.len()], + clock_shown: [0.0; CLOCKS.len()], + produced: None, trail_seconds: layout.trail_seconds, bpm_index: SCALARS.iter().position(|name| *name == "syn_BPM"), source: Source::Synthetic, @@ -418,12 +449,23 @@ impl AudioInput { } } - /// The audio for the frame at scene time `time`. - /// - /// Live frames are copied from the analyser's latest snapshot; - /// otherwise the synthetic track is evaluated at `time`. No allocation - /// and no waiting after the first call. - pub fn frame_at(&mut self, time: f32) -> &AudioFrame { + /// Advances the run clock the synthetic track is read at, once a frame, + /// by the time the app counts as heard (D300, D701). + pub fn advance(&mut self, dt: f32) { + if dt.is_finite() && dt > 0.0 { + self.run_time += f64::from(dt); + } + } + + /// The audio for this frame. `_time` is the caller's scene time, kept + /// for the callers' sake: live frames are the analyser's latest + /// snapshot, and the synthetic track is read at the run clock + /// ([`Self::advance`]), so a scene that restarts at a cue does not + /// restart its audio (D701). Across a change of source every clock in + /// [`CLOCKS`] carries on from where it was shown. No allocation and no + /// waiting after the first call. + pub fn frame_at(&mut self, _time: f32) -> &AudioFrame { + let time = self.run_time as f32; self.poll_threads(); let desired = match self.capture.as_ref().map(Capture::state) { Some(CaptureState::Running) => Some(Source::Live), @@ -437,7 +479,10 @@ impl AudioInput { } } match (self.source, self.capture.as_ref()) { - (Source::Live, Some(capture)) => capture.snapshot_into(&mut self.frame), + (Source::Live, Some(capture)) => { + capture.snapshot_into(&mut self.frame); + self.carry_clocks(Source::Live); + } (Source::Live | Source::Synthetic, _) => { strobe_scene::synthetic::fill(time, &mut self.frame.scalars); // The synthetic track has no song structure (D226). @@ -459,11 +504,41 @@ impl AudioInput { if let Some(ambient) = &self.ambient { ambient.calm(time, &mut self.frame); } + self.carry_clocks(Source::Synthetic); } } &self.frame } + /// Keeps the clocks continuous across a change of `source` (D701, P-09). + /// At a switch each clock's offset becomes what was last shown less the + /// new source's value, so it carries on from there; a beat clock's + /// offset is rounded up to a whole beat, so the count never runs back + /// and the beat stays on the beat. Allocates nothing. + fn carry_clocks(&mut self, source: Source) { + let switched = self.produced.is_some_and(|before| before != source); + self.produced = Some(source); + for (k, (_, beats)) in CLOCKS.iter().enumerate() { + let Some(Some(slot)) = self.clock_slots.get(k).copied() else { + continue; + }; + let Some(value) = self.frame.scalars.get_mut(slot) else { + continue; + }; + let (Some(offset), Some(shown)) = + (self.clock_offsets.get_mut(k), self.clock_shown.get_mut(k)) + else { + continue; + }; + if switched { + let gap = *shown - *value; + *offset = if *beats { gap.ceil() } else { gap }; + } + *value += *offset; + *shown = *value; + } + } + /// Logs a change of source, once, and refreshes the label. fn announce(&mut self, source: Source) { self.announced = Some(source); @@ -662,13 +737,17 @@ mod tests { use anyhow::Result; #[test] - fn forced_synthetic_audio_is_a_function_of_time() -> Result<()> { + fn forced_synthetic_audio_is_a_function_of_the_run_clock() -> Result<()> { let mut input = AudioInput::new(false); let at_beat = input.frame_at(0.0).scalars.clone(); - let later = input.frame_at(0.25).scalars.clone(); - let again = input.frame_at(0.0).scalars.clone(); - assert_eq!(at_beat, again, "same time, same frame"); - assert_ne!(at_beat, later, "the track moves"); + let other_scene = input.frame_at(7.0).scalars.clone(); + assert_eq!( + at_beat, other_scene, + "every scene hears the same moment, whatever its own clock (D701)" + ); + input.advance(0.25); + let later = input.frame_at(0.0).scalars.clone(); + assert_ne!(at_beat, later, "the track moves with the run clock"); assert_eq!( input.source(), Source::Synthetic, @@ -699,6 +778,80 @@ mod tests { Ok(()) } + /// The clocks of a frame, in [`CLOCKS`] order. + fn clocks(input: &AudioInput) -> Vec { + input + .clock_slots + .iter() + .map(|slot| { + slot.and_then(|i| input.frame.scalars.get(i).copied()) + .unwrap_or(f32::NAN) + }) + .collect() + } + + #[test] + fn every_clock_carries_on_across_live_to_synthetic_to_live_an_hour_in() -> Result<()> { + // D701, P-09: an hour of synthetic audio, a "live" frame whose clocks + // sit somewhere else entirely, then synthetic again. At each switch + // every clock must move by less than a frame's step, a beat clock by + // a whole beat at most, and never backwards. + const FRAME: f32 = 1.0 / 60.0; + let mut input = AudioInput::new(false); + input.advance(3600.0); + input.frame_at(0.0); + let before_live = clocks(&input); + + // A live snapshot: every clock far from the synthetic track's. + for (k, slot) in input.clock_slots.iter().enumerate() { + if let Some(value) = slot.and_then(|i| input.frame.scalars.get_mut(i)) { + *value = 12.345 + k as f32 * 100.0; + } + } + input.carry_clocks(Source::Live); + let live = clocks(&input); + for (k, (name, beats)) in CLOCKS.iter().enumerate() { + let (was, now) = (before_live[k], live[k]); + assert!( + now >= was, + "{name} ran back across the switch: {was} -> {now}" + ); + let limit = if *beats { 1.0 } else { FRAME }; + assert!( + now - was <= limit, + "{name} jumped {} at the switch to live", + now - was + ); + if *beats { + let offset = input.clock_offsets[k]; + assert_eq!( + offset, + offset.round(), + "{name}'s offset is whole beats: {offset}" + ); + } + } + + // Back to synthetic a frame later. + input.advance(FRAME); + input.frame_at(0.0); + let back = clocks(&input); + for (k, (name, beats)) in CLOCKS.iter().enumerate() { + let (was, now) = (live[k], back[k]); + assert!( + now >= was, + "{name} ran back on the way back: {was} -> {now}" + ); + let limit = if *beats { 1.0 } else { FRAME * 4.0 }; + assert!( + now - was <= limit, + "{name} jumped {} back to synthetic", + now - was + ); + } + Ok(()) + } + #[test] fn a_controller_run_does_not_warn_about_a_missing_input() -> Result<()> { assert!( @@ -732,6 +885,11 @@ mod tests { let mut beat_seen = false; for step in 0..240 { let t = step as f32 / 60.0; + // The synthetic track runs on the run clock (D701). + if step > 0 { + plain.advance(1.0 / 60.0); + calm.advance(1.0 / 60.0); + } let synthetic = plain.frame_at(t).scalars.clone(); beat_seen |= synthetic[hits] > 0.1 || synthetic[on_beat] > 0.5; let frame = calm.frame_at(t); diff --git a/docs/IMPLEMENTATION_BACKLOG.md b/docs/IMPLEMENTATION_BACKLOG.md index 66d81a2f..31083125 100644 --- a/docs/IMPLEMENTATION_BACKLOG.md +++ b/docs/IMPLEMENTATION_BACKLOG.md @@ -357,7 +357,7 @@ - **Acceptance:** decision recorded before adding aliases; tests cover every supported address and invalid payloads. ### P-09 Audio clocks that carry on across a microphone drop -- **Status:** Open. Decided by the owner (D701, R29): the synthetic fallback runs on one run-wide clock. +- **Status:** Built (2026-12-02), decided by the owner (D701, R29). `AudioInput` reads the synthetic track at its own run clock, advanced once a frame by the app's heard time, and `carry_clocks` keeps the six band clocks and the two beat clocks continuous across every switch of source, beat clocks by whole beats. `every_clock_carries_on_across_live_to_synthetic_to_live_an_hour_in` checks it. Not covered: the `syn_BPMSin`/`Tri` family follows the new source's beat phase, and the downbeat is the analyser's; a real device unplugged mid-show (R-01) is still to be seen. - **Work:** move the synthetic track onto one run-wide clock instead of each scene's, then build D393's offsets in `AudioInput`: each clock continues from the value last shown, the beat clocks move by a whole number of beats (rounded up), and the downbeat offset by that number mod 4. - **Acceptance:** a test switches live to synthetic to live an hour in and checks every clock moves less than a frame's step across each switch, the beat offset is whole, and the bars do not move.