diff --git a/app/src/screens/review.rs b/app/src/screens/review.rs index 7e08ec8..fb478c2 100644 --- a/app/src/screens/review.rs +++ b/app/src/screens/review.rs @@ -17,6 +17,11 @@ pub struct Review { /// Set while that file is on its way to the screen. pub loading: bool, pub player: Option, + /// The clip's soundtrack, played by its own player. Rune's video player asks Media Foundation + /// for pictures and sound from one reader, and that reader stamps the sound from its own + /// clock instead of the clip's, so the sound never lines up with the clock and plays silent. + /// An audio-only player reads the same file and needs no video, so its sound starts at zero. + pub sound: Option, pub time: f64, pub duration: f64, pub scrubber_width: f32, @@ -24,17 +29,16 @@ pub struct Review { pub highlights: Vec, pub export: Option>>, pub export_note: Option, - /// Player gain, 0 to 1. Rune starts a video muted, so the review unmutes its own player. + /// Soundtrack gain, 0 to 1; the transport bar sets it. pub volume: f32, pub muted: bool, pub error: Option, } impl Review { - /// A new player takes the volume and the mute the last one had: a viewer sets them once. - pub fn apply_sound(&self, player: &Player) { - player.set_volume(self.volume); - player.set_muted(self.muted); + /// The video player only draws; the soundtrack player carries the gain and the mute. + fn mute_video(player: &Player) { + player.set_muted(true); } /// Set the gain from the transport bar. Sliding off zero is also how a viewer unmutes. @@ -43,27 +47,52 @@ impl Review { if self.volume > 0.0 { self.muted = false; } - if let Some(player) = &self.player { - player.set_volume(self.volume); - player.set_muted(self.muted); + if let Some(sound) = &self.sound { + sound.set_volume(self.volume); + sound.set_muted(self.muted); } } pub fn toggle_muted(&mut self) { self.muted = !self.muted; - if let Some(player) = &self.player { - player.set_muted(self.muted); + if let Some(sound) = &self.sound { + sound.set_muted(self.muted); } } + /// Start the soundtrack's player, at the place the viewer is watching. + fn open_sound(&mut self, path: &str) { + let sound = Player::audio(path.to_string()); + sound.set_looping(false); + sound.set_volume(self.volume); + sound.set_muted(self.muted); + let playing = self.player.as_ref().is_some_and(Player::is_playing); + let from = self.time.max(0.0); + if from > 0.0 { + sound.seek(from); + } + if playing { + sound.play(); + } + self.sound = Some(sound); + } + fn playback(&mut self) { + let playing = self.player.as_ref().is_some_and(Player::is_playing); if let Some(player) = &self.player { - if player.is_playing() { + if playing { player.pause(); } else { player.play(); } } + if let Some(sound) = &self.sound { + if playing { + sound.pause(); + } else { + sound.play(); + } + } } fn seek(&mut self, seconds: f64) { @@ -72,6 +101,9 @@ impl Review { player.seek(at); self.time = at; } + if let Some(sound) = &self.sound { + sound.seek(at); + } } } @@ -81,6 +113,7 @@ impl Default for Review { game: None, loading: false, player: None, + sound: None, time: 0.0, duration: 0.0, scrubber_width: 0.0, @@ -140,8 +173,29 @@ pub fn show(app: &mut App, game: Timeline) { if let Some(waker) = &app.waker { player.set_waker(waker.clone()); } - app.review.apply_sound(&player); app.review.player = Some(player); + app.review.sound = None; + Review::mute_video(app.review.player.as_ref().expect("just set")); +} + +/// The soundtrack player follows the video player: it starts once the file is known to have +/// sound, and from then on keeps the same place, the same playing state and the same gain. +fn follow_sound(app: &mut App) { + let Some(player) = &app.review.player else { + return; + }; + if app.review.sound.is_some() || player.has_sound() != Some(true) { + return; + } + let Some(path) = app + .review + .game + .as_ref() + .and_then(history::Timeline::video_path) + else { + return; + }; + app.review.open_sound(&path.to_string_lossy()); } /// Leave the review screen and stop playback. @@ -149,6 +203,7 @@ pub fn leave(app: &mut App) { app.review_rx = None; app.review.loading = false; app.review.player = None; + app.review.sound = None; app.review.game = None; app.review.highlights.clear(); app.review.time = 0.0; @@ -158,6 +213,21 @@ pub fn leave(app: &mut App) { app.review.export = None; } /// Called from `poll` while the review screen is up. +/// Sound from a second player drifts from the pictures by a few milliseconds a minute. Past a +/// quarter second it is worth a seek, and short of that a seek would be audible. +fn keep_in_step(app: &mut App) { + let (Some(player), Some(sound)) = (&app.review.player, &app.review.sound) else { + return; + }; + if !player.is_playing() { + return; + } + let watching = app.review.time; + let hearing = sound.time(); + if (hearing - watching).abs() > 0.25 { + sound.seek(watching); + } +} pub fn tick(app: &mut App, tx: &mut Tx) -> bool { let mut changed = false; if let Some(player) = &app.review.player { @@ -183,6 +253,8 @@ pub fn tick(app: &mut App, tx: &mut Tx) -> bool { tx.wake_after(std::time::Duration::from_millis(33)); } } + follow_sound(app); + keep_in_step(app); if let Some(rx) = &app.review.export { match rx.try_recv() { Ok(Ok(path)) => {