Mono sources up-mix onto wider timelines (DEBT #46)
Browse files at this commitd7c614bbc1cf60d35e679d3a096f06d7f6a590aeParent: 9dc7454- fix
- mono sources up-mix onto wider timelines (DEBT #46)
5 changed files +124 -29
- crates/engine/src/audio.rs +33 -2
- crates/engine/src/playback.rs +10 -0
- crates/engine/tests/audio_files.rs +50 -0
- crates/engine/tests/playback.rs +7 -8
- crates/engine/tests/saveload.rs +24 -19
crates/engine/src/audio.rsmodified+33 -2
| @@ -473,8 +473,12 @@ impl AudioSession { | ||
| 473 | 473 | // very interpolator a speeded clip already goes through, so it plays |
| 474 | 474 | // at the pitch it was recorded at over the seconds it was placed for. |
| 475 | 475 | // The layout still is -- one output device carries one width, and |
| 476 | - // everything wider than a pair is already folded to one. | |
| 477 | - if track.channels() != meta.channels { | |
| 476 | + // everything wider than a pair is already folded to one. A mono | |
| 477 | + // source is the one exception (DEBT #46): [`widen`] duplicates it | |
| 478 | + // into every channel below, the same as [`PlaybackSession`]'s own | |
| 479 | + // gate ([`crate::playback::audio_matches_probed`]) now allows. | |
| 480 | + if track.channels() != meta.channels && !(track.channels() == 1 && meta.channels > 1) | |
| 481 | + { | |
| 478 | 482 | return Err(format!( |
| 479 | 483 | "source {source} is {} ch, the timeline is {} ch", |
| 480 | 484 | track.channels(), |
| @@ -2045,6 +2049,24 @@ fn downmix(samples: &mut Vec<f32>, from: usize) { | ||
| 2045 | 2049 | samples.truncate(samples.len() / from * 2); |
| 2046 | 2050 | } |
| 2047 | 2051 | |
| 2052 | +/// The inverse of [`downmix`]: a mono buffer duplicated into every channel of | |
| 2053 | +/// a wider timeline, in place. `from` is the width the decoder actually | |
| 2054 | +/// handed back (already folded to 1 or 2 by [`downmix`] at its own door) and | |
| 2055 | +/// `to` is the timeline's own width ([`mix`]'s `channels`); anything that is | |
| 2056 | +/// not exactly the mono-into-wider case is left untouched -- a source already | |
| 2057 | +/// at `to`, and a source wider than `to`, which stays [`downmix`]'s refusal | |
| 2058 | +/// (DEBT #46: only 1-ch -> N-ch became legal, not the reverse). | |
| 2059 | +fn widen(samples: &mut Vec<f32>, from: usize, to: usize) { | |
| 2060 | + if from != 1 || to <= 1 { | |
| 2061 | + return; | |
| 2062 | + } | |
| 2063 | + let mut out = Vec::with_capacity(samples.len() * to); | |
| 2064 | + for &s in samples.iter() { | |
| 2065 | + out.extend(std::iter::repeat_n(s, to)); | |
| 2066 | + } | |
| 2067 | + *samples = out; | |
| 2068 | +} | |
| 2069 | + | |
| 2048 | 2070 | /// One source's AC-3 track: the same mp4 sample tables the AAC track is read |
| 2049 | 2071 | /// from, decoded through `oxideav-ac3` and **downmixed to stereo by the decoder |
| 2050 | 2072 | /// itself** (ATSC A/52 §7.8, `channels: Some(2)`), because one output device and |
| @@ -3163,6 +3185,9 @@ fn run(mut w: Worker) { | ||
| 3163 | 3185 | eprintln!("audio decode error at sample {id}: {e}"); |
| 3164 | 3186 | break; |
| 3165 | 3187 | } |
| 3188 | + // decoder.decode already folded to track.channels(); widen the mono | |
| 3189 | + // case up to the timeline's own width before dividing by it below. | |
| 3190 | + widen(&mut interleaved, usize::from(track.channels()), channels); | |
| 3166 | 3191 | let next = pos + (interleaved.len() / channels) as u64; |
| 3167 | 3192 | if !emit( |
| 3168 | 3193 | &mut interleaved, |
| @@ -3318,6 +3343,7 @@ fn run_ac3( | ||
| 3318 | 3343 | // The library's §7.8 downmix already handles everything but its own 2.1 |
| 3319 | 3344 | // passthrough, which comes out at 3 and is folded here like any width. |
| 3320 | 3345 | downmix(&mut interleaved, usize::from(track.channels)); |
| 3346 | + widen(&mut interleaved, usize::from(track.channels), channels); | |
| 3321 | 3347 | let next = pos + samples; |
| 3322 | 3348 | if !emit( |
| 3323 | 3349 | &mut interleaved, |
| @@ -3382,6 +3408,7 @@ fn run_mkv_ac3( | ||
| 3382 | 3408 | }; |
| 3383 | 3409 | // ...and the 2.1 passthrough folded here, as on the mp4 side. |
| 3384 | 3410 | downmix(&mut interleaved, usize::from(track.channels)); |
| 3411 | + widen(&mut interleaved, usize::from(track.channels), channels); | |
| 3385 | 3412 | let next = pos + samples; |
| 3386 | 3413 | if !emit( |
| 3387 | 3414 | &mut interleaved, |
| @@ -3455,6 +3482,10 @@ fn run_sym( | ||
| 3455 | 3482 | eprintln!("audio decode error at sample {pos}: {e}"); |
| 3456 | 3483 | return true; |
| 3457 | 3484 | } |
| 3485 | + // decoder.decode already folded to track.channels ([`SymDecoder::decode`], | |
| 3486 | + // [`downmix`]); widen the mono case up to the timeline's own width | |
| 3487 | + // before anything below divides by it. | |
| 3488 | + widen(&mut interleaved, usize::from(track.channels), channels); | |
| 3458 | 3489 | let next = pos + (interleaved.len() / channels) as u64; |
| 3459 | 3490 | at = Some(next); |
| 3460 | 3491 | // Reborrowed per packet: the filter memory has to carry across the |
crates/engine/src/playback.rsmodified+10 -0
| @@ -4065,6 +4065,16 @@ fn audio_matches_probed( | ||
| 4065 | 4065 | if probe.map(|p| p.channels) == first.map(|p| p.channels) { |
| 4066 | 4066 | return Ok(()); |
| 4067 | 4067 | } |
| 4068 | + // A mono file joins a wider timeline: [`crate::audio::widen`] duplicates | |
| 4069 | + // its one channel into every channel the timeline carries, the inverse of | |
| 4070 | + // the [`crate::audio::downmix`] a wider file already folds through. The | |
| 4071 | + // reverse -- a wider file onto a mono timeline -- stays refused below; | |
| 4072 | + // nothing here narrows a source, only the decoder's own downmix does. | |
| 4073 | + if let (Some(p), Some(b)) = (probe, first) { | |
| 4074 | + if p.channels == 1 && b.channels > 1 { | |
| 4075 | + return Ok(()); | |
| 4076 | + } | |
| 4077 | + } | |
| 4068 | 4078 | // ...and a *silent* file joins whatever the timeline is: it contributes |
| 4069 | 4079 | // silence over its span, which is the very thing both audio paths already |
| 4070 | 4080 | // synthesise for a gap ([`AudioSession::open_multi_streams_speed`] plays |
crates/engine/tests/audio_files.rsmodified+50 -0
| @@ -693,3 +693,53 @@ fn a_project_scaffolded_from_an_audio_only_mp4_reloads() { | ||
| 693 | 693 | drop(reloaded); |
| 694 | 694 | let _ = std::fs::remove_dir_all(&dir); |
| 695 | 695 | } |
| 696 | + | |
| 697 | +/// DEBT #46: a mono source used to be refused outright by a stereo timeline | |
| 698 | +/// ("audio 1 ch does not match the timeline's 2 ch") -- the only layout gate | |
| 699 | +/// left after the 2026-08-12 convert-at-import work. It now up-mixes, | |
| 700 | +/// [`engine::audio::widen`] duplicating the one channel into both of a stereo | |
| 701 | +/// timeline's, mirroring the [`engine::audio::downmix`] a wider source already | |
| 702 | +/// folds through at the decoder's door. Reverted to before that fix, this test | |
| 703 | +/// fails at `import_and_place` itself: `place_stream_at` returns | |
| 704 | +/// `Err("audio 1 ch does not match the timeline's 2 ch")` instead of landing | |
| 705 | +/// the clip, because `widen` and the `audio_matches_probed` mono exception | |
| 706 | +/// did not exist. | |
| 707 | +#[test] | |
| 708 | +fn a_mono_source_up_mixes_onto_a_stereo_timeline() { | |
| 709 | + let dir = Scratch::dir("ve_audio_upmix"); | |
| 710 | + let mut session = open(asset("test_tone.mp3")); // a 3 s stereo timeline | |
| 711 | + // 44100 Hz, 1 ch AAC -- no rate to resample, only the layout in question. | |
| 712 | + import_and_place(&mut session, &asset("test_audio_only.mp4")); | |
| 713 | + assert_eq!(session.lane_clips(Lane::A1).len(), 2); | |
| 714 | + assert!((session.timeline_duration() - 6.0).abs() < 0.1); | |
| 715 | + | |
| 716 | + let wav = dir.join("upmix.wav"); | |
| 717 | + wait(&session.export_to_with( | |
| 718 | + &wav, | |
| 719 | + &ExportSettings { | |
| 720 | + format: Format::Wav, | |
| 721 | + ..ExportSettings::default() | |
| 722 | + }, | |
| 723 | + )) | |
| 724 | + .expect("wav export of the up-mixed timeline"); | |
| 725 | + let (rate, channels, samples) = decode(&wav); | |
| 726 | + assert_eq!((rate, channels), (44100, 2)); | |
| 727 | + | |
| 728 | + // Inside the mono clip's own span (the second half of the timeline): left | |
| 729 | + // and right must carry the identical signal `widen` duplicated, and it | |
| 730 | + // must not be silence -- a bug in `widen` could zero the buffer instead of | |
| 731 | + // filling it. | |
| 732 | + let start = (3.5 * f64::from(rate)) as usize; | |
| 733 | + let end = (5.5 * f64::from(rate)) as usize; | |
| 734 | + let mut peak = 0.0f32; | |
| 735 | + for frame in start..end { | |
| 736 | + let (l, r) = (samples[frame * 2], samples[frame * 2 + 1]); | |
| 737 | + assert!( | |
| 738 | + (l - r).abs() < 1e-6, | |
| 739 | + "frame {frame}: left {l} and right {r} must carry the up-mixed mono signal" | |
| 740 | + ); | |
| 741 | + peak = peak.max(l.abs()); | |
| 742 | + } | |
| 743 | + assert!(peak > 0.01, "the up-mixed span is silent: peak {peak}"); | |
| 744 | + let _ = std::fs::remove_dir_all(&dir); | |
| 745 | +} |
crates/engine/tests/playback.rsmodified+7 -8
| @@ -1199,13 +1199,12 @@ fn import_refuses_what_does_not_match() { | ||
| 1199 | 1199 | .remove_source(&asset("test_tone_48k.wav"), 0) |
| 1200 | 1200 | .expect("...and the row it made comes back off"); |
| 1201 | 1201 | |
| 1202 | - // What is still refused, and all that is: one output device carries one | |
| 1203 | - // layout, and a mono track is not the stereo pair this timeline plays. | |
| 1204 | - let err = session | |
| 1202 | + // A mono track is no longer a refusal either (DEBT #46): [`engine::audio`] | |
| 1203 | + // duplicates its one channel into both of this stereo timeline's, the same | |
| 1204 | + // way a wider file is already folded down to it. | |
| 1205 | + session | |
| 1205 | 1206 | .import(&asset("test_ac3.mp4")) |
| 1206 | - .expect_err("a mono track cannot join a stereo timeline") | |
| 1207 | - .to_string(); | |
| 1208 | - assert_eq!(err, "audio 1 ch does not match the timeline's 2 ch"); | |
| 1207 | + .expect("a mono track up-mixes onto a stereo timeline"); | |
| 1209 | 1208 | |
| 1210 | 1209 | assert!( |
| 1211 | 1210 | session.import(&asset("no_such_file.mp4")).is_err(), |
| @@ -1214,8 +1213,8 @@ fn import_refuses_what_does_not_match() { | ||
| 1214 | 1213 | assert_eq!(session.clip_spans().len(), 1, "an import places nothing"); |
| 1215 | 1214 | assert_eq!( |
| 1216 | 1215 | session.sources().len(), |
| 1217 | - rows + 1, | |
| 1218 | - "only the silent file was taken in" | |
| 1216 | + rows + 2, | |
| 1217 | + "the silent file and the mono file were both taken in" | |
| 1219 | 1218 | ); |
| 1220 | 1219 | assert!((session.timeline_duration() - 5.0).abs() < 1e-9); |
| 1221 | 1220 |
crates/engine/tests/saveload.rsmodified+24 -19
| @@ -74,18 +74,15 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() { | ||
| 74 | 74 | assert_eq!(loaded.sources(), session.sources()); |
| 75 | 75 | assert_eq!(loaded.timeline_duration(), session.timeline_duration()); |
| 76 | 76 | |
| 77 | - // And the refusals, in the engine's own words: a stream that cannot share | |
| 78 | - // one output device with the timeline, and a file that is not on it. | |
| 77 | + // And the refusal left, in the engine's own words: a file that is not on | |
| 78 | + // this timeline. Its *layout* (stream 1 is mono, the timeline stereo) is no | |
| 79 | + // longer one (DEBT #46): the mono stream up-mixes onto both channels. | |
| 79 | 80 | let other = copy_in(&dir, "test_multiaudio.mp4"); |
| 80 | 81 | let mut session = PlaybackSession::open(&other).expect("open the fixture"); |
| 81 | 82 | session.set_gain(0.0); |
| 82 | - // Its rate would be conformed now; its *layout* is what one output device | |
| 83 | - // cannot have two of, and the refusal says which. | |
| 84 | - let err = session | |
| 83 | + session | |
| 85 | 84 | .place_stream_at(0.0, &other, 1, None) |
| 86 | - .expect_err("a mono stream cannot join a stereo timeline") | |
| 87 | - .to_string(); | |
| 88 | - assert_eq!(err, "audio 1 ch does not match the timeline's 2 ch"); | |
| 85 | + .expect("a mono stream up-mixes onto a stereo timeline"); | |
| 89 | 86 | assert!( |
| 90 | 87 | session |
| 91 | 88 | .place_stream_at(0.0, &media, 1, None) |
| @@ -93,7 +90,11 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() { | ||
| 93 | 90 | .to_string() |
| 94 | 91 | .contains("not on this timeline") |
| 95 | 92 | ); |
| 96 | - assert_eq!(session.sources().len(), 1, "a refusal added a source"); | |
| 93 | + assert_eq!( | |
| 94 | + session.sources().len(), | |
| 95 | + 2, | |
| 96 | + "the mono stream landed, the not-a-source refusal added nothing more" | |
| 97 | + ); | |
| 97 | 98 | std::fs::remove_dir_all(&dir).expect("cleanup"); |
| 98 | 99 | } |
| 99 | 100 | |
| @@ -814,8 +815,14 @@ fn malformed_files_are_numbered_errors_and_never_panics() { | ||
| 814 | 815 | #[test] |
| 815 | 816 | fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() { |
| 816 | 817 | let dir = scratch("mismatch"); |
| 817 | - copy_in(&dir, "test_av.mp4"); | |
| 818 | - std::fs::copy(asset("test_ac3.mp4"), dir.join("test_av2.mp4")).expect("substitute"); | |
| 818 | + // `test_av.mp4` -- source 0, which sets the timeline's norm -- is | |
| 819 | + // substituted with the mono fixture, so the norm this reload settles on is | |
| 820 | + // mono; `test_av2.mp4` stays the real, stereo file. A mono source onto a | |
| 821 | + // wider timeline up-mixes now (DEBT #46), but the reverse does not: a | |
| 822 | + // stereo source is still wider than the mono timeline it would join, and | |
| 823 | + // that is the one layout mismatch left to refuse. | |
| 824 | + std::fs::copy(asset("test_ac3.mp4"), dir.join("test_av.mp4")).expect("substitute"); | |
| 825 | + copy_in(&dir, "test_av2.mp4"); | |
| 819 | 826 | let path = dir.join("swapped.edith"); |
| 820 | 827 | std::fs::write( |
| 821 | 828 | &path, |
| @@ -826,17 +833,15 @@ fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() { | ||
| 826 | 833 | .err() |
| 827 | 834 | .expect("a source that stopped matching must not open") |
| 828 | 835 | .to_string(); |
| 829 | - // The suffix is `import`'s own refusal, word for word. The substitute is | |
| 830 | - // mono, which is the shape of what is left: one output device carries one | |
| 831 | - // layout. A resolution of its own is placed on the project canvas, a frame | |
| 832 | - // rate of its own is read through `Rate`, a *sample* rate of its own is | |
| 833 | - // resampled at the decoder's door, a codec of its own opens its own decoder | |
| 834 | - // and a file with no sound plays silence -- none of those is a refusal any | |
| 835 | - // more. | |
| 836 | + // The suffix is `import`'s own refusal, word for word. A resolution of its | |
| 837 | + // own is placed on the project canvas, a frame rate of its own is read | |
| 838 | + // through `Rate`, a *sample* rate of its own is resampled at the decoder's | |
| 839 | + // door, a codec of its own opens its own decoder and a file with no sound | |
| 840 | + // plays silence -- none of those is a refusal any more. | |
| 836 | 841 | assert_eq!( |
| 837 | 842 | err, |
| 838 | 843 | format!( |
| 839 | - "source {}: audio 1 ch does not match the timeline's 2 ch", | |
| 844 | + "source {}: audio 2 ch does not match the timeline's 1 ch", | |
| 840 | 845 | dir.join("test_av2.mp4").display() |
| 841 | 846 | ) |
| 842 | 847 | ); |