From e6a2fc11a58a183ed8a504b3c4cbb45594e32c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 19 Aug 2026 21:59:26 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(engine):=20a=20library=20previ?= =?UTF-8?q?ew=20opens=20the=20row's=20own=20audio=20stream?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PlaybackSession::open_with_audio_stream binds a chosen audio track instead of always the file's first, closing the corner-cut open_preview named. Library rows already carry their own stream index (one row per audio track on a remux), so the preview button now passes it straight through. --- crates/app/src/player/library.rs | 20 ++++++------ crates/app/src/ui/library.rs | 2 +- crates/engine/src/playback.rs | 55 ++++++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/crates/app/src/player/library.rs b/crates/app/src/player/library.rs index a52de8b..5bcd688 100644 --- a/crates/app/src/player/library.rs +++ b/crates/app/src/player/library.rs @@ -210,15 +210,15 @@ impl Player { /// The row's own play button: opens `path` as a session of its own and /// shows it in place of the timeline's picture, without moving the /// timeline's playhead or touching its undo stack -- what a library - /// preview is. [`PlaybackSession::open`] already builds exactly this (a - /// one-clip session, ungraded, passthrough), so nothing new is asked of - /// the engine. - /// - /// corner-cut: opens the file's own first audio stream rather than the - /// row's particular one -- `PlaybackSession::open` takes no stream - /// argument. Ceiling: a remux with several audio tracks previews on its - /// first. Upgrade path: an engine door that opens a chosen stream. - pub(crate) fn open_preview(&mut self, path: &std::path::Path, cx: &mut Context) { + /// preview is. [`PlaybackSession::open_with_audio_stream`] builds exactly + /// this (a one-clip session, ungraded, passthrough) bound to `stream`, the + /// row's own audio track rather than always the file's first. + pub(crate) fn open_preview( + &mut self, + path: &std::path::Path, + stream: usize, + cx: &mut Context, + ) { if self.exporting().is_some() { return; } @@ -229,7 +229,7 @@ impl Player { if let Some(session) = self.session.as_mut() { session.pause(); } - match PlaybackSession::open(path) { + match PlaybackSession::open_with_audio_stream(path, stream) { Ok(mut session) => { session.drop_late_pictures(true); session.set_gain(self.volume.gain()); diff --git a/crates/app/src/ui/library.rs b/crates/app/src/ui/library.rs index 0e3b07d..ffaa6da 100644 --- a/crates/app/src/ui/library.rs +++ b/crates/app/src/ui/library.rs @@ -305,7 +305,7 @@ impl Player { .tooltip(move |_, cx| cx.new(|_| Tip(preview_tip.clone())).into()) .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { cx.stop_propagation(); - this.open_preview(&preview_path, cx); + this.open_preview(&preview_path, stream, cx); })) .child("▶"), ) diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index cd17f6e..c91efbd 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -456,13 +456,30 @@ impl PlaybackSession { /// Opens `path` and starts both decode workers. Only video failure is fatal: /// a file we cannot hear is still a file we can watch. pub fn open(path: impl AsRef) -> crate::Result { + Self::open_stream(path, 0) + } + + /// Opens `path` exactly as [`open`](Self::open) does, but binds `stream` -- + /// the index a library row names ([`crate::probe`]'s own numbering of a + /// file's audio tracks) -- as the one that keeps the clock and plays, + /// rather than always the first. What a library preview asks for when the + /// row *is* a particular stream, so a remux with several audio tracks + /// previews the one that was clicked. + pub fn open_with_audio_stream( + path: impl AsRef, + audio_stream: usize, + ) -> crate::Result { + Self::open_stream(path, audio_stream) + } + + fn open_stream(path: impl AsRef, audio_stream: usize) -> crate::Result { let path = path.as_ref().to_path_buf(); // A timeline is normally scaffolded from source 0's picture -- its size, // its frame rate, its clock. A song has none of that, so it scaffolds // the *canvas* instead and its own sound keeps the clock: an audio-only // project is a project like any other, it simply plays black. if crate::is_audio(&path) { - return Self::open_audio_only(&path); + return Self::open_audio_only(&path, audio_stream); } // A still is the other source with no timeline in it: it has a picture // (which becomes the canvas) but no rate and no length, so it too @@ -488,9 +505,10 @@ impl PlaybackSession { // picked one for, exactly as it is a project nobody has graded. crate::tonemap::Preset::default(), )?; - // A file is opened on its first audio stream, like `Project::single` - // names it: nothing has picked one yet. - let (audio, audio_disabled) = open_audio(&path, 0); + // A file is opened on its first audio stream by default, like + // `Project::single` names it -- unless a caller through + // `open_with_audio_stream` picked another. + let (audio, audio_disabled) = open_audio(&path, audio_stream); let source = match audio { Some(_) => ClockSource::Audio, None => ClockSource::Wall, @@ -566,7 +584,7 @@ impl PlaybackSession { /// picture nor a playable length is not a timeline, and that is what /// [`audio_frames`] answers -- the *device* is a different question, and a /// machine without one opens this like any other session, silently. - fn open_audio_only(path: &Path) -> crate::Result { + fn open_audio_only(path: &Path, stream: usize) -> crate::Result { let (width, height, frame_rate) = AUDIO_ONLY_CANVAS; let meta = VideoMeta { width, @@ -584,7 +602,7 @@ impl PlaybackSession { // the default description is what it is drawn in. color: ColorDescription::default(), }; - let (audio, audio_disabled) = open_audio(path, 0); + let (audio, audio_disabled) = open_audio(path, stream); // Through `from_parts` rather than `Project::single`, which is the // *video* open's pair of grouped clips: here there is no picture to // group the sound with, so the video lane starts empty. @@ -3920,6 +3938,31 @@ mod tests { assert_eq!(session.probes.len(), 2, "and nothing was opened to say so"); } + /// The door a library preview opens a chosen stream through: `test_multiaudio.mp4` + /// has stream 0 stereo and stream 1 mono + /// ([`crate::waveform::tests::each_stream_of_a_file_has_its_own_envelope`] + /// names the same fixture), so a session that actually bound the stream it + /// was asked for plays a different layout than one that did not -- and + /// plain [`PlaybackSession::open`] still lands on stream 0, unchanged. + #[test] + fn open_with_audio_stream_binds_the_named_track() { + let multi = asset("test_multiaudio.mp4"); + let zero = PlaybackSession::open_with_audio_stream(&multi, 0).expect("stream 0 opens"); + let one = PlaybackSession::open_with_audio_stream(&multi, 1).expect("stream 1 opens"); + let default = PlaybackSession::open(&multi).expect("default open"); + let (Some(a0), Some(a1), Some(ad)) = + (zero.audio.as_ref(), one.audio.as_ref(), default.audio.as_ref()) + else { + // No audio device on this machine: nothing further to check -- + // the device is the machine's business, not this door's. + return; + }; + assert_eq!(a0.channels, 2, "stream 0 is the stereo pair"); + assert_eq!(a1.channels, 1, "stream 1 is the mono track"); + assert_ne!(a0.channels, a1.channels, "the two streams must differ"); + assert_eq!(ad.channels, a0.channels, "open() still defaults to stream 0"); + } + #[test] fn boundary_seconds_round_trip_to_their_own_frame() { for f in 0u32..300 {