Captions keep their own clock through a re-rate
Browse files at this commitf8f5c466d39e93540236bf3b837b864987b2c550Parent: 3a0448f- feat
- captions keep their own clock through a re-rate
7 changed files +239 -17
- crates/app/src/player/cards.rs +28 -5
- crates/app/src/player/view.rs +28 -5
- crates/app/src/tests/layout.rs +36 -0
- crates/app/src/tests/media.rs +7 -5
- crates/app/src/timeline_math.rs +31 -0
- crates/app/src/ui/timeline.rs +32 -0
- crates/engine/src/project.rs +77 -2
crates/app/src/player/cards.rsmodified+28 -5
| @@ -176,11 +176,34 @@ impl Player { | |||
| 176 | cx.notify(); | 176 | cx.notify(); |
| 177 | return; | 177 | return; |
| 178 | }; | 178 | }; |
| 179 | - let target = self | 179 | + // A caption the hand is on names the clip it was pinned to, and the |
| 180 | - .selected | 180 | + // card opens on that; one in no group has no picture to grade and it |
| 181 | - .anchor() | 181 | + // says so, rather than falling to the playhead's clip behind the |
| 182 | - .filter(|(lane, _)| lane.kind == LaneKind::Video) | 182 | + // hand's back. |
| 183 | - .or_else(|| session.video_clip_at(session.now())); | 183 | + let target = match self.selected.anchor() { |
| 184 | + // A caption the hand is on names the clip it was pinned to, and the | ||
| 185 | + // grade is that clip's; one in no group has no picture to grade | ||
| 186 | + // and it says so, rather than grading the playhead's clip behind | ||
| 187 | + // the hand's back. | ||
| 188 | + Some((lane, idx)) if lane.kind == LaneKind::Subtitle => { | ||
| 189 | + match caption_media_half(session, (lane, idx), LaneKind::Video) { | ||
| 190 | + Some(half) => Some(half), | ||
| 191 | + None => { | ||
| 192 | + self.notify_user( | ||
| 193 | + "NOTHING TO GRADE — a caption has no picture; group it with a clip \ | ||
| 194 | + first (ctrl-click both, then Group)" | ||
| 195 | + .into(), | ||
| 196 | + ); | ||
| 197 | + cx.notify(); | ||
| 198 | + return; | ||
| 199 | + } | ||
| 200 | + } | ||
| 201 | + } | ||
| 202 | + // A sound half has no picture of its own: the playhead's clip, as | ||
| 203 | + // it always was. | ||
| 204 | + Some((lane, _)) if lane.kind == LaneKind::Audio => session.video_clip_at(session.now()), | ||
| 205 | + other => other.or_else(|| session.video_clip_at(session.now())), | ||
| 206 | + }; | ||
| 184 | match target { | 207 | match target { |
| 185 | Some(clip) => { | 208 | Some(clip) => { |
| 186 | self.color_open = Some(clip); | 209 | self.color_open = Some(clip); |
crates/app/src/player/view.rsmodified+28 -5
| @@ -130,11 +130,34 @@ impl Player { | |||
| 130 | cx.notify(); | 130 | cx.notify(); |
| 131 | return; | 131 | return; |
| 132 | }; | 132 | }; |
| 133 | - let target = self | 133 | + // A caption the hand is on names the clip it was pinned to, and the |
| 134 | - .selected | 134 | + // policy is that clip's; one in no group has no picture to fit and it |
| 135 | - .anchor() | 135 | + // says so, rather than fitting the playhead's clip behind the hand's |
| 136 | - .filter(|(lane, _)| lane.kind == LaneKind::Video) | 136 | + // back. |
| 137 | - .or_else(|| session.video_clip_at(session.now())); | 137 | + let target = match self.selected.anchor() { |
| 138 | + // A caption the hand is on names the clip it was pinned to, and the | ||
| 139 | + // policy is that clip's; one in no group has no picture to fit and | ||
| 140 | + // it says so, rather than fitting the playhead's clip behind the | ||
| 141 | + // hand's back. | ||
| 142 | + Some((lane, idx)) if lane.kind == LaneKind::Subtitle => { | ||
| 143 | + match caption_media_half(session, (lane, idx), LaneKind::Video) { | ||
| 144 | + Some(half) => Some(half), | ||
| 145 | + None => { | ||
| 146 | + self.notify_user( | ||
| 147 | + "NOTHING TO FIT — a caption has no picture; group it with a clip \ | ||
| 148 | + first (ctrl-click both, then Group)" | ||
| 149 | + .into(), | ||
| 150 | + ); | ||
| 151 | + cx.notify(); | ||
| 152 | + return; | ||
| 153 | + } | ||
| 154 | + } | ||
| 155 | + } | ||
| 156 | + // A sound half has no picture of its own: the playhead's clip, as | ||
| 157 | + // it always was. | ||
| 158 | + Some((lane, _)) if lane.kind == LaneKind::Audio => session.video_clip_at(session.now()), | ||
| 159 | + other => other.or_else(|| session.video_clip_at(session.now())), | ||
| 160 | + }; | ||
| 138 | let Some((lane, idx)) = target else { | 161 | let Some((lane, idx)) = target else { |
| 139 | self.notify_user("no clip under the playhead to fit".into()); | 162 | self.notify_user("no clip under the playhead to fit".into()); |
| 140 | cx.notify(); | 163 | cx.notify(); |
crates/app/src/tests/layout.rsmodified+36 -0
| @@ -266,6 +266,42 @@ fn a_lane_row_is_a_fixed_header_and_a_bed_that_can_be_hit() { | |||
| 266 | assert_eq!(lanes_h(2), 2. * LANE_H + 8.); | 266 | assert_eq!(lanes_h(2), 2. * LANE_H + 8.); |
| 267 | } | 267 | } |
| 268 | 268 | ||
| 269 | +/// A caption's box wears the rate its window plays at, derived off the | ||
| 270 | +/// placement itself: unity placements -- including odd widths whose frames | ||
| 271 | +/// came off `frames_of_us` rounding -- do not badge, and a re-rate does, with | ||
| 272 | +/// the rate surviving the group coming apart. | ||
| 273 | +#[test] | ||
| 274 | +fn a_caption_box_wears_the_rate_its_window_plays_at() { | ||
| 275 | + use crate::caption_rate; | ||
| 276 | + | ||
| 277 | + let sub = |frames: u32, out_us: i64| SubClip { | ||
| 278 | + start: 0, | ||
| 279 | + frames, | ||
| 280 | + track: 0, | ||
| 281 | + in_us: 0, | ||
| 282 | + out_us, | ||
| 283 | + link: None, | ||
| 284 | + }; | ||
| 285 | + // Unity, exact and rounded: neither is a re-timing. | ||
| 286 | + assert_eq!(caption_rate(sub(300, 10_000_000), 30.), None); | ||
| 287 | + assert_eq!( | ||
| 288 | + caption_rate(sub(100, 3_333_333), 30.), | ||
| 289 | + None, | ||
| 290 | + "placement rounding is not a re-timing" | ||
| 291 | + ); | ||
| 292 | + // A 2x re-rate: a 5s span holding the same 10s of words. | ||
| 293 | + assert_eq!( | ||
| 294 | + caption_rate(sub(150, 10_000_000), 30.).map(|s| s.permille()), | ||
| 295 | + Some(2000) | ||
| 296 | + ); | ||
| 297 | + // ...and a slowed one -- a 20s span for the same words -- which the badge | ||
| 298 | + // keeps after any detach: the proportion is the placement's own. | ||
| 299 | + assert_eq!( | ||
| 300 | + caption_rate(sub(600, 10_000_000), 30.).map(|s| s.permille()), | ||
| 301 | + Some(500) | ||
| 302 | + ); | ||
| 303 | +} | ||
| 304 | + | ||
| 269 | /// The selection itself: clicks in order, the anchor under the hand, the | 305 | /// The selection itself: clicks in order, the anchor under the hand, the |
| 270 | /// toggle that assembles a group, and the plain click that replaces it all. | 306 | /// toggle that assembles a group, and the plain click that replaces it all. |
| 271 | #[test] | 307 | #[test] |
crates/app/src/tests/media.rsmodified+7 -5
| @@ -1096,12 +1096,14 @@ fn subtitles_arrive_beside_the_media_and_inside_it() { | |||
| 1096 | .map(|c| c.text.clone()) | 1096 | .map(|c| c.text.clone()) |
| 1097 | .collect() | 1097 | .collect() |
| 1098 | }; | 1098 | }; |
| 1099 | - // The placement keeps the track's own clock (it starts at frame 0 and | 1099 | + // The placement holds five seconds of the track in a three-second box, |
| 1100 | - // holds the whole of it), so the words are back at the times the file | 1100 | + // so the words cross the plate by the placement's own proportion: the |
| 1101 | - // wrote them -- the cut above moved the *carried* map, not this one. | 1101 | + // cue times compress with it (first line [0.5, 1.5) -> [0.3, 0.9), |
| 1102 | + // second [2, 3.25) -> [1.2, 1.95)) -- a unity placement would keep the | ||
| 1103 | + // file's own times, to the microsecond. | ||
| 1102 | assert_eq!(shown(0.7), ["first line"]); | 1104 | assert_eq!(shown(0.7), ["first line"]); |
| 1103 | - assert!(shown(1.8).is_empty(), "between two cues the plate goes"); | 1105 | + assert!(shown(0.95).is_empty(), "between two cues the plate goes"); |
| 1104 | - assert_eq!(shown(2.5), ["second line\nwith a break"]); | 1106 | + assert_eq!(shown(1.5), ["second line\nwith a break"]); |
| 1105 | } | 1107 | } |
| 1106 | 1108 | ||
| 1107 | /// One lane over the picture and never two ([`Player::active_sub_lane`]): which | 1109 | /// One lane over the picture and never two ([`Player::active_sub_lane`]): which |
crates/app/src/timeline_math.rsmodified+31 -0
| @@ -147,7 +147,38 @@ impl Selection { | |||
| 147 | self.picks.push(pick); | 147 | self.picks.push(pick); |
| 148 | } | 148 | } |
| 149 | } | 149 | } |
| 150 | +} | ||
| 150 | 151 | ||
| 152 | +/// The rate a caption's box plays its window at, read off the placement | ||
| 153 | +/// itself: how long the words are, against how much timeline they cross. A | ||
| 154 | +/// clip's badge comes from its own `speed`; a caption has none, so its box | ||
| 155 | +/// derives one -- and keeps it even after the group that re-timed it comes | ||
| 156 | +/// apart, because the proportion is the placement's own. `None` at unity: | ||
| 157 | +/// placement rounding leaves an odd-width window a frame or so off real time, | ||
| 158 | +/// and a badge is for a box that was *re-timed*, not one that rounds. | ||
| 159 | +pub(crate) fn caption_rate(sub: SubClip, fps: f64) -> Option<Speed> { | ||
| 160 | + if !(fps.is_finite() && fps > 0.0) || sub.window_us() <= 0 { | ||
| 161 | + return None; | ||
| 162 | + } | ||
| 163 | + let span_us = f64::from(sub.frames) / fps * 1e6; | ||
| 164 | + // The rate is the window over the span -- content per timeline, a clip's | ||
| 165 | + // own `speed` in the same spelling: 10s of words crossing 5s of timeline | ||
| 166 | + // is 2.00x. | ||
| 167 | + let permille = (sub.window_us() as f64 / span_us * 1000.).round(); | ||
| 168 | + // The gate is generous by two permille either way: placement rounding | ||
| 169 | + // (`frames_of_us`) leaves an odd-width unity window a frame off real | ||
| 170 | + // time, and that is not a re-timing. | ||
| 171 | + ((permille - 1000.).abs() > 2.).then(|| { | ||
| 172 | + Speed::from_permille( | ||
| 173 | + permille.clamp( | ||
| 174 | + f64::from(Speed::MIN.permille()), | ||
| 175 | + f64::from(Speed::MAX.permille()), | ||
| 176 | + ) as u16, | ||
| 177 | + ) | ||
| 178 | + }) | ||
| 179 | +} | ||
| 180 | + | ||
| 181 | +impl Selection { | ||
| 151 | pub(crate) fn len(&self) -> usize { | 182 | pub(crate) fn len(&self) -> usize { |
| 152 | self.picks.len() | 183 | self.picks.len() |
| 153 | } | 184 | } |
crates/app/src/ui/timeline.rsmodified+32 -0
| @@ -1363,6 +1363,38 @@ impl Player { | |||
| 1363 | this.open_menu(lane, i, event.position, cx); | 1363 | this.open_menu(lane, i, event.position, cx); |
| 1364 | }), | 1364 | }), |
| 1365 | ) | 1365 | ) |
| 1366 | + // The rate the caption's own window plays at, off | ||
| 1367 | + // the placement itself: a re-timed group leaves it | ||
| 1368 | + // on the box even after the group comes apart, and | ||
| 1369 | + // the badge is the clip's badge -- same place, same | ||
| 1370 | + // look -- on the visible slice, for the clip's | ||
| 1371 | + // reason (zoomed in, the box's own edge is off the | ||
| 1372 | + // screen and a badge at it with it). | ||
| 1373 | + .when_some( | ||
| 1374 | + caption_rate(shown_sub, self.fps).filter(|_| vis_w > 0.), | ||
| 1375 | + |d, rate| { | ||
| 1376 | + d.child( | ||
| 1377 | + div() | ||
| 1378 | + .absolute() | ||
| 1379 | + .top_0() | ||
| 1380 | + .left(px(vis_x)) | ||
| 1381 | + .w(px(vis_w)) | ||
| 1382 | + .flex() | ||
| 1383 | + .justify_end() | ||
| 1384 | + .overflow_hidden() | ||
| 1385 | + .child( | ||
| 1386 | + div() | ||
| 1387 | + .flex_none() | ||
| 1388 | + .px(px(3.)) | ||
| 1389 | + .rounded(px(3.)) | ||
| 1390 | + .bg(rgb(ACCENT_PRIMARY())) | ||
| 1391 | + .text_size(px(9.)) | ||
| 1392 | + .text_color(rgb(BG_RAISED())) | ||
| 1393 | + .child(format!("{rate}")), | ||
| 1394 | + ), | ||
| 1395 | + ) | ||
| 1396 | + }, | ||
| 1397 | + ) | ||
| 1366 | // The two strips a drag lengthens it by, on the same | 1398 | // The two strips a drag lengthens it by, on the same |
| 1367 | // rule a clip's are drawn by ([`trims`]). | 1399 | // rule a clip's are drawn by ([`trims`]). |
| 1368 | .children( | 1400 | .children( |
crates/engine/src/project.rsmodified+77 -2
| @@ -2009,8 +2009,16 @@ impl Project { | |||
| 2009 | continue; | 2009 | continue; |
| 2010 | }; | 2010 | }; |
| 2011 | // Where the placement's own first microsecond lands on the | 2011 | // Where the placement's own first microsecond lands on the |
| 2012 | - // timeline; every cue in the window is that far along from it. | 2012 | + // timeline, and how many microseconds of timeline one microsecond |
| 2013 | - let onto = |t: i64| (f64::from(s.start) / fps * 1e6).round() as i64 + (t - s.in_us); | 2013 | + // of the window is worth: the placement's own proportion, which a |
| 2014 | + // group's re-rate changes (the frames compress, the window does | ||
| 2015 | + // not -- [`write_speed`]'s law). At unity the ratio is 1.0 and | ||
| 2016 | + // every cue lands where it always did; at 2x the words cross the | ||
| 2017 | + // screen in half the time, cue for cue. | ||
| 2018 | + let per = f64::from(s.frames) / fps * 1e6 / (s.window_us().max(1) as f64); | ||
| 2019 | + let onto = |t: i64| { | ||
| 2020 | + (f64::from(s.start) / fps * 1e6 + (t - s.in_us) as f64 * per).round() as i64 | ||
| 2021 | + }; | ||
| 2014 | for cue in &track.cues { | 2022 | for cue in &track.cues { |
| 2015 | let (a, b) = (cue.start_us.max(s.in_us), cue.end_us.min(s.out_us)); | 2023 | let (a, b) = (cue.start_us.max(s.in_us), cue.end_us.min(s.out_us)); |
| 2016 | if b <= a { | 2024 | if b <= a { |
| @@ -5104,6 +5112,73 @@ mod tests { | |||
| 5104 | assert!(subs_sorted_disjoint(&p.lanes[p.index(s1).unwrap()].subs)); | 5112 | assert!(subs_sorted_disjoint(&p.lanes[p.index(s1).unwrap()].subs)); |
| 5105 | } | 5113 | } |
| 5106 | 5114 | ||
| 5115 | + /// A grouped caption's cues re-time with its clip: the placement's window | ||
| 5116 | + /// crossed the timeline at its own proportion before any rate, and a | ||
| 5117 | + /// re-rate changes that proportion (the frames compress, the window does | ||
| 5118 | + /// not). A cue eight seconds into a window played at 2x crosses the screen | ||
| 5119 | + /// four seconds in -- and at unity every cue lands exactly where it always | ||
| 5120 | + /// did, byte for byte. | ||
| 5121 | + #[test] | ||
| 5122 | + fn a_grouped_captions_cues_re_time_with_its_clip() { | ||
| 5123 | + let track = SubtitleTrack { | ||
| 5124 | + path: FILE.into(), | ||
| 5125 | + track: None, | ||
| 5126 | + language: "eng".into(), | ||
| 5127 | + name: String::new(), | ||
| 5128 | + label: "eng".into(), | ||
| 5129 | + cues: vec![crate::subtitle::Cue { | ||
| 5130 | + start_us: 8_000_000, | ||
| 5131 | + end_us: 9_000_000, | ||
| 5132 | + text: "late line".into(), | ||
| 5133 | + image: None, | ||
| 5134 | + }], | ||
| 5135 | + bitmap: false, | ||
| 5136 | + refused: None, | ||
| 5137 | + }; | ||
| 5138 | + let mut p = Project::single(FILE, 300).with_subtitles(vec![track]); | ||
| 5139 | + let s1 = p.add_lane(LaneKind::Subtitle); | ||
| 5140 | + // Ten seconds of the track over ten seconds of timeline: unity. | ||
| 5141 | + p.place_sub( | ||
| 5142 | + s1, | ||
| 5143 | + 0, | ||
| 5144 | + SubClip { | ||
| 5145 | + start: 0, | ||
| 5146 | + frames: 300, | ||
| 5147 | + track: 0, | ||
| 5148 | + in_us: 0, | ||
| 5149 | + out_us: 10_000_000, | ||
| 5150 | + link: None, | ||
| 5151 | + }, | ||
| 5152 | + ) | ||
| 5153 | + .expect("placed"); | ||
| 5154 | + | ||
| 5155 | + // Unity: the cue sits at its own [8s, 9s), to the microsecond. | ||
| 5156 | + let unity = p.sub_lane_cues(s1, FPS); | ||
| 5157 | + assert_eq!( | ||
| 5158 | + unity | ||
| 5159 | + .iter() | ||
| 5160 | + .map(|c| (c.start_us, c.end_us, &*c.text)) | ||
| 5161 | + .collect::<Vec<_>>(), | ||
| 5162 | + [(8_000_000, 9_000_000, "late line")], | ||
| 5163 | + "a unity placement maps exactly as it always did" | ||
| 5164 | + ); | ||
| 5165 | + | ||
| 5166 | + // Grouped with the clip and re-rated to 2x: the same window now crosses | ||
| 5167 | + // in half the time, so the cue at 8s of it lands 4s in. | ||
| 5168 | + p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); | ||
| 5169 | + p.set_speed(Lane::V1, 0, Speed::from_permille(2000)) | ||
| 5170 | + .expect("room for it"); | ||
| 5171 | + let re_timed = p.sub_lane_cues(s1, FPS); | ||
| 5172 | + assert_eq!( | ||
| 5173 | + re_timed | ||
| 5174 | + .iter() | ||
| 5175 | + .map(|c| (c.start_us, c.end_us)) | ||
| 5176 | + .collect::<Vec<_>>(), | ||
| 5177 | + [(4_000_000, 4_500_000)], | ||
| 5178 | + "the cue crosses with the clip's new rate" | ||
| 5179 | + ); | ||
| 5180 | + } | ||
| 5181 | + | ||
| 5107 | /// The region's caption pieces re-time by mapped boundaries forced apart: | 5182 | /// The region's caption pieces re-time by mapped boundaries forced apart: |
| 5108 | /// rounding start and length independently can land two cues on one frame, | 5183 | /// rounding start and length independently can land two cues on one frame, |
| 5109 | /// and the artifact is clamped -- one frame each, in order, none past the | 5184 | /// and the artifact is clamped -- one frame each, in order, none past the |