forge
Explore
Log in

Don't you dare go Hollow

The sound watch is keyed to the session it describes

Browse files at this commit

e87ca8852c38d502663bf020f95332fe5a3d1a70Ahmet Kaan GümüşParent: c80d1f7
  • fix
    • the sound watch is keyed to the session it describes
Show full message
🐛 fix(audio): the sound watch is keyed to the session it describes

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.rsmodified+6 -0
@@ -796,6 +796,11 @@ struct Player {
796796 /// while the sound is up, and seeded by `Player::seed_audio_watch` at every
797797 /// open, whose own line already carries the same fact.
798798 audio_reason: Option<String>,
799+ /// Which session generation (`session_gen`) the cached reason above describes:
800+ /// the watch is keyed to the session it is a statement about, so a preview
801+ /// opening or closing seeds the cache instead of announcing the *other*
802+ /// session's device.
803+ audio_watch_gen: u64,
799804 /// Which of the clip's two inks the look section's wheel and colour field
800805 /// act on ([`VizSlot`]): the clip's own colour, or the ring's dual half.
801806 /// The pick stays picked across clips, the way the dock tab's does.
@@ -1128,6 +1133,7 @@ fn main() {
11281133 // Nothing heard yet: the first pump seeds it off the session
11291134 // it serves.
11301135 audio_reason: None,
1136+ audio_watch_gen: 0,
11311137 viz_slot: VizSlot::Main,
11321138 viz_hex_edit: None,
11331139 pending_viz: None,
crates/app/src/notices.rsmodified+44 -0
@@ -64,6 +64,50 @@ pub(crate) fn audio_lost_line(reason: &str) -> String {
6464 format!("AUDIO LOST{}", audio_tail(reason))
6565 }
6666
67+/// What a frame of the sound watch decides, as a pure value: the cache's own
68+/// session generation against the frame's says whether the two are even talking
69+/// about the same session, and only then what the reason did.
70+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
71+pub(crate) enum AudioStep {
72+ /// The cache describes the session *before* this one
73+ /// (`Player::session_swapped`): it is seeded and nothing is announced -- a
74+ /// preview that opens or closes is not a device that came back.
75+ Seed,
76+ /// The same session's reason arrived, or changed to a new one.
77+ Push,
78+ /// ...and the same session's reason is gone: the line it earned is retracted.
79+ Clear,
80+ /// The ordinary frame.
81+ Nothing,
82+}
83+
84+/// [`Player::watch_audio`]'s whole decision, pure: `cached_gen` is the session
85+/// generation the cache describes, `session_gen` the one the frame is serving, and
86+/// two reasons are what the engine says the sound is (`None` for sound that is
87+/// up).
88+///
89+/// The generation is what keeps another session's reason out of this session's
90+/// cache: `pump` serves the preview while one is up and the timeline otherwise,
91+/// so a flip reads a reason that was never about the cached session at all -- a
92+/// live device on one side and a dead one on the other is not a change, it is
93+/// two different questions.
94+pub(crate) fn audio_step(
95+ cached_gen: u64,
96+ session_gen: u64,
97+ was: Option<&str>,
98+ now: Option<&str>,
99+) -> AudioStep {
100+ if cached_gen != session_gen {
101+ return AudioStep::Seed;
102+ }
103+ match (was, now) {
104+ (Some(was), Some(now)) if was == now => AudioStep::Nothing,
105+ (_, Some(_)) => AudioStep::Push,
106+ (Some(_), None) => AudioStep::Clear,
107+ (None, None) => AudioStep::Nothing,
108+ }
109+}
110+
67111 /// What a *change* in the engine's sound reason does to the notice queue, as a
68112 /// pure step: the death line pushed when a reason arrives, the same line taken
69113 /// back when the reason clears ([`PlaybackSession::audio_disabled_reason`]'s
crates/app/src/player/library.rsmodified+5 -10
@@ -716,6 +716,11 @@ impl Player {
716716 cx.notify();
717717 }
718718 }
719+ // The picture swaps back to the timeline's, so everything keyed to a
720+ // session hears it (`Player::session_swapped`) -- the sound watch among
721+ // them, or the timeline's own unchanged reason would read as a device
722+ // that came back the moment the preview closed.
723+ self.session_swapped();
719724 self.preview_playing = false;
720725 }
721726
@@ -1481,11 +1486,6 @@ impl Player {
14811486 // Read before the session moves: a file that plays silent says
14821487 // so here or nowhere.
14831488 let silent = audio_notice(&session);
1484- // The watch's own cache, seeded here rather than on the first
1485- // pump after: this line already carried the fact, so a reason
1486- // unchanged across the open is not announced a second time
1487- // ([`Player::seed_audio_watch`]).
1488- self.seed_audio_watch(&session);
14891489 // A file replaces the one that was open, and track 3 of that one
14901490 // is not track 3 of this.
14911491 self.sub_track = 0;
@@ -1800,11 +1800,6 @@ impl Player {
18001800 // As at the file door above: the window watches in real time.
18011801 session.drop_late_pictures(true);
18021802 let silent = audio_notice(&session);
1803- // The watch's own cache, seeded here rather than on the first
1804- // pump after: this line already carried the fact, so a reason
1805- // unchanged across the open is not announced a second time
1806- // ([`Player::seed_audio_watch`]).
1807- self.seed_audio_watch(&session);
18081803 // A project is named after itself but still exports beside its
18091804 // media: that is the only place an export has ever landed.
18101805 self.export_path = retarget(&export_path(&session.sources()[0].path), self.format);
crates/app/src/player/transport.rsmodified+34 -29
@@ -34,23 +34,46 @@ impl Player {
3434
3535 /// The engine's word on the sound device, watched rather than asked once.
3636 /// A file that *opens* silent says so in its own open line
37- /// ([`audio_notice`], `library.rs`), but a device that dies mid-session used
38- /// to leave the window playing on in silence with nothing said at all: the
39- /// engine knew (it sets its reason to its lost-device word and clears it
40- /// again when its own re-arm takes) and no consumer ever looked.
37+ /// ([`audio_notice`], `library.rs`); what used to reach no consumer at all is
38+ /// a reason that changes *under* a session that is already up. This is the
39+ /// reader of that state -- the writer of it is the engine's own
40+ /// `PlaybackSession::audio_disabled_reason`, which this lane never spells.
4141 ///
4242 /// Called from [`Player::pump`] because that is the one place every frame
4343 /// passes through, and written to cost an ordinary frame nothing: one read of
44- /// an `Option<&str>` and one compare, with the strings built only on the
45- /// change itself.
44+ /// an `Option<&str>` and one compare, with the strings built only on the step
45+ /// the pure [`audio_step`] names.
46+ ///
47+ /// Keyed by session ([`Player::audio_watch_gen`] against `session_gen`):
48+ /// `pump` serves the preview while one is up and the timeline otherwise, so
49+ /// without the key a preview opening or closing would read the *other*
50+ /// session's reason and announce a device loss that never happened.
4651 pub(crate) fn watch_audio(&mut self) {
47- if self.sound_reason() == self.audio_reason.as_deref() {
52+ let step = audio_step(
53+ self.audio_watch_gen,
54+ self.session_gen,
55+ self.audio_reason.as_deref(),
56+ self.sound_reason(),
57+ );
58+ if step == AudioStep::Nothing {
59+ return;
60+ }
61+ let was = self.audio_reason.take();
62+ self.audio_reason = self.sound_reason().map(str::to_owned);
63+ self.audio_watch_gen = self.session_gen;
64+ if step == AudioStep::Seed {
65+ // A session swap is not a change: the new session's own open line
66+ // carries its reason, so only a line left over from the session
67+ // before is taken back -- and only when this one's sound is up.
68+ if self.audio_reason.is_none() {
69+ if let Some(was) = was {
70+ let line = audio_lost_line(&was);
71+ self.notices
72+ .retain(|notice| notice.as_ref() != line.as_str());
73+ }
74+ }
4875 return;
4976 }
50- // Re-read rather than carry a borrow across the write: this line is only
51- // reached on the change, so the allocation it costs is the news itself.
52- let reason = self.sound_reason().map(str::to_owned);
53- let was = std::mem::replace(&mut self.audio_reason, reason);
5477 audio_change_notice(
5578 &mut self.notices,
5679 was.as_deref(),
@@ -58,24 +81,6 @@ impl Player {
5881 );
5982 }
6083
61- /// [`Player::watch_audio`]'s cache, seeded at the moment an open's own line
62- /// carried the same fact: a reason unchanged across the open is not news, so
63- /// only the *clearing* half runs here -- a line left over from the file
64- /// before it comes off the strip.
65- pub(crate) fn seed_audio_watch(&mut self, session: &PlaybackSession) {
66- let was = std::mem::replace(
67- &mut self.audio_reason,
68- session.audio_disabled_reason().map(str::to_owned),
69- );
70- if self.audio_reason.is_none() {
71- if let Some(was) = was {
72- let line = audio_lost_line(&was);
73- self.notices
74- .retain(|notice| notice.as_ref() != line.as_str());
75- }
76- }
77- }
78-
7984 /// Catches the display up to the clock: everything already due is taken off
8085 /// the channel and only the last of them is shown, which *is* the
8186 /// drop-when-behind policy. A frame that is not due yet waits in `held`, and
crates/app/src/tests/media.rsmodified+60 -6
@@ -65,14 +65,68 @@ fn a_device_that_dies_under_an_open_file_says_so_and_unsays_it() {
6565 "the pump stopped watching the engine's sound reason, so a device that \
6666 dies mid-session is silent again"
6767 );
68+ // ...and the watch keys its cache to the session it is a statement about:
69+ // the two generations are what the *step call* is asked, so a preview flip
70+ // cannot be answered as if it were the cached session's own change.
71+ let watch = fn_body("watch_audio");
72+ let args = watch
73+ .split_once("audio_step(")
74+ .expect(
75+ "the watch decides for itself instead of through the pure step the flip test drives",
76+ )
77+ .1;
78+ let args = args.split_once(')').map_or(args, |(args, _)| args);
79+ assert!(
80+ args.contains("self.audio_watch_gen") && args.contains("self.session_gen"),
81+ "the watch asks the step about something other than the session it serves, \
82+ so a preview opening or closing announces the other session's device"
83+ );
6884 assert!(
69- src_text("player/library.rs")
70- .matches("self.seed_audio_watch(&session);")
71- .count()
72- == 2,
73- "an open path stopped seeding the watch, so its own NO AUDIO line comes \
74- back a frame later as a second one"
85+ fn_body("sound_reason").contains("self.active_session()"),
86+ "the watch reads a reason off a session the pump is not serving, so its \
87+ cache and the frame disagree the moment a preview is up"
88+ );
89+ // ...and every door that installs or drops a session says so, the preview's
90+ // two among them (the one the flip was missed on).
91+ for door in ["close_session", "open_preview", "close_preview"] {
92+ assert!(
93+ fn_body(door).contains("self.session_swapped();"),
94+ "{door} installs or drops a session without telling anything keyed to \
95+ one, so a flip reads as news"
96+ );
97+ }
98+}
99+
100+/// The flip logic itself, pure: a preview opening or closing is a *different*
101+/// session, so the reason it reports is not news about the cached one -- the
102+/// phantom device loss the verifier caught, and the reason the cache carries a
103+/// generation at all. A change on the session the cache describes is the one that
104+/// speaks.
105+#[test]
106+fn a_preview_flip_is_not_a_device_that_came_back() {
107+ use crate::notices::{audio_step, AudioStep};
108+ // A swap, every way round: seeded, never announced.
109+ assert_eq!(audio_step(7, 8, None, Some("AUDIO_LOST")), AudioStep::Seed);
110+ assert_eq!(audio_step(7, 8, Some("AUDIO_LOST"), None), AudioStep::Seed);
111+ assert_eq!(
112+ audio_step(7, 8, Some("AUDIO_LOST"), Some("AUDIO_LOST")),
113+ AudioStep::Seed,
114+ "a flip whose reason reads the same on both sides is still a flip"
115+ );
116+ // The same session, the reason arriving, changing, and going.
117+ assert_eq!(audio_step(7, 7, None, Some("AUDIO_LOST")), AudioStep::Push);
118+ assert_eq!(
119+ audio_step(7, 7, Some("AUDIO_LOST"), Some("device gone")),
120+ AudioStep::Push
121+ );
122+ assert_eq!(audio_step(7, 7, Some("AUDIO_LOST"), None), AudioStep::Clear);
123+ assert_eq!(
124+ audio_step(7, 7, Some("AUDIO_LOST"), Some("AUDIO_LOST")),
125+ AudioStep::Nothing,
126+ "the same reason read again on the next frame is not news"
75127 );
128+ assert_eq!(audio_step(7, 7, None, None), AudioStep::Nothing);
129+ assert_eq!(audio_step(0, 0, None, None), AudioStep::Nothing);
76130 }
77131
78132 /// The toast tail an audio import earns: rate, channel count and rounded