The sound watch is keyed to the session it describes
Browse files at this commite87ca8852c38d502663bf020f95332fe5a3d1a70Parent: c80d1f7- fix
- the sound watch is keyed to the session it describes
5 changed files +149 -45
- crates/app/src/main.rs +6 -0
- crates/app/src/notices.rs +44 -0
- crates/app/src/player/library.rs +5 -10
- crates/app/src/player/transport.rs +34 -29
- crates/app/src/tests/media.rs +60 -6
crates/app/src/main.rsmodified+6 -0
| @@ -796,6 +796,11 @@ struct Player { | |||
| 796 | /// while the sound is up, and seeded by `Player::seed_audio_watch` at every | 796 | /// while the sound is up, and seeded by `Player::seed_audio_watch` at every |
| 797 | /// open, whose own line already carries the same fact. | 797 | /// open, whose own line already carries the same fact. |
| 798 | audio_reason: Option<String>, | 798 | 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, | ||
| 799 | /// Which of the clip's two inks the look section's wheel and colour field | 804 | /// Which of the clip's two inks the look section's wheel and colour field |
| 800 | /// act on ([`VizSlot`]): the clip's own colour, or the ring's dual half. | 805 | /// act on ([`VizSlot`]): the clip's own colour, or the ring's dual half. |
| 801 | /// The pick stays picked across clips, the way the dock tab's does. | 806 | /// The pick stays picked across clips, the way the dock tab's does. |
| @@ -1128,6 +1133,7 @@ fn main() { | |||
| 1128 | // Nothing heard yet: the first pump seeds it off the session | 1133 | // Nothing heard yet: the first pump seeds it off the session |
| 1129 | // it serves. | 1134 | // it serves. |
| 1130 | audio_reason: None, | 1135 | audio_reason: None, |
| 1136 | + audio_watch_gen: 0, | ||
| 1131 | viz_slot: VizSlot::Main, | 1137 | viz_slot: VizSlot::Main, |
| 1132 | viz_hex_edit: None, | 1138 | viz_hex_edit: None, |
| 1133 | pending_viz: None, | 1139 | pending_viz: None, |
crates/app/src/notices.rsmodified+44 -0
| @@ -64,6 +64,50 @@ pub(crate) fn audio_lost_line(reason: &str) -> String { | |||
| 64 | format!("AUDIO LOST{}", audio_tail(reason)) | 64 | format!("AUDIO LOST{}", audio_tail(reason)) |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 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 | + | ||
| 67 | /// What a *change* in the engine's sound reason does to the notice queue, as a | 111 | /// What a *change* in the engine's sound reason does to the notice queue, as a |
| 68 | /// pure step: the death line pushed when a reason arrives, the same line taken | 112 | /// pure step: the death line pushed when a reason arrives, the same line taken |
| 69 | /// back when the reason clears ([`PlaybackSession::audio_disabled_reason`]'s | 113 | /// back when the reason clears ([`PlaybackSession::audio_disabled_reason`]'s |
crates/app/src/player/library.rsmodified+5 -10
| @@ -716,6 +716,11 @@ impl Player { | |||
| 716 | cx.notify(); | 716 | cx.notify(); |
| 717 | } | 717 | } |
| 718 | } | 718 | } |
| 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(); | ||
| 719 | self.preview_playing = false; | 724 | self.preview_playing = false; |
| 720 | } | 725 | } |
| 721 | 726 | ||
| @@ -1481,11 +1486,6 @@ impl Player { | |||
| 1481 | // Read before the session moves: a file that plays silent says | 1486 | // Read before the session moves: a file that plays silent says |
| 1482 | // so here or nowhere. | 1487 | // so here or nowhere. |
| 1483 | let silent = audio_notice(&session); | 1488 | 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); | ||
| 1489 | // A file replaces the one that was open, and track 3 of that one | 1489 | // A file replaces the one that was open, and track 3 of that one |
| 1490 | // is not track 3 of this. | 1490 | // is not track 3 of this. |
| 1491 | self.sub_track = 0; | 1491 | self.sub_track = 0; |
| @@ -1800,11 +1800,6 @@ impl Player { | |||
| 1800 | // As at the file door above: the window watches in real time. | 1800 | // As at the file door above: the window watches in real time. |
| 1801 | session.drop_late_pictures(true); | 1801 | session.drop_late_pictures(true); |
| 1802 | let silent = audio_notice(&session); | 1802 | 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); | ||
| 1808 | // A project is named after itself but still exports beside its | 1803 | // A project is named after itself but still exports beside its |
| 1809 | // media: that is the only place an export has ever landed. | 1804 | // media: that is the only place an export has ever landed. |
| 1810 | self.export_path = retarget(&export_path(&session.sources()[0].path), self.format); | 1805 | 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 { | |||
| 34 | 34 | ||
| 35 | /// The engine's word on the sound device, watched rather than asked once. | 35 | /// The engine's word on the sound device, watched rather than asked once. |
| 36 | /// A file that *opens* silent says so in its own open line | 36 | /// 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 | 37 | + /// ([`audio_notice`], `library.rs`); what used to reach no consumer at all is |
| 38 | - /// to leave the window playing on in silence with nothing said at all: the | 38 | + /// a reason that changes *under* a session that is already up. This is the |
| 39 | - /// engine knew (it sets its reason to its lost-device word and clears it | 39 | + /// reader of that state -- the writer of it is the engine's own |
| 40 | - /// again when its own re-arm takes) and no consumer ever looked. | 40 | + /// `PlaybackSession::audio_disabled_reason`, which this lane never spells. |
| 41 | /// | 41 | /// |
| 42 | /// Called from [`Player::pump`] because that is the one place every frame | 42 | /// Called from [`Player::pump`] because that is the one place every frame |
| 43 | /// passes through, and written to cost an ordinary frame nothing: one read of | 43 | /// 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 | 44 | + /// an `Option<&str>` and one compare, with the strings built only on the step |
| 45 | - /// change itself. | 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. | ||
| 46 | pub(crate) fn watch_audio(&mut self) { | 51 | 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 | + } | ||
| 48 | return; | 75 | return; |
| 49 | } | 76 | } |
| 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); | ||
| 54 | audio_change_notice( | 77 | audio_change_notice( |
| 55 | &mut self.notices, | 78 | &mut self.notices, |
| 56 | was.as_deref(), | 79 | was.as_deref(), |
| @@ -58,24 +81,6 @@ impl Player { | |||
| 58 | ); | 81 | ); |
| 59 | } | 82 | } |
| 60 | 83 | ||
| 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 | - | ||
| 79 | /// Catches the display up to the clock: everything already due is taken off | 84 | /// Catches the display up to the clock: everything already due is taken off |
| 80 | /// the channel and only the last of them is shown, which *is* the | 85 | /// the channel and only the last of them is shown, which *is* the |
| 81 | /// drop-when-behind policy. A frame that is not due yet waits in `held`, and | 86 | /// 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() { | |||
| 65 | "the pump stopped watching the engine's sound reason, so a device that \ | 65 | "the pump stopped watching the engine's sound reason, so a device that \ |
| 66 | dies mid-session is silent again" | 66 | dies mid-session is silent again" |
| 67 | ); | 67 | ); |
| 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 | + ); | ||
| 68 | assert!( | 84 | assert!( |
| 69 | - src_text("player/library.rs") | 85 | + fn_body("sound_reason").contains("self.active_session()"), |
| 70 | - .matches("self.seed_audio_watch(&session);") | 86 | + "the watch reads a reason off a session the pump is not serving, so its \ |
| 71 | - .count() | 87 | + cache and the frame disagree the moment a preview is up" |
| 72 | - == 2, | 88 | + ); |
| 73 | - "an open path stopped seeding the watch, so its own NO AUDIO line comes \ | 89 | + // ...and every door that installs or drops a session says so, the preview's |
| 74 | - back a frame later as a second one" | 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" | ||
| 75 | ); | 127 | ); |
| 128 | + assert_eq!(audio_step(7, 7, None, None), AudioStep::Nothing); | ||
| 129 | + assert_eq!(audio_step(0, 0, None, None), AudioStep::Nothing); | ||
| 76 | } | 130 | } |
| 77 | 131 | ||
| 78 | /// The toast tail an audio import earns: rate, channel count and rounded | 132 | /// The toast tail an audio import earns: rate, channel count and rounded |