forge
Explore
Log in

Don't you dare go Hollow

Mono sources up-mix onto wider timelines (DEBT #46)

Browse files at this commit

d7c614bbc1cf60d35e679d3a096f06d7f6a590aeAhmet Kaan GümüşParent: 9dc7454
  • fix
    • mono sources up-mix onto wider timelines (DEBT #46)
Show full message
🐛 fix(engine): mono sources up-mix onto wider timelines (DEBT #46)

widen() (audio.rs) duplicates a mono decode buffer into every timeline
channel -- the inverse of downmix(), wired into every decode site that
feeds mix() (run_ac3, run_mkv_ac3, run_sym, the AAC loop in run()).
audio_matches_probed (playback.rs) and the export-time width check
(audio.rs:open_multi_streams_speed_at_fade) both now let a 1ch source
join a wider timeline; a wider-than-timeline source stays refused.
crates/engine/src/audio.rsmodified+33 -2
@@ -473,8 +473,12 @@ impl AudioSession {
473473 // very interpolator a speeded clip already goes through, so it plays
474474 // at the pitch it was recorded at over the seconds it was placed for.
475475 // 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+ {
478482 return Err(format!(
479483 "source {source} is {} ch, the timeline is {} ch",
480484 track.channels(),
@@ -2045,6 +2049,24 @@ fn downmix(samples: &mut Vec<f32>, from: usize) {
20452049 samples.truncate(samples.len() / from * 2);
20462050 }
20472051
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+
20482070 /// One source's AC-3 track: the same mp4 sample tables the AAC track is read
20492071 /// from, decoded through `oxideav-ac3` and **downmixed to stereo by the decoder
20502072 /// itself** (ATSC A/52 §7.8, `channels: Some(2)`), because one output device and
@@ -3163,6 +3185,9 @@ fn run(mut w: Worker) {
31633185 eprintln!("audio decode error at sample {id}: {e}");
31643186 break;
31653187 }
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);
31663191 let next = pos + (interleaved.len() / channels) as u64;
31673192 if !emit(
31683193 &mut interleaved,
@@ -3318,6 +3343,7 @@ fn run_ac3(
33183343 // The library's §7.8 downmix already handles everything but its own 2.1
33193344 // passthrough, which comes out at 3 and is folded here like any width.
33203345 downmix(&mut interleaved, usize::from(track.channels));
3346+ widen(&mut interleaved, usize::from(track.channels), channels);
33213347 let next = pos + samples;
33223348 if !emit(
33233349 &mut interleaved,
@@ -3382,6 +3408,7 @@ fn run_mkv_ac3(
33823408 };
33833409 // ...and the 2.1 passthrough folded here, as on the mp4 side.
33843410 downmix(&mut interleaved, usize::from(track.channels));
3411+ widen(&mut interleaved, usize::from(track.channels), channels);
33853412 let next = pos + samples;
33863413 if !emit(
33873414 &mut interleaved,
@@ -3455,6 +3482,10 @@ fn run_sym(
34553482 eprintln!("audio decode error at sample {pos}: {e}");
34563483 return true;
34573484 }
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);
34583489 let next = pos + (interleaved.len() / channels) as u64;
34593490 at = Some(next);
34603491 // 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(
40654065 if probe.map(|p| p.channels) == first.map(|p| p.channels) {
40664066 return Ok(());
40674067 }
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+ }
40684078 // ...and a *silent* file joins whatever the timeline is: it contributes
40694079 // silence over its span, which is the very thing both audio paths already
40704080 // 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() {
693693 drop(reloaded);
694694 let _ = std::fs::remove_dir_all(&dir);
695695 }
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() {
11991199 .remove_source(&asset("test_tone_48k.wav"), 0)
12001200 .expect("...and the row it made comes back off");
12011201
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
12051206 .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");
12091208
12101209 assert!(
12111210 session.import(&asset("no_such_file.mp4")).is_err(),
@@ -1214,8 +1213,8 @@ fn import_refuses_what_does_not_match() {
12141213 assert_eq!(session.clip_spans().len(), 1, "an import places nothing");
12151214 assert_eq!(
12161215 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"
12191218 );
12201219 assert!((session.timeline_duration() - 5.0).abs() < 1e-9);
12211220
crates/engine/tests/saveload.rsmodified+24 -19
@@ -74,18 +74,15 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() {
7474 assert_eq!(loaded.sources(), session.sources());
7575 assert_eq!(loaded.timeline_duration(), session.timeline_duration());
7676
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.
7980 let other = copy_in(&dir, "test_multiaudio.mp4");
8081 let mut session = PlaybackSession::open(&other).expect("open the fixture");
8182 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
8584 .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");
8986 assert!(
9087 session
9188 .place_stream_at(0.0, &media, 1, None)
@@ -93,7 +90,11 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() {
9390 .to_string()
9491 .contains("not on this timeline")
9592 );
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+ );
9798 std::fs::remove_dir_all(&dir).expect("cleanup");
9899 }
99100
@@ -814,8 +815,14 @@ fn malformed_files_are_numbered_errors_and_never_panics() {
814815 #[test]
815816 fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() {
816817 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");
819826 let path = dir.join("swapped.edith");
820827 std::fs::write(
821828 &path,
@@ -826,17 +833,15 @@ fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() {
826833 .err()
827834 .expect("a source that stopped matching must not open")
828835 .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.
836841 assert_eq!(
837842 err,
838843 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",
840845 dir.join("test_av2.mp4").display()
841846 )
842847 );