diff --git a/crates/app/src/player/cards.rs b/crates/app/src/player/cards.rs index 2f29a1e..071d4f6 100644 --- a/crates/app/src/player/cards.rs +++ b/crates/app/src/player/cards.rs @@ -176,11 +176,34 @@ impl Player { cx.notify(); return; }; - let target = self - .selected - .anchor() - .filter(|(lane, _)| lane.kind == LaneKind::Video) - .or_else(|| session.video_clip_at(session.now())); + // A caption the hand is on names the clip it was pinned to, and the + // card opens on that; one in no group has no picture to grade and it + // says so, rather than falling to the playhead's clip behind the + // hand's back. + let target = match self.selected.anchor() { + // A caption the hand is on names the clip it was pinned to, and the + // grade is that clip's; one in no group has no picture to grade + // and it says so, rather than grading the playhead's clip behind + // the hand's back. + 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 GRADE — a caption has no picture; group it with a clip \ + first (ctrl-click both, then Group)" + .into(), + ); + cx.notify(); + return; + } + } + } + // A sound half has no picture of its own: the playhead's clip, as + // it always was. + Some((lane, _)) if lane.kind == LaneKind::Audio => session.video_clip_at(session.now()), + other => other.or_else(|| session.video_clip_at(session.now())), + }; match target { Some(clip) => { self.color_open = Some(clip); diff --git a/crates/app/src/player/view.rs b/crates/app/src/player/view.rs index 607a8a8..aad8834 100644 --- a/crates/app/src/player/view.rs +++ b/crates/app/src/player/view.rs @@ -130,11 +130,34 @@ impl Player { cx.notify(); return; }; - let target = self - .selected - .anchor() - .filter(|(lane, _)| lane.kind == LaneKind::Video) - .or_else(|| session.video_clip_at(session.now())); + // A caption the hand is on names the clip it was pinned to, and the + // policy is that clip's; one in no group has no picture to fit and it + // says so, rather than fitting the playhead's clip behind the hand's + // back. + let target = match self.selected.anchor() { + // A caption the hand is on names the clip it was pinned to, and the + // policy is that clip's; one in no group has no picture to fit and + // it says so, rather than fitting the playhead's clip behind the + // hand's back. + 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 FIT — a caption has no picture; group it with a clip \ + first (ctrl-click both, then Group)" + .into(), + ); + cx.notify(); + return; + } + } + } + // A sound half has no picture of its own: the playhead's clip, as + // it always was. + Some((lane, _)) if lane.kind == LaneKind::Audio => session.video_clip_at(session.now()), + other => other.or_else(|| session.video_clip_at(session.now())), + }; let Some((lane, idx)) = target else { self.notify_user("no clip under the playhead to fit".into()); cx.notify(); diff --git a/crates/app/src/tests/layout.rs b/crates/app/src/tests/layout.rs index 8d32672..da2c9a1 100644 --- a/crates/app/src/tests/layout.rs +++ b/crates/app/src/tests/layout.rs @@ -266,6 +266,42 @@ fn a_lane_row_is_a_fixed_header_and_a_bed_that_can_be_hit() { assert_eq!(lanes_h(2), 2. * LANE_H + 8.); } +/// A caption's box wears the rate its window plays at, derived off the +/// placement itself: unity placements -- including odd widths whose frames +/// came off `frames_of_us` rounding -- do not badge, and a re-rate does, with +/// the rate surviving the group coming apart. +#[test] +fn a_caption_box_wears_the_rate_its_window_plays_at() { + use crate::caption_rate; + + let sub = |frames: u32, out_us: i64| SubClip { + start: 0, + frames, + track: 0, + in_us: 0, + out_us, + link: None, + }; + // Unity, exact and rounded: neither is a re-timing. + assert_eq!(caption_rate(sub(300, 10_000_000), 30.), None); + assert_eq!( + caption_rate(sub(100, 3_333_333), 30.), + None, + "placement rounding is not a re-timing" + ); + // A 2x re-rate: a 5s span holding the same 10s of words. + assert_eq!( + caption_rate(sub(150, 10_000_000), 30.).map(|s| s.permille()), + Some(2000) + ); + // ...and a slowed one -- a 20s span for the same words -- which the badge + // keeps after any detach: the proportion is the placement's own. + assert_eq!( + caption_rate(sub(600, 10_000_000), 30.).map(|s| s.permille()), + Some(500) + ); +} + /// The selection itself: clicks in order, the anchor under the hand, the /// toggle that assembles a group, and the plain click that replaces it all. #[test] diff --git a/crates/app/src/tests/media.rs b/crates/app/src/tests/media.rs index adb7527..cc700ba 100644 --- a/crates/app/src/tests/media.rs +++ b/crates/app/src/tests/media.rs @@ -1096,12 +1096,14 @@ fn subtitles_arrive_beside_the_media_and_inside_it() { .map(|c| c.text.clone()) .collect() }; - // The placement keeps the track's own clock (it starts at frame 0 and - // holds the whole of it), so the words are back at the times the file - // wrote them -- the cut above moved the *carried* map, not this one. + // The placement holds five seconds of the track in a three-second box, + // so the words cross the plate by the placement's own proportion: the + // cue times compress with it (first line [0.5, 1.5) -> [0.3, 0.9), + // second [2, 3.25) -> [1.2, 1.95)) -- a unity placement would keep the + // file's own times, to the microsecond. assert_eq!(shown(0.7), ["first line"]); - assert!(shown(1.8).is_empty(), "between two cues the plate goes"); - assert_eq!(shown(2.5), ["second line\nwith a break"]); + assert!(shown(0.95).is_empty(), "between two cues the plate goes"); + assert_eq!(shown(1.5), ["second line\nwith a break"]); } /// One lane over the picture and never two ([`Player::active_sub_lane`]): which diff --git a/crates/app/src/timeline_math.rs b/crates/app/src/timeline_math.rs index 4e416e6..8e08446 100644 --- a/crates/app/src/timeline_math.rs +++ b/crates/app/src/timeline_math.rs @@ -147,7 +147,38 @@ impl Selection { self.picks.push(pick); } } +} +/// The rate a caption's box plays its window at, read off the placement +/// itself: how long the words are, against how much timeline they cross. A +/// clip's badge comes from its own `speed`; a caption has none, so its box +/// derives one -- and keeps it even after the group that re-timed it comes +/// apart, because the proportion is the placement's own. `None` at unity: +/// placement rounding leaves an odd-width window a frame or so off real time, +/// and a badge is for a box that was *re-timed*, not one that rounds. +pub(crate) fn caption_rate(sub: SubClip, fps: f64) -> Option { + if !(fps.is_finite() && fps > 0.0) || sub.window_us() <= 0 { + return None; + } + let span_us = f64::from(sub.frames) / fps * 1e6; + // The rate is the window over the span -- content per timeline, a clip's + // own `speed` in the same spelling: 10s of words crossing 5s of timeline + // is 2.00x. + let permille = (sub.window_us() as f64 / span_us * 1000.).round(); + // The gate is generous by two permille either way: placement rounding + // (`frames_of_us`) leaves an odd-width unity window a frame off real + // time, and that is not a re-timing. + ((permille - 1000.).abs() > 2.).then(|| { + Speed::from_permille( + permille.clamp( + f64::from(Speed::MIN.permille()), + f64::from(Speed::MAX.permille()), + ) as u16, + ) + }) +} + +impl Selection { pub(crate) fn len(&self) -> usize { self.picks.len() } diff --git a/crates/app/src/ui/timeline.rs b/crates/app/src/ui/timeline.rs index 676e67e..e257e89 100644 --- a/crates/app/src/ui/timeline.rs +++ b/crates/app/src/ui/timeline.rs @@ -1363,6 +1363,38 @@ impl Player { this.open_menu(lane, i, event.position, cx); }), ) + // The rate the caption's own window plays at, off + // the placement itself: a re-timed group leaves it + // on the box even after the group comes apart, and + // the badge is the clip's badge -- same place, same + // look -- on the visible slice, for the clip's + // reason (zoomed in, the box's own edge is off the + // screen and a badge at it with it). + .when_some( + caption_rate(shown_sub, self.fps).filter(|_| vis_w > 0.), + |d, rate| { + d.child( + div() + .absolute() + .top_0() + .left(px(vis_x)) + .w(px(vis_w)) + .flex() + .justify_end() + .overflow_hidden() + .child( + div() + .flex_none() + .px(px(3.)) + .rounded(px(3.)) + .bg(rgb(ACCENT_PRIMARY())) + .text_size(px(9.)) + .text_color(rgb(BG_RAISED())) + .child(format!("{rate}")), + ), + ) + }, + ) // The two strips a drag lengthens it by, on the same // rule a clip's are drawn by ([`trims`]). .children( diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 199d5d5..94b5e23 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -2009,8 +2009,16 @@ impl Project { continue; }; // Where the placement's own first microsecond lands on the - // timeline; every cue in the window is that far along from it. - let onto = |t: i64| (f64::from(s.start) / fps * 1e6).round() as i64 + (t - s.in_us); + // timeline, and how many microseconds of timeline one microsecond + // of the window is worth: the placement's own proportion, which a + // group's re-rate changes (the frames compress, the window does + // not -- [`write_speed`]'s law). At unity the ratio is 1.0 and + // every cue lands where it always did; at 2x the words cross the + // screen in half the time, cue for cue. + let per = f64::from(s.frames) / fps * 1e6 / (s.window_us().max(1) as f64); + let onto = |t: i64| { + (f64::from(s.start) / fps * 1e6 + (t - s.in_us) as f64 * per).round() as i64 + }; for cue in &track.cues { let (a, b) = (cue.start_us.max(s.in_us), cue.end_us.min(s.out_us)); if b <= a { @@ -5104,6 +5112,73 @@ mod tests { assert!(subs_sorted_disjoint(&p.lanes[p.index(s1).unwrap()].subs)); } + /// A grouped caption's cues re-time with its clip: the placement's window + /// crossed the timeline at its own proportion before any rate, and a + /// re-rate changes that proportion (the frames compress, the window does + /// not). A cue eight seconds into a window played at 2x crosses the screen + /// four seconds in -- and at unity every cue lands exactly where it always + /// did, byte for byte. + #[test] + fn a_grouped_captions_cues_re_time_with_its_clip() { + let track = SubtitleTrack { + path: FILE.into(), + track: None, + language: "eng".into(), + name: String::new(), + label: "eng".into(), + cues: vec![crate::subtitle::Cue { + start_us: 8_000_000, + end_us: 9_000_000, + text: "late line".into(), + image: None, + }], + bitmap: false, + refused: None, + }; + let mut p = Project::single(FILE, 300).with_subtitles(vec![track]); + let s1 = p.add_lane(LaneKind::Subtitle); + // Ten seconds of the track over ten seconds of timeline: unity. + p.place_sub( + s1, + 0, + SubClip { + start: 0, + frames: 300, + track: 0, + in_us: 0, + out_us: 10_000_000, + link: None, + }, + ) + .expect("placed"); + + // Unity: the cue sits at its own [8s, 9s), to the microsecond. + let unity = p.sub_lane_cues(s1, FPS); + assert_eq!( + unity + .iter() + .map(|c| (c.start_us, c.end_us, &*c.text)) + .collect::>(), + [(8_000_000, 9_000_000, "late line")], + "a unity placement maps exactly as it always did" + ); + + // Grouped with the clip and re-rated to 2x: the same window now crosses + // in half the time, so the cue at 8s of it lands 4s in. + p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); + p.set_speed(Lane::V1, 0, Speed::from_permille(2000)) + .expect("room for it"); + let re_timed = p.sub_lane_cues(s1, FPS); + assert_eq!( + re_timed + .iter() + .map(|c| (c.start_us, c.end_us)) + .collect::>(), + [(4_000_000, 4_500_000)], + "the cue crosses with the clip's new rate" + ); + } + /// The region's caption pieces re-time by mapped boundaries forced apart: /// rounding start and length independently can land two cues on one frame, /// and the artifact is clamped -- one frame each, in order, none past the