app: play the soundtrack from its own player
Unmuting the review's video player was not enough: rune asks Media Foundation for pictures and sound from one reader, and that reader stamps the sound samples from its own session clock rather than the clip's start. The stamps land about six seconds after the playhead, so the queue never lines up with the clock and every fill writes zeros. An audio-only player reads the same file and starts at zero. The review now keeps one for the soundtrack, muted the video player, and drives both players from the transport bar: play, pause, seek, volume and mute. They are kept within a quarter second of each other, and the soundtrack waits until the file has reported it has sound, so clips without an audio track open no second player. Measured with rune instrumented against a 57 s recording: the video player's render buffer carried zeros on every fill, the soundtrack player carries the clip's samples at the set gain.
This commit is contained in:
1 parent
1e7e9dfc41
commit
ac9faba229
1 file changed
+84
-12
+84
-12
@@ -17,6 +17,11 @@ pub struct Review {
|
|||||||
/// Set while that file is on its way to the screen.
|
/// Set while that file is on its way to the screen.
|
||||||
pub loading: bool,
|
pub loading: bool,
|
||||||
pub player: Option<Player>,
|
pub player: Option<Player>,
|
||||||
|
/// 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<Player>,
|
||||||
pub time: f64,
|
pub time: f64,
|
||||||
pub duration: f64,
|
pub duration: f64,
|
||||||
pub scrubber_width: f32,
|
pub scrubber_width: f32,
|
||||||
@@ -24,17 +29,16 @@ pub struct Review {
|
|||||||
pub highlights: Vec<Highlight>,
|
pub highlights: Vec<Highlight>,
|
||||||
pub export: Option<Receiver<Result<String, String>>>,
|
pub export: Option<Receiver<Result<String, String>>>,
|
||||||
pub export_note: Option<String>,
|
pub export_note: Option<String>,
|
||||||
/// 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 volume: f32,
|
||||||
pub muted: bool,
|
pub muted: bool,
|
||||||
pub error: Option<String>,
|
pub error: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Review {
|
impl Review {
|
||||||
/// A new player takes the volume and the mute the last one had: a viewer sets them once.
|
/// The video player only draws; the soundtrack player carries the gain and the mute.
|
||||||
pub fn apply_sound(&self, player: &Player) {
|
fn mute_video(player: &Player) {
|
||||||
player.set_volume(self.volume);
|
player.set_muted(true);
|
||||||
player.set_muted(self.muted);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the gain from the transport bar. Sliding off zero is also how a viewer unmutes.
|
/// 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 {
|
if self.volume > 0.0 {
|
||||||
self.muted = false;
|
self.muted = false;
|
||||||
}
|
}
|
||||||
if let Some(player) = &self.player {
|
if let Some(sound) = &self.sound {
|
||||||
player.set_volume(self.volume);
|
sound.set_volume(self.volume);
|
||||||
player.set_muted(self.muted);
|
sound.set_muted(self.muted);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toggle_muted(&mut self) {
|
pub fn toggle_muted(&mut self) {
|
||||||
self.muted = !self.muted;
|
self.muted = !self.muted;
|
||||||
if let Some(player) = &self.player {
|
if let Some(sound) = &self.sound {
|
||||||
player.set_muted(self.muted);
|
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) {
|
fn playback(&mut self) {
|
||||||
|
let playing = self.player.as_ref().is_some_and(Player::is_playing);
|
||||||
if let Some(player) = &self.player {
|
if let Some(player) = &self.player {
|
||||||
if player.is_playing() {
|
if playing {
|
||||||
player.pause();
|
player.pause();
|
||||||
} else {
|
} else {
|
||||||
player.play();
|
player.play();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Some(sound) = &self.sound {
|
||||||
|
if playing {
|
||||||
|
sound.pause();
|
||||||
|
} else {
|
||||||
|
sound.play();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn seek(&mut self, seconds: f64) {
|
fn seek(&mut self, seconds: f64) {
|
||||||
@@ -72,6 +101,9 @@ impl Review {
|
|||||||
player.seek(at);
|
player.seek(at);
|
||||||
self.time = at;
|
self.time = at;
|
||||||
}
|
}
|
||||||
|
if let Some(sound) = &self.sound {
|
||||||
|
sound.seek(at);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,6 +113,7 @@ impl Default for Review {
|
|||||||
game: None,
|
game: None,
|
||||||
loading: false,
|
loading: false,
|
||||||
player: None,
|
player: None,
|
||||||
|
sound: None,
|
||||||
time: 0.0,
|
time: 0.0,
|
||||||
duration: 0.0,
|
duration: 0.0,
|
||||||
scrubber_width: 0.0,
|
scrubber_width: 0.0,
|
||||||
@@ -140,8 +173,29 @@ pub fn show(app: &mut App, game: Timeline) {
|
|||||||
if let Some(waker) = &app.waker {
|
if let Some(waker) = &app.waker {
|
||||||
player.set_waker(waker.clone());
|
player.set_waker(waker.clone());
|
||||||
}
|
}
|
||||||
app.review.apply_sound(&player);
|
|
||||||
app.review.player = Some(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.
|
/// Leave the review screen and stop playback.
|
||||||
@@ -149,6 +203,7 @@ pub fn leave(app: &mut App) {
|
|||||||
app.review_rx = None;
|
app.review_rx = None;
|
||||||
app.review.loading = false;
|
app.review.loading = false;
|
||||||
app.review.player = None;
|
app.review.player = None;
|
||||||
|
app.review.sound = None;
|
||||||
app.review.game = None;
|
app.review.game = None;
|
||||||
app.review.highlights.clear();
|
app.review.highlights.clear();
|
||||||
app.review.time = 0.0;
|
app.review.time = 0.0;
|
||||||
@@ -158,6 +213,21 @@ pub fn leave(app: &mut App) {
|
|||||||
app.review.export = None;
|
app.review.export = None;
|
||||||
}
|
}
|
||||||
/// Called from `poll` while the review screen is up.
|
/// 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 {
|
pub fn tick(app: &mut App, tx: &mut Tx) -> bool {
|
||||||
let mut changed = false;
|
let mut changed = false;
|
||||||
if let Some(player) = &app.review.player {
|
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));
|
tx.wake_after(std::time::Duration::from_millis(33));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
follow_sound(app);
|
||||||
|
keep_in_step(app);
|
||||||
if let Some(rx) = &app.review.export {
|
if let Some(rx) = &app.review.export {
|
||||||
match rx.try_recv() {
|
match rx.try_recv() {
|
||||||
Ok(Ok(path)) => {
|
Ok(Ok(path)) => {
|
||||||
|
|||||||
Reference in new issue
Block a user