Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Ripple trim: a shorter head after an L cut keeps the earlier sound in place

Peet Guercio77d3b41a5c6a
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs
index 741a580db..b400afb58 100644
--- a/crates/edit/src/lib.rs
+++ b/crates/edit/src/lib.rs
@@ -672,13 +672,31 @@ pub fn ripple_trim_group(seq: &mut Sequence, clips: &[ClipId], edge: Edge, delta
let later = tr.items.iter().filter(|i| !clips.contains(&i.id) && i.start >= *from);
shifting_links.extend(later.filter_map(|i| i.link).filter(|l| !own_links.contains(l)));
}
+ // links that tie two or more clips together
+ let mut seen: Vec<u64> = Vec::new();
+ let mut paired: Vec<u64> = Vec::new();
+ for l in work.all_tracks().flat_map(|tr| tr.items.iter().filter_map(|i| i.link)) {
+ if seen.contains(&l) {
+ paired.push(l);
+ } else {
+ seen.push(l);
+ }
+ }
for (tid, from, member) in rippling {
let Some(tr) = work.track_mut(tid) else { continue };
let follows = |i: &TrackItem| i.start < from && i.end() > from && i.link.is_some_and(|l| shifting_links.contains(&l));
+ // An L cut: the sound of a clip before the cut runs on into the shortened head. Its clip stays
+ // put, so the sound stays with it and the L cut still ends where it did. (Unlinked material
+ // across the cut, a music bed, still refuses.)
+ let stays = |i: &TrackItem| {
+ edge == Edge::In
+ && i.start < from - Tick(1)
+ && i.link.is_some_and(|l| paired.contains(&l) && !shifting_links.contains(&l) && !own_links.contains(&l))
+ };
if !member && shift < Tick::ZERO {
let at = if edge == Edge::In { from - Tick(1) } else { from };
let closing = TimeRange::new(at + shift, -shift);
- if tr.items.iter().any(|i| i.range().overlaps(&closing) && !follows(i)) {
+ if tr.items.iter().any(|i| i.range().overlaps(&closing) && !follows(i) && !stays(i)) {
return Err(EditError::SyncLockConflict);
}
}
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index 069e30f99..90af2f31e 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -571,3 +571,42 @@ fn hostile_speeds_are_refused_or_clamped_never_overflow() {
}
}
}
+
+#[test]
+fn shorter_head_into_an_l_cut_keeps_the_earlier_sound_where_it_is() {
+ // V1: a [0,10), b [10,20), c [20,30). a's sound on sync-locked A1 runs 3 into b: [0,13) (an L
+ // cut). b loses 3 at its head: a never moves, so its sound stays and the L cut still ends at 13.
+ let mut fx = Fx::new();
+ let (v1, a1) = (fx.v(0), fx.a(0));
+ let a = fx.put(v1, 0, 10, 0);
+ let b = fx.put(v1, 10, 10, 0);
+ fx.put(v1, 20, 10, 0);
+ let a_sound = fx.put(a1, 0, 13, 0);
+ for x in [a, a_sound] {
+ fx.seq.find_item_mut(x).unwrap().1.link = Some(7);
+ }
+ let mut n = fx.next;
+ ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!((fx.spans(v1), fx.spans(a1)), (vec![(0, 10), (10, 7), (17, 10)], vec![(0, 13)]));
+ fx.seq.check().unwrap();
+
+ // later material on A1 moves up with the pictures; landing on the L cut's sound refuses
+ let later = fx.put(a1, 15, 5, 0);
+ assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));
+ assert_eq!(fx.spans(a1), vec![(0, 13), (15, 5)], "unchanged on failure");
+ fx.seq.find_item_mut(later).unwrap().1.start = f(20);
+ ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(2), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!((fx.spans(v1), fx.spans(a1)), (vec![(0, 10), (10, 5), (15, 10)], vec![(0, 13), (18, 5)]));
+
+ // unlinked material across the cut (a music bed) still refuses
+ let mut fx = Fx::new();
+ let (v1, a1) = (fx.v(0), fx.a(0));
+ fx.put(v1, 0, 10, 0);
+ let b = fx.put(v1, 10, 10, 0);
+ fx.put(a1, 0, 13, 0);
+ let mut n = fx.next;
+ assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));
+ // so does a link with no partner left
+ fx.seq.track_mut(a1).unwrap().items[0].link = Some(9);
+ assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));
+}
diff --git a/crates/engine/src/split_edit_ripple_trim_tests.rs b/crates/engine/src/split_edit_ripple_trim_tests.rs
index 0b824e158..249f40e9c 100644
--- a/crates/engine/src/split_edit_ripple_trim_tests.rs
+++ b/crates/engine/src/split_edit_ripple_trim_tests.rs
@@ -85,3 +85,31 @@ fn ripple_trim_is_refused_when_the_early_sound_has_no_room() {
assert_eq!(*s.project, *before, "a refused trim changes nothing");
assert_eq!(start(&s, second) - start(&s, sound), rate.tick_of(12));
}
+
+/// The mirror, an L cut: the first clip's sound runs 6 frames on into the second picture. Shortening
+/// the second clip's head was refused as a sync-lock conflict, because the first clip's sound crossed
+/// the cut. The first clip never moves, so its sound stays and the L cut still ends where it did.
+#[test]
+fn shortening_the_head_after_an_l_cut_keeps_the_earlier_sound_in_place() {
+ let mut s = Session::default();
+ let (first, second, sound) = j_cut(&mut s);
+ let rate = s.sequence_rate();
+ // undo the J cut, then run the first clip's sound (A1) 6 frames past its picture
+ s.execute("sequence.linkedSelection", json!({"on": false})).unwrap();
+ s.execute("timeline.trim", json!({"clip": sound.0, "edge": "in", "mode": "regular", "deltaFrames": 12})).unwrap();
+ let first_sound = s.active_sequence().unwrap().audio_tracks[0].items[0].id;
+ s.execute("timeline.trim", json!({"clip": first_sound.0, "edge": "out", "mode": "regular", "deltaFrames": 6})).unwrap();
+ s.execute("sequence.linkedSelection", json!({"on": true})).unwrap();
+ let end = |s: &Session, c: ClipId| s.active_sequence().unwrap().find_item(c).map(|(_, i)| i.end()).unwrap();
+ assert_eq!(end(&s, first_sound), start(&s, second) + rate.tick_of(6), "the L cut runs 6 frames into the second picture");
+ let (l_cut_ends, picture_at, picture_ends) = (end(&s, first_sound), start(&s, second), end(&s, second));
+ let before = s.project.clone();
+ s.execute("timeline.trim", json!({"clip": second.0, "edge": "in", "mode": "ripple", "deltaFrames": 3})).unwrap();
+ assert_eq!(end(&s, first_sound), l_cut_ends, "the L cut still ends where it did");
+ assert_eq!((start(&s, first), start(&s, first_sound)), (Tick::ZERO, Tick::ZERO));
+ assert_eq!((start(&s, second), end(&s, second)), (picture_at, picture_ends - rate.tick_of(3)), "the second picture is 3 frames shorter");
+ assert_eq!((start(&s, sound), end(&s, sound)), (start(&s, second), end(&s, second)), "its own sound with it");
+ s.active_sequence().unwrap().check().unwrap();
+ s.execute("edit.undo", json!({})).unwrap();
+ assert_eq!(*s.project, *before, "one undo step puts it back");
+}