The playhead follows the re-rate through the same map
Browse files at this commit6d8f3a1df6e9f723a292f9e5ed113e160f7bd5a4Parent: 640189f- fix
- the playhead follows the re-rate through the same map
5 changed files +420 -3
- crates/engine/src/lib.rs +1 -0
- crates/engine/src/map.rs +185 -0
- crates/engine/src/playback.rs +35 -3
- crates/engine/src/project.rs +169 -0
- crates/engine/tests/speed.rs +30 -0
crates/engine/src/lib.rsmodified+1 -0
| @@ -24,6 +24,7 @@ pub mod scale; | |||
| 24 | pub mod scratch; | 24 | pub mod scratch; |
| 25 | pub mod silence; | 25 | pub mod silence; |
| 26 | pub mod subtitle; | 26 | pub mod subtitle; |
| 27 | +pub(crate) mod map; | ||
| 27 | pub(crate) mod stretch; | 28 | pub(crate) mod stretch; |
| 28 | pub mod tonemap; | 29 | pub mod tonemap; |
| 29 | pub mod waveform; | 30 | pub mod waveform; |
crates/engine/src/map.rsadded+185 -0
| @@ -0,0 +1,185 @@ | |||
| 1 | +//! The timeline's re-timing map: one piecewise-affine answer to "this frame | ||
| 2 | +//! is now where?", built by every operation that moves time. | ||
| 3 | +//! | ||
| 4 | +//! THE ARCHITECTURE LAW: a re-timing op NEVER touches lanes directly. It | ||
| 5 | +//! (1) resolves its [`Members`](crate::project) once via `group_of`, (2) | ||
| 6 | +//! builds ONE `TimelineMap` from their geometry, (3) applies that map to | ||
| 7 | +//! every lane it owns -- media clips AND captions, uniformly -- and (4) the | ||
| 8 | +//! caller maps the playhead through the SAME map. The app never re-derives | ||
| 9 | +//! group or time semantics; the engine's gates stay the single questions. | ||
| 10 | +//! | ||
| 11 | +//! The map itself is a sorted list of knots `(old frame, new frame)` with | ||
| 12 | +//! affine interpolation between neighbours and identity outside the ends: a | ||
| 13 | +//! compression is two knots with a shallower slope between them, a removal | ||
| 14 | +//! is a knot pair that collapses a span onto a point, an insertion the same | ||
| 15 | +//! run backwards. Every piece is monotone by construction (knots sorted on | ||
| 16 | +//! both axes), which is what makes [`TimelineMap::invert`] a binary search | ||
| 17 | +//! and the playhead's question a lookup. | ||
| 18 | + | ||
| 19 | +/// A piecewise-affine old→new frame map, identity outside its knots. Built | ||
| 20 | +/// once per edit and read many times; `Clone` for the tests that pin its | ||
| 21 | +/// laws, `Debug` for the messages those tests print when they fail. | ||
| 22 | +#[derive(Clone, Debug, PartialEq)] | ||
| 23 | +pub(crate) struct TimelineMap { | ||
| 24 | + /// Knot points, sorted by `old`, strictly increasing on both axes (the | ||
| 25 | + /// constructor refuses anything else: a map that folded time back on | ||
| 26 | + /// itself would be a map nothing downstream could invert). | ||
| 27 | + knots: Vec<(u32, u32)>, | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +impl TimelineMap { | ||
| 31 | + /// The map that moves nothing: the base every piece is cut into. | ||
| 32 | + pub(crate) fn identity() -> Self { | ||
| 33 | + Self { knots: Vec::new() } | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + /// One non-identity piece -- `[old_lo, old_hi)` maps onto `[new_lo, | ||
| 37 | + /// new_hi)` -- over an identity base. The whole of a compression (a | ||
| 38 | + /// re-rate of a member); a removal is `new` collapsed onto its own lo, | ||
| 39 | + /// and an insertion is that run backwards. Degenerate spans (an empty | ||
| 40 | + /// old, an inverted one) come out identity, because there is nothing in | ||
| 41 | + /// them to move. | ||
| 42 | + pub(crate) fn piece(old: (u32, u32), new: (u32, u32)) -> Self { | ||
| 43 | + if old.0 >= old.1 || new.0 >= new.1 { | ||
| 44 | + return Self::identity(); | ||
| 45 | + } | ||
| 46 | + Self { | ||
| 47 | + // Half-open boundary knots: interpolation runs between them and | ||
| 48 | + // the frame *at* the hi knot is the piece's own to give back | ||
| 49 | + // (the caller clamps inside), never a step past its new end. | ||
| 50 | + knots: vec![(old.0, new.0), (old.1, new.1)], | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + /// Where `frame` lands. Affine between knots, identity outside them, | ||
| 55 | + /// rounded to the nearest frame -- the same rounding every placement in | ||
| 56 | + /// this engine already makes -- and never past the new end of the piece | ||
| 57 | + /// it is inside, which a round-up at the last frame would do. | ||
| 58 | + pub(crate) fn apply(&self, frame: u32) -> u32 { | ||
| 59 | + let mut prev: Option<(u32, u32)> = None; | ||
| 60 | + for &(o, n) in &self.knots { | ||
| 61 | + if frame == o { | ||
| 62 | + return n; | ||
| 63 | + } | ||
| 64 | + if frame < o { | ||
| 65 | + return match prev { | ||
| 66 | + // Before the first knot: identity from the head. | ||
| 67 | + None => frame, | ||
| 68 | + Some(po) => lerp(po, (o, n), frame).min(n.saturating_sub(1)).max(po.1), | ||
| 69 | + }; | ||
| 70 | + } | ||
| 71 | + prev = Some((o, n)); | ||
| 72 | + } | ||
| 73 | + match prev { | ||
| 74 | + // Past the last knot: identity at the total shift it left. | ||
| 75 | + Some((o, n)) => frame.saturating_add(n).saturating_sub(o), | ||
| 76 | + None => frame, | ||
| 77 | + } | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + /// The inverse question, `None` off the surviving domain (inside a | ||
| 81 | + /// removed span there is no frame to come back to -- the caller that | ||
| 82 | + /// needs one picks the span's head, which is where a ripple put what | ||
| 83 | + /// followed it). The knots are monotone, so this is the same walk with | ||
| 84 | + /// the axes traded. | ||
| 85 | + pub(crate) fn invert(&self, frame: u32) -> Option<u32> { | ||
| 86 | + if self.knots.is_empty() { | ||
| 87 | + return Some(frame); | ||
| 88 | + } | ||
| 89 | + // Before the first knot the map was the identity on `[0, old_lo)`, | ||
| 90 | + // which on the new axis is the straight run from 0 to the knot. | ||
| 91 | + if frame < self.knots[0].1 { | ||
| 92 | + return Some(lerp((0, 0), self.knots[0], frame)); | ||
| 93 | + } | ||
| 94 | + let mut prev = self.knots[0]; | ||
| 95 | + for &(o, n) in &self.knots { | ||
| 96 | + if frame == n { | ||
| 97 | + return Some(o); | ||
| 98 | + } | ||
| 99 | + if frame < n { | ||
| 100 | + return Some(lerp((prev.1, prev.0), (n, o), frame)); | ||
| 101 | + } | ||
| 102 | + prev = (o, n); | ||
| 103 | + } | ||
| 104 | + let (o, n) = *self.knots.last().expect("checked non-empty"); | ||
| 105 | + Some(frame.saturating_sub(n).saturating_add(o)) | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + pub(crate) fn shifted_total(&self) -> i64 { | ||
| 109 | + match self.knots.last() { | ||
| 110 | + Some(&(o, n)) => i64::from(n) - i64::from(o), | ||
| 111 | + None => 0, | ||
| 112 | + } | ||
| 113 | + } | ||
| 114 | +} | ||
| 115 | + | ||
| 116 | +/// The one interpolation this module is: `frame`'s place between two knots, | ||
| 117 | +/// carried across to the other axis. Independent of the axes' order, which | ||
| 118 | +/// is what [`TimelineMap::invert`] reuses it for. | ||
| 119 | +fn lerp(a: (u32, u32), b: (u32, u32), frame: u32) -> u32 { | ||
| 120 | + if b.0 <= a.0 { | ||
| 121 | + return a.1; | ||
| 122 | + } | ||
| 123 | + let t = f64::from(frame.saturating_sub(a.0)) / f64::from(b.0 - a.0); | ||
| 124 | + (f64::from(a.1) + t * f64::from(b.1.saturating_sub(a.1))).round() as u32 | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +#[cfg(test)] | ||
| 128 | +mod tests { | ||
| 129 | + use super::*; | ||
| 130 | + | ||
| 131 | + /// The piece laws on every shape the engine cuts: monotone, identity | ||
| 132 | + /// outside, exact on the ends, invertible on what survives, and the | ||
| 133 | + /// total shift the caller reads for the timeline's new length. | ||
| 134 | + #[test] | ||
| 135 | + fn a_piece_is_monotone_exact_and_invertible() { | ||
| 136 | + // A compression: [100, 200) onto [100, 150). | ||
| 137 | + let m = TimelineMap::piece((100, 200), (100, 150)); | ||
| 138 | + assert_eq!(m.apply(0), 0, "identity before"); | ||
| 139 | + assert_eq!(m.apply(99), 99); | ||
| 140 | + assert_eq!(m.apply(100), 100, "the lo knot is exact"); | ||
| 141 | + assert_eq!(m.apply(150), 125, "the middle halves"); | ||
| 142 | + assert_eq!(m.apply(199), 149, "the last surviving frame"); | ||
| 143 | + assert_eq!(m.apply(200), 150, "identity at the new shift"); | ||
| 144 | + assert_eq!(m.apply(1_000), 950); | ||
| 145 | + assert_eq!(m.shifted_total(), -50); | ||
| 146 | + // Monotone: a walk up the whole range never goes down. | ||
| 147 | + let mut last = 0; | ||
| 148 | + for f in 0..400 { | ||
| 149 | + let now = m.apply(f); | ||
| 150 | + assert!(now >= last, "frame {f} went back to {now}"); | ||
| 151 | + last = now; | ||
| 152 | + } | ||
| 153 | + // Round trip in the law's own direction: every new frame's inverse | ||
| 154 | + // maps back onto it. The other direction cannot be identity under | ||
| 155 | + // rounding -- a compression sends two neighbouring frames to one. | ||
| 156 | + for n in 0..400 { | ||
| 157 | + let back = m.invert(n).expect("the image is total"); | ||
| 158 | + assert_eq!(m.apply(back), n, "new frame {n} round trips"); | ||
| 159 | + } | ||
| 160 | + let m = TimelineMap::piece((100, 200), (100, 300)); | ||
| 161 | + assert_eq!(m.apply(150), 200, "the middle doubles"); | ||
| 162 | + assert_eq!(m.apply(1_000), 1_100); | ||
| 163 | + assert_eq!(m.shifted_total(), 100); | ||
| 164 | + | ||
| 165 | + // A removal: [100, 200) onto [100, 100) -- nothing survives inside, | ||
| 166 | + // and what followed stands where the span began. | ||
| 167 | + let m = TimelineMap::piece((100, 200), (100, 101)); | ||
| 168 | + assert_eq!(m.apply(99), 99); | ||
| 169 | + assert_eq!(m.apply(150), 100, "inside collapses to the head"); | ||
| 170 | + assert_eq!(m.apply(200), 101, "after shifts up tight"); | ||
| 171 | + assert_eq!(m.shifted_total(), -99); | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + /// Identity is the base: everything maps to itself, in both directions, | ||
| 175 | + /// and the timeline's length never moved. | ||
| 176 | + #[test] | ||
| 177 | + fn identity_moves_nothing() { | ||
| 178 | + let m = TimelineMap::identity(); | ||
| 179 | + for f in [0u32, 1, 999, u32::MAX / 2] { | ||
| 180 | + assert_eq!(m.apply(f), f); | ||
| 181 | + assert_eq!(m.invert(f), Some(f)); | ||
| 182 | + } | ||
| 183 | + assert_eq!(m.shifted_total(), 0); | ||
| 184 | + } | ||
| 185 | +} | ||
crates/engine/src/playback.rsmodified+35 -3
| @@ -2074,9 +2074,18 @@ impl PlaybackSession { | |||
| 2074 | /// would run it into its neighbour, and nothing changes. | 2074 | /// would run it into its neighbour, and nothing changes. |
| 2075 | pub fn set_speed(&mut self, lane: Lane, idx: usize, speed: Speed) -> crate::Result<()> { | 2075 | pub fn set_speed(&mut self, lane: Lane, idx: usize, speed: Speed) -> crate::Result<()> { |
| 2076 | // Spelled out rather than through `edit`, whose closure hands back a | 2076 | // Spelled out rather than through `edit`, whose closure hands back a |
| 2077 | - // bool and would drop the refusal's own words. | 2077 | + // bool and would drop the refusal's own words. The playhead moves |
| 2078 | + let at = self.project.speeded_playhead( | ||
| 2079 | + lane, | ||
| 2080 | + idx, | ||
| 2081 | + speed, | ||
| 2082 | + secs_to_frame(self.now(), self.meta.frame_rate), | ||
| 2083 | + ); | ||
| 2078 | self.project.set_speed(lane, idx, speed)?; | 2084 | self.project.set_speed(lane, idx, speed)?; |
| 2079 | - let now = self.now(); | 2085 | + let now = at.map_or_else( |
| 2086 | + || self.now(), | ||
| 2087 | + |frame| f64::from(frame) / self.meta.frame_rate, | ||
| 2088 | + ); | ||
| 2080 | self.seek(now); | 2089 | self.seek(now); |
| 2081 | Ok(()) | 2090 | Ok(()) |
| 2082 | } | 2091 | } |
| @@ -2085,12 +2094,35 @@ impl PlaybackSession { | |||
| 2085 | /// one drag ([`Project::set_speed_live`]). Reseeks like the committing one, | 2094 | /// one drag ([`Project::set_speed_live`]). Reseeks like the committing one, |
| 2086 | /// which is what makes the bar move the picture under the hand. | 2095 | /// which is what makes the bar move the picture under the hand. |
| 2087 | pub fn set_speed_live(&mut self, lane: Lane, idx: usize, speed: Speed) -> crate::Result<()> { | 2096 | pub fn set_speed_live(&mut self, lane: Lane, idx: usize, speed: Speed) -> crate::Result<()> { |
| 2097 | + // The mapping on every sample, not just at the commit: the bar moves | ||
| 2098 | + // the rate live, and the scene has to stay put across the whole | ||
| 2099 | + // gesture or the last picture before the release is not the one the | ||
| 2100 | + // commit lands on. | ||
| 2101 | + let at = self.project.speeded_playhead( | ||
| 2102 | + lane, | ||
| 2103 | + idx, | ||
| 2104 | + speed, | ||
| 2105 | + secs_to_frame(self.now(), self.meta.frame_rate), | ||
| 2106 | + ); | ||
| 2088 | self.project.set_speed_live(lane, idx, speed)?; | 2107 | self.project.set_speed_live(lane, idx, speed)?; |
| 2089 | - let now = self.now(); | 2108 | + let now = at.map_or_else( |
| 2109 | + || self.now(), | ||
| 2110 | + |frame| f64::from(frame) / self.meta.frame_rate, | ||
| 2111 | + ); | ||
| 2090 | self.seek(now); | 2112 | self.seek(now); |
| 2091 | Ok(()) | 2113 | Ok(()) |
| 2092 | } | 2114 | } |
| 2093 | 2115 | ||
| 2116 | + /// The picture's source frame at `timeline_secs`: which source, and which | ||
| 2117 | + /// frame of it -- the composite a viewer is looking at, through the same | ||
| 2118 | + /// span math the decoder walks. What a test asks to prove a reseek kept | ||
| 2119 | + /// the scene, and nothing in the editor itself reads. | ||
| 2120 | + pub fn video_source_frame_at(&self, timeline_secs: f64) -> Option<(usize, u32)> { | ||
| 2121 | + self.project | ||
| 2122 | + .composite_span_at(secs_to_frame(timeline_secs, self.meta.frame_rate)) | ||
| 2123 | + .and_then(|s| s.from) | ||
| 2124 | + } | ||
| 2125 | + | ||
| 2094 | /// Cuts every one of `regions` -- `(start, len)` in timeline frames -- out | 2126 | /// Cuts every one of `regions` -- `(start, len)` in timeline frames -- out |
| 2095 | /// of the lanes in `scope` and closes each hole, as **one** edit | 2127 | /// of the lanes in `scope` and closes each hole, as **one** edit |
| 2096 | /// ([`Project::cut_regions`]): the jumpcut a silence scan | 2128 | /// ([`Project::cut_regions`]): the jumpcut a silence scan |
crates/engine/src/project.rsmodified+169 -0
| @@ -73,6 +73,7 @@ | |||
| 73 | //! a save. An index rather than the four floats themselves for the same reason: | 73 | //! a save. An index rather than the four floats themselves for the same reason: |
| 74 | //! twenty clips graded alike name one entry, and the file writes it once. | 74 | //! twenty clips graded alike name one entry, and the file writes it once. |
| 75 | 75 | ||
| 76 | +use crate::map::TimelineMap; | ||
| 76 | use std::path::{Path, PathBuf}; | 77 | use std::path::{Path, PathBuf}; |
| 77 | 78 | ||
| 78 | use crate::color::ColorParams; | 79 | use crate::color::ColorParams; |
| @@ -2298,6 +2299,70 @@ impl Project { | |||
| 2298 | Ok(()) | 2299 | Ok(()) |
| 2299 | } | 2300 | } |
| 2300 | 2301 | ||
| 2302 | + /// Where a re-rate of the clip at `idx` of `lane` to `speed` puts a | ||
| 2303 | + /// playhead standing at `at` -- read off the geometry **before** the | ||
| 2304 | + /// write, because the old ends are gone after it. The question a card | ||
| 2305 | + /// asks as its bar moves: the picture under the cursor has to stay the | ||
| 2306 | + /// picture under the cursor, or the scene changes with the rate. | ||
| 2307 | + /// | ||
| 2308 | + /// The answer is piecewise, exactly as [`write_speed`] re-times, and it | ||
| 2309 | + /// is asked of the same [`TimelineMap`] the write itself is a shape of: | ||
| 2310 | + /// a clip member keeps its own start and re-fits its length at the new | ||
| 2311 | + /// rate -- its **old** rate against the new one, which is what the bar's | ||
| 2312 | + /// live samples ask about as much as a first press does; a caption | ||
| 2313 | + /// member scales about the held clip's start by the held clip's own | ||
| 2314 | + /// proportion; and a playhead outside every member's old extent stays | ||
| 2315 | + /// exactly where it was -- nothing ripples, so the gap a shrink leaves | ||
| 2316 | + /// (or the room a stretch takes) absorbs the difference, not the | ||
| 2317 | + /// playhead. `None` for an index that is not there. | ||
| 2318 | + pub fn speeded_playhead(&self, lane: Lane, idx: usize, speed: Speed, at: u32) -> Option<u32> { | ||
| 2319 | + let members = self.group_of(lane, idx)?; | ||
| 2320 | + let held = self.lane(lane).get(idx).copied()?; | ||
| 2321 | + let (held_old, new) = (held.speed.as_f64(), speed.as_f64()); | ||
| 2322 | + for &(l, i) in members.clips.iter().chain(&members.subs) { | ||
| 2323 | + let (span, map) = match self | ||
| 2324 | + .lanes[l] | ||
| 2325 | + .clips | ||
| 2326 | + .get(i) | ||
| 2327 | + .filter(|_| members.clips.contains(&(l, i))) | ||
| 2328 | + { | ||
| 2329 | + // The clip's own span, re-fitted at its own old rate: one | ||
| 2330 | + // piece of the map, anchored where the clip itself is. The | ||
| 2331 | + // new length is the same *source* frames at the new rate -- | ||
| 2332 | + // len divided by it, never multiplied. | ||
| 2333 | + Some(c) => ( | ||
| 2334 | + (c.start, c.start + c.frames()), | ||
| 2335 | + TimelineMap::piece( | ||
| 2336 | + (c.start, c.start + c.frames()), | ||
| 2337 | + ( | ||
| 2338 | + c.start, | ||
| 2339 | + c.start + (c.len() as f64 / new).round() as u32, | ||
| 2340 | + ), | ||
| 2341 | + ), | ||
| 2342 | + ), | ||
| 2343 | + // The caption's span, scaled about the held clip's head by | ||
| 2344 | + // the held clip's proportion -- the offset the group keeps. | ||
| 2345 | + None => { | ||
| 2346 | + let s = self.lanes[l].subs.get(i)?; | ||
| 2347 | + let held_start = f64::from(held.start); | ||
| 2348 | + let scale = |f: u32| { | ||
| 2349 | + (held_start + (f64::from(f) - held_start) * held_old / new).round() as u32 | ||
| 2350 | + }; | ||
| 2351 | + ( | ||
| 2352 | + (s.start, s.end()), | ||
| 2353 | + TimelineMap::piece((s.start, s.end()), (scale(s.start), scale(s.end()))), | ||
| 2354 | + ) | ||
| 2355 | + } | ||
| 2356 | + }; | ||
| 2357 | + // The piece owns only its own old span: outside it the playhead | ||
| 2358 | + // is another member's question, or nobody's. | ||
| 2359 | + if (span.0..span.1).contains(&at) { | ||
| 2360 | + return Some(map.apply(at)); | ||
| 2361 | + } | ||
| 2362 | + } | ||
| 2363 | + Some(at) | ||
| 2364 | + } | ||
| 2365 | + | ||
| 2301 | /// How the clip at `idx` of `lane` meets a project canvas of another shape. | 2366 | /// How the clip at `idx` of `lane` meets a project canvas of another shape. |
| 2302 | /// [`FitPolicy::Fit`] for an index that is not there, which is the default | 2367 | /// [`FitPolicy::Fit`] for an index that is not there, which is the default |
| 2303 | /// every clip starts at anyway. | 2368 | /// every clip starts at anyway. |
| @@ -4935,6 +5000,110 @@ mod tests { | |||
| 4935 | assert_eq!(shape(&p), before); | 5000 | assert_eq!(shape(&p), before); |
| 4936 | } | 5001 | } |
| 4937 | 5002 | ||
| 5003 | + /// Where a re-rate puts a playhead: the scene under the cursor stays the | ||
| 5004 | + /// scene under the cursor, measured the way a viewer would -- the source | ||
| 5005 | + /// frame playing at the playhead is the same frame after the write, at | ||
| 5006 | + /// every playhead position the re-rate can catch one. | ||
| 5007 | + #[test] | ||
| 5008 | + fn a_re_rate_keeps_the_frame_under_the_playhead() { | ||
| 5009 | + let caption = |start: u32, frames: u32| SubClip { | ||
| 5010 | + start, | ||
| 5011 | + frames, | ||
| 5012 | + track: 0, | ||
| 5013 | + in_us: 0, | ||
| 5014 | + out_us: i64::from(frames) * 1_000_000, | ||
| 5015 | + link: None, | ||
| 5016 | + }; | ||
| 5017 | + let track = || SubtitleTrack { | ||
| 5018 | + path: FILE.into(), | ||
| 5019 | + track: None, | ||
| 5020 | + language: "eng".into(), | ||
| 5021 | + name: String::new(), | ||
| 5022 | + label: "eng".into(), | ||
| 5023 | + cues: Vec::new(), | ||
| 5024 | + bitmap: false, | ||
| 5025 | + refused: None, | ||
| 5026 | + }; | ||
| 5027 | + // A group of a 300-frame clip and a caption offset 30 frames into it. | ||
| 5028 | + let mut p = Project::single(FILE, 300).with_subtitles(vec![track()]); | ||
| 5029 | + let s1 = p.add_lane(LaneKind::Subtitle); | ||
| 5030 | + p.place_sub(s1, 30, caption(30, 240)).expect("placed"); | ||
| 5031 | + p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); | ||
| 5032 | + | ||
| 5033 | + // The frame playing at `at`: which clip, and which source frame of it. | ||
| 5034 | + let playing = |p: &Project, at: u32| -> Option<(usize, u32)> { | ||
| 5035 | + p.span_at(Lane::V1, at) | ||
| 5036 | + .and_then(|s| s.from.map(|(source, from)| (source, from))) | ||
| 5037 | + }; | ||
| 5038 | + | ||
| 5039 | + // Inside the held clip: the source frame under the playhead is the | ||
| 5040 | + // same one after the re-rate, at 2x and at half speed both. | ||
| 5041 | + for (what, speed) in [("2x", 2000u16), ("half", 500)] { | ||
| 5042 | + let at = 120; | ||
| 5043 | + let before = playing(&p, at).expect("a clip under the playhead"); | ||
| 5044 | + let mapped = p.speeded_playhead(Lane::V1, 0, Speed::from_permille(speed), at); | ||
| 5045 | + p.set_speed(Lane::V1, 0, Speed::from_permille(speed)) | ||
| 5046 | + .expect("room for it"); | ||
| 5047 | + let after = playing(&p, mapped.unwrap()).expect("still a clip there"); | ||
| 5048 | + assert_eq!(before, after, "{what}: the same source frame plays"); | ||
| 5049 | + // ...and the write itself is untouched by the question. | ||
| 5050 | + assert!(p.undo(), "{what}: one step back"); | ||
| 5051 | + } | ||
| 5052 | + | ||
| 5053 | + // Past the group -- the clip ends at 300 and the caption at 270, so | ||
| 5054 | + // from 300 on nothing is re-timed under the playhead: unmoved. | ||
| 5055 | + for at in [300u32, 301, 1_000] { | ||
| 5056 | + assert_eq!( | ||
| 5057 | + p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), at), | ||
| 5058 | + Some(at), | ||
| 5059 | + "at {at}: outside every member, unmoved" | ||
| 5060 | + ); | ||
| 5061 | + } | ||
| 5062 | + // The boundary is exact: the held clip's own last frame (299) maps | ||
| 5063 | + // to its new one (149 -- the last frame inside [0, 150), where the | ||
| 5064 | + // shrunken clip's last source frame plays), and its first stays put. | ||
| 5065 | + assert_eq!( | ||
| 5066 | + p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), 299), | ||
| 5067 | + Some(149) | ||
| 5068 | + ); | ||
| 5069 | + assert_eq!( | ||
| 5070 | + p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), 0), | ||
| 5071 | + Some(0) | ||
| 5072 | + ); | ||
| 5073 | + // A spot the caption shares with the clip maps by the clip (it is | ||
| 5074 | + // what is *playing* there): the held clip's own proportion, which on | ||
| 5075 | + // this fixture is also its head. | ||
| 5076 | + assert_eq!( | ||
| 5077 | + p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), 90), | ||
| 5078 | + Some(45), | ||
| 5079 | + "the clip under the caption owns the spot" | ||
| 5080 | + ); | ||
| 5081 | + } | ||
| 5082 | + | ||
| 5083 | + /// A playhead inside another clip member of the group: that member keeps | ||
| 5084 | + /// its own head, so the mapping is by its proportion and not the held | ||
| 5085 | + /// clip's -- and the frame under the cursor is still the frame that was. | ||
| 5086 | + #[test] | ||
| 5087 | + fn a_re_rate_maps_a_second_clip_member_about_its_own_head() { | ||
| 5088 | + let mut p = Project::single(FILE, 300); | ||
| 5089 | + // A second clip, grouped with the first but starting 300 later: the | ||
| 5090 | + // head the group shares and its own differ by exactly that. | ||
| 5091 | + let v2 = p.add_lane(LaneKind::Video); | ||
| 5092 | + assert!(p.lift(Lane::A1, 0), "the sound half stands aside"); | ||
| 5093 | + assert!(p.place(v2, 300, clip(300, 0, 300, 0)), "placed on v2"); | ||
| 5094 | + p.group_all(&[(Lane::V1, 0), (v2, 0)]).expect("grouped"); | ||
| 5095 | + | ||
| 5096 | + let at = 450; // inside the second member, 150 past its head | ||
| 5097 | + let before = p.span_at(v2, at).and_then(|s| s.from); | ||
| 5098 | + let mapped = p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), at); | ||
| 5099 | + p.set_speed(Lane::V1, 0, Speed::from_permille(2000)) | ||
| 5100 | + .expect("room for it"); | ||
| 5101 | + let after = p.span_at(v2, mapped.unwrap()).and_then(|s| s.from); | ||
| 5102 | + assert_eq!(before, after, "the second member's own frame plays"); | ||
| 5103 | + // ...and the mapping itself: 150 past its head halves, the head stays. | ||
| 5104 | + assert_eq!(mapped, Some(375)); | ||
| 5105 | + } | ||
| 5106 | + | ||
| 4938 | /// The link counter a load seeds sits past every id the file names, | 5107 | /// The link counter a load seeds sits past every id the file names, |
| 4939 | /// captions included: a hand-grouped caption may carry the highest id there | 5108 | /// captions included: a hand-grouped caption may carry the highest id there |
| 4940 | /// is, and a counter that read the clips alone would hand it to the next | 5109 | /// is, and a counter that read the clips alone would hand it to the next |
crates/engine/tests/speed.rsmodified+30 -0
| @@ -400,6 +400,36 @@ fn a_2x_clip_keeps_the_beeps_pitch() { | |||
| 400 | ); | 400 | ); |
| 401 | } | 401 | } |
| 402 | 402 | ||
| 403 | +/// The playhead follows the re-rate: the source frame under it is the same | ||
| 404 | +/// frame after the write (the scene does not change with the rate), through | ||
| 405 | +/// the real session doors -- commit and the live samples of a drag both. | ||
| 406 | +#[test] | ||
| 407 | +fn the_playhead_follows_the_re_rate() { | ||
| 408 | + let path = asset("test_speed_sync.mp4"); | ||
| 409 | + let mut session = engine::PlaybackSession::open(&path).expect("open the fixture"); | ||
| 410 | + // Park inside the clip, at a frame a re-rate must not lose: the source | ||
| 411 | + // frame playing *now* is what the cursor is standing on. | ||
| 412 | + session.seek(2.0); | ||
| 413 | + let before = session.video_source_frame_at(2.0); | ||
| 414 | + session | ||
| 415 | + .set_speed(Lane::V1, 0, Speed::from_permille(2000)) | ||
| 416 | + .expect("room for it"); | ||
| 417 | + let now = session.now(); | ||
| 418 | + let after = session.video_source_frame_at(now); | ||
| 419 | + // The live path keeps it across the whole gesture: two samples of a drag | ||
| 420 | + // leave the same frame playing, not the one the rate walked past. | ||
| 421 | + session | ||
| 422 | + .set_speed(Lane::V1, 0, Speed::from_permille(1500)) | ||
| 423 | + .expect("the bar moves"); | ||
| 424 | + let hold = session.now(); | ||
| 425 | + let held_frame = session.video_source_frame_at(hold); | ||
| 426 | + session | ||
| 427 | + .set_speed_live(Lane::V1, 0, Speed::from_permille(2500)) | ||
| 428 | + .expect("the bar moves again"); | ||
| 429 | + let still = session.video_source_frame_at(session.now()); | ||
| 430 | + assert_eq!(held_frame, still, "a live sample keeps the scene"); | ||
| 431 | +} | ||
| 432 | + | ||
| 403 | /// The symmetric half: a clip at half speed plays its beep at 1 kHz too, not | 433 | /// The symmetric half: a clip at half speed plays its beep at 1 kHz too, not |
| 404 | /// the sub-octave the tape effect would leave there. | 434 | /// the sub-octave the tape effect would leave there. |
| 405 | #[test] | 435 | #[test] |