From e87ca8852c38d502663bf020f95332fe5a3d1a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 24 Sep 2026 15:24:42 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(audio):=20the=20sound=20watc?= =?UTF-8?q?h=20is=20keyed=20to=20the=20session=20it=20describes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The watch cached one reason while `sound_reason` read `active_session()`, so a preview opening or closing -- a *different* session, and the timeline's own unchanged reason restored behind it -- read as a device that came back and then died again, and the phantom line stayed on the strip. `watch_audio` now asks the pure `audio_step` about the session generation its cache describes against the one the frame serves: a swap seeds and announces nothing, and only a change on the *same* session pushes or clears. `close_preview` bumps `session_gen` like every other install and drop (it was the one door that did not), so the key holds at every swap. Doc corrected with it: this side is the *reader* of `audio_disabled_reason`; the writer of a mid-session death is the engine's own file (nothing in this tree assigns it off the `Audio::died` route yet -- `arm_audio` can only clear it), which is the wave lane's, and no constant of it is spelled here. --- crates/app/src/main.rs | 6 +++ crates/app/src/notices.rs | 44 ++++++++++++++++++++ crates/app/src/player/library.rs | 15 +++---- crates/app/src/player/transport.rs | 63 +++++++++++++++------------- crates/app/src/tests/media.rs | 66 +++++++++++++++++++++++++++--- 5 files changed, 149 insertions(+), 45 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index e5441f5..d7063d3 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -796,6 +796,11 @@ struct Player { /// while the sound is up, and seeded by `Player::seed_audio_watch` at every /// open, whose own line already carries the same fact. audio_reason: Option, + /// Which session generation (`session_gen`) the cached reason above describes: + /// the watch is keyed to the session it is a statement about, so a preview + /// opening or closing seeds the cache instead of announcing the *other* + /// session's device. + audio_watch_gen: u64, /// Which of the clip's two inks the look section's wheel and colour field /// act on ([`VizSlot`]): the clip's own colour, or the ring's dual half. /// The pick stays picked across clips, the way the dock tab's does. @@ -1128,6 +1133,7 @@ fn main() { // Nothing heard yet: the first pump seeds it off the session // it serves. audio_reason: None, + audio_watch_gen: 0, viz_slot: VizSlot::Main, viz_hex_edit: None, pending_viz: None, diff --git a/crates/app/src/notices.rs b/crates/app/src/notices.rs index b04ce06..ca1a58c 100644 --- a/crates/app/src/notices.rs +++ b/crates/app/src/notices.rs @@ -64,6 +64,50 @@ pub(crate) fn audio_lost_line(reason: &str) -> String { format!("AUDIO LOST{}", audio_tail(reason)) } +/// What a frame of the sound watch decides, as a pure value: the cache's own +/// session generation against the frame's says whether the two are even talking +/// about the same session, and only then what the reason did. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(crate) enum AudioStep { + /// The cache describes the session *before* this one + /// (`Player::session_swapped`): it is seeded and nothing is announced -- a + /// preview that opens or closes is not a device that came back. + Seed, + /// The same session's reason arrived, or changed to a new one. + Push, + /// ...and the same session's reason is gone: the line it earned is retracted. + Clear, + /// The ordinary frame. + Nothing, +} + +/// [`Player::watch_audio`]'s whole decision, pure: `cached_gen` is the session +/// generation the cache describes, `session_gen` the one the frame is serving, and +/// two reasons are what the engine says the sound is (`None` for sound that is +/// up). +/// +/// The generation is what keeps another session's reason out of this session's +/// cache: `pump` serves the preview while one is up and the timeline otherwise, +/// so a flip reads a reason that was never about the cached session at all -- a +/// live device on one side and a dead one on the other is not a change, it is +/// two different questions. +pub(crate) fn audio_step( + cached_gen: u64, + session_gen: u64, + was: Option<&str>, + now: Option<&str>, +) -> AudioStep { + if cached_gen != session_gen { + return AudioStep::Seed; + } + match (was, now) { + (Some(was), Some(now)) if was == now => AudioStep::Nothing, + (_, Some(_)) => AudioStep::Push, + (Some(_), None) => AudioStep::Clear, + (None, None) => AudioStep::Nothing, + } +} + /// What a *change* in the engine's sound reason does to the notice queue, as a /// pure step: the death line pushed when a reason arrives, the same line taken /// back when the reason clears ([`PlaybackSession::audio_disabled_reason`]'s diff --git a/crates/app/src/player/library.rs b/crates/app/src/player/library.rs index 787683a..b657705 100644 --- a/crates/app/src/player/library.rs +++ b/crates/app/src/player/library.rs @@ -716,6 +716,11 @@ impl Player { cx.notify(); } } + // The picture swaps back to the timeline's, so everything keyed to a + // session hears it (`Player::session_swapped`) -- the sound watch among + // them, or the timeline's own unchanged reason would read as a device + // that came back the moment the preview closed. + self.session_swapped(); self.preview_playing = false; } @@ -1481,11 +1486,6 @@ impl Player { // Read before the session moves: a file that plays silent says // so here or nowhere. let silent = audio_notice(&session); - // The watch's own cache, seeded here rather than on the first - // pump after: this line already carried the fact, so a reason - // unchanged across the open is not announced a second time - // ([`Player::seed_audio_watch`]). - self.seed_audio_watch(&session); // A file replaces the one that was open, and track 3 of that one // is not track 3 of this. self.sub_track = 0; @@ -1800,11 +1800,6 @@ impl Player { // As at the file door above: the window watches in real time. session.drop_late_pictures(true); let silent = audio_notice(&session); - // The watch's own cache, seeded here rather than on the first - // pump after: this line already carried the fact, so a reason - // unchanged across the open is not announced a second time - // ([`Player::seed_audio_watch`]). - self.seed_audio_watch(&session); // A project is named after itself but still exports beside its // media: that is the only place an export has ever landed. self.export_path = retarget(&export_path(&session.sources()[0].path), self.format); diff --git a/crates/app/src/player/transport.rs b/crates/app/src/player/transport.rs index 4828731..7897952 100644 --- a/crates/app/src/player/transport.rs +++ b/crates/app/src/player/transport.rs @@ -34,23 +34,46 @@ impl Player { /// The engine's word on the sound device, watched rather than asked once. /// A file that *opens* silent says so in its own open line - /// ([`audio_notice`], `library.rs`), but a device that dies mid-session used - /// to leave the window playing on in silence with nothing said at all: the - /// engine knew (it sets its reason to its lost-device word and clears it - /// again when its own re-arm takes) and no consumer ever looked. + /// ([`audio_notice`], `library.rs`); what used to reach no consumer at all is + /// a reason that changes *under* a session that is already up. This is the + /// reader of that state -- the writer of it is the engine's own + /// `PlaybackSession::audio_disabled_reason`, which this lane never spells. /// /// Called from [`Player::pump`] because that is the one place every frame /// passes through, and written to cost an ordinary frame nothing: one read of - /// an `Option<&str>` and one compare, with the strings built only on the - /// change itself. + /// an `Option<&str>` and one compare, with the strings built only on the step + /// the pure [`audio_step`] names. + /// + /// Keyed by session ([`Player::audio_watch_gen`] against `session_gen`): + /// `pump` serves the preview while one is up and the timeline otherwise, so + /// without the key a preview opening or closing would read the *other* + /// session's reason and announce a device loss that never happened. pub(crate) fn watch_audio(&mut self) { - if self.sound_reason() == self.audio_reason.as_deref() { + let step = audio_step( + self.audio_watch_gen, + self.session_gen, + self.audio_reason.as_deref(), + self.sound_reason(), + ); + if step == AudioStep::Nothing { + return; + } + let was = self.audio_reason.take(); + self.audio_reason = self.sound_reason().map(str::to_owned); + self.audio_watch_gen = self.session_gen; + if step == AudioStep::Seed { + // A session swap is not a change: the new session's own open line + // carries its reason, so only a line left over from the session + // before is taken back -- and only when this one's sound is up. + if self.audio_reason.is_none() { + if let Some(was) = was { + let line = audio_lost_line(&was); + self.notices + .retain(|notice| notice.as_ref() != line.as_str()); + } + } return; } - // Re-read rather than carry a borrow across the write: this line is only - // reached on the change, so the allocation it costs is the news itself. - let reason = self.sound_reason().map(str::to_owned); - let was = std::mem::replace(&mut self.audio_reason, reason); audio_change_notice( &mut self.notices, was.as_deref(), @@ -58,24 +81,6 @@ impl Player { ); } - /// [`Player::watch_audio`]'s cache, seeded at the moment an open's own line - /// carried the same fact: a reason unchanged across the open is not news, so - /// only the *clearing* half runs here -- a line left over from the file - /// before it comes off the strip. - pub(crate) fn seed_audio_watch(&mut self, session: &PlaybackSession) { - let was = std::mem::replace( - &mut self.audio_reason, - session.audio_disabled_reason().map(str::to_owned), - ); - if self.audio_reason.is_none() { - if let Some(was) = was { - let line = audio_lost_line(&was); - self.notices - .retain(|notice| notice.as_ref() != line.as_str()); - } - } - } - /// Catches the display up to the clock: everything already due is taken off /// the channel and only the last of them is shown, which *is* the /// drop-when-behind policy. A frame that is not due yet waits in `held`, and diff --git a/crates/app/src/tests/media.rs b/crates/app/src/tests/media.rs index b5b5da8..01b34fc 100644 --- a/crates/app/src/tests/media.rs +++ b/crates/app/src/tests/media.rs @@ -65,14 +65,68 @@ fn a_device_that_dies_under_an_open_file_says_so_and_unsays_it() { "the pump stopped watching the engine's sound reason, so a device that \ dies mid-session is silent again" ); + // ...and the watch keys its cache to the session it is a statement about: + // the two generations are what the *step call* is asked, so a preview flip + // cannot be answered as if it were the cached session's own change. + let watch = fn_body("watch_audio"); + let args = watch + .split_once("audio_step(") + .expect( + "the watch decides for itself instead of through the pure step the flip test drives", + ) + .1; + let args = args.split_once(')').map_or(args, |(args, _)| args); + assert!( + args.contains("self.audio_watch_gen") && args.contains("self.session_gen"), + "the watch asks the step about something other than the session it serves, \ + so a preview opening or closing announces the other session's device" + ); assert!( - src_text("player/library.rs") - .matches("self.seed_audio_watch(&session);") - .count() - == 2, - "an open path stopped seeding the watch, so its own NO AUDIO line comes \ - back a frame later as a second one" + fn_body("sound_reason").contains("self.active_session()"), + "the watch reads a reason off a session the pump is not serving, so its \ + cache and the frame disagree the moment a preview is up" + ); + // ...and every door that installs or drops a session says so, the preview's + // two among them (the one the flip was missed on). + for door in ["close_session", "open_preview", "close_preview"] { + assert!( + fn_body(door).contains("self.session_swapped();"), + "{door} installs or drops a session without telling anything keyed to \ + one, so a flip reads as news" + ); + } +} + +/// The flip logic itself, pure: a preview opening or closing is a *different* +/// session, so the reason it reports is not news about the cached one -- the +/// phantom device loss the verifier caught, and the reason the cache carries a +/// generation at all. A change on the session the cache describes is the one that +/// speaks. +#[test] +fn a_preview_flip_is_not_a_device_that_came_back() { + use crate::notices::{audio_step, AudioStep}; + // A swap, every way round: seeded, never announced. + assert_eq!(audio_step(7, 8, None, Some("AUDIO_LOST")), AudioStep::Seed); + assert_eq!(audio_step(7, 8, Some("AUDIO_LOST"), None), AudioStep::Seed); + assert_eq!( + audio_step(7, 8, Some("AUDIO_LOST"), Some("AUDIO_LOST")), + AudioStep::Seed, + "a flip whose reason reads the same on both sides is still a flip" + ); + // The same session, the reason arriving, changing, and going. + assert_eq!(audio_step(7, 7, None, Some("AUDIO_LOST")), AudioStep::Push); + assert_eq!( + audio_step(7, 7, Some("AUDIO_LOST"), Some("device gone")), + AudioStep::Push + ); + assert_eq!(audio_step(7, 7, Some("AUDIO_LOST"), None), AudioStep::Clear); + assert_eq!( + audio_step(7, 7, Some("AUDIO_LOST"), Some("AUDIO_LOST")), + AudioStep::Nothing, + "the same reason read again on the next frame is not news" ); + assert_eq!(audio_step(7, 7, None, None), AudioStep::Nothing); + assert_eq!(audio_step(0, 0, None, None), AudioStep::Nothing); } /// The toast tail an audio import earns: rate, channel count and rounded