Skip to content

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

Ripple trim: a shorter head closes the stretch after the cut

Peet Guercio27124080bf82
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs
index 741a580db..8364e0aa0 100644
--- a/crates/edit/src/lib.rs
+++ b/crates/edit/src/lib.rs
@@ -550,6 +550,7 @@ pub fn trim(seq: &mut Sequence, clip: ClipId, edge: Edge, mode: TrimMode, delta:
let (tid, it) = seq.find_item(clip).ok_or(EditError::NoItem(clip))?;
let old_end = it.end();
let old_start = it.start;
+ let own_link = it.link;
let speed = it.speed.abs();
let mut work = seq.clone();
{
@@ -582,8 +583,12 @@ pub fn trim(seq: &mut Sequence, clip: ClipId, edge: Edge, mode: TrimMode, delta:
continue;
}
if tr.id == tid || tr.sync_lock {
- if tr.id != tid && shift < Tick::ZERO && !track_range_empty(tr, TimeRange::new(at + shift - Tick(if edge == Edge::In { 1 } else { 0 }), -shift))
- {
+ // the stretch that closes: a shorter head takes it from just after the cut, a
+ // shorter tail from just before it
+ let closing = if edge == Edge::In { TimeRange::new(old_start, -shift) } else { TimeRange::new(at + shift, -shift) };
+ // (the clip's own linked partners are the caller's to trim or leave)
+ let in_the_way = tr.items.iter().any(|i| i.range().overlaps(&closing) && (own_link.is_none() || i.link != own_link));
+ if tr.id != tid && shift < Tick::ZERO && in_the_way {
return Err(EditError::SyncLockConflict);
}
let from = if edge == Edge::In { old_start + Tick(1) } else { old_end };
@@ -676,9 +681,10 @@ pub fn ripple_trim_group(seq: &mut Sequence, clips: &[ClipId], edge: Edge, delta
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));
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)) {
+ // a shorter head closes the stretch just after the cut, a shorter tail the one before it
+ let closing = if edge == Edge::In { TimeRange::new(from - Tick(1), -shift) } else { TimeRange::new(from + shift, -shift) };
+ let own = |i: &TrackItem| i.link.is_some_and(|l| own_links.contains(&l));
+ if tr.items.iter().any(|i| i.range().overlaps(&closing) && !follows(i) && !own(i)) {
return Err(EditError::SyncLockConflict);
}
}
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index 069e30f99..189a36320 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -571,3 +571,33 @@ fn hostile_speeds_are_refused_or_clamped_never_overflow() {
}
}
}
+
+#[test]
+fn shorter_head_closes_the_stretch_after_the_cut_on_sync_locked_tracks() {
+ // V1: a [0,10), b [10,20). a's sound on sync-locked A1 runs up to the cut: [0,10). b loses 3 at
+ // its head; the stretch that closes is [10,13), where A1 is empty, so nothing refuses.
+ for group in [false, true] {
+ 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(v1, 20, 10, 0);
+ fx.put(a1, 0, 10, 0);
+ let later = fx.put(a1, 20, 5, 0);
+ let mut n = fx.next;
+ if group {
+ ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)).unwrap();
+ } else {
+ trim(&mut fx.seq, b, Edge::In, TrimMode::Ripple, 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, 10), (17, 5)]), "group {group}");
+ // material inside the stretch that closes still refuses
+ fx.seq.find_item_mut(later).unwrap().1.start = f(11);
+ let r = if group {
+ ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(2), &mut Fx::ctx(&mut n))
+ } else {
+ trim(&mut fx.seq, b, Edge::In, TrimMode::Ripple, f(2), &mut Fx::ctx(&mut n))
+ };
+ assert_eq!(r, Err(EditError::SyncLockConflict), "group {group}");
+ }
+}
diff --git a/crates/engine/src/split_edit_ripple_trim_tests.rs b/crates/engine/src/split_edit_ripple_trim_tests.rs
index 0b824e158..01b121b5d 100644
--- a/crates/engine/src/split_edit_ripple_trim_tests.rs
+++ b/crates/engine/src/split_edit_ripple_trim_tests.rs
@@ -85,3 +85,44 @@ 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));
}
+
+/// A shorter head on the J-cut take itself: the picture and its early sound lose the same stretch
+/// and the sound still leads by 12. It was refused as a sync-lock conflict because the check looked
+/// at the stretch before the cut, where the first clip's sound sits on A1, instead of the stretch
+/// the shorter head takes away.
+#[test]
+fn shorter_head_on_a_j_cut_take_shortens_picture_and_sound_together() {
+ let mut s = Session::default();
+ let (first, second, sound) = j_cut(&mut s);
+ let rate = s.sequence_rate();
+ let len = |s: &Session, c: ClipId| s.active_sequence().unwrap().find_item(c).map(|(_, i)| (i.duration, i.source_in)).unwrap();
+ let (picture_at, sound_at) = (start(&s, second), start(&s, sound));
+ let ((pic_len, pic_in), (snd_len, snd_in)) = (len(&s, second), len(&s, sound));
+ let first_before = len(&s, first);
+ let before = s.project.clone();
+ let r = s.execute("timeline.trim", json!({"clip": second.0, "edge": "in", "mode": "ripple", "deltaFrames": 8})).unwrap();
+ let by = rate.tick_of(8);
+ assert_eq!(r["delta"], json!(by.0));
+ assert_eq!((start(&s, second), start(&s, sound)), (picture_at, sound_at), "both stay where they start");
+ assert_eq!(len(&s, second), (pic_len - by, pic_in + by), "the picture loses 8 from its head");
+ assert_eq!(len(&s, sound), (snd_len - by, snd_in + by), "its sound loses the same 8");
+ assert_eq!(start(&s, second) - start(&s, sound), rate.tick_of(12), "the sound still leads by 12");
+ assert_eq!(len(&s, first), first_before, "the clip before is untouched");
+ s.active_sequence().unwrap().check().unwrap();
+ s.execute("edit.undo", json!({})).unwrap();
+ assert_eq!(*s.project, *before, "one undo step puts both back");
+}
+
+/// Material on a sync-locked track inside the stretch a shorter head takes away still refuses it.
+#[test]
+fn shorter_head_is_refused_when_a_sync_locked_track_has_material_in_the_stretch() {
+ let mut s = Session::default();
+ let (_, second, _) = j_cut(&mut s);
+ let rate = s.sequence_rate();
+ let tone = s.project.items.values().find(|i| !i.has_video() && i.has_audio() && i.as_media().is_some()).map(|i| i.id).unwrap();
+ s.execute("timeline.place", json!({"item": tone.0, "audioTrack": "A1", "frame": 50, "duration": rate.tick_of(24).0})).unwrap();
+ let before = s.project.clone();
+ let e = s.execute("timeline.trim", json!({"clip": second.0, "edge": "in", "mode": "ripple", "deltaFrames": 8})).unwrap_err();
+ assert!(e.to_string().contains("sync"), "{e}");
+ assert_eq!(*s.project, *before, "a refused trim changes nothing");
+}