From cd0df099d6c4a182c2482d0f0a5456b9041830b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 13 Aug 2026 07:43:47 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(timeline):=20a=20layer=20tak?= =?UTF-8?q?es=20the=20sound=20down=20with=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A file with audio let go over a further video track landed silent: the placement door put the picture on that lane alone, although its own words said 'its picture on V2, its sound on A2'. Both halves go down now, on the same frames and in one group, with the audio row added when the + V button left the project without one -- one snapshot, so one undo takes the pair and the row back together. --- crates/app/src/main.rs | 52 ++++++++++++------ crates/engine/src/playback.rs | 19 +++++-- crates/engine/src/project.rs | 100 +++++++++++++++++++++++++++++++++- 3 files changed, 149 insertions(+), 22 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index e10f7dd..3b1107b 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -11243,11 +11243,11 @@ mod tests { let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); session.set_gain(0.0); let v2 = session.add_lane(LaneKind::Video); - let path = session.sources()[0].path.clone(); + // A bare layer, through the one-lane door: a *drop* on V2 brings the + // file's sound down with it and would put a third clip over this span. + let whole = session.lane_clips(Lane::V1)[0]; assert!( - session - .place_stream_at(0.0, &path, 0, Some(v2)) - .expect("its own file is on this timeline"), + session.place_at(v2, 0.0, whole), "a layer covering the same frames as the take" ); @@ -11679,30 +11679,50 @@ mod tests { ); assert_eq!(session.lane_clips(v2).len(), 1, "the drop landed on V2"); assert_eq!(session.lane_clips(v2)[0].start, 30, "at the playhead"); + // ...with the sound it came with, on the row of its own the drop added: + // a file with audio dropped on a layer used to land silent. + let a2 = Lane::new(LaneKind::Audio, 1); + assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1, v2, a2]); + assert_eq!(session.lane_clips(a2), session.lane_clips(v2), "same take"); + assert_eq!( + session.lane_clips(v2)[0].link, + session.lane_clips(a2)[0].link, + "grouped, so a drag moves both" + ); // And nowhere else: the first pair is exactly as it was, one take each. assert_eq!(session.lane_clips(Lane::V1).len(), 1); assert_eq!(session.lane_clips(Lane::A1).len(), 1); + // ...and it is a project that saves and opens again as it stands: a + // grouped pair on a further row is what the file has to carry. + let dir = engine::scratch::Scratch::dir("ve_layer"); + let file = dir.join("layer.edith"); + session.save_project(&file).expect("save the project"); + let back = PlaybackSession::open_project(&file).expect("it loads as it stands"); + std::fs::remove_dir_all(&dir).ok(); + assert_eq!(back.lanes(), session.lanes()); + for lane in back.lanes() { + assert_eq!(back.lane_clips(lane), session.lane_clips(lane), "{lane:?}"); + } // Delete on the layer is a lift: it is laid over the timeline, so // closing a hole under it would drag the take beneath out of step with // it. The take on the first pair is still a take, and still ripples. assert!(!whole_take(&session, v2, 0)); assert!(whole_take(&session, Lane::V1, 0)); assert!(whole_take(&session, Lane::A1, 0)); + assert_eq!(a2.label(), "A2"); assert!(session.lift_clip(v2, 0)); assert!(session.lane_clips(v2).is_empty()); assert_eq!(session.lane_clips(Lane::V1).len(), 1, "V1 stayed put"); + assert_eq!( + session.lane_clips(a2).len(), + 1, + "a lift takes the half it names: the sound is still there to lift" + ); + assert!(session.lift_clip(a2, 0)); // A second audio lane used to be what an mp4 export could not write -- // it copied one AAC track and a mix is not a copy. It mixes now // (`export::copy_audio`), so no row is greyed by a count of lanes. - assert_eq!(format_refusal(&session, Format::Mp4), None); - let a2 = session.add_lane(LaneKind::Audio); - assert_eq!(a2.label(), "A2"); - assert!( - session - .place_stream_at(0.0, &path, 0, Some(a2)) - .expect("its own file is on this timeline") - ); for format in [Format::Mp4, Format::Av1, Format::Av1Mp4] { assert_eq!( format_refusal(&session, format), @@ -11711,10 +11731,10 @@ mod tests { ); } - // Undo, one edit at a time and backwards: the drop on A2, the lane A2 - // itself, the lift, the drop on V2, and last the lane V2 -- an added - // track is one step like every other edit. - for lanes in [4, 3, 3, 3, 2] { + // Undo, one edit at a time and backwards: the lift of the sound, the + // lift of the picture, the drop on V2 with the lane A2 it added, and + // last the lane V2 -- an added track is one step like every other edit. + for lanes in [4, 4, 3, 2] { assert!(session.undo()); assert_eq!(session.lanes().len(), lanes); } diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 6eb6745..90dcd14 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -1944,9 +1944,11 @@ impl PlaybackSession { /// Which lane it lands on is decided here, from the file and from `onto` -- /// the lane it was let go over, if it was let go over one at all: a file /// with a picture asked for by no lane, or for one of the first pair, is - /// pasted across `V1` and `A1` as a grouped take; asked for by any further - /// lane it is *placed* there alone, overwriting what it lands on and - /// rippling nothing. A file with no picture ([`crate::is_audio`]) is placed + /// pasted across `V1` and `A1` as a grouped take; asked for by a further + /// *video* lane it is *placed* there with its sound on that lane's own audio + /// row (`V2` -> `A2`, added if it is not there yet), overwriting what the two + /// halves land on and rippling nothing; asked for by a further audio lane it + /// is that file's sound alone. A file with no picture ([`crate::is_audio`]) is placed /// on the audio lane it was asked for, or on `A1`, and never on a video /// lane. A still image ([`crate::is_image`]) is its mirror: the video lane /// it was asked for or `V1`, never an audio one, and it goes down @@ -2019,11 +2021,20 @@ impl PlaybackSession { // A picture: the lane it was let go over, unless that is one of the // first pair. Those two are the grouped take a paste spans, and a // further lane is a layer of its own -- its picture on `V2`, its - // sound on `A2`. + // sound on `A2` (below). (false, Some(lane)) if lane.ord > 0 => Some(lane), (false, _) => None, }; Ok(match onto { + // A picture let go over a further *video* track lands there with the + // sound it came with -- `V2`'s picture, `A2`'s sound, one take and + // one undo step ([`Project::place_take`]). Dropped on a further + // audio lane it is the sound alone, which is what asking an audio + // row for an mp4 means. + Some(lane) if lane.kind == LaneKind::Video => { + let at = secs_to_frame(timeline_secs, self.meta.frame_rate); + self.edit(Dirty::Both, |p| p.place_take(lane, at, clip)) + } Some(lane) => self.place_at(lane, timeline_secs, clip), // The grouped take, at the frame it was let go on -- past the last // clip included, where the bed is black and the ghost was drawn. diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index b18ee5f..21a690e 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -1995,12 +1995,61 @@ impl Project { link: None, ..clip }; - let clips = self.lane_mut(lane).expect("checked above"); + self.insert_over(lane, clip); + true + } + + /// Place `clip` on `lane` **and its sound on that lane's own audio row** as + /// one grouped take -- what a picture let go over a further video track + /// means: `V2`'s picture plays on `V2` and its sound on `A2`, which is added + /// if it is not there yet (the `+ V` button adds a video row alone). Without + /// this half a file with sound landed on a layer silent, and the only way to + /// hear it was to drop it a second time on an audio lane. + /// + /// Both halves overwrite what they land on and neither ripples, exactly as + /// [`place`](Project::place) does -- a layer is laid *over* the timeline, so + /// nothing under it moves. One history snapshot for the pair, the added lane + /// included, so one [`Project::undo`] takes the whole placement back. The two + /// carry one link, which is what makes them one take to a drag, a trim and a + /// save. + /// + /// Refused, changing nothing, for an empty `clip` and for a lane that is not + /// there or is not a video lane: sound placed alone is + /// [`place`](Project::place)'s door, and a still has no sound to pair. + pub fn place_take(&mut self, lane: Lane, timeline_frame: u32, clip: Clip) -> bool { + if clip.out_frame <= clip.in_frame + || lane.kind != LaneKind::Video + || self.index(lane).is_none() + { + return false; + } + self.snapshot(); + // `A2` for `V2` -- and the rows between it and the last audio lane, since + // an ord names a lane only while every ord below it does. + while self.lane_count(LaneKind::Audio) <= lane.ord { + self.lanes.push(LaneData::new(LaneKind::Audio, Vec::new())); + } + let clip = Clip { + start: timeline_frame, + link: Some(self.new_link()), + ..clip + }; + for half in [lane, Lane::new(LaneKind::Audio, lane.ord)] { + self.insert_over(half, clip); + } + debug_assert!(links_are_consistent(&self.lanes).is_ok()); + true + } + + /// `clip` into `lane` at its own start, over whatever it lands on: the + /// insert both placement doors make, which is what keeps a lane sorted and + /// disjoint. The caller has checked that the lane is there. + fn insert_over(&mut self, lane: Lane, clip: Clip) { + let clips = self.lane_mut(lane).expect("the caller checked the lane"); clear(clips, clip.start, clip.end()); let idx = clips.partition_point(|c| c.start < clip.start); clips.insert(idx, clip); debug_assert!(sorted_disjoint(clips)); - true } /// Move the clip at `idx` of `from` onto `to` with its head at timeline @@ -3884,6 +3933,53 @@ mod tests { assert!(!p.place(Lane::V1, 0, clip(0, 7, 7, 0)), "empty clip"); } + /// A file with sound let go over a further video track: both halves go down, + /// on the same frames, and the audio row is added when the `+ V` button left + /// the project without one. One undo takes the lane and both clips back. + #[test] + fn a_take_placed_on_a_layer_lands_with_its_sound() { + let mut p = three(); + let v2 = p.add_lane(LaneKind::Video); + assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2], "no A2 yet"); + + assert!(p.place_take(v2, 4, clip(0, 100, 102, 0))); + let a2 = Lane::new(LaneKind::Audio, 1); + assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2, a2], "A2 was added"); + assert_eq!(p.lane(v2), p.lane(a2), "same frames, same source"); + assert_eq!(p.lane(v2)[0].start, 4); + assert_eq!(p.lane(v2)[0].end(), 6); + assert_eq!( + p.lane(v2)[0].link, + p.lane(a2)[0].link, + "one take, not two placements" + ); + assert!(p.lane(v2)[0].link.is_some()); + assert_eq!(shape(&p)[0], shape(&three())[0], "V1 untouched"); + assert_eq!(shape(&p)[1], shape(&three())[1], "and A1 with it"); + + // The pair overwrites its own two lanes and nothing else, exactly as a + // one-lane place does. + assert!(p.place_take(v2, 5, clip(0, 200, 203, 0))); + assert_eq!(p.lane(v2), p.lane(a2)); + assert_eq!(p.lane(v2).len(), 2, "the tail of the first is still there"); + assert_eq!(shape(&p)[0], shape(&three())[0], "V1 still untouched"); + + assert!(p.undo(), "one step for the second pair"); + assert_eq!(p.lane(v2).len(), 1); + assert!(p.undo(), "one step for the first pair *and* its lane"); + assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2]); + assert!(p.lane(v2).is_empty()); + + // Refusals cost nothing: sound placed alone is `place`'s door. + assert!(!p.place_take(Lane::A1, 0, clip(0, 100, 102, 0)), "audio lane"); + assert!(!p.place_take(v2, 0, clip(0, 7, 7, 0)), "empty clip"); + assert!( + !p.place_take(Lane::new(LaneKind::Video, 9), 0, clip(0, 1, 2, 0)), + "a lane that is not there" + ); + assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2]); + } + /// The drag between tracks: let go at the frames it already covers the clip /// keeps them, one undo takes it back, and every way it can be refused /// leaves the project untouched.