From d7c614bbc1cf60d35e679d3a096f06d7f6a590ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 02:45:40 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(engine):=20mono=20sources=20?= =?UTF-8?q?up-mix=20onto=20wider=20timelines=20(DEBT=20#46)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.rs | 35 +++++++++++++++++++-- crates/engine/src/playback.rs | 10 ++++++ crates/engine/tests/audio_files.rs | 50 ++++++++++++++++++++++++++++++ crates/engine/tests/playback.rs | 15 +++++---- crates/engine/tests/saveload.rs | 43 +++++++++++++------------ 5 files changed, 124 insertions(+), 29 deletions(-) diff --git a/crates/engine/src/audio.rs b/crates/engine/src/audio.rs index 86bb0d5..0cc0499 100644 --- a/crates/engine/src/audio.rs +++ b/crates/engine/src/audio.rs @@ -473,8 +473,12 @@ impl AudioSession { // very interpolator a speeded clip already goes through, so it plays // at the pitch it was recorded at over the seconds it was placed for. // The layout still is -- one output device carries one width, and - // everything wider than a pair is already folded to one. - if track.channels() != meta.channels { + // everything wider than a pair is already folded to one. A mono + // source is the one exception (DEBT #46): [`widen`] duplicates it + // into every channel below, the same as [`PlaybackSession`]'s own + // gate ([`crate::playback::audio_matches_probed`]) now allows. + if track.channels() != meta.channels && !(track.channels() == 1 && meta.channels > 1) + { return Err(format!( "source {source} is {} ch, the timeline is {} ch", track.channels(), @@ -2045,6 +2049,24 @@ fn downmix(samples: &mut Vec, from: usize) { samples.truncate(samples.len() / from * 2); } +/// The inverse of [`downmix`]: a mono buffer duplicated into every channel of +/// a wider timeline, in place. `from` is the width the decoder actually +/// handed back (already folded to 1 or 2 by [`downmix`] at its own door) and +/// `to` is the timeline's own width ([`mix`]'s `channels`); anything that is +/// not exactly the mono-into-wider case is left untouched -- a source already +/// at `to`, and a source wider than `to`, which stays [`downmix`]'s refusal +/// (DEBT #46: only 1-ch -> N-ch became legal, not the reverse). +fn widen(samples: &mut Vec, from: usize, to: usize) { + if from != 1 || to <= 1 { + return; + } + let mut out = Vec::with_capacity(samples.len() * to); + for &s in samples.iter() { + out.extend(std::iter::repeat_n(s, to)); + } + *samples = out; +} + /// One source's AC-3 track: the same mp4 sample tables the AAC track is read /// from, decoded through `oxideav-ac3` and **downmixed to stereo by the decoder /// itself** (ATSC A/52 §7.8, `channels: Some(2)`), because one output device and @@ -3163,6 +3185,9 @@ fn run(mut w: Worker) { eprintln!("audio decode error at sample {id}: {e}"); break; } + // decoder.decode already folded to track.channels(); widen the mono + // case up to the timeline's own width before dividing by it below. + widen(&mut interleaved, usize::from(track.channels()), channels); let next = pos + (interleaved.len() / channels) as u64; if !emit( &mut interleaved, @@ -3318,6 +3343,7 @@ fn run_ac3( // The library's §7.8 downmix already handles everything but its own 2.1 // passthrough, which comes out at 3 and is folded here like any width. downmix(&mut interleaved, usize::from(track.channels)); + widen(&mut interleaved, usize::from(track.channels), channels); let next = pos + samples; if !emit( &mut interleaved, @@ -3382,6 +3408,7 @@ fn run_mkv_ac3( }; // ...and the 2.1 passthrough folded here, as on the mp4 side. downmix(&mut interleaved, usize::from(track.channels)); + widen(&mut interleaved, usize::from(track.channels), channels); let next = pos + samples; if !emit( &mut interleaved, @@ -3455,6 +3482,10 @@ fn run_sym( eprintln!("audio decode error at sample {pos}: {e}"); return true; } + // decoder.decode already folded to track.channels ([`SymDecoder::decode`], + // [`downmix`]); widen the mono case up to the timeline's own width + // before anything below divides by it. + widen(&mut interleaved, usize::from(track.channels), channels); let next = pos + (interleaved.len() / channels) as u64; at = Some(next); // Reborrowed per packet: the filter memory has to carry across the diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 90581e4..3443381 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -4065,6 +4065,16 @@ fn audio_matches_probed( if probe.map(|p| p.channels) == first.map(|p| p.channels) { return Ok(()); } + // A mono file joins a wider timeline: [`crate::audio::widen`] duplicates + // its one channel into every channel the timeline carries, the inverse of + // the [`crate::audio::downmix`] a wider file already folds through. The + // reverse -- a wider file onto a mono timeline -- stays refused below; + // nothing here narrows a source, only the decoder's own downmix does. + if let (Some(p), Some(b)) = (probe, first) { + if p.channels == 1 && b.channels > 1 { + return Ok(()); + } + } // ...and a *silent* file joins whatever the timeline is: it contributes // silence over its span, which is the very thing both audio paths already // synthesise for a gap ([`AudioSession::open_multi_streams_speed`] plays diff --git a/crates/engine/tests/audio_files.rs b/crates/engine/tests/audio_files.rs index 2484850..56074b8 100644 --- a/crates/engine/tests/audio_files.rs +++ b/crates/engine/tests/audio_files.rs @@ -693,3 +693,53 @@ fn a_project_scaffolded_from_an_audio_only_mp4_reloads() { drop(reloaded); let _ = std::fs::remove_dir_all(&dir); } + +/// DEBT #46: a mono source used to be refused outright by a stereo timeline +/// ("audio 1 ch does not match the timeline's 2 ch") -- the only layout gate +/// left after the 2026-08-12 convert-at-import work. It now up-mixes, +/// [`engine::audio::widen`] duplicating the one channel into both of a stereo +/// timeline's, mirroring the [`engine::audio::downmix`] a wider source already +/// folds through at the decoder's door. Reverted to before that fix, this test +/// fails at `import_and_place` itself: `place_stream_at` returns +/// `Err("audio 1 ch does not match the timeline's 2 ch")` instead of landing +/// the clip, because `widen` and the `audio_matches_probed` mono exception +/// did not exist. +#[test] +fn a_mono_source_up_mixes_onto_a_stereo_timeline() { + let dir = Scratch::dir("ve_audio_upmix"); + let mut session = open(asset("test_tone.mp3")); // a 3 s stereo timeline + // 44100 Hz, 1 ch AAC -- no rate to resample, only the layout in question. + import_and_place(&mut session, &asset("test_audio_only.mp4")); + assert_eq!(session.lane_clips(Lane::A1).len(), 2); + assert!((session.timeline_duration() - 6.0).abs() < 0.1); + + let wav = dir.join("upmix.wav"); + wait(&session.export_to_with( + &wav, + &ExportSettings { + format: Format::Wav, + ..ExportSettings::default() + }, + )) + .expect("wav export of the up-mixed timeline"); + let (rate, channels, samples) = decode(&wav); + assert_eq!((rate, channels), (44100, 2)); + + // Inside the mono clip's own span (the second half of the timeline): left + // and right must carry the identical signal `widen` duplicated, and it + // must not be silence -- a bug in `widen` could zero the buffer instead of + // filling it. + let start = (3.5 * f64::from(rate)) as usize; + let end = (5.5 * f64::from(rate)) as usize; + let mut peak = 0.0f32; + for frame in start..end { + let (l, r) = (samples[frame * 2], samples[frame * 2 + 1]); + assert!( + (l - r).abs() < 1e-6, + "frame {frame}: left {l} and right {r} must carry the up-mixed mono signal" + ); + peak = peak.max(l.abs()); + } + assert!(peak > 0.01, "the up-mixed span is silent: peak {peak}"); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/crates/engine/tests/playback.rs b/crates/engine/tests/playback.rs index 5dafddf..a275c2b 100644 --- a/crates/engine/tests/playback.rs +++ b/crates/engine/tests/playback.rs @@ -1199,13 +1199,12 @@ fn import_refuses_what_does_not_match() { .remove_source(&asset("test_tone_48k.wav"), 0) .expect("...and the row it made comes back off"); - // What is still refused, and all that is: one output device carries one - // layout, and a mono track is not the stereo pair this timeline plays. - let err = session + // A mono track is no longer a refusal either (DEBT #46): [`engine::audio`] + // duplicates its one channel into both of this stereo timeline's, the same + // way a wider file is already folded down to it. + session .import(&asset("test_ac3.mp4")) - .expect_err("a mono track cannot join a stereo timeline") - .to_string(); - assert_eq!(err, "audio 1 ch does not match the timeline's 2 ch"); + .expect("a mono track up-mixes onto a stereo timeline"); assert!( session.import(&asset("no_such_file.mp4")).is_err(), @@ -1214,8 +1213,8 @@ fn import_refuses_what_does_not_match() { assert_eq!(session.clip_spans().len(), 1, "an import places nothing"); assert_eq!( session.sources().len(), - rows + 1, - "only the silent file was taken in" + rows + 2, + "the silent file and the mono file were both taken in" ); assert!((session.timeline_duration() - 5.0).abs() < 1e-9); diff --git a/crates/engine/tests/saveload.rs b/crates/engine/tests/saveload.rs index 4cefad3..2e892e6 100644 --- a/crates/engine/tests/saveload.rs +++ b/crates/engine/tests/saveload.rs @@ -74,18 +74,15 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() { assert_eq!(loaded.sources(), session.sources()); assert_eq!(loaded.timeline_duration(), session.timeline_duration()); - // And the refusals, in the engine's own words: a stream that cannot share - // one output device with the timeline, and a file that is not on it. + // And the refusal left, in the engine's own words: a file that is not on + // this timeline. Its *layout* (stream 1 is mono, the timeline stereo) is no + // longer one (DEBT #46): the mono stream up-mixes onto both channels. let other = copy_in(&dir, "test_multiaudio.mp4"); let mut session = PlaybackSession::open(&other).expect("open the fixture"); session.set_gain(0.0); - // Its rate would be conformed now; its *layout* is what one output device - // cannot have two of, and the refusal says which. - let err = session + session .place_stream_at(0.0, &other, 1, None) - .expect_err("a mono stream cannot join a stereo timeline") - .to_string(); - assert_eq!(err, "audio 1 ch does not match the timeline's 2 ch"); + .expect("a mono stream up-mixes onto a stereo timeline"); assert!( session .place_stream_at(0.0, &media, 1, None) @@ -93,7 +90,11 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() { .to_string() .contains("not on this timeline") ); - assert_eq!(session.sources().len(), 1, "a refusal added a source"); + assert_eq!( + session.sources().len(), + 2, + "the mono stream landed, the not-a-source refusal added nothing more" + ); std::fs::remove_dir_all(&dir).expect("cleanup"); } @@ -814,8 +815,14 @@ fn malformed_files_are_numbered_errors_and_never_panics() { #[test] fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() { let dir = scratch("mismatch"); - copy_in(&dir, "test_av.mp4"); - std::fs::copy(asset("test_ac3.mp4"), dir.join("test_av2.mp4")).expect("substitute"); + // `test_av.mp4` -- source 0, which sets the timeline's norm -- is + // substituted with the mono fixture, so the norm this reload settles on is + // mono; `test_av2.mp4` stays the real, stereo file. A mono source onto a + // wider timeline up-mixes now (DEBT #46), but the reverse does not: a + // stereo source is still wider than the mono timeline it would join, and + // that is the one layout mismatch left to refuse. + std::fs::copy(asset("test_ac3.mp4"), dir.join("test_av.mp4")).expect("substitute"); + copy_in(&dir, "test_av2.mp4"); let path = dir.join("swapped.edith"); std::fs::write( &path, @@ -826,17 +833,15 @@ fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() { .err() .expect("a source that stopped matching must not open") .to_string(); - // The suffix is `import`'s own refusal, word for word. The substitute is - // mono, which is the shape of what is left: one output device carries one - // layout. A resolution of its own is placed on the project canvas, a frame - // rate of its own is read through `Rate`, a *sample* rate of its own is - // resampled at the decoder's door, a codec of its own opens its own decoder - // and a file with no sound plays silence -- none of those is a refusal any - // more. + // The suffix is `import`'s own refusal, word for word. A resolution of its + // own is placed on the project canvas, a frame rate of its own is read + // through `Rate`, a *sample* rate of its own is resampled at the decoder's + // door, a codec of its own opens its own decoder and a file with no sound + // plays silence -- none of those is a refusal any more. assert_eq!( err, format!( - "source {}: audio 1 ch does not match the timeline's 2 ch", + "source {}: audio 2 ch does not match the timeline's 1 ch", dir.join("test_av2.mp4").display() ) );