From b801cf59a189909e2989bcb28a265f97bc676841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 15 Aug 2026 21:55:08 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(timeline):=20tempo=20ops=20car?= =?UTF-8?q?ry=20the=20captions=20of=20a=20group?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A group's caption now travels with everything that re-times it. A rate set on any member re-times the caption members by the ratio the held clip's own frames take -- the span on the timeline compresses about the held clip's head while the words keep their own window -- and a stretch that would run a caption into its neighbour is refused in the clip arm's words. The silence scan's speed-up shrinks the caption pieces inside its regions and slides the rest up with the clips; its Take scope carries the caption's lane so the cut does too; and the scope law names a caption half left outside a scoped cut, in the words it always used for a clip's. The cards stop dead-ending on a caption in hand: speed and silence resolve a caption anchor to the media half of its group (the picture and the sound respectively) and say so out loud -- with the how-to -- when there is no media behind the words; the equalizer takes the group's sound the same way. Colour and Fit are untouched: a caption anchor retargets to the playhead's picture exactly as an audio anchor always has. --- crates/app/src/export_ui.rs | 24 +++ crates/app/src/player/cards.rs | 70 +++++++- crates/app/src/tests/editing.rs | 30 ++++ crates/engine/src/project.rs | 303 +++++++++++++++++++++++++++++--- 4 files changed, 401 insertions(+), 26 deletions(-) diff --git a/crates/app/src/export_ui.rs b/crates/app/src/export_ui.rs index 1d63619..f9bfbdb 100644 --- a/crates/app/src/export_ui.rs +++ b/crates/app/src/export_ui.rs @@ -271,6 +271,30 @@ pub(crate) fn audio_half(session: &PlaybackSession, (lane, idx): (Lane, usize)) .unwrap_or((lane, idx)) } +/// The member of a caption's group on a media lane of the wanted kind: what a +/// card that is about media opens on when the hand is on the caption pinned to +/// it. `None` for a caption in no group with clips -- there are no pictures or +/// sound behind the words to be reaching at, and the card that opened anyway +/// would be a card of settings nothing plays. +pub(crate) fn caption_media_half( + session: &PlaybackSession, + (lane, idx): (Lane, usize), + kind: LaneKind, +) -> Option<(Lane, usize)> { + let link = session.sub_lane(lane).get(idx).and_then(|s| s.link)?; + session + .lanes() + .into_iter() + .filter(|l| l.kind == kind) + .find_map(|l| { + session + .lane_clips(l) + .iter() + .position(|c| c.link == Some(link)) + .map(|i| (l, i)) + }) +} + pub(crate) fn color_key(key: &str) -> Option { Some(match key { ESCAPE => ColorKey::Close, diff --git a/crates/app/src/player/cards.rs b/crates/app/src/player/cards.rs index 0610084..2f29a1e 100644 --- a/crates/app/src/player/cards.rs +++ b/crates/app/src/player/cards.rs @@ -309,11 +309,32 @@ impl Player { cx.notify(); return; }; - match self + // A caption the hand is on names the clip it was pinned to, and the + // card opens on that. A caption in no group has no rate to change and + // nothing behind it that does: said out loud, rather than a card that + // reads 1.00x and writes nothing. + let anchor = match self .selected .anchor() .or_else(|| session.video_clip_at(session.now())) { + Some((lane, idx)) if lane.kind == LaneKind::Subtitle => { + match caption_media_half(session, (lane, idx), LaneKind::Video) { + Some(half) => Some(half), + None => { + self.notify_user( + "NOTHING TO RE-TIME — a caption has no speed of its own; group it \ + with a clip first (ctrl-click both, then Group)" + .into(), + ); + cx.notify(); + return; + } + } + } + other => other, + }; + match anchor { Some(clip) => { self.speed_open = Some(clip); self.speed_dragging = false; @@ -452,12 +473,32 @@ impl Player { cx.notify(); return; }; - match self + // A caption the hand is on names the sound its group plays: the card + // scans that half. A caption with no sound behind it -- alone, or + // grouped with clips that have none -- is a scan with nothing to hear, + // and it says so instead of returning without a word. + let anchor = match self .selected .anchor() .or_else(|| session.video_clip_at(session.now())) - .map(|clip| audio_half(session, clip)) { + Some((lane, idx)) if lane.kind == LaneKind::Subtitle => { + match caption_media_half(session, (lane, idx), LaneKind::Audio) { + Some(half) => Some(half), + None => { + self.notify_user( + "NOTHING TO SCAN — a caption has no sound of its own; group it with \ + the take's sound first (ctrl-click both, then Group)" + .into(), + ); + cx.notify(); + return; + } + } + } + other => other, + }; + match anchor.map(|clip| audio_half(session, clip)) { Some((lane, idx)) => { let found = self.session.as_ref().and_then(|session| { let clip = *session.lane_clips(lane).get(idx)?; @@ -783,7 +824,12 @@ impl Player { .lanes() .into_iter() .filter(|&l| { - l == lane || session.lane_clips(l).iter().any(|c| c.link == Some(id)) + l == lane + || session.lane_clips(l).iter().any(|c| c.link == Some(id)) + // The caption pinned to the take travels with it: + // the ripple closes its lane too, or the words + // would stay put while the clip under them moved. + || session.sub_lane(l).iter().any(|s| s.link == Some(id)) }) .collect(), }, @@ -961,7 +1007,19 @@ impl Player { if self.exporting().is_some() { return; } - let refusal = match (self.selected.anchor(), &self.session) { + // A caption the hand is on names the sound its group plays, and the + // card opens on that; one in no group has no sound to equalize and + // falls to the lane refusal below, in the words it always used. + let anchor = match (self.selected.anchor(), self.session.as_ref()) { + (Some((lane, idx)), Some(session)) + if lane.kind == LaneKind::Subtitle => + { + caption_media_half(session, (lane, idx), LaneKind::Audio) + .or(self.selected.anchor()) + } + _ => self.selected.anchor(), + }; + let refusal = match (anchor, &self.session) { (_, None) => Some("NO TIMELINE — open a file first".to_string()), (None, _) => Some(format!( "NOTHING SELECTED — click an audio clip or press {}, then ask again", @@ -977,7 +1035,7 @@ impl Player { cx.notify(); return; } - let (lane, idx) = self.selected.anchor().expect("checked above"); + let (lane, idx) = anchor.expect("checked above"); let session = self.session.as_ref().expect("checked above"); // What the clip already plays through, or the flat default -- so the // card opens on the curve that is in force and a reopen shows the last diff --git a/crates/app/src/tests/editing.rs b/crates/app/src/tests/editing.rs index 1080dcc..eda76b8 100644 --- a/crates/app/src/tests/editing.rs +++ b/crates/app/src/tests/editing.rs @@ -479,6 +479,36 @@ fn a_caption_grouped_with_a_clip_pairs_it_for_the_whole_take_question() { ); } +/// A caption anchor names the media half of its group: the video member for +/// the cards that are about the picture, the audio member for the ones about +/// the sound. A caption in no group names nothing -- which is what the cards' +/// new refusals say out loud. +#[test] +fn a_caption_anchor_names_the_media_half_of_its_group() { + use crate::caption_media_half; + + let (mut session, lane) = with_subtitle_lane(); + session + .place_sub(lane, 0, one_second(0)) + .expect("the caption goes down"); + // Ungrouped: no pictures and no sound behind the words. + assert_eq!(caption_media_half(&session, (lane, 0), LaneKind::Video), None); + assert_eq!(caption_media_half(&session, (lane, 0), LaneKind::Audio), None); + // Grouped with the take -- whose sound rides along unasked -- both halves + // answer by lane and index. + session + .group_all(&[(Lane::V1, 0), (lane, 0)]) + .expect("a clip and a caption are a group"); + assert_eq!( + caption_media_half(&session, (lane, 0), LaneKind::Video), + Some((Lane::V1, 0)) + ); + assert_eq!( + caption_media_half(&session, (lane, 0), LaneKind::Audio), + Some((Lane::A1, 0)) + ); +} + /// The manual group's oracle question: a selection of placements, one per lane. /// Two picks on two lanes is a group waiting to happen; a lane picked twice is /// the one thing a group may never hold; a single pick is the partner hunt. diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 7a26cf5..7b38982 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -2214,8 +2214,25 @@ impl Project { return Err(format!("there is no clip {idx} on {}", lane.label()).into()); }; let labels = handles(&self.lanes); - // Media members only: a caption has no rate to change, and what the - // id binds it to -- a move, a trim, a delete -- travels by frames. + // The clip the hand is holding is the group's clock: everything else + // in the group, caption members included, re-times by the ratio its new + // rate gives its own frames. The clips take the rate itself; a caption + // has none, so it takes the *ratio* -- its span on the timeline + // compresses or stretches about the held clip's head, while the words + // it reads keep their own timing ([`write_sub_edge`]'s law: the + // timeline moves, the track does not). + let Some(held) = self.lane(lane).get(idx).copied() else { + return Err(format!("there is no clip {idx} on {}", lane.label()).into()); + }; + let (old, new) = (f64::from(held.frames()), f64::from(speed.frames(held.len()))); + let ratio = new / old.max(1.); + let retimed = |s: SubClip| SubClip { + start: ((f64::from(s.start) - f64::from(held.start)) * ratio + f64::from(held.start)) + .round() + .clamp(0., f64::from(u32::MAX)) as u32, + frames: (f64::from(s.frames) * ratio).round().clamp(1., f64::from(u32::MAX)) as u32, + ..s + }; for &(l, i) in &members.clips { let clips = &self.lanes[l].clips; let mut moved = clips[i]; @@ -2233,6 +2250,29 @@ impl Project { .into()); } } + // ...and the captions, against their own lanes' captions: a stretch + // that runs one into its neighbour is refused for the clip arm's + // reason -- a lane may not overlap itself -- and by name like it. + let subs: Vec<(usize, usize, SubClip)> = members + .subs + .iter() + .map(|&(l, i)| (l, i, retimed(self.lanes[l].subs[i]))) + .collect(); + for &(l, i, moved) in &subs { + for (j, other) in self.lanes[l].subs.iter().enumerate() { + if j != i && moved.start < other.end() && other.start < moved.end() { + return Err(format!( + "at {speed} the {} caption at frame {} would run to frame {} and the next \ + starts at {}: move it along, or trim this one first", + labels[l].label(), + self.lanes[l].subs[i].start, + moved.end(), + other.start + ) + .into()); + } + } + } if snapshot { self.snapshot(); } @@ -2240,6 +2280,10 @@ impl Project { self.lanes[l].clips[i].speed = speed; debug_assert!(sorted_disjoint(&self.lanes[l].clips)); } + for (l, i, moved) in subs { + self.lanes[l].subs[i] = moved; + debug_assert!(subs_sorted_disjoint(&self.lanes[l].subs)); + } debug_assert!(links_are_consistent(&self.lanes).is_ok()); Ok(()) } @@ -3719,26 +3763,50 @@ impl Project { /// not move, so it is not this rule's business. fn scope_holds_takes_whole(&self, on: &[usize], from: u32) -> crate::Result<()> { let labels = handles(&self.lanes); + // The halves of a group are on either of a lane's lists, and the + // refusal names whichever sits outside the scope -- a clip like it + // always has, and a caption now that a group may hold one. + let halves = |start: u32, link: Option, what: &str| -> crate::Result<()> { + let Some(id) = link else { + return Ok(()); + }; + for (j, data) in self.lanes.iter().enumerate() { + if on.contains(&j) { + continue; + } + let clip = data.clips.iter().find(|o| o.link == Some(id)); + let sub = data.subs.iter().find(|s| s.link == Some(id)); + if let Some(half) = clip { + return Err(format!( + "the {what} at frame {start} is one take with the {} clip at frame {}: \ + moving one track of a take would pull it apart -- take the whole take, \ + or detach them first", + labels[j].label(), + half.start + ) + .into()); + } + if let Some(half) = sub { + return Err(format!( + "the {what} at frame {start} is one take with the {} caption at frame {}: \ + moving one track of a take would pull it apart -- take the whole take, \ + or detach them first", + labels[j].label(), + half.start + ) + .into()); + } + } + Ok(()) + }; for &i in on { for c in self.lanes[i].clips.iter().filter(|c| c.end() > from) { - let Some(id) = c.link else { continue }; - for (j, data) in self.lanes.iter().enumerate() { - if on.contains(&j) { - continue; - } - if let Some(half) = data.clips.iter().find(|o| o.link == Some(id)) { - return Err(format!( - "the {} clip at frame {} is one take with the {} clip at frame {}: \ - moving one track of a take would pull it apart -- take the whole take, \ - or detach them first", - labels[i].label(), - c.start, - labels[j].label(), - half.start - ) - .into()); - } - } + halves(c.start, c.link, &format!("{} clip", labels[i].label()))?; + } + // A caption before the first cut does not move, so it is not this + // rule's business -- exactly the clip test's own line. + for s in self.lanes[i].subs.iter().filter(|s| s.end() > from) { + halves(s.start, s.link, &format!("{} caption", labels[i].label()))?; } } Ok(()) @@ -3864,6 +3932,8 @@ impl Project { (None, None) => continue, (None, Some((_, after))) => after, }; + // The region's new length, on every scoped lane alike. + let after = shrunk; let Some(delta) = len.checked_sub(shrunk) else { self.undo(); return Err(format!( @@ -3881,6 +3951,21 @@ impl Project { } } debug_assert!(sorted_disjoint(&self.lanes[l].clips)); + // The captions on a scoped lane travel with the region's time: + // a piece inside it plays in `after` frames instead of `len` + // -- same words on less timeline, its window untouched, for + // [`write_speed`]'s reason -- and everything behind the region + // slides up by what the region gave back. + let share = f64::from(after) / f64::from(len).max(1.); + for s in &mut self.lanes[l].subs { + if s.start >= at + len { + s.start -= delta; + } else if s.start >= at && s.end() <= at + len { + s.start = at + (f64::from(s.start - at) * share).round() as u32; + s.frames = (f64::from(s.frames) * share).round().max(1.) as u32; + } + } + debug_assert!(subs_sorted_disjoint(&self.lanes[l].subs)); } } debug_assert!(links_are_consistent(&self.lanes).is_ok()); @@ -4873,6 +4958,184 @@ mod tests { assert!(links_are_consistent(&p.lanes).is_ok()); } + /// A rate re-times the caption members of the group by the ratio it gives + /// the held clip: the span on the timeline compresses about the held clip's + /// head, the words' own window untouched -- and one undo puts it back. + #[test] + fn a_rate_carries_the_group_caption_by_the_held_clips_ratio() { + let caption = |start: u32, frames: u32| SubClip { + start, + frames, + track: 0, + in_us: 0, + out_us: i64::from(frames) * 1_000_000, + link: None, + }; + let track = || SubtitleTrack { + path: FILE.into(), + track: None, + language: "eng".into(), + name: String::new(), + label: "eng".into(), + cues: Vec::new(), + bitmap: false, + refused: None, + }; + let grouped = || { + let mut p = Project::single(FILE, 90).with_subtitles(vec![track()]); + let s1 = p.add_lane(LaneKind::Subtitle); + p.place_sub(s1, 0, caption(0, 90)).expect("placed"); + p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); + (p, s1) + }; + + // An aligned take, doubled: the caption plays in half the frames. + let (mut p, s1) = grouped(); + p.set_speed(Lane::V1, 0, Speed::from_permille(2000)) + .expect("room for it"); + assert_eq!(p.lane(Lane::V1)[0].frames(), 45); + let s = p.sub_lane(s1)[0]; + assert_eq!((s.start, s.frames), (0, 45), "same words, half the timeline"); + assert_eq!((s.in_us, s.out_us), (0, 90_000_000), "the window is untouched"); + assert!(links_are_consistent(&p.lanes).is_ok()); + assert!(p.undo()); + assert_eq!(p.sub_lane(s1)[0].frames, 90, "one undo puts it back"); + + // An offset group, slowed to half: the caption's *offset from the held + // clip's head* is what scales -- it sits 30 frames in at 1x, 60 at + // 0.5x, and still ends where the clip does. + let (mut p, s1) = grouped(); + p.lift_sub(s1, 0); + p.place_sub(s1, 30, caption(30, 60)).expect("placed"); + p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); + p.set_speed(Lane::V1, 0, Speed::from_permille(500)) + .expect("room for it"); + assert_eq!(p.lane(Lane::V1)[0].frames(), 180); + let s = p.sub_lane(s1)[0]; + assert_eq!((s.start, s.frames), (60, 120), "the offset doubles with the clip"); + assert_eq!(s.end(), p.lane(Lane::V1)[0].end(), "still ends with the clip"); + + // A stretch that runs the caption into its neighbour is refused, by + // lane and frame, and costs no undo step. + let (mut p, s1) = grouped(); + p.lift_sub(s1, 0); + p.place_sub(s1, 0, caption(0, 90)).expect("placed"); + p.place_sub(s1, 95, caption(95, 5)).expect("the neighbour"); + p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); + let history = p.history.len(); + let err = p + .set_speed(Lane::V1, 0, Speed::from_permille(500)) + .unwrap_err() + .to_string(); + assert!( + err.contains("the S1 caption at frame 0 would run to frame 180 and the next starts at 95"), + "{err}" + ); + assert_eq!(p.history.len(), history, "a refusal costs no undo step"); + } + + /// The silence scan's speed-up re-times the captions on a scoped lane with + /// everything else: the piece inside the region plays in the region's new + /// frames, and what sits behind the region slides up with the clips. + #[test] + fn speeding_a_region_carries_the_captions_on_the_scoped_lanes() { + let caption = |start: u32, frames: u32| SubClip { + start, + frames, + track: 0, + in_us: 0, + out_us: i64::from(frames) * 1_000_000, + link: None, + }; + let track = || SubtitleTrack { + path: FILE.into(), + track: None, + language: "eng".into(), + name: String::new(), + label: "eng".into(), + cues: Vec::new(), + bitmap: false, + refused: None, + }; + let mut p = Project::single(FILE, 90).with_subtitles(vec![track()]); + let s1 = p.add_lane(LaneKind::Subtitle); + // Words over the region [30, 60) and words behind it. + p.place_sub(s1, 30, caption(30, 30)).expect("placed"); + p.place_sub(s1, 60, caption(60, 30)).expect("placed"); + + // The region [30, 60) plays at 4x: 30 frames become 8, everything + // behind slides up 22. + // The scope is what the card's Take row says, caption lane included. + p.speed_regions( + &[(30, 30)], + Speed::from_permille(4000), + &[Lane::V1, Lane::A1, s1], + ) + .expect("the region speeds up"); + let subs = p.sub_lane(s1); + assert_eq!( + (subs[0].start, subs[0].frames), + (30, 8), + "the piece inside plays in the region's new frames" + ); + assert_eq!( + (subs[1].start, subs[1].frames), + (38, 30), + "the piece behind slides up by what the region gave back" + ); + assert!(subs_sorted_disjoint(&p.lanes[p.index(s1).unwrap()].subs)); + } + + /// The scope law names a caption half too: a scoped cut that would move one + // track of a group whose caption sits outside the scope is refused. + #[test] + fn the_scope_law_refuses_a_caption_half_outside_the_scope() { + let caption = |start: u32, frames: u32| SubClip { + start, + frames, + track: 0, + in_us: 0, + out_us: i64::from(frames) * 1_000_000, + link: None, + }; + let track = || SubtitleTrack { + path: FILE.into(), + track: None, + language: "eng".into(), + name: String::new(), + label: "eng".into(), + cues: Vec::new(), + bitmap: false, + refused: None, + }; + let mut p = Project::single(FILE, 90).with_subtitles(vec![track()]); + let s1 = p.add_lane(LaneKind::Subtitle); + p.place_sub(s1, 0, caption(0, 90)).expect("placed"); + p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); + + // The subtitle lane is not in the scope, so the group's caption half + // sits outside it: refused, in the law's own words. + let err = p + .cut_regions(&[(10, 5)], &[Lane::V1, Lane::A1]) + .unwrap_err() + .to_string(); + assert!( + err.contains("is one take with the S1 caption at frame 0"), + "{err}" + ); + + // ...and with the caption's lane in the scope, the cut carries it: + // the words over the hole go, and the words behind slide up. + p.cut_regions(&[(10, 5)], &[Lane::V1, Lane::A1, s1]) + .expect("the whole group is in the scope"); + let subs = p.sub_lane(s1); + assert_eq!( + subs.iter().map(|s| (s.start, s.frames)).collect::>(), + [(0, 10), (10, 75)], + "the caption is cut and shifted with the clips" + ); + } + /// The caption a hand groups: `group_all` over clips *and* a caption, the /// mates riding along, the offsets kept, the refusals in words. #[test]