A library preview opens the row's own audio stream
Browse files at this commite6a2fc11a58a183ed8a504b3c4cbb45594e32c17Parent: dc4ce87- feat
- a library preview opens the row's own audio stream
3 changed files +60 -17
- crates/app/src/player/library.rs +10 -10
- crates/app/src/ui/library.rs +1 -1
- crates/engine/src/playback.rs +49 -6
crates/app/src/player/library.rsmodified+10 -10
| @@ -210,15 +210,15 @@ impl Player { | |||
| 210 | /// The row's own play button: opens `path` as a session of its own and | 210 | /// The row's own play button: opens `path` as a session of its own and |
| 211 | /// shows it in place of the timeline's picture, without moving the | 211 | /// shows it in place of the timeline's picture, without moving the |
| 212 | /// timeline's playhead or touching its undo stack -- what a library | 212 | /// timeline's playhead or touching its undo stack -- what a library |
| 213 | - /// preview is. [`PlaybackSession::open`] already builds exactly this (a | 213 | + /// preview is. [`PlaybackSession::open_with_audio_stream`] builds exactly |
| 214 | - /// one-clip session, ungraded, passthrough), so nothing new is asked of | 214 | + /// this (a one-clip session, ungraded, passthrough) bound to `stream`, the |
| 215 | - /// the engine. | 215 | + /// row's own audio track rather than always the file's first. |
| 216 | - /// | 216 | + pub(crate) fn open_preview( |
| 217 | - /// corner-cut: opens the file's own first audio stream rather than the | 217 | + &mut self, |
| 218 | - /// row's particular one -- `PlaybackSession::open` takes no stream | 218 | + path: &std::path::Path, |
| 219 | - /// argument. Ceiling: a remux with several audio tracks previews on its | 219 | + stream: usize, |
| 220 | - /// first. Upgrade path: an engine door that opens a chosen stream. | 220 | + cx: &mut Context<Self>, |
| 221 | - pub(crate) fn open_preview(&mut self, path: &std::path::Path, cx: &mut Context<Self>) { | 221 | + ) { |
| 222 | if self.exporting().is_some() { | 222 | if self.exporting().is_some() { |
| 223 | return; | 223 | return; |
| 224 | } | 224 | } |
| @@ -229,7 +229,7 @@ impl Player { | |||
| 229 | if let Some(session) = self.session.as_mut() { | 229 | if let Some(session) = self.session.as_mut() { |
| 230 | session.pause(); | 230 | session.pause(); |
| 231 | } | 231 | } |
| 232 | - match PlaybackSession::open(path) { | 232 | + match PlaybackSession::open_with_audio_stream(path, stream) { |
| 233 | Ok(mut session) => { | 233 | Ok(mut session) => { |
| 234 | session.drop_late_pictures(true); | 234 | session.drop_late_pictures(true); |
| 235 | session.set_gain(self.volume.gain()); | 235 | session.set_gain(self.volume.gain()); |
crates/app/src/ui/library.rsmodified+1 -1
| @@ -305,7 +305,7 @@ impl Player { | |||
| 305 | .tooltip(move |_, cx| cx.new(|_| Tip(preview_tip.clone())).into()) | 305 | .tooltip(move |_, cx| cx.new(|_| Tip(preview_tip.clone())).into()) |
| 306 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { | 306 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 307 | cx.stop_propagation(); | 307 | cx.stop_propagation(); |
| 308 | - this.open_preview(&preview_path, cx); | 308 | + this.open_preview(&preview_path, stream, cx); |
| 309 | })) | 309 | })) |
| 310 | .child("▶"), | 310 | .child("▶"), |
| 311 | ) | 311 | ) |
crates/engine/src/playback.rsmodified+49 -6
| @@ -456,13 +456,30 @@ impl PlaybackSession { | |||
| 456 | /// Opens `path` and starts both decode workers. Only video failure is fatal: | 456 | /// Opens `path` and starts both decode workers. Only video failure is fatal: |
| 457 | /// a file we cannot hear is still a file we can watch. | 457 | /// a file we cannot hear is still a file we can watch. |
| 458 | pub fn open(path: impl AsRef<Path>) -> crate::Result<Self> { | 458 | pub fn open(path: impl AsRef<Path>) -> crate::Result<Self> { |
| 459 | + Self::open_stream(path, 0) | ||
| 460 | + } | ||
| 461 | + | ||
| 462 | + /// Opens `path` exactly as [`open`](Self::open) does, but binds `stream` -- | ||
| 463 | + /// the index a library row names ([`crate::probe`]'s own numbering of a | ||
| 464 | + /// file's audio tracks) -- as the one that keeps the clock and plays, | ||
| 465 | + /// rather than always the first. What a library preview asks for when the | ||
| 466 | + /// row *is* a particular stream, so a remux with several audio tracks | ||
| 467 | + /// previews the one that was clicked. | ||
| 468 | + pub fn open_with_audio_stream( | ||
| 469 | + path: impl AsRef<Path>, | ||
| 470 | + audio_stream: usize, | ||
| 471 | + ) -> crate::Result<Self> { | ||
| 472 | + Self::open_stream(path, audio_stream) | ||
| 473 | + } | ||
| 474 | + | ||
| 475 | + fn open_stream(path: impl AsRef<Path>, audio_stream: usize) -> crate::Result<Self> { | ||
| 459 | let path = path.as_ref().to_path_buf(); | 476 | let path = path.as_ref().to_path_buf(); |
| 460 | // A timeline is normally scaffolded from source 0's picture -- its size, | 477 | // A timeline is normally scaffolded from source 0's picture -- its size, |
| 461 | // its frame rate, its clock. A song has none of that, so it scaffolds | 478 | // its frame rate, its clock. A song has none of that, so it scaffolds |
| 462 | // the *canvas* instead and its own sound keeps the clock: an audio-only | 479 | // the *canvas* instead and its own sound keeps the clock: an audio-only |
| 463 | // project is a project like any other, it simply plays black. | 480 | // project is a project like any other, it simply plays black. |
| 464 | if crate::is_audio(&path) { | 481 | if crate::is_audio(&path) { |
| 465 | - return Self::open_audio_only(&path); | 482 | + return Self::open_audio_only(&path, audio_stream); |
| 466 | } | 483 | } |
| 467 | // A still is the other source with no timeline in it: it has a picture | 484 | // A still is the other source with no timeline in it: it has a picture |
| 468 | // (which becomes the canvas) but no rate and no length, so it too | 485 | // (which becomes the canvas) but no rate and no length, so it too |
| @@ -488,9 +505,10 @@ impl PlaybackSession { | |||
| 488 | // picked one for, exactly as it is a project nobody has graded. | 505 | // picked one for, exactly as it is a project nobody has graded. |
| 489 | crate::tonemap::Preset::default(), | 506 | crate::tonemap::Preset::default(), |
| 490 | )?; | 507 | )?; |
| 491 | - // A file is opened on its first audio stream, like `Project::single` | 508 | + // A file is opened on its first audio stream by default, like |
| 492 | - // names it: nothing has picked one yet. | 509 | + // `Project::single` names it -- unless a caller through |
| 493 | - let (audio, audio_disabled) = open_audio(&path, 0); | 510 | + // `open_with_audio_stream` picked another. |
| 511 | + let (audio, audio_disabled) = open_audio(&path, audio_stream); | ||
| 494 | let source = match audio { | 512 | let source = match audio { |
| 495 | Some(_) => ClockSource::Audio, | 513 | Some(_) => ClockSource::Audio, |
| 496 | None => ClockSource::Wall, | 514 | None => ClockSource::Wall, |
| @@ -566,7 +584,7 @@ impl PlaybackSession { | |||
| 566 | /// picture nor a playable length is not a timeline, and that is what | 584 | /// picture nor a playable length is not a timeline, and that is what |
| 567 | /// [`audio_frames`] answers -- the *device* is a different question, and a | 585 | /// [`audio_frames`] answers -- the *device* is a different question, and a |
| 568 | /// machine without one opens this like any other session, silently. | 586 | /// machine without one opens this like any other session, silently. |
| 569 | - fn open_audio_only(path: &Path) -> crate::Result<Self> { | 587 | + fn open_audio_only(path: &Path, stream: usize) -> crate::Result<Self> { |
| 570 | let (width, height, frame_rate) = AUDIO_ONLY_CANVAS; | 588 | let (width, height, frame_rate) = AUDIO_ONLY_CANVAS; |
| 571 | let meta = VideoMeta { | 589 | let meta = VideoMeta { |
| 572 | width, | 590 | width, |
| @@ -584,7 +602,7 @@ impl PlaybackSession { | |||
| 584 | // the default description is what it is drawn in. | 602 | // the default description is what it is drawn in. |
| 585 | color: ColorDescription::default(), | 603 | color: ColorDescription::default(), |
| 586 | }; | 604 | }; |
| 587 | - let (audio, audio_disabled) = open_audio(path, 0); | 605 | + let (audio, audio_disabled) = open_audio(path, stream); |
| 588 | // Through `from_parts` rather than `Project::single`, which is the | 606 | // Through `from_parts` rather than `Project::single`, which is the |
| 589 | // *video* open's pair of grouped clips: here there is no picture to | 607 | // *video* open's pair of grouped clips: here there is no picture to |
| 590 | // group the sound with, so the video lane starts empty. | 608 | // group the sound with, so the video lane starts empty. |
| @@ -3920,6 +3938,31 @@ mod tests { | |||
| 3920 | assert_eq!(session.probes.len(), 2, "and nothing was opened to say so"); | 3938 | assert_eq!(session.probes.len(), 2, "and nothing was opened to say so"); |
| 3921 | } | 3939 | } |
| 3922 | 3940 | ||
| 3941 | + /// The door a library preview opens a chosen stream through: `test_multiaudio.mp4` | ||
| 3942 | + /// has stream 0 stereo and stream 1 mono | ||
| 3943 | + /// ([`crate::waveform::tests::each_stream_of_a_file_has_its_own_envelope`] | ||
| 3944 | + /// names the same fixture), so a session that actually bound the stream it | ||
| 3945 | + /// was asked for plays a different layout than one that did not -- and | ||
| 3946 | + /// plain [`PlaybackSession::open`] still lands on stream 0, unchanged. | ||
| 3947 | + #[test] | ||
| 3948 | + fn open_with_audio_stream_binds_the_named_track() { | ||
| 3949 | + let multi = asset("test_multiaudio.mp4"); | ||
| 3950 | + let zero = PlaybackSession::open_with_audio_stream(&multi, 0).expect("stream 0 opens"); | ||
| 3951 | + let one = PlaybackSession::open_with_audio_stream(&multi, 1).expect("stream 1 opens"); | ||
| 3952 | + let default = PlaybackSession::open(&multi).expect("default open"); | ||
| 3953 | + let (Some(a0), Some(a1), Some(ad)) = | ||
| 3954 | + (zero.audio.as_ref(), one.audio.as_ref(), default.audio.as_ref()) | ||
| 3955 | + else { | ||
| 3956 | + // No audio device on this machine: nothing further to check -- | ||
| 3957 | + // the device is the machine's business, not this door's. | ||
| 3958 | + return; | ||
| 3959 | + }; | ||
| 3960 | + assert_eq!(a0.channels, 2, "stream 0 is the stereo pair"); | ||
| 3961 | + assert_eq!(a1.channels, 1, "stream 1 is the mono track"); | ||
| 3962 | + assert_ne!(a0.channels, a1.channels, "the two streams must differ"); | ||
| 3963 | + assert_eq!(ad.channels, a0.channels, "open() still defaults to stream 0"); | ||
| 3964 | + } | ||
| 3965 | + | ||
| 3923 | #[test] | 3966 | #[test] |
| 3924 | fn boundary_seconds_round_trip_to_their_own_frame() { | 3967 | fn boundary_seconds_round_trip_to_their_own_frame() { |
| 3925 | for f in 0u32..300 { | 3968 | for f in 0u32..300 { |