//! Edit list: N ordered lanes of *placed* source ranges. Pure data, no I/O. //! //! A [`Project`] holds video lanes and audio lanes -- `V1..Vn`, `A1..An`, named //! by a [`Lane`] handle of a [`LaneKind`] and a 0-based `ord`. Each lane is a //! list of //! [`Clip`]s, and a clip says three things: which half-open `[in, out)` range of //! *source* frames it plays, which file it plays it from, and -- the whole model //! -- where on the timeline it sits ([`Clip::start`]). Clips are placed at an //! offset rather than queued end to end, so: //! //! * a **gap** is not an object, it is the absence of a placement. Nothing has //! to be inserted, kept in sync or garbage-collected for a hole to exist, and //! a hole cannot disagree with its neighbours about how long it is; //! * **moving and trimming are arithmetic** on `start`/`in_frame`, never a //! splice of the list; //! * the lanes are independent and are peers: deleting audio under a picture is //! one lane's business, and the picture does not shift. No lane is special //! except by what a *caller* reads -- and what playback and export read is the //! whole stack: [`Project::composite_span_at`] resolves the video lanes to the //! one picture that shows, [`Project::audio_segments_from`] hands every audio //! lane over to be summed. //! //! The price is one invariant this module enforces at every mutation and every //! constructor: within a lane, clips are **sorted by `start` and never overlap** //! ([`sorted_disjoint`]). Everything else -- mapping, spans, segments -- is a //! walk over that order. //! //! Two frame spaces meet here and are never mixed: *source* frames index the //! decoded file, *timeline* frames index what the viewer sees. Both are 0-based //! (sample ids in the demuxer are 1-based; that conversion stays in `decode`). //! //! Grouping is [`Clip::link`]: clips carrying the same id were split from the //! same take and move together. An id is *not* a pairing of two lanes -- it //! names at most one clip per lane and every clip carrying it covers the same //! timeline span, on however many lanes ([`links_are_consistent`]). //! [`Project::split`] cuts every lane at a timeline frame and hands each side //! fresh ids, one per group of lanes whose halves line up; [`Project::regroup`] //! is its inverse and rejoins them. //! //! Editing is metadata only. [`Project::split`] changes no timeline->source //! mapping, so a running decoder stays correct across it; everything else does //! and the caller must reseek. Every successful edit snapshots every lane, so //! [`Project::undo`] is an exact restore -- of the clips *and* of the lane list. //! //! Words are a third kind of lane ([`LaneKind::Subtitle`]) and not a setting on //! the project: a caption is placed, moved, trimmed, cut and rippled by the //! very machinery above, and it joins the undo history for free because a //! snapshot is the whole lane list. What such a lane holds is a [`SubClip`] -- //! a `[in_us, out_us)` window of one of the project's subtitle tracks //! ([`Project::subtitles`], which stays the *palette* the cues are read into), //! placed at a timeline frame like everything else. It holds no [`Clip`] at //! all, which is what keeps every media path (the composite, the mix, an //! equalizer, a speed) from ever meeting one: they read a lane's clips, and //! there are none. //! //! A clip names its file by *index* into [`Project::sources`], which is //! append-only: an index handed out once stays valid forever, so a clip on the //! clipboard or inside an undo snapshot can never dangle. An index -- not an //! `Arc` -- because [`Clip`] is `Copy`, which is what makes copy/paste a //! plain assignment. //! //! A clip names its equalizer the same way, by index into a second append-only //! table ([`Clip::eq`], [`Project::set_eq`]): the params are a `Vec` of bands //! and a `Copy` clip cannot hold one. So an EQ'd clip that is split hands both //! halves the same settings, a copy carries them, and an undo restores whatever //! index the clip had. Only a *save* prunes the table //! ([`Project::without_orphan_sources`]), for the reason an undone import //! leaves its source entry behind: an index handed out is an index forever. //! //! Its colour grade is a third such table ([`Clip::color`], //! [`Project::set_color`]) and behaves in every way like the equalizer one -- //! shared by equal settings, inherited by both halves of a split, pruned only at //! a save. An index rather than the four floats themselves for the same reason: //! twenty clips graded alike name one entry, and the file writes it once. use crate::map::TimelineMap; use std::path::{Path, PathBuf}; use crate::color::ColorParams; use crate::duck::Duck; use crate::voice::VoiceFx; use crate::eq::EqParams; use crate::limiter::{Limiter, db_to_linear}; use crate::scale::FitPolicy; use crate::subtitle::{Cue as SubtitleCue, SubtitleTrack}; mod multicam; pub use multicam::{AngleSpec, MAX_ANGLES, MulticamMade, MulticamUnder}; mod sequence; pub use sequence::{ MAX_SEQUENCES, MAX_SEQUENCE_NAME_CHARS, Nested, RenderView, SEQ_BASE, SequenceInfo, is_sequence_source, sequence_id_of, sequence_source, }; use sequence::{FlatCache, Seqs}; /// The shortest a cue a cut or trim shortened may be on the timeline, in /// frames (about 0.15 s at 30 fps). Shorter, and [`Project::sub_lane_cue_refs`] /// drops it. const CUT_SLIVER_FRAMES: f64 = 4.0; use crate::text::{Align, HPlace, Outline, Place, SIZE_RANGE, TextStyle, Weight}; use crate::titleanim::{KEY_SCALE_RANGE, TitleAnim, TitleKey}; use crate::color::{ColorKey, MAX_COLOR_KEYS, evaluate_color}; use crate::transform::{ KeyEase, Keyed, MAX_TRANSFORM_KEYS, OpacityKey, TransformKey, TransformParams, ease_shape, evaluate as keyed_transform, evaluate_opacity, keys_after_still_head, keys_within, sample, }; /// How far down a lane's own volume goes ([`Project::set_lane_gain_db`]). /// -60 dB is a thousandth of the amplitude -- a track that is out of the mix /// for every purpose but the honest one, which is that it is still a level and /// not a mute. pub const MIN_GAIN_DB: f32 = -60.0; /// ...and how far up. +12 dB is four times the amplitude, which is as much as /// a mix bus can take before the limiter is doing all the work. pub const MAX_GAIN_DB: f32 = 12.0; /// The longest `[in_us, out_us)` window a [`SubClip`] may show, in /// microseconds: **30 days** of track. /// /// A window is a stretch of *source media*, not of the timeline, so its width /// is the length of the recording a caption was cut from -- and that is not /// bounded by anything a day. A two-day conference stream, a lecture archive, a /// long interview session: all produce a palette row whose `out_us` is tens of /// hours, and a ceiling that refuses those refuses the user's own media back at /// them. The value is therefore "past any recording", and nothing more /// sophisticated: 30 days is a round number four orders of magnitude above the /// longest thing anybody captions and three orders below the point where a /// typed number becomes plausible as a duration at all. /// /// What it has to clear, concretely, and the honest arithmetic for each: /// /// - **The i64 multiply in [`SubClip::window_at`].** `window_us() * into`, /// where `into` is a `u32` frame offset, so at most 4.29e9. It is a /// `saturating_mul`, so this is not about safety -- saturation is total -- /// but about the result still *meaning* something: 30 days x 4.29e9 is /// 1.1e22, which does NOT fit an i64. So the multiply saturates for any /// window above `i64::MAX / u32::MAX` = 2.1e9 us (~35 minutes), and 30 days /// is 1200x past that. The value is then finite but the proportion is not /// exact, and `set_window`'s clamp on the answer keeps the result inside the /// window rather than trusting it. That is the real precision cost, and it /// is why this is not "unlimited": past 35 minutes the cut degrades, past /// this it has degraded to "the far end of the window". /// - **`sub_lane_cues`'s f64 mapping.** `(t - in_us) as f64 * per`, where `per` /// is `frames / fps * 1e6 / window_us`. Here f64 exactness genuinely does /// apply: an i64 is exact in f64 to 2^53 = 9.0e15, and 30 days is 2.6e12 -- /// about 3.5 orders of magnitude inside it, so the cue mapping is exact and /// not merely close. /// - **Nothing else.** There is no allocation, no index and no length derived /// from a window, so a wider value costs no memory. The ceiling is a claim /// about meaning, not about resource. /// /// Past it, the value is not a caption any project holds -- a `.edith` a hand /// edited (or a hostile actor wrote) reaches this with a line the never-empty /// check at the parser cannot see past, because `out_us > in_us` says the /// window is *open* and never how wide it is. Every door -- the parse, /// [`Project::with_subs`], [`Project::place_sub`] -- refuses such a line by /// name, next to every other range check a `.edith` line has to pass. Every /// *composer* instead clamps to it through [`set_window`], which is the only /// function that writes a window; a trim that ran out of window stops at the /// window's end rather than failing, exactly like the frame walls it already /// has. pub const MAX_WINDOW_US: i64 = 30 * 24 * 60 * 60 * 1_000_000; /// How fast a clip plays, in **thousandths of real time**: 1000 is the speed it /// was shot at, 2000 twice that, 500 half. An integer and not an `f32` for /// [`Clip`]'s sake -- a clip is `Copy` *and* `Eq`, a float is neither exactly /// comparable nor exactly writable, and `.edith` has to read back the very /// number that was set. A thousandth is finer than any card can drag and coarser /// than any rounding anyone can hear. /// /// The rate alone: the audio path time-stretches a speeded clip /// (`crate::stretch`), so its pitch stays where it was recorded while its /// seconds compress or stretch -- the tape effect this used to be is gone. /// Nothing here plays backwards -- [`Speed::MIN`] is a quarter speed, /// [`Speed::MAX`] four times. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] pub struct Speed(u16); /// What [`Project::parts`] hands back: the comparable shape of everything an /// edit can change, for the sweep's undo round-trip. Test-only, like every /// accessor it exists for: the sweep lives behind `cfg(test)`. #[cfg(test)] #[derive(Clone, Debug, PartialEq)] pub(crate) struct SweepParts( pub(crate) usize, pub(crate) Vec<(LaneKind, Vec)>, pub(crate) Vec>, /// The palette, which the history holds beside the lanes and an undo /// therefore restores ([`Project::undo`]). Left out of this shape, a /// round-trip check passed with the palette restore deleted outright. pub(crate) Vec, pub(crate) ProjectSettings, ); impl Speed { /// Real time -- what every clip is until something says otherwise, and the /// value every path in this engine short-circuits on. pub const NORMAL: Speed = Speed(1000); pub const MIN: Speed = Speed(250); pub const MAX: Speed = Speed(4000); /// Clamped into `[MIN, MAX]`, which is what makes a zero (or a reverse) /// unrepresentable rather than merely refused: nothing downstream divides /// by a speed that could be nothing. pub fn from_permille(permille: u16) -> Self { Speed(permille.clamp(Self::MIN.0, Self::MAX.0)) } pub fn permille(self) -> u16 { self.0 } pub fn is_normal(self) -> bool { self == Self::NORMAL } /// As a multiplier, for the sound worker and for a label. pub fn as_f64(self) -> f64 { f64::from(self.0) / 1000. } /// How many *timeline* frames `source` source frames occupy at this rate -- /// the clip's footprint. Rounded to the nearest frame and **never zero**: a /// clip that occupied no frames would be an empty placement, which the lane /// invariant forbids and which nothing could ever click on again. pub fn frames(self, source: u32) -> u32 { let n = (u64::from(source) * 1000 + u64::from(self.0) / 2) / u64::from(self.0); n.clamp(1, u64::from(u32::MAX)) as u32 } /// The mapping itself: which source frame of a clip the `offset`th timeline /// frame of it plays. Floored, so the first timeline frame is the clip's own /// in-point at every rate. pub fn source_at(self, offset: u32) -> u32 { (u64::from(offset) * u64::from(self.0) / 1000).min(u64::from(u32::MAX)) as u32 } /// Its inverse, for a decoder's frames on their way back out: which timeline /// frame of the clip the `offset`th *source* frame of it lands on. /// /// **Ceil, and that is the whole point of the pair.** Playback stamps every /// decoded frame with this and shows the newest whose stamp has come due, so /// what is on screen at timeline offset `d` is the last source frame with /// `timeline_at(s) <= d` -- and with the floor above and the ceil here that /// frame is exactly [`source_at`](Speed::source_at)`(d)`, the frame an /// export encodes there, at every rate (`speed_maps_both_ways`). Rounding /// the two the same way would put the preview a frame off the export at some /// speeds and a whole held frame off at slow ones -- drift that no test of /// either side alone would catch. pub fn timeline_at(self, offset: u32) -> u32 { (u64::from(offset) * 1000) .div_ceil(u64::from(self.0)) .min(u64::from(u32::MAX)) as u32 } /// The longest source range that still *fits* in `frames` timeline frames at /// this rate -- and `None` when **nothing** does, which at a quarter speed is /// any room narrower than the four timeline frames one source frame occupies. /// /// What a trim commits and what a hole-punch leaves behind: rounding could /// otherwise hand back a range wider than the room, and a clip that outgrew /// its room would overlap its neighbour -- the one thing a lane may never do. /// `None` is not an error, it is "no remainder": the caller drops that piece /// (see [`clear`]) or refuses the edit ([`Project::trim`]). At real time this /// is `frames` itself, so nothing about an unspeeded edit changes. pub fn fit(self, frames: u32) -> Option { if frames == 0 || self.frames(1) > frames { return None; } // The floored estimate is a step or two off in either direction: `frames` // rounds, so at a slow rate the estimate can sit one short of the longest // range that fits (0.333x: 48 source frames take 144 timeline frames, and // `source_at(144)` is 47), and at a fast one one over. Both walked, so the // answer is the longest range that fits, exactly -- which is what a wall // ([`Project::trim_room`]) promises an edge can reach. let mut src = self.source_at(frames).max(1); while self.frames(src + 1) <= frames { src += 1; } while src > 1 && self.frames(src) > frames { src -= 1; } Some(src) } /// How many timeline frames `source` source frames of head (or tail) are /// worth -- what [`Project::trim_room`] measures a wall in. Zero for none of /// them, which is why this is not [`frames`](Speed::frames): a clip is never /// empty, but the room in front of one may well be. pub fn room(self, source: u32) -> u32 { match source { 0 => 0, n => self.frames(n), } } /// How many timeline frames from `offset` on show the **same** source frame: /// one at real time and faster, more when the clip is slowed. What an export /// encodes without decoding the same picture twice, and the count that makes /// its frame walk exactly as long as the span. pub fn repeats(self, offset: u32, len: u32) -> u32 { let want = self.source_at(offset); (offset..len) .take_while(|&t| self.source_at(t) == want) .count() as u32 } /// Where in the source a cut `offset` timeline frames into a clip of `len` /// source frames falls -- and `None` when this rate cannot address that /// frame at all. /// /// At half speed a clip of ten source frames is twenty timeline frames long /// and every source frame is on screen twice, so a cut between the two /// showings of one frame is not a cut in the *file*: taking it would leave /// two halves whose lengths no longer add up to the clip that was cut -- /// a hole in the lane, or an overlap of the next clip. Refused instead, so /// the model stays exact and the front-end can say why. At real time every /// interior frame answers, which is the path a project without speeds is on. pub fn split_at(self, len: u32, offset: u32) -> Option { let src = self.source_at(offset); (src > 0 && src < len && self.frames(src) == offset && self.frames(len - src) == self.frames(len) - offset) .then_some(src) } } /// Most keys a speed ramp may hold ([`Clip::speed_keys`]). A fixed cap, so that a /// [`Span`] carrying a ramp stays `Copy`. pub const MAX_SPEED_KEYS: usize = 8; /// How long a still image is placed for when the project does not say (the 5 s every project had /// before v52), and the shortest and longest length a project may set for one. pub const STILL_DEFAULT_SECS: f64 = 5.0; pub const STILL_MIN_SECS: f64 = 0.5; pub const STILL_MAX_SECS: f64 = 600.0; /// One point of a clip's speed ramp ([`Clip::speed_keys`]): the speed `speed` at /// the clip-relative timeline frame `at`. Between points the speed is linear and /// it holds before the first and after the last. Speeds are [`Speed`]s, in /// thousandths like the constant one, so a ramp reads back exactly as it was set. #[derive(Clone, Copy, PartialEq, Eq, Debug, Default)] pub struct SpeedKey { pub at: u32, pub speed: Speed, } /// The four ramp shapes a clip's speed can be laid in: the speed card and the palette lay them /// across a clip's length, and the MCP `speed_ramp` takes them by name ([`SpeedRampShape::name`]). #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum SpeedRampShape { /// Real time easing up to double. SpeedUp, /// Double easing down to real time. SlowDown, /// Slow, then fast through the middle, then slow: the classic speed ramp. FastMiddle, /// Fast, then slow through the middle, then fast: the hero shot. SlowMiddle, } impl SpeedRampShape { /// Every shape, in the order the speed card shows them. pub const ALL: [SpeedRampShape; 4] = [ SpeedRampShape::SpeedUp, SpeedRampShape::SlowDown, SpeedRampShape::FastMiddle, SpeedRampShape::SlowMiddle, ]; /// The shape's name as the MCP takes it: `speed_up`, `slow_down`, `fast_middle` or `slow_middle`. pub fn name(self) -> &'static str { match self { SpeedRampShape::SpeedUp => "speed_up", SpeedRampShape::SlowDown => "slow_down", SpeedRampShape::FastMiddle => "fast_middle", SpeedRampShape::SlowMiddle => "slow_middle", } } /// The keys of the shape over a clip `frames` timeline frames long. `None` when the clip is /// too short to hold the shape's keys one frame apart. pub fn keys(self, frames: u32) -> Option> { let key = |at: u32, permille: u16| SpeedKey { at, speed: Speed::from_permille(permille) }; match self { SpeedRampShape::SpeedUp => (frames >= 1).then(|| vec![key(0, 1000), key(frames, 2000)]), SpeedRampShape::SlowDown => (frames >= 1).then(|| vec![key(0, 2000), key(frames, 1000)]), SpeedRampShape::FastMiddle => { (frames >= 2).then(|| vec![key(0, 500), key(frames / 2, 2000), key(frames, 500)]) } SpeedRampShape::SlowMiddle => { (frames >= 2).then(|| vec![key(0, 2000), key(frames / 2, 500), key(frames, 2000)]) } } } } /// The source position a speed ramp has carried a clip through by its clip-relative /// timeline frame `t`: the integral of the piecewise-linear speed from frame 0 to /// `t`, in source frames (fractional). Each segment's integral is the trapezoid of /// its two ends, exact for a linear speed, so this is the closed form and needs no /// table. `keys` must be sorted by `at` with no two alike (see /// [`valid_speed_keys`]); empty keys are real time. /// /// A 1x-to-2x ramp over 60 frames -- keys `(0, 1x), (60, 2x)` -- gives 90 at frame 60. pub fn ramp_source_at(keys: &[SpeedKey], t: f64) -> f64 { let Some(first) = keys.first() else { return t.max(0.0); }; let t = t.max(0.0); // Held before the first key at its speed. let mut acc = t.min(f64::from(first.at)) * first.speed.as_f64(); for w in keys.windows(2) { let (a, b) = (w[0], w[1]); let (ta, tb) = (f64::from(a.at), f64::from(b.at)); if t <= ta { return acc; } let (ra, rb) = (a.speed.as_f64(), b.speed.as_f64()); let d = t.min(tb) - ta; // The speed at the end of the part walked: linear between the two keys. let rate_there = ra + (rb - ra) * d / (tb - ta); acc += d * (ra + rate_there) / 2.; if t <= tb { return acc; } } // Held after the last key. let last = keys[keys.len() - 1]; acc + (t - f64::from(last.at)).max(0.) * last.speed.as_f64() } /// The inverse of [`ramp_source_at`]: the clip-relative timeline frame (fractional) at /// which a ramp has carried the clip through `source` source frames. Exact to the /// float -- the integral is strictly increasing, so the bisection has one answer. /// Empty keys are real time. pub fn ramp_timeline_at(keys: &[SpeedKey], source: f64) -> f64 { if keys.is_empty() || source <= 0.0 { return source.max(0.0); } let (mut lo, mut hi) = (0., 4. * source + 1.); for _ in 0..64 { let mid = (lo + hi) / 2.; if ramp_source_at(keys, mid) < source { lo = mid; } else { hi = mid; } } (lo + hi) / 2. } /// Timeline frames a ramped clip of `source` source frames occupies: the clip-relative /// frame at which [`ramp_source_at`] reaches `source`, rounded, and never zero. The /// ramp's own footprint, the way [`Speed::frames`] is the constant one's. /// /// Solved by bisection: the integral is strictly increasing (every speed is at least /// [`Speed::MIN`]), so the answer is unique and the bisection is exact to the float. pub fn ramp_frames(keys: &[SpeedKey], source: u32) -> u32 { let target = f64::from(source); // Every speed is at least a quarter, so frame `4 * source` has covered `source`. let (mut lo, mut hi) = (0., 4. * target + 1.); for _ in 0..64 { let mid = (lo + hi) / 2.; if ramp_source_at(keys, mid) < target { lo = mid; } else { hi = mid; } } ((lo + hi) / 2.).round().max(1.) as u32 } /// Whether `keys` is a ramp the engine will play: between one and /// [`MAX_SPEED_KEYS`] keys, strictly increasing in `at`. The speeds are in range by /// construction. pub fn valid_speed_keys(keys: &[SpeedKey]) -> bool { (1..=MAX_SPEED_KEYS).contains(&keys.len()) && keys.windows(2).all(|w| w[0].at < w[1].at) } /// A clip's speed ramp as a [`Span`] carries it: the keys in a fixed array (so the /// span stays `Copy`) and the clip-relative frame the span starts at, which is what /// its own offsets are counted from. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct SpeedRamp { keys: [SpeedKey; MAX_SPEED_KEYS], len: u8, base: u32, } impl SpeedRamp { /// The ramp `keys` (valid, see [`valid_speed_keys`]) for a span starting `base` /// clip-relative frames into the clip. pub fn new(keys: &[SpeedKey], base: u32) -> Self { let mut arr = [SpeedKey::default(); MAX_SPEED_KEYS]; arr[..keys.len()].copy_from_slice(keys); Self { keys: arr, len: keys.len() as u8, base, } } fn keys(&self) -> &[SpeedKey] { &self.keys[..usize::from(self.len)] } /// The same ramp with every speed multiplied by `by`: what a pitch shift asks of the /// stretch that follows a ramp, which runs `1 / 2^(pitch/12)` times as fast. pub fn scaled(self, by: f64) -> Self { let mut keys = self.keys; for k in &mut keys[..usize::from(self.len)] { k.speed = Speed::from_permille((k.speed.as_f64() * by * 1000.0).round() as u16); } Self { keys, ..self } } /// The same ramp for a span that starts `by` timeline frames later on it, which is /// what a cut at the span's head does to where its offsets are counted from. pub fn advanced(self, by: u32) -> Self { Self { base: self.base + by, ..self } } /// Source frames from the span's first frame to its `offset`th, as the difference /// of two floored source positions -- so `from + source_offset(o)` is the same /// frame [`source_pos`] puts `base + o` on, with no rounding drift between them. pub fn source_offset(&self, offset: u32) -> u32 { let at = |o: u32| ramp_source_at(self.keys(), f64::from(self.base) + f64::from(o)).floor(); (at(offset) - at(0)).max(0.) as u32 } /// Source frames (fractional) from the floored source position of the span's first /// frame to the position `offset` timeline frames (fractional) into the span: what /// [`source_offset`](SpeedRamp::source_offset) is without the floors, from the very /// frame `from` names. The audio follows the ramp through this. pub fn source_frames(&self, offset: f64) -> f64 { let base = f64::from(self.base); ramp_source_at(self.keys(), base + offset.max(0.0)) - ramp_source_at(self.keys(), base).floor() } /// The inverse of [`source_frames`](SpeedRamp::source_frames): the timeline frame /// (fractional) into the span at which the ramp has read `source` source frames /// of it. pub fn timeline_frames(&self, source: f64) -> f64 { let base = f64::from(self.base); let target = source.max(0.0) + ramp_source_at(self.keys(), base).floor(); (ramp_timeline_at(self.keys(), target) - base).max(0.0) } /// The inverse of [`source_offset`](SpeedRamp::source_offset): the first offset /// whose frame is `source` or past it, which is [`Speed::timeline_at`] for a ramp. pub fn timeline_at(&self, source: u32) -> u32 { let (mut lo, mut hi) = (0u32, source.saturating_mul(4).saturating_add(8)); while lo < hi { let mid = lo + (hi - lo) / 2; if self.source_offset(mid) >= source { hi = mid; } else { lo = mid + 1; } } lo } } impl Default for Speed { fn default() -> Self { Self::NORMAL } } impl std::fmt::Display for Speed { /// `2.00x` -- what a card prints and what a clip box is marked with. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:.2}x", self.as_f64()) } } /// What a file's own frame rate is against the timeline's, as an exact /// rational: how many frames of the *file* one frame of the timeline is worth. /// /// Every frame number in this module counts **timeline** frames -- a clip's /// `in_frame` and `out_frame` included -- so a 23.976 fps file placed on a 30 fps /// timeline is as many frames long as the seconds it lasts, and every edit /// (trim, split, speed, paste) is the same arithmetic it always was. This is the /// one conversion, and it happens at the decoder's door: [`PlaybackSession`] /// opens a worker with it and [`crate::export`] pulls pictures through it. The /// file's own numbering exists nowhere else. /// /// The same floor/ceil pair as [`Speed`], for the same reason: the frame an /// export encodes at a timeline frame has to be the frame playback is holding /// there. Composed *after* a speed rather than folded into it, so a 2x 24 fps /// clip on a 30 fps timeline is exactly both (`rate_composes_with_speed`). /// /// Rational and not an `f64`, exactly: 24000/1001 over 30 is `800/1001` and /// stays `800/1001` however far the timeline runs, where a rate rounded to /// (say) milli-fps would leave a fraction of a frame per clip to pile up into a /// visible drift over an hour (`a_23_976_rate_is_exact_and_never_drifts`). /// /// [`PlaybackSession`]: crate::PlaybackSession #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct Rate { /// Frames of the file... source: u32, /// ...per this many frames of the timeline it plays on. Never zero. timeline: u32, } impl Rate { /// A file shot at the timeline's own rate: every conversion below is the /// identity, which is the path a single-rate project is on. pub const REAL_TIME: Self = Self { source: 1, timeline: 1, }; /// The rate of a file at `source_fps` on a timeline at `timeline_fps`, /// exactly. /// /// Both rates come out of a container as a division, so neither is ever the /// number it means (`23.976023976...`); [`crate::mux::frame_timing`] is what /// already knows how to name one exactly -- it is the same pair the muxer /// counts an export's frames in -- and this is that pair divided by that /// pair, reduced. A rate no timescale can name is an `Err` in that /// function's own words rather than a silent 1:1 -- `matches_timeline` is /// where a file carrying one is refused by name, beside the codec. /// /// Exactly [`REAL_TIME`](Self::REAL_TIME) when the two agree, which is every /// timeline that had ever opened in this editor before mixed rates. pub fn from_fps(source_fps: f64, timeline_fps: f64) -> crate::Result { let (src_scale, src_ticks) = crate::mux::frame_timing(source_fps)?; let (tl_scale, tl_ticks) = crate::mux::frame_timing(timeline_fps)?; // fps is scale/ticks, so the ratio is (src_scale/src_ticks) / (tl_scale/tl_ticks). let num = u64::from(src_scale) * u64::from(tl_ticks); let den = u64::from(src_ticks) * u64::from(tl_scale); Ok(Self::reduced(num, den)) } /// `num` frames of a file per `den` frames of a timeline, in lowest terms. fn reduced(num: u64, den: u64) -> Self { let g = gcd(num, den); let (mut num, mut den) = (num / g, den / g); // A ratio no pair of real frame rates produces, but the fields are `u32` // so that every multiplication below fits a `u64` at any frame count. while num > u64::from(u32::MAX) || den > u64::from(u32::MAX) { (num, den) = (num.div_ceil(2), den.div_ceil(2)); } Self { source: num.max(1) as u32, timeline: den.max(1) as u32, } } /// The same file against a timeline that has itself been retimed: `self` is /// its frames per frame of the old timeline and `k` is old frames per new /// one, so this is its frames per frame of the *new* one -- exactly, without /// going back through an `f64` fps that names neither rate /// ([`crate::PlaybackSession::set_frame_rate`]). pub fn then(self, k: Rate) -> Self { Self::reduced( u64::from(self.source) * u64::from(k.source), u64::from(self.timeline) * u64::from(k.timeline), ) } pub fn is_real_time(self) -> bool { self.source == self.timeline } /// As a multiplier of the timeline's rate: what a *file's* own frame rate /// is, given the timeline's, for a library row that names it. pub fn as_f64(self) -> f64 { f64::from(self.source) / f64::from(self.timeline) } /// Which frame of the file the `frame`th timeline-rate frame of it is: /// floored, [`Speed::source_at`]'s half of the pair. pub fn source_at(self, frame: u32) -> u32 { (u64::from(frame) * u64::from(self.source) / u64::from(self.timeline)) .min(u64::from(u32::MAX)) as u32 } /// Its inverse, ceiled ([`Speed::timeline_at`]'s half): the first /// timeline-rate frame that shows source frame `source_frame` -- which is /// the stamp playback puts on a decoded picture, and, applied to a file's /// frame *count*, how long that file is in timeline frames. pub fn timeline_at(self, source_frame: u32) -> u32 { (u64::from(source_frame) * u64::from(self.timeline)) .div_ceil(u64::from(self.source)) .min(u64::from(u32::MAX)) as u32 } /// The same map rounded to the *nearest* timeline frame, halves up, instead /// of up: what a retime ([`Project::retime`]) lands every boundary, length /// and envelope on. Ceiling every one of them biased each round trip later /// by up to a frame per boundary, so a dissolve and a voiceover clip grew; /// rounding to nearest is unbiased, so a boundary is never more than half a /// frame from where its seconds put it, and a round trip through a grid that /// holds the frame exactly returns it exactly. pub fn timeline_nearest(self, source_frame: u32) -> u32 { let num = u64::from(source_frame) * u64::from(self.timeline) * 2 + u64::from(self.source); (num / (2 * u64::from(self.source))).min(u64::from(u32::MAX)) as u32 } } /// The source frames a clip has left past its in-point, on a table of `frames` /// (its lane's length of the source): the wall an end edge is measured to /// ([`Project::clip_walls`]), as a count. Never less than one frame. fn source_left(clip: &Clip, frames: &[u32]) -> u32 { frames .get(clip.source) .copied() .unwrap_or(clip.out_frame) .saturating_sub(clip.in_frame) .max(1) } /// Timeline frames `n` source frames of `c` occupy: [`Speed::room`] at its constant /// rate, the ramp's footprint for a ramped clip. Zero for none. fn footprint(c: &Clip, n: u32) -> u32 { match n { 0 => 0, n if c.speed_keys.is_empty() => c.speed.frames(n), n => ramp_frames(&c.speed_keys, n), } } /// The most source an `edge` trim of `clip` may keep, on a table of `frames`: the /// side of the window the trim grows is the out-point at a forward clip's end and at /// a reversed clip's start (its head is the window's far end), and the in-point /// (which cannot go below the file's first frame) at a reversed clip's end. /// Unbounded where the edge's own wall already is the bound. fn keep_cap(clip: &Clip, edge: Edge, frames: &[u32]) -> u32 { match (edge, clip.reversed) { (Edge::End, false) | (Edge::Start, true) => source_left(clip, frames), (Edge::Start, false) => u32::MAX, (Edge::End, true) => clip.out_frame.max(1), } } /// Writes the trim of `c`'s `edge` to timeline frame `to`, keeping `keep` source /// frames: the frames that stay play what they played, so the window's end that is /// *not* at the edge is the one held still. fn write_trim(c: &mut Clip, edge: Edge, to: u32, keep: u32, still: bool) { match (edge, c.reversed && !still) { // A still has no earlier frame to walk an in-point back to -- // every frame of it is the same picture -- so its head grows // *forward*: the range it plays is however long the new room is, // anchored at the source's frame 0. Bounded by `lo`, which is where // the cap in `source_frames` is applied. (Edge::Start, _) if still => { c.in_frame = 0; c.out_frame = keep; c.start = to; } // Non-negative by `lo`, which is what keeps the in-point on the source. (Edge::Start, false) => { c.in_frame = c.out_frame - keep.min(c.out_frame); c.start = to; } (Edge::Start, true) => { c.out_frame = c.in_frame + keep; c.start = to; } // Never wider than the room the edge was clamped to: see [`Speed::fit`]. (Edge::End, false) => c.out_frame = c.in_frame + keep, (Edge::End, true) => c.in_frame = c.out_frame - keep.min(c.out_frame), } } /// Greatest common divisor, for reducing a [`Rate`] to the numbers that fit its /// fields. `a` for `gcd(a, 0)`, and never zero: something divides by it. fn gcd(a: u64, b: u64) -> u64 { match b { 0 => a.max(1), b => gcd(b, a % b), } } /// A half-open `[in_frame, out_frame)` range of frames of source /// [`source`](Clip::source), placed at timeline frame [`start`](Clip::start). /// Never empty. /// /// Counted at the **timeline's** frame rate, not the file's: a source shot at /// another rate is converted at the decoder's door ([`Rate`]), so every edit /// here is frames of one clock. #[derive(Clone, PartialEq, Debug)] pub struct Clip { /// Timeline frame the clip's first frame is shown at. Meaningless on a /// clipboard copy -- [`Project::paste`] and [`Project::place`] overwrite it /// with where the caller asked for. pub start: u32, pub in_frame: u32, pub out_frame: u32, /// Index into [`Project::sources`]. pub source: usize, /// Group id: clips sharing one were split from the same take. `None` for a /// clip that belongs to no group. pub link: Option, /// Index into the project's equalizer table /// ([`Project::eq_of`]/[`Project::set_eq`]), or `None` for a clip that plays /// flat. An index -- not the params -- for [`Clip::source`]'s reason: this /// stays `Copy`, so an EQ'd clip survives a copy, a paste and an undo as a /// plain assignment. The table is append-only within a session, so an index /// on a clipboard clip or inside an undo snapshot can never dangle. pub eq: Option, /// Index into the project's colour table /// ([`Project::color_of`]/[`Project::set_color`]), or `None` for a clip that /// plays ungraded. An index for [`Clip::eq`]'s reason, and with the same /// promises: `Copy`, append-only within a session, never dangling. pub color: Option, /// Index into the project's transform table /// ([`Project::transform_of`]/[`Project::set_transform`]), or `None` for a /// clip that plays at its fit policy's own placement. An index for /// [`Clip::color`]'s reason and with the same promises. pub transform: Option, /// How this clip's picture meets a project canvas of another shape /// ([`crate::scale::FitPolicy`]). Inline rather than a table index like the /// two above: it is one byte with no parameters, so there is nothing for a /// table to share, and a clip that is the project's own size never consults /// it at all. [`FitPolicy::Fit`] is the default -- the whole picture, bars /// where the aspect does not agree. pub fit: FitPolicy, /// How fast it plays ([`Speed`]). Inline like [`Clip::fit`] and unlike the /// eq and colour indexes: it is two bytes with nothing to share, and a table /// would put a level of indirection between the mapping and the one number /// every frame of it is divided by. /// /// It does **not** move `in_frame`/`out_frame`: those stay the source range /// the clip plays, so a trim and a split still address the file. What it /// changes is how many *timeline* frames that range is spread over /// ([`Clip::frames`]). pub speed: Speed, /// Timeline frames of ramp-up from silence at the clip's own start, inline /// like [`Clip::fit`] and [`Clip::speed`] for the same reason: one small /// number with nothing to share, and every consumer (mixer, exporter) wants /// it sitting right next to the frame range it shapes. Clamped to the /// clip's own length by the setter -- never wider than the clip is long. pub fade_in: u32, /// Timeline frames of ramp-down to silence at the clip's own end. Same /// promises as [`Clip::fade_in`]. pub fade_out: u32, /// Timeline frames of cross-dissolve into the clip immediately after this /// one on the same video lane, at this clip's own end -- `0` for a hard /// cut. Inline like [`Clip::fade_in`]/[`Clip::fade_out`] for the same /// reason, and clamped the same way by its setter /// ([`Project::set_transition_out`]): never wider than this clip's own /// length, nor than the successor it dissolves into. Meaningless -- and /// left as whatever it was -- the moment the next clip stops abutting /// this one; a reader that cares checks adjacency itself rather than /// trusting this field alone, exactly as a stored fade is trusted only /// because a clip's own length is checked beside it. pub transition_out: u32, /// How [`Clip::transition_out`] is drawn: the picture's blend over the /// overlap ([`crate::transitions`]). The sound is a crossfade whatever it /// is. Older project files have no field and mean a dissolve. pub transition_kind: crate::transitions::TransitionKind, /// Non-zero: this clip is a waveform of its source's sound, painted /// onto a video lane. Bit 0 is the flag; bits 1-2 style; bit 3 fast. /// Zero is an ordinary picture. Older project files have no field and /// mean zero. pub visualizer: u8, /// Packed look: hue 0-255, sat 0-255, strands 0-255, glow 0-255 in the /// low four bytes as they have been since v22, then the strand colour's /// own bytes above them -- its hue at 32 and sat at 40 /// ([`crate::decode::viz_strand_hue_ui`]) -- with the top bit as /// [`crate::decode::viz_strand_set`]. All-zero is the default (timeline /// azure, full sat, 8 strands, glow 2, no strand colour of its own). /// Older files have no field and mean zero; a v22 file's 32-bit value /// is the low half of this one, byte for byte. pub viz_paint: u64, /// The clip's own volume in dB, on top of its lane's /// ([`Project::lane_gain_db`]). `0.0` is unity and the default; the setter /// keeps it within [`MIN_CLIP_GAIN_DB`]..=[`MAX_CLIP_GAIN_DB`]. It is /// applied as a linear multiplier inside the clip's own envelope /// ([`Fade`]), so playback and export hear the same number. Audio clips /// only. Not `Eq`: a float, and nothing compared clips by it. pub gain_db: f32, /// The clip's volume envelope: points of dB keyed to SOURCE frames, sorted /// by [`GainKey::at`] with no two alike. Empty is 0 dB. Source-keyed, so a /// move, a trim or a ripple keeps each point on the same audio, and a split /// keeps the whole curve on both halves. It rides on top of /// [`Clip::gain_db`] and the fades, inside the same [`Fade`]. Audio clips /// only ([`Project::set_gain_keys`]). Capped at [`MAX_GAIN_KEYS`]. pub gain_keys: Vec, /// Where the clip sits between the speakers, in whole percent: `-100` (left) to /// `100` (right), `0` the default and the middle. One byte, because it sits in the /// padding where an `f32` would have grown the clip. A balance on a stereo mix: the far /// side is turned down, the near one left alone. Rides in the clip's [`Fade`] like its /// gain. Audio clips only ([`Project::set_clip_pan`]); [`Clip::pan_value`] reads it as /// `-1.0..=1.0`. pub pan: i8, /// The clip's pitch in whole semitones, `-12` (an octave down) to `12` (up), `0` the /// default and the original sound. Changes the pitch and keeps the speed: the /// stretch and the resample that make it are built by [`Project::lane_speeds_from`], /// so playback and export hear the same thing. Audio clips only /// ([`Project::set_clip_pitch`]). One byte, in the same padding as [`Clip::pan`]. pub pitch: i8, /// The clip's placement animation: whole [`TransformParams`] at SOURCE /// frames, sorted by [`TransformKey::at`] with no two alike. Empty is the /// single [`Clip::transform`] for the whole clip. Video lanes only /// ([`Project::set_transform_keys`]). Kept on the footage the way the /// volume points are: a trim leaves them, and a split divides them with a /// key at the cut ([`split_transform_keys`]). pub transform_keys: Vec, /// The clip's opacity animation: `0..=1` at SOURCE frames, sorted by /// [`OpacityKey::at`] with no two alike. With keys present they give the /// opacity (replacing [`TransformParams::opacity`], whose own keys are then /// not read for it); the fades still multiply on top. Empty is the /// transform's constant opacity. Video lanes only /// ([`Project::set_opacity_keys`]); kept on the footage as the placement /// keys are. pub opacity_keys: Vec, /// The clip's colour animation: the whole grade at SOURCE frames, sorted by /// [`ColorKey::at`] with no two alike. With keys present they give the /// grade (replacing the clip's constant one). Video lanes only /// ([`Project::set_color_keys`]); kept on the footage as the placement keys are. pub color_keys: Vec, /// The clip's speed ramp: keys of speed at CLIP-relative timeline frames, sorted /// by [`SpeedKey::at`] with no two alike, at most [`MAX_SPEED_KEYS`]. Empty is /// the constant [`Clip::speed`], which every path not named otherwise keeps. With /// keys present the ramp replaces the constant: [`Clip::frames`] is the ramp's /// footprint and [`source_pos`] the ramp's mapping, and [`Clip::speed`] is left /// as it was, to come back when the ramp is cleared. pub speed_keys: Vec, /// Whether the clip plays its source window backwards: the timeline's first /// frame shows the window's last source frame, the picture and the sound both. /// Composes with [`Clip::speed`] and [`Clip::speed_keys`] (the rate is how fast /// the window is walked, from its far end), leaves the window ([`Clip::in_frame`] /// and [`Clip::out_frame`]) and the footprint ([`Clip::frames`]) as they were, and /// keeps every key where it plays on the timeline ([`Project::set_reverse`] /// mirrors the source-keyed ones when it flips). Fades are timeline-relative and /// need nothing. pub reversed: bool, /// The clip's effects stack ([`crate::effects`]), drawn in order on its /// picture before it is laid over the lanes beneath. Empty, or every effect /// switched off, is no effect. Video lanes only ([`Project::set_effects`]); a /// split gives both halves the whole stack. pub effects: Vec, /// The clip's stabilisation, `None` for off ([`crate::stabilise`]): the /// camera shake measured from its footage is taken out of each picture /// before its placement ([`Clip::transform`], [`Clip::transform_keys`]) is /// applied, read by the file frame shown, so it survives trims, speed, /// ramps and reverse. pub stabilise: Option, /// Which angle of a multicam clip this clip shows ([`Angle`]); the default, angle 1, for /// every clip that is not a multicam clip. pub angle: Angle, /// How an interlaced source is made progressive ([`Deinterlace`]). Only /// matters for a clip whose source is interlaced; kept on the clip so a /// split, a copy, a nest and an undo carry it like any other setting. pub deinterlace: Deinterlace, /// The clip's colour label and whether it is switched off ([`ClipMark`]). pub mark: ClipMark, } /// The number of colour labels a clip can carry ([`LABELS`]); label 0 is none. pub const LABEL_COUNT: u8 = 8; /// The fixed palette of clip labels, as Premiere names them: a clip with label `n` (1-based) is /// tinted `LABELS[n - 1]`. Label 0 is no label. pub const LABELS: [(&str, [u8; 3]); LABEL_COUNT as usize] = [ ("Violet", [0x9a, 0x6a, 0xd0]), ("Iris", [0x5a, 0x6c, 0xd8]), ("Caribbean", [0x2a, 0xa5, 0x9a]), ("Lavender", [0xc0, 0x8a, 0xc4]), ("Cerulean", [0x3a, 0x9a, 0xd6]), ("Forest", [0x3e, 0x8a, 0x4a]), ("Rose", [0xd8, 0x5a, 0x8a]), ("Mango", [0xe0, 0x93, 0x2c]), ]; /// The name of label `n` (1-based), `None` for no label or one past the palette. pub fn label_name(n: u8) -> Option<&'static str> { LABELS.get(usize::from(n).checked_sub(1)?).map(|l| l.0) } /// The label number a name (any case) or a number spells; `Some(0)` for `none`. pub fn label_by_name(name: &str) -> Option { let name = name.trim(); if name.eq_ignore_ascii_case("none") || name == "0" { return Some(0); } if let Ok(n) = name.parse::() { return (n <= LABEL_COUNT).then_some(n); } LABELS.iter().position(|l| l.0.eq_ignore_ascii_case(name)).map(|i| i as u8 + 1) } /// What a clip carries that no render reads but the person editing does -- its colour label -- /// and the switch that takes it out of the render ([`Clip::mark`]). #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)] pub struct ClipMark { /// 0 for none, else 1..=[`LABEL_COUNT`] ([`LABELS`]). pub label: u8, /// Switched off: the clip stays on the timeline but is skipped by playback, export and the /// mix, as if its stretch were a gap. pub off: bool, } impl ClipMark { /// Whether this is the default (nothing to write to a file). pub fn is_default(self) -> bool { self == ClipMark::default() } } /// The angle a multicam clip ([`Project::make_multicam`]) shows: `video` picks the picture /// and, unless `audio` is set, the sound too (audio follows video). Angles count from 0. #[derive(Clone, Copy, PartialEq, Eq, Debug, Default)] pub struct Angle { pub video: u8, /// The angle the sound is taken from when it does not follow the picture. pub audio: Option, } impl Angle { /// The angle `kind` plays: the picture's own, or the sound's (which follows the picture /// unless pinned). pub fn of(self, kind: LaneKind) -> u8 { match kind { LaneKind::Audio => self.audio.unwrap_or(self.video), _ => self.video, } } /// Whether this is the default (nothing to write to a file). pub fn is_default(self) -> bool { self == Angle::default() } } /// How a clip's interlaced source is turned into progressive pictures. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum Deinterlace { /// One picture per frame (the two fields blended by the deinterlacer). #[default] Frame, /// One picture per field (double rate). Field, /// Leave the fields woven as they are. Off, /// Remove pulldown when the file shows a stable 3:2 cadence (or states soft /// pulldown), otherwise deinterlace as `Frame` ([`crate::ivtc`]). IvtcAuto, /// Remove pulldown: 23.976p film frames back out of 29.97i, whether or not the /// cadence is clear ([`crate::ivtc`]). Ivtc, } impl Deinterlace { pub const ALL: [Deinterlace; 5] = [Deinterlace::Frame, Deinterlace::Field, Deinterlace::Off, Deinterlace::IvtcAuto, Deinterlace::Ivtc]; /// The word a file and the MCP use: `frame`, `field`, `off`. pub fn name(self) -> &'static str { match self { Deinterlace::Frame => "frame", Deinterlace::Field => "field", Deinterlace::Off => "off", Deinterlace::IvtcAuto => "ivtc-auto", Deinterlace::Ivtc => "ivtc", } } pub fn from_name(name: &str) -> Option { Self::ALL.into_iter().find(|d| d.name() == name) } } /// The quietest a clip's own gain can be set, in dB ([`Project::set_clip_gain`]). pub const MIN_CLIP_GAIN_DB: f32 = -60.0; /// The loudest a clip's own gain can be set, in dB ([`Project::set_clip_gain`]). pub const MAX_CLIP_GAIN_DB: f32 = 24.0; /// The widest a clip's pitch shift may be, in semitones either way ([`Clip::pitch`]). pub const MAX_CLIP_PITCH: i8 = 12; /// The lowest a clip's pitch shift may be ([`Clip::pitch`]). pub const MIN_CLIP_PITCH: i8 = -12; /// The most points a clip's gain envelope may hold ([`Clip::gain_keys`]). pub const MAX_GAIN_KEYS: usize = 256; /// One point of a clip's volume envelope ([`Clip::gain_keys`]): `db` at the /// source frame `at`, which is the clip's in/out unit -- media time, counted at /// the timeline's fps. Between points the dB is linear; before the first and /// after the last it holds. #[derive(Clone, Copy, PartialEq, Debug)] pub struct GainKey { /// Source frame of the point. pub at: u32, /// Gain there, in dB, within [`MIN_CLIP_GAIN_DB`]..=[`MAX_CLIP_GAIN_DB`]. pub db: f32, /// The easing of the segment that leaves this point ([`KeyEase::Linear`] /// unless set). pub ease: KeyEase, } /// Which animation a keyframe button or action is about: the placement /// ([`Clip::transform_keys`]), the opacity ([`Clip::opacity_keys`]) or the grade /// ([`Clip::color_keys`]). #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum KeyKind { Placement, Opacity, Color, } impl GainKey { /// A linear point: `db` at source frame `at`. pub fn new(at: u32, db: f32) -> Self { Self { at, db, ease: KeyEase::Linear } } } /// The envelope's dB at source position `src` (fractional, in source frames) /// over `points`, which are `(position, dB)` pairs sorted by position with no /// two alike. Eased in dB between neighbours by the earlier point's ease /// (linear by default), held flat outside the first and last point, and `0.0` for no points at all. The one interpolation both /// [`Clip::gain_keys`] and the playback [`Fade`] evaluate. fn lerp_db(points: &[T], at: impl Fn(&T) -> (f64, f32, KeyEase), src: f64) -> f32 { let (Some(first), Some(last)) = (points.first(), points.last()) else { return 0.0; }; let (x0, db0, _) = at(first); if src <= x0 { return db0; } let (xn, dbn, _) = at(last); if src >= xn { return dbn; } // The first point strictly after `src`: its predecessor is the left end // of the segment `src` is in, and both exist by the two checks above. let i = points.partition_point(|p| at(p).0 <= src); let (xa, dba, ease) = at(&points[i - 1]); let (xb, dbb, _) = at(&points[i]); let t = ease_shape(ease, ((src - xa) / (xb - xa)) as f32); dba + (dbb - dba) * t } /// The placement `clip` shows at timeline `timeline_frame`, given its base /// `base` ([`Clip::transform`] resolved): its [`Clip::transform_keys`] at the /// source frame that frame plays, the same way [`Project::gain_db_at`] reads /// the volume envelope, or `base` with no keys. Timeline frames before the /// clip read as its first frame. pub fn clip_transform_at(clip: &Clip, base: TransformParams, timeline_frame: u32) -> TransformParams { if clip.transform_keys.is_empty() { return base; } keyed_transform(&base, &clip.transform_keys, key_sample_pos(clip, timeline_frame)) } /// The opacity `clip` shows at timeline `timeline_frame`, given the opacity /// `base` its transform gives there: its [`Clip::opacity_keys`] when it has /// any, otherwise `base`. The fades are not in it; they multiply on top. pub fn clip_opacity_at(clip: &Clip, base: f32, timeline_frame: u32) -> f32 { if clip.opacity_keys.is_empty() { return base; } evaluate_opacity(base, &clip.opacity_keys, key_sample_pos(clip, timeline_frame)) } /// The grade `clip` shows at timeline `timeline_frame`, given its constant /// grade `base` ([`Project::color_of`] resolved, identity for none): its /// [`Clip::color_keys`] interpolated at the source frame that frame plays, or /// `base` with no keys. pub fn clip_color_at(clip: &Clip, base: ColorParams, timeline_frame: u32) -> ColorParams { if clip.color_keys.is_empty() { return base; } evaluate_color(base, &clip.color_keys, key_sample_pos(clip, timeline_frame)) } /// Whether `clip` has any animation that changes the picture per frame /// (placement, opacity or colour keys): the clips a layered path composes. pub fn clip_animated(clip: &Clip) -> bool { !(clip.transform_keys.is_empty() && clip.opacity_keys.is_empty() && clip.color_keys.is_empty()) || crate::effects::active(&clip.effects) || clip.stabilise.is_some() } /// `c`'s placement keys after a trim that moved its `edge` from the state in /// `old` (see [`keys_within`]). A still's head keeps the keys on the timeline /// ([`keys_after_still_head`]); everything else keeps them on their footage. fn rekey_trimmed(c: &mut Clip, old: &Clip, edge: Edge, still: bool) { fn rekey(old_keys: &[K], oc: &Clip, c: &Clip, edge: Edge, still: bool) -> Vec { match edge { Edge::Start if still => keys_after_still_head( old_keys, oc.start, oc.in_frame, c.start, c.out_frame, oc.speed.as_f64(), ), _ => keys_within(old_keys, c.in_frame, c.out_frame), } } if !old.transform_keys.is_empty() { c.transform_keys = rekey(&old.transform_keys, old, c, edge, still); } if !old.opacity_keys.is_empty() { c.opacity_keys = rekey(&old.opacity_keys, old, c, edge, still); } if !old.color_keys.is_empty() { c.color_keys = rekey(&old.color_keys, old, c, edge, still); } // An effect's keys follow its stack position: the same effect, rekeyed the same way. let masks: Vec>> = old .effects .iter() .map(|e| (!e.keys.is_empty()).then(|| rekey(&e.keys, old, c, edge, still))) .collect(); for (e, keys) in c.effects.iter_mut().zip(masks) { if let Some(keys) = keys { e.keys = keys; } } } /// The split of a clip's placement animation at source frame `cut`: the left /// half keeps the keys before the cut and the right half the keys after it, /// and both get a key at `cut` holding the placement the whole animation gives /// there, so each half still moves exactly as the clip did over its own frames. /// The cut key takes the easing of the segment it sits in. Empty in, empty out. pub fn split_transform_keys(keys: &[K], cut: u32) -> (Vec, Vec) { let Some(at) = sample(keys, f64::from(cut)) else { return (Vec::new(), Vec::new()); }; let ease = keys .iter() .rev() .find(|k| k.at() <= cut) .map_or(KeyEase::Linear, |k| k.ease()); let boundary = at.placed(cut, ease); let mut left: Vec = keys.iter().copied().filter(|k| k.at() < cut).collect(); left.push(boundary); let mut right = vec![boundary]; right.extend(keys.iter().copied().filter(|k| k.at() > cut)); (left, right) } /// The gain envelope's dB at source frame `src` over `keys` /// ([`Clip::gain_keys`]): linear between points, held outside them, `0.0` /// when there are none. Exact at a point, and the midpoint of two points is /// the midpoint of their dB. pub fn gain_keys_db_at(keys: &[GainKey], src: f64) -> f32 { lerp_db(keys, |k| (f64::from(k.at), k.db, k.ease), src) } impl Clip { /// A plain clip of `source` reading `in_frame..out_frame`, at start 0, normal /// speed and no look: the shape a front-end hands /// [`Project::edit_from_source`] for one half of a take. pub fn source_range(source: usize, in_frame: u32, out_frame: u32) -> Clip { Clip { speed_keys: Vec::new(), reversed: false, stabilise: None, deinterlace: Default::default(), mark: Default::default(), angle: Default::default(), start: 0, in_frame, out_frame, source, link: None, eq: None, color: None, transform: None, transform_keys: Vec::new(), opacity_keys: Vec::new(), color_keys: Vec::new(), effects: Vec::new(), fit: FitPolicy::default(), speed: Speed::NORMAL, fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), visualizer: 0, viz_paint: 0, gain_db: 0.0, gain_keys: Vec::new(), pan: 0, pitch: 0, } } /// Source frame count; `>= 1` by the never-empty invariant. What the clip /// reads out of its file -- not how long it is on the timeline, which is /// [`Clip::frames`] and differs the moment it is speeded. pub fn len(&self) -> u32 { self.out_frame - self.in_frame } /// Always false: a clip reads at least one source frame. pub fn is_empty(&self) -> bool { self.len() == 0 } /// Timeline frames it occupies: its source range at its own speed, never /// zero (see [`Speed::frames`]). Every placement question -- where it ends, /// what it overlaps, how wide its box is -- is asked of this and not of /// [`Clip::len`]. pub fn frames(&self) -> u32 { if self.speed_keys.is_empty() { self.speed.frames(self.len()) } else { ramp_frames(&self.speed_keys, self.len()) } } /// One past the last timeline frame it covers. pub fn end(&self) -> u32 { self.start + self.frames() } /// Whether a cut at `frame` lands cleanly on this clip: outside it, or inside at a frame /// its rate (or ramp) can address. What the razor asks before it offers to cut. pub fn splits_at(&self, frame: u32) -> bool { (self.start < frame && frame < self.end()) && split_source(self, frame).is_some() } /// Source frames from the clip's in point to the one its `clip_frame`th timeline /// frame plays: [`Speed::source_at`], or for a ramped clip the ramp's integral /// floored ([`source_pos`]). What every "which source frame is on screen" asks, /// at a constant speed or not. pub fn source_offset(&self, clip_frame: u32) -> u32 { if self.speed_keys.is_empty() { self.speed.source_at(clip_frame) } else { ramp_source_at(&self.speed_keys, f64::from(clip_frame)).floor() as u32 } } /// The inverse of [`source_offset`](Clip::source_offset), ceiled: the first /// clip-relative timeline frame whose source frame is `source_offset` or past it /// -- [`Speed::timeline_at`], or the ramp's. Where a source instant that starts /// something (a word, a speech region) lands. pub fn timeline_offset(&self, source_offset: u32) -> u32 { if self.speed_keys.is_empty() { self.speed.timeline_at(source_offset) } else { SpeedRamp::new(&self.speed_keys, 0).timeline_at(source_offset) } } /// [`timeline_offset`](Clip::timeline_offset) floored instead: the rounding an /// *end* needs, so a region never eats the frame the next thing starts on. pub fn timeline_offset_floor(&self, source_offset: u32) -> u32 { if self.speed_keys.is_empty() { (u64::from(source_offset) * 1000 / u64::from(self.speed.permille())).min(u64::from(u32::MAX)) as u32 } else { ramp_timeline_at(&self.speed_keys, f64::from(source_offset)).floor() as u32 } } /// [`source_offset`](Clip::source_offset) without the floor, for a fractional /// clip-relative timeline frame: source frames from the in point, continuous. pub fn source_offset_f(&self, clip_frame: f64) -> f64 { if self.speed_keys.is_empty() { clip_frame.max(0.0) * self.speed.as_f64() } else { ramp_source_at(&self.speed_keys, clip_frame) } } /// The inverse of [`source_offset_f`](Clip::source_offset_f): the clip-relative /// timeline frame (fractional) at which the clip has played `source_frames` source /// frames of itself. pub fn timeline_offset_f(&self, source_frames: f64) -> f64 { if self.speed_keys.is_empty() { source_frames / self.speed.as_f64() } else { ramp_timeline_at(&self.speed_keys, source_frames) } } /// The longest source range that fits in `frames` timeline frames at this clip's /// speed -- [`Speed::fit`], or for a ramped clip the largest length whose ramp /// footprint ([`ramp_frames`]) fits. `None` when nothing does. pub fn fit_source(&self, frames: u32) -> Option { if self.speed_keys.is_empty() { return self.speed.fit(frames); } if frames == 0 || ramp_frames(&self.speed_keys, 1) > frames { return None; } // The footprint is monotone in the source length, so bisect for the largest // that still fits. Doubling first bounds the search. let (mut lo, mut hi) = (1u32, 2u32); while ramp_frames(&self.speed_keys, hi) <= frames && hi < u32::MAX / 2 { lo = hi; hi *= 2; } while lo < hi { let mid = lo + (hi - lo).div_ceil(2); if ramp_frames(&self.speed_keys, mid) <= frames { lo = mid; } else { hi = mid - 1; } } Some(lo) } } /// The speed a ramp runs at clip-relative frame `t`, linear between keys and held at /// the ends, rounded to a [`Speed`] (thousandths). pub fn ramp_speed_at(keys: &[SpeedKey], t: u32) -> Speed { let Some(first) = keys.first() else { return Speed::NORMAL; }; if t <= first.at { return first.speed; } for w in keys.windows(2) { let (a, b) = (w[0], w[1]); if t <= b.at { let f = f64::from(t - a.at) / f64::from(b.at - a.at); let (ra, rb) = (a.speed.as_f64(), b.speed.as_f64()); return Speed::from_permille(((ra + (rb - ra) * f) * 1000.).round() as u16); } } keys[keys.len() - 1].speed } /// The keys of the part of a ramped clip from clip-relative frame `cut` on, rebased so /// that `cut` is frame 0: the speed there first, then the keys after it, shifted. /// What a clip's tail keeps when its head is cut away. pub fn ramp_keys_from(keys: &[SpeedKey], cut: u32) -> Vec { let mut out = vec![SpeedKey { at: 0, speed: ramp_speed_at(keys, cut), }]; out.extend( keys.iter() .filter(|k| k.at > cut) .map(|k| SpeedKey { at: k.at - cut, speed: k.speed }), ); out } /// A file the timeline plays from, and *which* of its audio streams it plays: /// a file carrying one track per language is several sources, one per stream, /// sharing a path. Two entries differing only in the stream are two sources -- /// they are what a clip names, and a clip plays exactly one stream. /// /// The path is always canonical ([`Source::new`]), so the same file reached by /// two paths is one source. #[derive(Clone, Debug, Default)] pub struct Source { pub path: PathBuf, /// Position among the file's audio tracks in file order, the numbering /// [`crate::AudioSession::probe_streams`] hands out. `0` for a file with a /// single track, and for a silent one. pub audio_stream: usize, /// Starred in the library (v51). Not part of the source's identity. pub fav: bool, /// The [`Bin`] this source is filed in (v51); `0` for none, the library's top level. pub bin: u32, } /// A source is the file and the stream: starring it or filing it in a bin does not make it /// another source. impl PartialEq for Source { fn eq(&self, other: &Self) -> bool { self.path == other.path && self.audio_stream == other.audio_stream } } impl Eq for Source {} /// A named folder in the media library (v51). Ids start at 1 and are never reused in a session. /// `parent` is `0` for a top-level bin, or the id of a top-level bin: one level of nesting. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Bin { pub id: u32, pub parent: u32, pub name: String, } /// The longest bin name. pub const MAX_BIN_NAME_CHARS: usize = 60; /// The most bins a project holds. pub const MAX_BINS: usize = 1_000; /// A bin name as the library keeps it: trimmed, non-empty, short, no control characters. pub fn clean_bin_name(name: &str) -> Result { let name = name.trim(); if name.is_empty() { return Err("a bin needs a name".into()); } if name.chars().count() > MAX_BIN_NAME_CHARS { return Err(format!("a bin name is at most {MAX_BIN_NAME_CHARS} characters")); } if name.chars().any(char::is_control) { return Err("a bin name holds no control characters".into()); } Ok(name.to_string()) } impl Project { /// The library's bins, in creation order (v51). pub fn bins(&self) -> &[Bin] { &self.bins } /// The bins a load puts back, with no undo step. Ids are kept; new ones start past the largest. pub fn with_bins(mut self, bins: Vec) -> Self { self.bin_next = bins.iter().map(|b| b.id).max().unwrap_or(0) + 1; self.bins = bins; self } fn bin_name_taken(&self, parent: u32, name: &str, except: u32) -> bool { self.bins.iter().any(|b| b.parent == parent && b.id != except && b.name.eq_ignore_ascii_case(name)) } /// Makes a bin named `name` inside bin `parent` (`0`: at the top level). Only a top-level bin /// holds bins, and two bins in one place cannot share a name. One undo step; returns the id. pub fn create_bin(&mut self, name: &str, parent: u32) -> Result { let name = clean_bin_name(name)?; if parent != 0 { match self.bins.iter().find(|b| b.id == parent) { None => return Err(format!("there is no bin {parent}")), Some(b) if b.parent != 0 => return Err("bins nest one level deep: that bin is already inside another".into()), Some(_) => {} } } if self.bin_name_taken(parent, &name, 0) { return Err(format!("there is already a bin named {name} there")); } if self.bins.len() >= MAX_BINS { return Err(format!("a project holds at most {MAX_BINS} bins")); } self.snapshot(); let id = self.bin_next; self.bin_next += 1; self.bins.push(Bin { id, parent, name }); Ok(id) } /// Renames bin `id`. `Ok(false)`, and no undo step, when the name is already that. pub fn rename_bin(&mut self, id: u32, name: &str) -> Result { let name = clean_bin_name(name)?; let Some(at) = self.bins.iter().position(|b| b.id == id) else { return Err(format!("there is no bin {id}")); }; if self.bins[at].name == name { return Ok(false); } if self.bin_name_taken(self.bins[at].parent, &name, id) { return Err(format!("there is already a bin named {name} there")); } self.snapshot(); self.bins[at].name = name; Ok(true) } /// Removes bin `id`. What it held is not lost: its sources go to the bin it was in (the top /// level for a top-level one) and the bins inside it come out to the top level. Returns how /// many sources moved. One undo step. pub fn remove_bin(&mut self, id: u32) -> Result { let Some(bin) = self.bins.iter().find(|b| b.id == id).cloned() else { return Err(format!("there is no bin {id}")); }; self.snapshot(); let mut moved = 0; for s in self.sources.iter_mut().filter(|s| s.bin == id) { s.bin = bin.parent; moved += 1; } self.bins.retain(|b| b.id != id); let children: Vec = self.bins.iter().filter(|b| b.parent == id).map(|b| b.id).collect(); for c in children { let at = self.bins.iter().position(|b| b.id == c).expect("listed above"); let mut name = self.bins[at].name.clone(); let mut n = 1; while self.bin_name_taken(0, &name, c) { n += 1; name = format!("{} {n}", self.bins[at].name); } self.bins[at].name = name; self.bins[at].parent = 0; } Ok(moved) } /// Files sources `srcs` in bin `bin` (`0`: out of every bin, to the top level). Returns how /// many changed; one undo step, none when nothing changes. pub fn move_to_bin(&mut self, srcs: &[usize], bin: u32) -> Result { if bin != 0 && !self.bins.iter().any(|b| b.id == bin) { return Err(format!("there is no bin {bin}")); } if let Some(bad) = srcs.iter().find(|&&i| i >= self.sources.len()) { return Err(format!("there is no source {bad}")); } let mut todo: Vec = srcs.iter().copied().filter(|&i| self.sources[i].bin != bin).collect(); todo.sort_unstable(); todo.dedup(); if todo.is_empty() { return Ok(0); } self.snapshot(); for &i in &todo { self.sources[i].bin = bin; } Ok(todo.len()) } /// Stars or unstars sources `srcs`. Returns how many changed; one undo step, none when /// nothing changes. pub fn set_favourite(&mut self, srcs: &[usize], on: bool) -> Result { if let Some(bad) = srcs.iter().find(|&&i| i >= self.sources.len()) { return Err(format!("there is no source {bad}")); } let mut todo: Vec = srcs.iter().copied().filter(|&i| self.sources[i].fav != on).collect(); todo.sort_unstable(); todo.dedup(); if todo.is_empty() { return Ok(0); } self.snapshot(); for &i in &todo { self.sources[i].fav = on; } Ok(todo.len()) } } impl Source { pub fn new(path: impl AsRef, audio_stream: usize) -> Self { Self { path: canonical(path.as_ref()), audio_stream, fav: false, bin: 0, } } } /// Which three-point edit [`Project::edit_from_source`] makes. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum SourceEdit { /// Opens room: what is at and after the frame moves later. Insert, /// Replaces what is there for the take's length; nothing moves. Overwrite, } /// What a lane carries, which is what a *gap* in it means: black frames on a /// video lane, silence on an audio one, nothing over the picture on a subtitle /// one. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum LaneKind { Video, Audio, /// Words over the picture, placed and trimmed like everything else /// ([`SubClip`]). A lane of its own rather than a setting on the project, /// so a caption is dragged, cut, rippled and undone by the machinery every /// other clip already goes through. /// /// It carries no [`Clip`] at all, and that is what keeps the media paths /// free of it: [`Project::lane`] hands back an empty slice, so every /// clip-indexed call ([`Project::trim`], [`Project::set_eq`], /// [`Project::set_speed`], [`Project::lift`]) refuses it by the bounds /// check it already had, [`Project::composite_span_at`] and /// [`Project::audio_lanes`] never look at it, and the three doors that /// could still put a picture on one -- [`Project::from_parts`], /// [`Project::place`], [`Project::append_clip`] -- say so by name. Subtitle, } /// Which lane an operation acts on: a kind plus a 0-based position among the /// lanes of that kind, so [`Lane::V1`] is the first video lane and /// `Lane::new(LaneKind::Audio, 1)` is `A2`. /// /// A handle, not the lane itself. It stays meaningful while lanes are added, /// and one naming a lane that is not there reads as an empty lane and refuses /// every mutation -- a front-end that always asks for `V1`/`A1` needs no /// bounds check of its own. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct Lane { pub kind: LaneKind, pub ord: usize, } impl Lane { /// The first video lane: what a decoder plays and what an export renders. pub const V1: Lane = Lane::new(LaneKind::Video, 0); /// The first audio lane: what the audio worker plays. pub const A1: Lane = Lane::new(LaneKind::Audio, 0); /// The first subtitle lane -- the one a project has only once something /// added it ([`Project::add_lane`]); a project starts with `V1` and `A1` /// alone. pub const S1: Lane = Lane::new(LaneKind::Subtitle, 0); pub const fn new(kind: LaneKind, ord: usize) -> Self { Self { kind, ord } } /// `V1`, `A2`: how a lane is written in a header column and named in an /// error about it. pub fn label(self) -> String { let kind = match self.kind { LaneKind::Video => 'V', LaneKind::Audio => 'A', LaneKind::Subtitle => 'S', }; format!("{kind}{}", self.ord + 1) } } /// The shortest a cue may be trimmed to, in microseconds. pub const MIN_CUE_US: i64 = 100_000; /// One cue of a caption lane as the timeline shows it: where it lands, which cue of which track it /// is, and the part `window` of the cue's own clock that is shown. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct CueSpan { pub start_us: i64, pub end_us: i64, pub track: usize, pub cue: usize, pub window: (i64, i64), } /// One cue's saved word times: `(track, cue, [(start_us, end_us, word)])`. pub type CueWordRow = (usize, usize, Vec<(i64, i64, String)>); /// A placed subtitle: a `[in_us, out_us)` window of one of the project's /// subtitle tracks ([`Project::subtitles`], which is the *palette*), shown from /// timeline frame [`start`](SubClip::start) for [`frames`](SubClip::frames) /// frames. Never empty at either end. /// /// A [`Clip`] with the two frame spaces swapped for the two clocks a subtitle /// actually has, and placed by the same one number: the window is in the /// microseconds every parser here speaks ([`crate::subtitle::Cue`]), the /// placement is in timeline frames like everything else on a lane. So the lane /// invariant is the lane's ([`subs_sorted_disjoint`] is /// [`sorted_disjoint`] over the same arithmetic), a move is `start` and nothing /// else, and only the two calls that change a *duration* -- a trim, and the /// mapping out -- ever need the timeline's rate. /// /// `Copy` for [`Clip`]'s reason: the palette is named by index, so a copy, a /// paste and an undo snapshot are a plain assignment and can never dangle. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct SubClip { /// Timeline frame its first frame is shown at. pub start: u32, /// How many timeline frames it covers; `>= 1`. Held rather than derived /// from the window because a [`Project`] has no frame rate -- the rate /// enters at the door of the two calls that need it -- and every placement /// question (where it ends, what it overlaps) is asked in frames. pub frames: u32, /// Index into [`Project::subtitles`]: *which* track's words these are. pub track: usize, /// The half-open window of that track it shows, in microseconds from the /// start of the track -- exactly the clock its [`crate::subtitle::Cue`]s are /// timed in, so no conversion stands between a cue and the window that /// keeps it. pub in_us: i64, pub out_us: i64, /// Group id: `Some` exactly when a hand put this caption in a group with /// clips on other lanes ([`Project::group_all`]) -- a caption arrives in /// no group, and what a cut cuts apart loses the one it had /// ([`sub_open_room`], for [`Clip::link`]'s reason). Sharing an id with a /// clip is what makes the caption move, trim and delete with it. pub link: Option, } impl SubClip { /// One past the last timeline frame it covers -- [`Clip::end`]'s twin, and /// what every overlap question is asked of. pub fn end(&self) -> u32 { self.start + self.frames } /// The whole of caption track `track`, whose last cue ends at `end_us`, as /// one placement at `fps` -- what a palette row dropped on a subtitle lane /// carries. The window is the frames' own length, not the track's: a track /// that ends between two frames (4.750 s on 143 frames) would otherwise be /// stretched over the frames it occupies, and every cue would show late by /// that fraction. The frames are the fewest that hold the last cue. pub fn whole_track(track: usize, end_us: i64, fps: f64) -> SubClip { let us_of = |f: u32| (f64::from(f) / fps * 1e6).round() as i64; let mut frames = ((end_us as f64) * fps / 1e6).ceil().clamp(1., f64::from(u32::MAX)) as u32; // The ceiling is at most one frame short, once the microsecond rounding is counted. if us_of(frames) < end_us { frames += 1; } let out_us = us_of(frames); SubClip { start: 0, frames, track, in_us: 0, out_us, link: None } } /// How much of its track it shows, in microseconds; `>= 1` by the /// never-empty invariant, and never more than [`MAX_WINDOW_US`] for one /// that came through a door. pub fn window_us(&self) -> i64 { self.out_us.saturating_sub(self.in_us) } /// Where in its window the timeline frame `frame` falls, in microseconds -- /// the cut a split, a ripple and a trim all land on. /// /// By proportion and not by the timeline's rate: the window and the frame /// count measure the same stretch of time, so the fraction of the frames /// before `frame` is the fraction of the microseconds before it. That is /// what lets a subtitle be cut by [`clear`]'s and [`open_room`]'s twins /// without either of them learning an fps. /// /// Total arithmetic, and clamped to the window it is cutting: the /// `saturating_mul` is what a `SubClip` built by a caller rather than a /// door gets (a hand-built one, a unit test, a future door that forgot the /// bound) -- a wrapped window would put a caption's clock *before* its own /// start, and the split that produced it would then hand back two halves /// whose windows do not meet. The clamp is the never-empty invariant /// restated: the answer is a point inside `[in_us, out_us]` or nothing. fn window_at(&self, frame: u32) -> i64 { let into = i64::from(frame.saturating_sub(self.start)); let window = self.window_us().max(0); let at = window.saturating_mul(into) / i64::from(self.frames.max(1)); self.in_us.saturating_add(at.min(window)) } } /// One lane: what it is and what it holds. A struct rather than a bare /// `Vec` because per-lane state is what the next slices add -- mute, a /// compositing mode, a name -- and it belongs next to the clips it applies to. #[derive(Clone, Debug, PartialEq)] struct LaneData { kind: LaneKind, /// Sorted by `start` and disjoint ([`sorted_disjoint`]). Always empty on a /// [`LaneKind::Subtitle`] lane, which is what keeps every media path from /// ever seeing a subtitle: they all read this list. clips: Vec, /// The same, for a subtitle lane ([`subs_sorted_disjoint`]), and always /// empty on a video or audio one. Beside the clips rather than an enum over /// them so that everything a lane *is* -- its place in the stack, its /// label, its undo snapshot, its removal -- stays one code path for all /// three kinds. subs: Vec, /// How loud this lane plays, in dB, `0.0` for one nobody has touched -- /// [`Project::lane_gain_db`]. Whole-lane and whole-band, which is what /// makes it a different thing from a clip's equalizer: it moves everything /// on the track by the same amount, cuts included. Meaningless on a video /// lane, where it is kept at 0 and asked by nothing. gain_db: f32, /// Where this audio lane sits between the speakers, `-1.0..=1.0`, `0.0` the /// middle ([`Project::lane_pan`]); applied on top of each clip's own pan. pan: f32, /// The auto-ducks on this audio lane ([`Project::lane_duck_list`]): each /// drops the lane while the key lane it names is loud, so a lane can be /// ducked under several keys at once and the drops multiply. At most one per /// key, in the order they were set. Empty for every other lane. ducks: Vec, /// The voice processors of this audio lane ([`Project::lane_voice`]): gate, /// noise reduction, compressor and de-esser, all off for a lane nobody has /// cleaned. Default on every other lane. voice: VoiceFx, /// Whether a video lane is left out of the picture, or a subtitle lane's /// words are left out of the screen and the export /// ([`Project::lane_hidden`]). Kept on the lane, not in a view, so that the /// editor and an export read one answer. hidden: bool, /// Whether an audio lane is silenced in the mix ([`Project::lane_muted`]). /// Its clips and its volume are untouched, so unmuting restores exactly /// what was there. muted: bool, /// Pure state for the front-end, which refuses edits to a locked lane. The /// engine's own operations ignore it ([`Project::lane_locked`]). locked: bool, /// The display label the user gave this lane ([`Project::lane_name`]); empty /// for none. The address (V1, A2) is unchanged by it. name: String, } /// The longest lane name, in characters. pub const MAX_LANE_NAME_CHARS: usize = 40; /// The three per-lane switches a save writes ([`Project::lane_flags`]), as one /// value so that the file and the editor name them the same way. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct LaneFlags { pub hidden: bool, pub muted: bool, pub locked: bool, /// An audio lane's pan ([`Project::lane_pan`]); carried here because it is saved and /// loaded with the switches. pub pan: f32, } impl LaneData { fn new(kind: LaneKind, clips: Vec) -> Self { Self { kind, clips, subs: Vec::new(), gain_db: 0.0, pan: 0.0, ducks: Vec::new(), voice: VoiceFx::default(), hidden: false, muted: false, locked: false, name: String::new(), } } /// The two lanes a project starts with: `V1` then `A1`, in display order. /// One place, because a freshly opened file is exactly this pair. fn two_lanes(video: Vec, audio: Vec) -> Vec { vec![ LaneData::new(LaneKind::Video, video), LaneData::new(LaneKind::Audio, audio), ] } } /// The placements one group id names, split by the two lists a lane holds: /// what a drag, a trim and a delete each have to walk, now that a group may /// hold a caption beside its clips ([`Project::group_all`]). A clip and a /// caption of one group never share a lane -- a subtitle lane holds no `Clip` /// -- so the pairs are `(lane storage index, index into that lane's list)`. #[derive(Default)] struct Members { clips: Vec<(usize, usize)>, subs: Vec<(usize, usize)>, } impl Members { /// Whether the group is the one placement and no more: what a lift leaves /// behind, and what every whole-group question answers "not one" for. fn alone(&self) -> bool { self.clips.len() + self.subs.len() < 2 } } /// Which end of a clip a [`Project::trim`] moves: the one the pointer grabbed. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Edge { /// Its first timeline frame; moving it changes where in the source the clip /// starts reading, so the picture behind the edge slides with it. Start, /// One past its last timeline frame; moving it only says how much of the /// source to keep. End, } /// The two walls an edge may travel between, `(first, last)` timeline frame /// inclusive, and which of them is the source's own media limit: its head /// (for a start edge) or its tail (for an end edge). The other wall is a /// neighbouring clip, the timeline's first frame, or the clip's own minimum /// length -- never media. `lo_media`/`hi_media` say which, so a front-end can /// tell a trim that ran out of source from one that ran into a clip. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct TrimWalls { pub lo: u32, pub hi: u32, pub lo_media: bool, pub hi_media: bool, } /// What stops an edge at one of its walls ([`Project::trim_bounds`]): the thing a /// front-end names when a drag or a trim comes to rest there. Wherever a wall is /// the frame a neighbour or the timeline sets, the clip sitting at that wall does /// not say which, so this does. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum WallBound { /// The source's own limit: its head for a start edge, its tail for an end /// edge. A clip that has run out of source. Source, /// The neighbouring clip on the same lane: the end of the one in front of a /// start edge, or the start of the one behind an end edge -- at this timeline /// frame. Neighbour(u32), /// The timeline's first frame, with no clip in front of the edge. Timeline, /// The clip's own minimum length: one source frame, at its rate. MinLength, /// The length a still image is held to -- the table's cap, which is not a /// source's end (a still has none to run out of). Cap, } /// The length each source has on each kind of lane, in timeline frames: what a /// trim measures its walls against. A picture and a sound of one file need not /// end on the same frame ([`crate::playback`]'s audio-lane table), and a linked /// take drags both edges together, so a trim of one lane has to hold the other /// to the other's own length. #[derive(Clone, Copy, Debug)] pub struct SourceWalls<'a> { /// The length on a video lane (and on a subtitle lane, which has none). pub picture: &'a [u32], /// The length on an audio lane. pub sound: &'a [u32], } impl<'a> SourceWalls<'a> { /// One table for both kinds of lane: for a caller that has only the one. pub fn same(frames: &'a [u32]) -> Self { Self { picture: frames, sound: frames, } } /// The table a clip on a lane of `kind` is walled by. fn for_kind(&self, kind: LaneKind) -> &'a [u32] { if kind == LaneKind::Audio { self.sound } else { self.picture } } } /// What a lane holds over one stretch of the timeline: either a placed clip or /// a gap. Returned already trimmed to the position it was asked about, so a /// caller can hand `len` straight to a decoder (or to a black-frame generator). #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct Span { /// Timeline frame the span starts at. pub start: u32, /// Frames it covers; `>= 1`. pub len: u32, /// `(source index, first source frame)`, or `None` for a gap -- which the /// engine renders as black frames and as silence. pub from: Option<(usize, u32)>, /// The rate the clip this came off plays at ([`Clip::speed`]); /// [`Speed::NORMAL`] for a gap, which has no source to run fast. `len` is /// timeline frames whatever it says -- the speed is how many *source* frames /// those cover ([`Span::source_len`]) and how a decoder's own frame numbers /// come back to the timeline. pub speed: Speed, /// The speed ramp of the clip this came off, when it has one ([`Clip::speed_keys`]), /// based at this span's first frame. A ramped span's [`speed`](Span::speed) is /// [`Speed::NORMAL`]: a constant rate would be wrong for it, and real time is what /// a decoder reads every frame at, so every mapping goes through /// [`source_offset`](Span::source_offset) instead. pub ramp: Option, /// Whether the clip this came off plays backwards ([`Clip::reversed`]). Then /// `from` still names the source frame the span's first timeline frame shows -- /// the highest of the frames it reads -- and every later frame is /// [`source_offset`](Span::source_offset) *below* it ([`Span::source_at`]); the /// offsets themselves, [`source_len`](Span::source_len) and /// [`timeline_at`](Span::timeline_at) count distance walked, as they do forwards. /// `false` for a gap. pub reverse: bool, /// The pitch in semitones of the clip this came off ([`Clip::pitch`]); `0` for a gap. pub pitch: i8, } impl Span { /// One past the last timeline frame it covers. pub fn end(&self) -> u32 { self.start + self.len } /// Source frames from this span's first frame to its `offset`th: the mapping /// `speed.source_at(offset)` is for a constant rate, and the ramp's integral when /// the span carries one. Added to the span's `from` source frame it gives the /// frame the timeline frame at `offset` shows. pub fn source_offset(&self, offset: u32) -> u32 { match &self.ramp { None => self.speed.source_at(offset), Some(ramp) => ramp.source_offset(offset), } } /// The source frame the timeline frame `offset` into the span shows, given /// `first`, the span's own first one (`from`'s): up for a span that plays /// forwards, down for a reversed one. pub fn source_at(&self, first: u32, offset: u32) -> u32 { match self.reverse { false => first + self.source_offset(offset), true => first.saturating_sub(self.source_offset(offset)), } } /// The half-open range of source frames this span reads, `first..end`, in the /// order they are numbered in the file whichever way they are played. `None` for /// a gap. The window a decoder is opened over, and an audio segment's. pub fn source_window(&self) -> Option<(usize, u32, u32)> { let (source, first) = self.from?; let len = self.source_len(); Some(match self.reverse { false => (source, first, first + len), true => (source, (first + 1).saturating_sub(len), first + 1), }) } /// How many source frames it reads: what a decoder is asked for and how wide /// an audio window is. `len` at real time, and the one arithmetic both the /// picture and the sound go through, so they cannot disagree about where a /// speeded clip ends. pub fn source_len(&self) -> u32 { match self.from { // The last timeline frame's own source frame is the last one needed // -- never one more, which at a slow rate would read a frame the // next clip owns and at a fast one would decode a picture nothing // shows. Some(_) => self.source_offset(self.len.saturating_sub(1)) + 1, None => self.len, } } /// Where a source frame of this span lands on the timeline: the stamp /// playback puts on a decoded frame, through the ceil half of the pair /// ([`Speed::timeline_at`]). `in_frame` is the span's own first source /// frame, so a frame before it stamps at the span's start. pub fn timeline_at(&self, source_frame: u32) -> u32 { let base = self.from.map_or(0, |(_, in_frame)| in_frame); let rel = source_frame.saturating_sub(base); self.start + match &self.ramp { None => self.speed.timeline_at(rel), Some(ramp) => ramp.timeline_at(rel), } } } /// What one speeded audio segment asks of the worker: how many source frames to /// consume per output frame, and how many seconds of timeline it has to fill. /// [`Project::audio_speeds_from`] builds it; `None` there means real time and no /// resampling at all. #[derive(Clone, Copy, PartialEq, Debug)] pub struct Stretch { /// Source frames per output frame -- the speed as a multiplier. pub step: f64, /// What the segment owes the timeline, which is what it is padded or /// trimmed to. pub timeline_secs: f64, /// The segment's speed ramp, when it has one: the stretch then follows the ramp's /// instantaneous rate rather than `step`, which is only the mean it falls back to. pub ramp: Option, /// The timeline's frame rate, which the ramp's frames are counted in. The /// openers do not all know it, so the stretch carries it. pub fps: f64, /// Whether the segment's samples are played last to first /// ([`Clip::reversed`]). Then `step` is `1.0` when only the direction asks for a /// rate chain at all, and the window the segment names is the one to be reversed. pub reverse: bool, /// The pitch in semitones the segment is shifted by ([`Clip::pitch`]), `0` for none. /// The stretch then runs at `step / 2^(pitch/12)` and the resample at the same ratio /// up: the pitch moves, the length does not. pub pitch: i8, } impl Stretch { /// Whether the segment is only played backwards, at the rate it was recorded: /// no change of time, so no stretch is built for it. pub fn is_straight(&self) -> bool { self.reverse && self.step == 1.0 && self.ramp.is_none() && self.pitch == 0 } /// The frequency ratio the segment's pitch shift is: `2^(pitch/12)`, `1.0` for none. pub fn pitch_ratio(&self) -> f64 { pitch_ratio(self.pitch) } } /// The frequency ratio of a shift of `semitones`: `2^(semitones/12)`, so `12` is an octave up. pub fn pitch_ratio(semitones: i8) -> f64 { (f64::from(semitones) / 12.0).exp2() } /// What one audio segment's gain envelope is: [`Project::audio_fades_from`] /// builds it off the clip's own [`Clip::fade_in`]/[`Clip::fade_out`], resolved /// against where in the *clip* -- not the segment, which may start mid-clip /// on a seek -- this segment's first frame lands. #[derive(Clone, PartialEq, Debug)] pub struct Fade { /// How many of the clip's own frames already played before this /// segment's first frame: `0` unless playback started mid-clip. pub elapsed: u32, /// The clip's [`Clip::fade_in`]. pub fade_in: u32, /// The clip's [`Clip::fade_out`]. pub fade_out: u32, /// The clip's own [`Clip::frames`] -- the envelope's whole width, of /// which this segment may only cover the tail. pub total: u32, /// The clip's [`Clip::gain_db`] as a linear multiplier, `1.0` for unity. /// It rides in the envelope because the envelope is what both the /// playback feeder and the export are handed, so the two cannot disagree. pub gain: f32, /// The left and right multipliers of the clip's pan and its lane's /// ([`pan_gains`]), `(1.0, 1.0)` for the middle. Only a stereo mix has a /// side to turn down; any other width plays it as it is. pub pan: (f32, f32), /// The clip's [`Clip::gain_keys`] as `(position, dB, ease)` triples, `None` for a /// clip with none. The position is clip-relative and in this envelope's own /// unit: timeline frames when [`Project::lane_fades_from`] builds it, audio /// frames once [`Fade::scaled`] has counted it. Shared, so the envelope /// stays cheap to hand around. pub curve: Option>, } /// The left and right multipliers of a clip pan and a lane pan, each `-1.0..=1.0`: a /// balance, where the side away from the pan is turned down along a quarter sine /// (`cos(pan * pi/2)`, silent at the far end) and the near side is left alone, /// so the middle is exactly unity. impl Clip { /// [`Clip::pan`] as `-1.0..=1.0`. pub fn pan_value(&self) -> f32 { f32::from(self.pan) / 100.0 } } pub fn pan_gains(clip: f32, lane: f32) -> (f32, f32) { let side = |p: f32| { let p = p.clamp(-1.0, 1.0); (p.max(0.0) * std::f32::consts::FRAC_PI_2, (-p).max(0.0) * std::f32::consts::FRAC_PI_2) }; let (cl, cr) = side(clip); let (ll, lr) = side(lane); // cos(pi/2) is not exactly 0 in f32: the far end is silence, so say so. let snap = |g: f32| if g < 1e-6 { 0.0 } else { g }; (snap(cl.cos() * ll.cos()), snap(cr.cos() * lr.cos())) } impl Fade { /// The gain at clip-relative frame `pos`, an equal-power curve on each /// edge that plays through unchanged (gain `1.0`) wherever neither ramp /// reaches: `sin(t * pi/2)` for `t` the fraction of the way through the /// ramp, so a fade lands on silence at its very first (or very last) /// frame and reaches unity smoothly rather than at a constant slope -- /// the same curve [`Project::crossfade`] relies on to keep two clips' /// sum around one level through the join. Both ramps multiply where a /// clip is short enough for them to overlap. fn gain_at(&self, pos: u32) -> f32 { let mut g = self.gain; if let Some(curve) = &self.curve { g *= db_to_linear(lerp_db(curve, |&p| p, f64::from(pos))); } if self.fade_in > 0 && pos < self.fade_in { let t = pos as f32 / self.fade_in as f32; g *= (t * std::f32::consts::FRAC_PI_2).sin(); } if self.fade_out > 0 { let out_start = self.total.saturating_sub(self.fade_out); if pos >= out_start { let remaining = self.total.saturating_sub(pos); let t = remaining as f32 / self.fade_out as f32; g *= (t.clamp(0.0, 1.0) * std::f32::consts::FRAC_PI_2).sin(); } } g } /// Multiplies `frames` in place -- `channels`-wide interleaved samples, /// starting at this segment's own frame `0` -- by [`Self::gain_at`]. pub fn apply(&self, frames: &mut [f32], channels: usize) { for (i, block) in frames.chunks_mut(channels).enumerate() { let g = self.gain_at(self.elapsed.saturating_add(i as u32)); for s in block.iter_mut() { *s *= g; } if channels == 2 && block.len() == 2 { block[0] *= self.pan.0; block[1] *= self.pan.1; } } } /// Every field re-counted at `ratio` frames-out per frame-in, rounded to /// the nearest whole frame. [`Project::lane_fades_from`] builds a `Fade` /// in *timeline* frames ([`Clip::fade_in`]'s own unit, the fps every clip /// position is in) but [`Self::apply`] multiplies *audio* frames -- /// `channels`-wide sample blocks at whatever rate the mix is running at, /// which is almost never the video's fps. Left unconverted, `elapsed` (an /// audio-frame count) raced past `total`/`fade_out` (fps-frame counts) a /// few milliseconds into any clip, so a fade-out silenced the rest of the /// clip outright and a fade-in finished before a listener's ear caught up /// -- audible on nothing shorter than a clip a few fps-frames long, which /// is why 338 green tests never once ran the two curves out of step. /// `ratio` is `sample_rate / fps`, applied once where the audio session /// first learns its own rate ([`crate::AudioSession::open_multi_streams_speed_at_fade`]). pub fn scaled(self, ratio: f64) -> Fade { let conv = |f: u32| (f64::from(f) * ratio).round() as u32; Fade { elapsed: conv(self.elapsed), fade_in: conv(self.fade_in), fade_out: conv(self.fade_out), total: conv(self.total), gain: self.gain, pan: self.pan, curve: self.curve.as_ref().map(|c| { c.iter() .map(|&(x, db, ease)| (x * ratio, db, ease)) .collect::>() .into() }), } } } /// What a save writes and a load takes back: the sources, every lane in /// display order with its kind, and the equalizer and colour tables their clips /// index into -- [`Project::without_orphan_sources`] out, /// [`Project::from_parts`] in. pub type Parts = ( Vec, Vec<(LaneKind, Vec)>, Vec, Vec, Vec, ); /// The next revision id to hand out. Process-wide, so two projects never share /// an id and a revision read from one session means nothing in another. static NEXT_REVISION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); fn next_revision() -> u64 { NEXT_REVISION.fetch_add(1, std::sync::atomic::Ordering::Relaxed) } /// How many undo steps a project keeps. One gesture is one step, so 100 is past /// any chain a person walks back by hand, and the oldest step is dropped rather /// than the history growing without end: a snapshot is the whole lane list plus /// the palette and the project's own settings, so on a 1394-clip jumpcut /// timeline this is a bounded ~10 MB instead of a leak that grows for as long /// as the session lasts. const HISTORY_CAP: usize = 100; /// The project's own settings, as one value: the header a `.edith` file carries /// beside the edit list, and what every undo step restores beside the lanes. /// None of it is the media's own -- the scaffolding file's picture and rate /// ([`PlaybackSession::native`]) stay on the session and are never rolled back. #[derive(Clone, Debug, PartialEq)] pub(crate) struct ProjectSettings { /// The canvas every clip is composed onto: the project resolution, which a /// clip of another size is placed onto rather than refused for. pub(crate) resolution: (u32, u32), /// The rate the timeline is cut at, which every clip and source is /// conformed to ([`Project::retime`]). pub(crate) frame_rate: f64, /// Which encoder an export of this project opens its picture on /// ([`crate::export::EncoderSeat`]). pub(crate) encoder_seat: crate::export::EncoderSeat, /// The rate the mix was picked to run at, or `None` for the one the first /// audio source derives. pub(crate) sample_rate: Option, /// Whether the picture is decoded from the stand-ins ([`crate::proxy`]). pub(crate) proxies: bool, /// Whether a source that wants a stand-in gets one made for it as it /// arrives ([`crate::proxy::wanted`]). pub(crate) auto_proxies: bool, /// Which HDR-to-SDR rendition the picture is watched and exported in. pub(crate) tone: crate::tonemap::Preset, /// The master limiter every lane's sound is summed through. pub(crate) limiter: Limiter, /// How burned captions animate, the default for `burn_captions`. pub(crate) caption_style: crate::captionanim::CaptionStyle, /// The free-text notes the project carries (v52). Empty until somebody writes one. pub(crate) notes: String, /// How long a still image is placed for, in seconds (v52). pub(crate) still_secs: f64, } impl ProjectSettings { /// What a project built by hand carries before its constructor names the /// canvas: the defaults every fresh open has -- nobody has picked stand-ins /// (the films themselves are cut), an encoder or a mix rate, and the /// rendition and limiter are the reference ones. Every door that builds a /// session chains [`Project::with_canvas`] straight after, so the zero /// canvas here is never a project anybody plays from. fn placeholder() -> Self { Self { resolution: (0, 0), frame_rate: 0.0, encoder_seat: crate::export::EncoderSeat::default(), sample_rate: None, proxies: false, auto_proxies: false, tone: crate::tonemap::Preset::default(), limiter: Limiter::default(), caption_style: crate::captionanim::CaptionStyle::default(), notes: String::new(), still_secs: STILL_DEFAULT_SECS, } } } /// One undo step: the lanes, the subtitle palette and the project's own /// settings exactly as they were before the edit that pushed it. Whole values /// rather than the lane list alone, so an undo (or a redo) restores all three /// together and no half of one edit can come back without the others -- which /// is what lets a subtitle removal reindex every placement and still undo: the /// restored palette names the very tracks the restored placements index. /// What a freshly pushed undo step displaced: the redo branch its edit cleared /// and, when the cap was full, the oldest step it evicted. Moved, never cloned. #[derive(Clone, Debug)] struct PendingStep { redo: Vec, evicted: Option, } #[derive(Clone, Debug)] struct Snapshot { /// The revision of the state this step restores ([`Project::revision`]). revision: u64, /// The name of the edit that pushed this step ([`Project::name_step`]): what /// Undo and Redo say. `None` for a step nobody named, which reads as plain "Undo". /// It travels with the step, so an undo's redo entry carries the same name. label: Option, lanes: Vec, subtitles: Vec, cue_edits: Vec<(usize, usize, String)>, cue_adds: Vec, titles: Vec, chapters: Vec<Chapter>, markers: Vec<Marker>, settings: ProjectSettings, /// The files the clips index into, as the step had them: a relink is undone by putting the /// old path back here. A [`Source`] is a path and a stream index, so a copy is cheap. sources: Vec<Source>, /// The library's bins (v51). bins: Vec<Bin>, /// The sequence list: which is active, every name, and the parked ones' data. seq: Seqs, } /// The source list an undo or redo step puts back: the step's own list, and past its end the /// entries the step never knew of (imported after it). No clip in the step names one of those, /// so keeping them leaves every index the step holds valid, and an undone edit leaves an import /// in the library as an import always does. fn restored_sources(step: Vec<Source>, now: &[Source]) -> Vec<Source> { let mut out = step; if now.len() > out.len() { out.extend_from_slice(&now[out.len()..]); } out } /// The redo stack moved out by [`Project::take_redo`]: opaque, and only /// [`Project::restore_redo`] reads it back. pub struct RedoCheckpoint(Vec<Snapshot>); /// A cue somebody added to a subtitle track, which no file has: its track, its /// times on the track's own clock and its words. Kept beside the file's cue /// overrides ([`Project::cue_adds`]) because a `(track, cue, text)` override /// names a cue by where the file has it, and an added cue has no such place. #[derive(Clone, Debug, PartialEq, Eq)] pub struct AddedCue { pub track: usize, pub start_us: i64, pub end_us: i64, pub text: String, } /// One gap a close-all sweep left open, with the frame it still starts on and /// the same refusal a single [`Project::gap_take_scope`] close would have said. #[derive(Clone, Debug, PartialEq, Eq)] pub struct GapSkip { pub start: u32, pub reason: String, } /// What [`Project::close_all_gaps_on_lane`] did: closed gaps count as edits, /// skipped gaps are the take-safety refusals the sweep did not hide. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct GapSweep { pub closed: usize, pub skipped: Vec<GapSkip>, } /// A chapter marker: a named frame on the timeline, the point a viewer's chapter /// list jumps to. Sorted by frame, at most one per frame ([`Project::chapters`]). /// Saved as `chapter` lines ([`crate::edith`]). #[derive(Clone, Debug, PartialEq, Eq)] pub struct Chapter { /// The timeline frame the chapter starts on. pub frame: u32, /// What the chapter is called in the list. pub title: String, } /// The longest chapter name, in characters: the most a video platform's chapter /// list shows. pub const MAX_CHAPTER_CHARS: usize = 100; /// Whether `chapter` is a chapter the project may hold: the checks /// [`Project::add_chapter`] makes and the `.edith` loader makes of every `chapter` line. pub fn check_chapter(chapter: &Chapter) -> crate::Result<()> { if chapter.title.trim().is_empty() { return Err("a chapter needs a name".into()); } if chapter.title.chars().count() > MAX_CHAPTER_CHARS { return Err(format!("a chapter name holds at most {MAX_CHAPTER_CHARS} characters").into()); } Ok(()) } /// A marker's colour: the small fixed palette an editor's marker strip draws. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum MarkerColor { Red, Orange, Yellow, Green, #[default] Blue, Purple, } impl MarkerColor { /// Every colour, in palette order. pub const ALL: [MarkerColor; 6] = [ MarkerColor::Red, MarkerColor::Orange, MarkerColor::Yellow, MarkerColor::Green, MarkerColor::Blue, MarkerColor::Purple, ]; /// The name the `.edith` file and the MCP tools use. pub fn name(self) -> &'static str { match self { MarkerColor::Red => "red", MarkerColor::Orange => "orange", MarkerColor::Yellow => "yellow", MarkerColor::Green => "green", MarkerColor::Blue => "blue", MarkerColor::Purple => "purple", } } /// The colour called `name` (any case), if it is one of the palette. pub fn from_name(name: &str) -> Option<MarkerColor> { MarkerColor::ALL.into_iter().find(|c| c.name().eq_ignore_ascii_case(name.trim())) } /// The colour after this one in the palette, wrapping: what the GUI's "next colour" steps through. pub fn next(self) -> MarkerColor { let i = MarkerColor::ALL.iter().position(|c| *c == self).unwrap_or(0); MarkerColor::ALL[(i + 1) % MarkerColor::ALL.len()] } } /// A comment marker: a named, coloured point on the timeline with an optional note, or a /// range when `length` is not 0. Separate from a [`Chapter`]; it moves with the picture the /// same way. Sorted by frame; several may share a frame ([`Project::markers`]). Saved as /// `marker` lines ([`crate::edith`]). #[derive(Clone, Debug, PartialEq, Eq)] pub struct Marker { /// The timeline frame the marker starts on. pub frame: u32, /// How many frames it spans; 0 is a point. pub length: u32, pub name: String, /// A free-text note; may be empty. pub note: String, pub color: MarkerColor, } impl Marker { /// The frame a range marker ends on (exclusive); its start for a point. pub fn end(&self) -> u32 { self.frame.saturating_add(self.length) } } /// The longest marker name, in characters. pub const MAX_MARKER_NAME_CHARS: usize = 100; /// The longest marker note, in characters. pub const MAX_MARKER_NOTE_CHARS: usize = 2000; /// Whether `marker` is a marker the project may hold: the checks [`Project::add_marker`] /// makes and the `.edith` loader makes of every `marker` line. pub fn check_marker(marker: &Marker) -> crate::Result<()> { if marker.name.trim().is_empty() { return Err("a marker needs a name".into()); } if marker.name.chars().count() > MAX_MARKER_NAME_CHARS { return Err(format!("a marker name holds at most {MAX_MARKER_NAME_CHARS} characters").into()); } if marker.note.chars().count() > MAX_MARKER_NOTE_CHARS { return Err(format!("a marker note holds at most {MAX_MARKER_NOTE_CHARS} characters").into()); } if marker.frame.checked_add(marker.length).is_none_or(|e| e > MAX_TITLE_END) { return Err("a marker runs past the last frame there is".into()); } Ok(()) } /// The longest text a title can hold, in characters. pub const MAX_TITLE_CHARS: usize = 500; /// The furthest frame a title may end on. Far past any timeline anyone cuts, /// and far below `u32::MAX`, so `start + frames` cannot wrap. pub const MAX_TITLE_END: u32 = 100_000_000; /// A text overlay on the timeline: a title card over black, or a caption such /// as a lower third over the picture. Titles are a project-level list, not a /// lane: they are drawn over the composed picture (black where no clip plays), /// in list order, so a later title sits on top of an earlier one. Saved as /// `title` lines ([`crate::edith`]). #[derive(Clone, Debug, PartialEq)] pub struct Title { /// The first timeline frame the title shows on. pub start: u32, /// How many frames it shows for; `>= 1`. pub frames: u32, /// The words, with `\n` as a line break. Non-empty, at most /// [`MAX_TITLE_CHARS`] characters. pub text: String, /// How the words are set. Its size is kept inside [`SIZE_RANGE`]. pub style: TextStyle, /// Frames the title takes to come up from nothing: the first frame shows /// `1 / (fade_in + 1)` of it. `0` for no fade-in. pub fade_in: u32, /// Frames the title takes to go back to nothing, counted off the end. /// `0` for no fade-out. pub fade_out: u32, /// The animation it comes in with ([`crate::titleanim`]); `None` for none. pub in_anim: TitleAnim, /// The animation it goes out with; the reverse of `in_anim`'s motion. pub out_anim: TitleAnim, /// How many frames each animation takes; `>= 1` when either is set and at most `frames`. pub anim_frames: u32, /// The title's own keyframe animation, counted from its first frame: sorted by `at`, no two /// at one frame, at most [`MAX_TRANSFORM_KEYS`]. Empty for a title without one. pub keys: Vec<TitleKey>, } impl Title { /// One past the last frame the title covers. pub fn end(&self) -> u32 { self.start.saturating_add(self.frames) } /// How much of the title shows on timeline frame `frame`, 0 to 255: its /// fades and animations ramp it up and down, and a frame outside the title shows none. pub fn alpha_at(&self, frame: u32) -> u8 { self.motion_at(frame).map_or(0, |m| m.alpha) } } /// A one-click look for a title. The app's title card and MCP's `preset` argument both /// apply these, so the two never disagree on what "lower third" means. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum TitlePreset { LowerThird, Centered, CaptionBar, } impl TitlePreset { /// Every preset, in the order the app lists them. pub const ALL: [TitlePreset; 3] = [TitlePreset::LowerThird, TitlePreset::Centered, TitlePreset::CaptionBar]; /// The word that names the preset in MCP: `lower_third`, `centered` or `caption_bar`. pub fn key(self) -> &'static str { match self { TitlePreset::LowerThird => "lower_third", TitlePreset::Centered => "centered", TitlePreset::CaptionBar => "caption_bar", } } /// The preset a word names, if it names one. pub fn from_key(word: &str) -> Option<TitlePreset> { TitlePreset::ALL.into_iter().find(|p| p.key() == word) } /// The style and the fade length in seconds (in and out). pub fn look(self) -> (TextStyle, f64) { let base = TextStyle::default(); match self { TitlePreset::LowerThird => ( TextStyle { size: 45, outline: Outline::None, boxed: true, place: Place::Bottom, hplace: HPlace::Left, align: Align::Left, weight: Weight::Bold, ..base }, 0.5, ), TitlePreset::Centered => (TextStyle { size: 100, outline: Outline::Thin, boxed: false, place: Place::Centre, ..base }, 0.5), TitlePreset::CaptionBar => (TextStyle { size: 40, outline: Outline::None, boxed: true, place: Place::Bottom, ..base }, 0.0), } } } impl Title { /// `self` in `preset`'s look. The words and timing stay; fades are held to half the /// title's length, so in and out never cross. pub fn apply_preset(&mut self, preset: TitlePreset, fps: f64) { let (style, fade) = preset.look(); let fade = ((fade * fps).round() as u32).min(self.frames / 2); self.style = style; self.fade_in = fade; self.fade_out = fade; } } /// Whether `title` is a title the project may hold: the checks [`Project::add_title`] /// makes and the `.edith` loader makes of every `title` line. pub fn check_title(title: &Title) -> crate::Result<()> { if title.frames == 0 { return Err("a title must show for at least one frame".into()); } if title.text.trim().is_empty() { return Err("a title needs some text".into()); } if title.text.chars().count() > MAX_TITLE_CHARS { return Err(format!("a title holds at most {MAX_TITLE_CHARS} characters").into()); } if title.end() > MAX_TITLE_END { return Err(format!("a title ends past frame {MAX_TITLE_END}").into()); } if title.fade_in > title.frames || title.fade_out > title.frames { return Err("a title's fades are no longer than the title".into()); } let animated = title.in_anim != TitleAnim::None || title.out_anim != TitleAnim::None; if animated && (title.anim_frames == 0 || title.anim_frames > title.frames) { return Err("a title's animations last at least one frame and no longer than the title".into()); } if !(SIZE_RANGE.0..=SIZE_RANGE.1).contains(&title.style.size) { return Err(format!( "a title's size is {}..={} permille of the frame height", SIZE_RANGE.0, SIZE_RANGE.1 ) .into()); } if title.keys.len() > MAX_TRANSFORM_KEYS { return Err(format!("a title holds at most {MAX_TRANSFORM_KEYS} keys").into()); } for (i, k) in title.keys.iter().enumerate() { // A NaN fails every range check, so it is refused along with the out-of-range values. let in_range = (-1.0..=1.0).contains(&k.x) && (-1.0..=1.0).contains(&k.y) && (KEY_SCALE_RANGE.0..=KEY_SCALE_RANGE.1).contains(&k.scale) && (0.0..=1.0).contains(&k.opacity); if !in_range { return Err("a title key's x and y are -1..=1, its scale 0.05..=10 and its opacity 0..=1".into()); } if i > 0 && title.keys[i - 1].at >= k.at { return Err("a title's keys are in frame order, with no two at one frame".into()); } } Ok(()) } /// One clip [`Project::clamp_to_walls`] shortened: its lane, its source, and its /// out-point before (`from`) and after (`to`), in source frames. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct WallClamp { pub lane: Lane, pub source: usize, pub from: u32, pub to: u32, } /// The edit list plus its undo history. #[derive(Clone, Debug)] pub struct Project { /// Append-only: never popped, never reordered. See the module docs. sources: Vec<Source>, /// In display order, which is the order [`Lane::ord`] counts in: the nth /// lane of a kind here is that kind's `ord` n. Never empty. lanes: Vec<LaneData>, /// Append-only, exactly as `sources` is: what [`Clip::eq`] indexes into. eq: Vec<EqParams>, /// The same, for [`Clip::color`]. color: Vec<ColorParams>, /// The same, for [`Clip::transform`]. transform: Vec<TransformParams>, /// Snapshots pushed *before* each successful edit; `undo` pops one. The /// whole lane list, the subtitle palette and the project's own settings, /// so adding a lane undoes as well and so does picking a resolution. Bounded /// by [`HISTORY_CAP`]: at the cap the oldest step goes. history: Vec<Snapshot>, /// Snapshots `undo` has stepped past; `redo` pops one. Cleared by /// [`Project::snapshot`], since a fresh edit invalidates whatever branch /// the undone steps came from. Not saved to `.edith`, matching `history`. redo: Vec<Snapshot>, /// Every snapshot pushed since the project was made, evicted ones included: /// [`Project::snapshot`] bumps it on each push. A caller that measures a /// batch by the history's length cannot, once the cap holds that length at /// [`HISTORY_CAP`]; this count keeps rising, so a batch's pushes are the /// difference of two reads. Not saved to `.edith`, and an undo never rolls /// it back. pushes: u64, /// Which state the project is in, as an id no other state of any project /// has ([`Project::revision`]). Every edit takes a fresh one and every undo /// or redo puts back the one its step carries, so a caller that saved at one /// revision knows whether a later undo has walked back to it. revision: u64, /// Set by [`Project::begin_batch`]: while it holds, the first snapshot of the /// batch is the only one kept, so the batch is one undo step however many edits it makes. batching: bool, /// Whether the open batch has pushed its one snapshot yet. An undo or redo clears it, /// since the state it protects is then not the one the batch started from. batch_snapped: bool, /// What the newest step pushed took out of the way, so [`Project::settle`] /// can put it back should that step turn out to have changed nothing. `None` /// before any step and after an undo or redo. pending: Option<PendingStep>, /// Set by [`Project::hold_history`]: while it holds, a snapshot keeps every step /// (the history may run past [`HISTORY_CAP`]) so a trial batch that is rolled /// back takes its own steps off again and drops nothing. hold_cap: bool, /// Never rolled back by an undo: an id retired by an undone split must not /// come back and group two clips that were never together. next_link: u32, /// The subtitle tracks this timeline shows, in the order they were added. /// Saved as a *reference* to the file the cues came out of /// ([`crate::subtitle`]). In the history snapshots since the widened /// [`Snapshot`]: a placement names a track by index into this list, so a /// removal that reindexes the placements can only undo together with the /// palette it removed the row from. subtitles: Vec<SubtitleTrack>, /// The cue overrides: `(track, cue, text)`, the track by its position in /// `subtitles` and the cue by its position in that track. Saved as `cue` /// lines ([`crate::edith`]) and in every snapshot, so an undo takes a retype /// back with the words it changed. cue_edits: Vec<(usize, usize, String)>, /// The cues added to a track ([`AddedCue`]), in the order they were added. /// Each sits in its track's cue list at its own start, and a save writes /// them as `cueadd` lines. In every snapshot, as the overrides are. cue_adds: Vec<AddedCue>, /// The title cards and text overlays, in draw order: sorted by start, and /// overlaps allowed. In every snapshot, as the subtitle palette is. titles: Vec<Title>, chapters: Vec<Chapter>, /// The comment markers, sorted by frame ([`Marker`]). In every snapshot, as the /// chapters are. markers: Vec<Marker>, /// The project's own settings ([`ProjectSettings`]) -- the canvas, the /// rate, the proxies switches, the encoder seat, the mix rate, the /// rendition and the limiter. In every snapshot, so each of them undoes /// like a lane edit: they are the header of the project's own `.edith` /// file, not this machine's, and a pick a ctrl+z could not take back /// would be the one setting with no way out. settings: ProjectSettings, /// The sequences ([`sequence`]): the active one is the fields above, the rest are parked here. seq: Seqs, /// The library's bins (v51), in creation order. In every snapshot. bins: Vec<Bin>, /// The next bin id; never rolled back, so an id is never reused. bin_next: u32, /// The next sequence id; never rolled back, so an id is never reused. seq_next: u32, /// The expanded copy of the active sequence playback reads ([`Project::render_view`]). flat: FlatCache, } impl Project { /// `V1` and `A1`, one clip each covering the whole of `path` and the two /// grouped -- the state of a freshly opened video, where the timeline is the /// source. `frame_count` of 0 would break the never-empty invariant, so it /// is clamped to one frame. pub fn single(path: impl AsRef<Path>, frame_count: u32) -> Self { let clip = Clip { speed_keys: Vec::new(), reversed: false, stabilise: None, deinterlace: Default::default(), mark: Default::default(), angle: Default::default(), fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), visualizer: 0, viz_paint: 0, gain_db: 0.0, gain_keys: Vec::new(), pan: 0, pitch: 0, transform_keys: Vec::new(), opacity_keys: Vec::new(), color_keys: Vec::new(), effects: Vec::new(), start: 0, in_frame: 0, out_frame: frame_count.max(1), source: 0, link: Some(0), eq: None, color: None, transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; Self { // A file is opened on its first audio stream: nothing has picked // one yet, and every file with audio at all has that one. sources: vec![Source::new(path, 0)], lanes: LaneData::two_lanes(vec![clip.clone()], vec![clip]), eq: Vec::new(), color: Vec::new(), transform: Vec::new(), history: Vec::new(), redo: Vec::new(), pushes: 0, revision: next_revision(), batching: false, batch_snapped: false, pending: None, hold_cap: false, next_link: 1, subtitles: Vec::new(), cue_edits: Vec::new(), cue_adds: Vec::new(), titles: Vec::new(), chapters: Vec::new(), markers: Vec::new(), settings: ProjectSettings::placeholder(), seq: Seqs::single(), bins: Vec::new(), bin_next: 1, seq_next: 1, flat: FlatCache::default(), } } /// A project rebuilt from a saved edit list -- the load half of /// [`crate::edith`]. The lanes come in display order, each with its kind, and /// an `ord` is a lane's position among the lanes of that kind, exactly as /// [`Project::lanes`] hands them back. History is *not* saved, so /// [`Project::undo`] is `false` until the first edit of the new session. /// This is the one door untrusted parts come in through, so every invariant /// every other constructor keeps is checked here, by name and in release: /// no lanes at all, an empty clip, a clip naming a source (or an equalizer, /// or a colour) that is not there, a clip whose end overflows, a lane that is /// unsorted or self-overlapping, and the grouping rules of [`Clip::link`] /// below. /// /// A project whose lanes are all *empty* is not among them: an emptied /// timeline is a project like any other -- it plays black and silent, it /// saves, and it loads back. What a project cannot be is laneless, because /// [`Project::lanes`] is what a front-end lays out and what every `Lane` /// handle indexes into. pub fn from_parts( sources: Vec<Source>, lanes: Vec<(LaneKind, Vec<Clip>)>, eq: Vec<EqParams>, color: Vec<ColorParams>, transform: Vec<TransformParams>, ) -> crate::Result<Self> { if let Some(bad) = eq.iter().position(|p| !finite(p)) { return Err(format!("eq {bad} holds a band that is not a finite number").into()); } if let Some(bad) = color.iter().position(|p| !color_finite(p)) { return Err(format!("color {bad} holds a value that is not a finite number").into()); } if let Some(bad) = transform.iter().position(|p| !transform_finite(p)) { return Err(format!( "transform {bad} holds a value that is not a finite number in its range" ) .into()); } if lanes.is_empty() { return Err("no lanes at all: that is not a project".into()); } let lanes: Vec<LaneData> = lanes .into_iter() .map(|(kind, clips)| LaneData::new(kind, clips)) .collect(); for (data, lane) in lanes.iter().zip(handles(&lanes)) { let name = lane.label(); // A subtitle lane carries words and never a picture // ([`LaneKind::Subtitle`]); a file that puts one there names a // source on a lane that has nothing to play it with, and every // media path below would then read a clip off a lane it does not // know about. if data.kind == LaneKind::Subtitle && !data.clips.is_empty() { return Err(format!( "{name} is a subtitle track and holds {} media clip(s): a picture cannot play on one", data.clips.len() ) .into()); } for c in &data.clips { // The setters' own rules, which the file loader enforces too: a // picture has no volume of its own and only a picture is placed // by keys, and a clip gain outside its range is refused on the way // back in. A clip that breaks one would save a file that will not // load. if data.kind == LaneKind::Video && c.gain_db != 0.0 { return Err(format!( "{name} clip at {} has a gain: a picture has no volume of its own", c.start ) .into()); } if data.kind == LaneKind::Audio && !(c.opacity_keys.is_empty() && c.color_keys.is_empty()) { return Err(format!( "{name} clip at {} has opacity or colour keys: only a picture is animated by them", c.start ) .into()); } if data.kind == LaneKind::Audio && !c.transform_keys.is_empty() { return Err(format!( "{name} clip at {} has placement keys: only a picture is placed by them", c.start ) .into()); } if !(MIN_CLIP_GAIN_DB..=MAX_CLIP_GAIN_DB).contains(&c.gain_db) { return Err(format!( "{name} clip at {} has a gain of {} dB, outside its range", c.start, c.gain_db ) .into()); } if c.out_frame <= c.in_frame { return Err(format!( "{name} clip at {} plays the empty range [{}, {})", c.start, c.in_frame, c.out_frame ) .into()); } if c.source >= sources.len() && !is_sequence_source(c.source) { return Err(format!( "{name} clip at {} names source {} of {}", c.start, c.source, sources.len() ) .into()); } if c.start.checked_add(c.frames()).is_none() { return Err(format!( "{name} clip at {} runs past the last frame there is", c.start ) .into()); } if c.eq.is_some_and(|i| usize::from(i) >= eq.len()) { return Err(format!( "{name} clip at {} names eq {} of {}", c.start, c.eq.unwrap_or_default(), eq.len() ) .into()); } if c.color.is_some_and(|i| usize::from(i) >= color.len()) { return Err(format!( "{name} clip at {} names color {} of {}", c.start, c.color.unwrap_or_default(), color.len() ) .into()); } if c.transform .is_some_and(|i| usize::from(i) >= transform.len()) { return Err(format!( "{name} clip at {} names transform {} of {}", c.start, c.transform.unwrap_or_default(), transform.len() ) .into()); } } if !sorted_disjoint(&data.clips) { return Err(format!("the {name} lane is out of order or overlaps itself").into()); } } links_are_consistent(&lanes)?; // The counter sits past every id the file names, on either of a lane's // lists: a v16 caption may carry the highest link there is, and a // re-issued id would silently group it with the next placement that // asks for one -- the loader refusing a save the editor just wrote. let next_link = lanes .iter() .flat_map(|l| { let clips = l.clips.iter().filter_map(|c| c.link); let subs = l.subs.iter().filter_map(|s| s.link); clips.chain(subs) }) .max() // Saturating so a crafted file cannot make the counter wrap: at the // ceiling ids stop being fresh, which loses grouping, not memory. .map_or(0, |m| m.saturating_add(1)); Ok(Self { sources: sources .iter() .map(|s| Source { fav: s.fav, bin: s.bin, ..Source::new(&s.path, s.audio_stream) }) .collect(), lanes, eq, color, transform, history: Vec::new(), redo: Vec::new(), pushes: 0, revision: next_revision(), batching: false, batch_snapped: false, pending: None, hold_cap: false, next_link, subtitles: Vec::new(), cue_edits: Vec::new(), cue_adds: Vec::new(), titles: Vec::new(), chapters: Vec::new(), markers: Vec::new(), settings: ProjectSettings::placeholder(), seq: Seqs::single(), bins: Vec::new(), bin_next: 1, seq_next: 1, flat: FlatCache::default(), }) } /// The first video lane -- what an export renders and what a single-lane /// caller still means by "the clips". pub fn clips(&self) -> &[Clip] { self.lane(Lane::V1) } /// The clips of `lane`, or nothing at all for a lane that is not there. pub fn lane(&self, lane: Lane) -> &[Clip] { self.index(lane).map_or(&[][..], |i| &self.lanes[i].clips) } /// Shortens every clip whose out-point runs past its source's wall: `picture` is /// the wall of a video lane's clip and `sound` of an audio lane's, per source (the /// tables a playback session holds each lane to). Each lane is held to its own /// wall, as the load check holds it. Returns what was shortened, so a save can say /// so. Sequence sources and walls of zero are left alone. pub fn clamp_to_walls(&mut self, picture: &[u32], sound: &[u32]) -> Vec<WallClamp> { let wall = |kind: LaneKind, source: usize| -> Option<u32> { let table = if kind == LaneKind::Audio { sound } else { picture }; table.get(source).copied().filter(|&w| w > 0) }; let mut clamps = Vec::new(); for (lane, data) in handles(&self.lanes).into_iter().zip(self.lanes.iter_mut()) { for c in data.clips.iter_mut().filter(|c| !is_sequence_source(c.source)) { let Some(cap) = wall(data.kind, c.source) else { continue }; if c.out_frame > cap { clamps.push(WallClamp { lane, source: c.source, from: c.out_frame, to: cap }); c.out_frame = cap; } } } clamps } /// Every lane's handle, in display order -- what a walk over "all lanes" /// iterates, and what a front-end lays out top to bottom. pub fn lanes(&self) -> Vec<Lane> { handles(&self.lanes) } /// How many lanes of `kind` there are: `Lane::new(kind, ord)` names a lane /// for every `ord` below it, and none at or above it. pub fn lane_count(&self, kind: LaneKind) -> usize { self.lanes.iter().filter(|l| l.kind == kind).count() } /// Appends an empty lane of `kind` and hands back its handle. One undo /// step, because the lane list is state a front-end shows: an added lane /// that could not be taken back would be the one edit with no way out. /// Nothing plays differently until something is placed on it. /// /// An empty lane is a lane like any other -- so is a whole project of them /// ([`Project::from_parts`]); nothing plays until something is placed. pub fn add_lane(&mut self, kind: LaneKind) -> Lane { self.snapshot(); self.lanes.push(LaneData::new(kind, Vec::new())); Lane::new(kind, self.lane_count(kind) - 1) } /// Drops an *empty* lane -- [`add_lane`](Project::add_lane) taken back, and /// one undo step like it, which restores the lane where it stood. /// /// Refused, changing nothing, while the lane holds anything: the refusal /// names the clips (file and first frame), because a track removal that /// deleted takes with it would be the one edit nobody sees coming. Refused /// too for the last lane of its kind, which is `V1` or `A1`: those two are /// where an import and a paste land ([`Project::paste`] takes lane 0 of each /// kind and *skips* a kind that is not there), so a project without one /// would swallow half of every file dropped on it, silently. /// /// The lanes below it move up one `ord`, so a handle a caller was holding /// (a selection, an open card) names the lane after it from here on -- the /// one thing a front-end owes this call. pub fn remove_lane(&mut self, lane: Lane) -> crate::Result<()> { let Some(i) = self.index(lane) else { return Err(format!("there is no {} to remove", lane.label()).into()); }; // A subtitle lane is not in that rule: nothing lands on one by itself // -- a project starts without any and one is added when a caption is -- // so the last of them comes off like any other empty track. if self.lane_count(lane.kind) == 1 && lane.kind != LaneKind::Subtitle { return Err(format!( "{} is the only {} track: every import lands on it", lane.label(), match lane.kind { LaneKind::Video => "video", LaneKind::Audio => "audio", // Unreachable while the guard above lets the last subtitle // lane go, and its own word here so the sentence stays true // if that rule is ever relaxed. LaneKind::Subtitle => "subtitle", } ) .into()); } if !self.lanes[i].subs.is_empty() { // Three names and a count, exactly as the clips below get: the way // out of the refusal is the drag, so the words say so. let named: Vec<String> = self.lanes[i] .subs .iter() .take(3) .map(|s| format!("{} at frame {}", self.track_name(s.track), s.start)) .collect(); let rest = match self.lanes[i].subs.len().saturating_sub(named.len()) { 0 => String::new(), n => format!(" and {n} more"), }; return Err(format!( "{} still holds {}{rest}: delete those captions (or drag them to \ another track) first", lane.label(), named.join(", ") ) .into()); } let clips = &self.lanes[i].clips; if !clips.is_empty() { // Three names and a count: a lane can hold forty clips and a // refusal nobody reads to the end says nothing at all. let named: Vec<String> = clips .iter() .take(3) .map(|c| { let name = self.sources.get(c.source).map_or_else( || format!("source {}", c.source), |s| s.path.display().to_string(), ); format!("{name} at frame {}", c.start) }) .collect(); let rest = match clips.len().saturating_sub(named.len()) { 0 => String::new(), n => format!(" and {n} more"), }; return Err(format!( "{} still holds {}{rest}: delete those clips (or drag them to \ another track) first", lane.label(), named.join(", ") ) .into()); } self.snapshot(); self.lanes.remove(i); Ok(()) } /// Moves `lane` to display position `to` -- an index into /// [`Project::lanes`], the slot the lane there gives up -- and hands back /// the handle it answers to from now on. `None`, changing nothing and /// snapshotting nothing, for a lane that is not there, a position that is /// not, and a lane already standing in it. /// /// The whole track travels: its clips, its gain, its kind. Nothing is /// remapped, so no take changes lane and no group is broken -- this is the /// *order* of the lanes and nothing else, which is why one call is one undo /// step like an add or a removal. /// /// Display order **is** the stack: [`Project::composite_span_at`] shows the /// last video lane covering a frame, so moving one video lane past another /// changes which picture wins, in the preview and in an export alike. The /// audio lanes are summed and their sum does not care about order, so an /// audio track moved among the video ones is a rearrangement of the screen /// alone. /// /// A label is a *position* ([`Lane::label`] reads `ord`, which is the /// position among the lanes of that kind), so a lane that crosses one of /// its own kind swaps names with it -- `V2` dragged above `V1` becomes /// `V1`, clips and all -- while one that crosses only lanes of the other /// kind keeps its name. That is the returned handle: unchanged means every /// handle a caller holds still names the track it named before. pub fn move_lane(&mut self, lane: Lane, to: usize) -> Option<Lane> { let i = self.index(lane)?; if to >= self.lanes.len() || to == i { return None; } self.snapshot(); let data = self.lanes.remove(i); self.lanes.insert(to, data); Some(handles(&self.lanes)[to]) } /// Where `lane` sits in [`Project::lanes`], or `None` for a lane that is /// not there. The one place a handle becomes a position. fn index(&self, lane: Lane) -> Option<usize> { self.lanes .iter() .enumerate() .filter(|(_, l)| l.kind == lane.kind) .map(|(i, _)| i) .nth(lane.ord) } /// For an export of `range` only: drops the clips that lie wholly outside it and read a file /// `present` says is not there, so a range that never touches a missing source can be written /// without opening it. Nothing is dropped when that would shorten the timeline below what the /// range needs (the length of the file would change). A snapshot's edit, never the project's. pub fn drop_offline_outside(&mut self, range: (u32, u32), present: impl Fn(&Path) -> bool) { let gone: Vec<bool> = self.sources.iter().map(|s| !present(&s.path)).collect(); if !gone.iter().any(|g| *g) { return; } let keep_len = self.timeline_frames().min(range.1); let saved: Vec<Vec<Clip>> = self.lanes.iter().map(|l| l.clips.clone()).collect(); for lane in self.lanes.iter_mut().filter(|l| l.kind != LaneKind::Subtitle) { lane.clips.retain(|c| { let outside = c.end() <= range.0 || c.start >= range.1; !(outside && gone.get(c.source).copied().unwrap_or(false)) }); } if self.timeline_frames() < keep_len { for (lane, clips) in self.lanes.iter_mut().zip(saved) { lane.clips = clips; } } } fn lane_mut(&mut self, lane: Lane) -> Option<&mut Vec<Clip>> { let i = self.index(lane)?; Some(&mut self.lanes[i].clips) } /// The files the clips index into, in import order; index 0 is the file the /// project was opened with. pub fn sources(&self) -> &[Source] { &self.sources } /// Points source `idx` at `path`, a file the caller has already checked is /// the same material. No clip changes; a source's index is its identity. /// One undo step, which puts the old path back ([`Snapshot::sources`]); a /// relink to the path the source already has is no change and no step. pub fn relink_source(&mut self, idx: usize, path: &Path) -> crate::Result<()> { let Some(source) = self.sources.get(idx) else { return Err(format!("there is no source {idx}").into()); }; let to = canonical(path); if source.path == to { return Ok(()); } self.snapshot(); self.sources[idx].path = to; Ok(()) } /// Points several sources at new files in one undo step ([`Self::relink_source`] for each, /// unchecked: the caller has copies of the same material). Sources already there and indexes /// that name nothing are left alone; no step when nothing moves. Returns how many moved. pub fn relink_sources(&mut self, pairs: &[(usize, PathBuf)]) -> usize { let moves: Vec<(usize, PathBuf)> = pairs .iter() .filter(|(i, to)| self.sources.get(*i).is_some_and(|s| s.path != canonical(to))) .map(|(i, to)| (*i, canonical(to))) .collect(); if moves.is_empty() { return 0; } self.snapshot(); for (i, to) in &moves { self.sources[*i].path = to.clone(); } moves.len() } /// Takes `other`'s timeline over as one undo step: what this project was /// before the call is one step back, and it is what a recovery does with an /// autosave once the saved project is open. The lanes, the subtitle palette, /// the cue edits and adds, the titles, the chapters, the markers, the settings /// and the sources all come from `other`. Its clip effect tables (equalizer, colour, transform) are /// appended to this project's own and its clips re-pointed at them, so the /// indexes a step in the history holds stay valid; the link counter is raised /// past `other`'s links, so a new split never reuses one. `false`, with /// nothing changed, when the appended tables would not fit a clip's `u16`. pub fn adopt_as_step(&mut self, other: &Project) -> bool { let fits = |have: usize, add: usize| have + add <= usize::from(u16::MAX); if !fits(self.eq.len(), other.eq.len()) || !fits(self.color.len(), other.color.len()) || !fits(self.transform.len(), other.transform.len()) { return false; } let (eq_at, color_at, transform_at) = (self.eq.len(), self.color.len(), self.transform.len()); // A `u16` index shifted past the appended table: the fit check above keeps it in range. let shift = |i: u16, by: usize| (usize::from(i) + by) as u16; let mut lanes = other.lanes.clone(); for c in lanes.iter_mut().flat_map(|l| &mut l.clips) { c.eq = c.eq.map(|i| shift(i, eq_at)); c.color = c.color.map(|i| shift(i, color_at)); c.transform = c.transform.map(|i| shift(i, transform_at)); } self.snapshot(); self.eq.extend(other.eq.iter().cloned()); self.color.extend(other.color.iter().cloned()); self.transform.extend(other.transform.iter().cloned()); let mut parked = other.seq.parked.clone(); for c in parked.iter_mut().flat_map(|(_, d)| d.lanes.iter_mut()).flat_map(|l| &mut l.clips) { c.eq = c.eq.map(|i| shift(i, eq_at)); c.color = c.color.map(|i| shift(i, color_at)); c.transform = c.transform.map(|i| shift(i, transform_at)); } self.seq = Seqs { active: other.seq.active, names: other.seq.names.clone(), parked, mcam: other.seq.mcam.clone(), nested: Vec::new() }; self.seq_next = self.seq_next.max(other.seq_next); self.flat.clear(); self.lanes = lanes; self.subtitles = other.subtitles.clone(); self.cue_edits = other.cue_edits.clone(); self.cue_adds = other.cue_adds.clone(); self.titles = other.titles.clone(); // The active sequence's chapters and markers are the other timeline's too: left // out, a recovery drops them and undo cannot bring them back. self.chapters = other.chapters.clone(); self.markers = other.markers.clone(); self.settings = other.settings.clone(); self.sources = other.sources.clone(); self.bins = other.bins.clone(); self.bin_next = self.bin_next.max(other.bin_next); self.next_link = self.next_link.max(other.next_link); true } /// The same list in the `(path, stream)` shape the audio engine indexes /// into -- [`crate::AudioSession::open_multi_streams`] and /// [`crate::AudioSession::copy_multi_streams`] take it, so the stream a clip /// was placed with is the stream that plays *and* the stream that exports. pub fn audio_sources(&self) -> Vec<(PathBuf, usize)> { self.sources .iter() .map(|s| (s.path.clone(), s.audio_stream)) .collect() } /// Index for `path` played on `audio_stream`, appending it if it is new. /// Deduped by `fs::canonicalize` *and* by stream, so the same file reached /// by two paths imports once and two streams of one file are two sources. /// The lanes are untouched -- see [`Project::append_clip`] -- so this pushes /// no history: a source entry alone changes nothing playable. pub fn import(&mut self, path: impl AsRef<Path>, audio_stream: usize) -> usize { let source = Source::new(path, audio_stream); match self.sources.iter().position(|s| *s == source) { Some(idx) => idx, None => { self.sources.push(source); self.sources.len() - 1 } } } /// Append a whole-file clip of `source` to the end of every *media* lane, /// grouped -- an import lands as one take. Subtitle lanes are skipped: they /// carry no [`Clip`] ([`LaneKind::Subtitle`]), and a file's own subtitles /// come in through [`Project::add_subtitles`], which is the palette. /// /// One history snapshot, so an import is one undo step, and undoing it /// leaves the (harmless) source entry behind, because indexes are forever. /// Refused for an unknown source index. pub fn append_clip(&mut self, source: usize, frame_count: u32) -> bool { if source >= self.sources.len() { return false; } self.snapshot(); let start = self.timeline_frames(); let clip = Clip { speed_keys: Vec::new(), reversed: false, stabilise: None, deinterlace: Default::default(), mark: Default::default(), angle: Default::default(), fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), visualizer: 0, viz_paint: 0, gain_db: 0.0, gain_keys: Vec::new(), pan: 0, pitch: 0, transform_keys: Vec::new(), opacity_keys: Vec::new(), color_keys: Vec::new(), effects: Vec::new(), start, in_frame: 0, out_frame: frame_count.max(1), source, link: Some(self.new_link()), eq: None, color: None, transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; for data in self .lanes .iter_mut() .filter(|l| l.kind != LaneKind::Subtitle) { data.clips.push(clip.clone()); } true } /// Drops a source entry -- the file a library row *is* -- and renumbers /// every clip that indexed past it, which is why this is more than a /// `Vec::remove`. /// /// Refused, changing nothing, while any clip still plays from it: the /// refusal names the lanes and how many clips each holds, so a caller can /// say what has to be deleted first. The *last* entry goes like any other /// -- a project may name no file at all, which is an empty library over an /// empty timeline (nothing can play, since a clip would have refused the /// removal). What a front-end does with that is its own decision: `edith`'s /// window goes back to the empty state it launches in, and /// [`PlaybackSession::save_project`](crate::PlaybackSession::save_project) /// refuses to write a project that names nothing, because no such file /// could be opened again. /// /// Every index past `idx` moves down by one, so a caller holding a *raw* /// source index of its own -- a clipboard, which is the one thing outside /// this type that does -- has to fix it up or drop it, or a paste plays a /// different file. [`PlaybackSession::remove_source`] hands back the index /// that went for exactly that. /// /// corner-cut: this retires the undo stack. `history` holds the lanes, the /// subtitle palette and the project's own settings -- no source list -- so a /// snapshot older than the removal can name the very source being removed /// (delete a file's clips, then remove the file) and restoring it would /// point a clip at an entry that is gone. The upgrade path is snapshotting /// `sources` beside `lanes` in [`snapshot`](Project::snapshot) -- which the /// "indexes are forever" rule ([`append_clip`](Project::append_clip), and /// the reason [`without_orphan_sources`](Project::without_orphan_sources) /// exists) currently rules out. pub fn remove_source(&mut self, idx: usize) -> crate::Result<()> { let Some(source) = self.sources.get(idx) else { return Err(format!("there is no source {idx} to remove").into()); }; let name = source.path.display().to_string(); let used: Vec<String> = handles(&self.lanes) .into_iter() .zip(&self.lanes) .filter_map(|(lane, data)| { match data.clips.iter().filter(|c| c.source == idx).count() { 0 => None, 1 => Some(format!("{} (1 clip)", lane.label())), n => Some(format!("{} ({n} clips)", lane.label())), } }) .collect(); if !used.is_empty() { return Err(format!("{name} still plays on {}", used.join(", ")).into()); } if let Some((id, _)) = self .seq .parked .iter() .find(|(_, d)| d.lanes.iter().any(|l| l.clips.iter().any(|c| c.source == idx))) { let who = self.sequence_name(*id).unwrap_or("another sequence").to_string(); return Err(format!("{name} still plays in the sequence \"{who}\"").into()); } self.sources.remove(idx); let all = self .lanes .iter_mut() .chain(self.seq.parked.iter_mut().flat_map(|(_, d)| d.lanes.iter_mut())); for c in all.flat_map(|l| &mut l.clips) { if c.source > idx && !is_sequence_source(c.source) { c.source -= 1; } } self.flat.clear(); // The redo steps renumber nothing and name the old indexes: they go with the history. self.history.clear(); self.redo.clear(); Ok(()) } /// Drops the **last** source, if nothing names it, and leaves the undo /// history exactly as it was. The narrow door behind a rollback: a source /// imported by a batch that then failed is the newest entry and nothing on /// the timeline plays it, so taking it back out renumbers nothing and /// invalidates no step. /// /// Refused, with the source left in place, when it is not the last entry (a /// removal below it would renumber every index above, which is what /// [`remove_source`](Project::remove_source) does and why that one clears /// history), or when a clip plays it or an undo or redo step does. The steps /// that held the entry past the new end are cut to the list that is left, so /// no restore brings it back. pub fn remove_unused_source(&mut self, idx: usize) -> crate::Result<()> { if idx >= self.sources.len() { return Err(format!("there is no source {idx} to remove").into()); } let name = self.sources[idx].path.display().to_string(); if idx + 1 != self.sources.len() { return Err(format!( "{name} is not the last source, so removing it would renumber the sources after it" ) .into()); } let named = |lanes: &[LaneData]| { lanes.iter().any(|l| l.clips.iter().any(|c| c.source == idx)) }; let parked = |seq: &Seqs| seq.parked.iter().any(|(_, d)| named(&d.lanes)); if named(&self.lanes) || parked(&self.seq) || self.history.iter().any(|s| named(&s.lanes) || parked(&s.seq)) || self.redo.iter().any(|s| named(&s.lanes) || parked(&s.seq)) { return Err(format!("{name} is still played by a clip or an undo step").into()); } self.sources.pop(); // The steps that hold the entry past the new end (none of them names it) are cut to the // list that is left, so an undo does not bring the removed entry back. for step in self.history.iter_mut().chain(self.redo.iter_mut()) { step.sources.truncate(idx); } Ok(()) } /// The sources, equalizer and colour tables, and transform table a save /// writes, with the clips reindexed onto their pruned copies. Every /// imported source is kept, in session order, so a source's index in the /// file is the index it had in the session and stays put across save and /// open. Equalizer, colour and transform entries nothing names are still /// pruned: an undone `set_eq`/`set_color` leaves a setting nothing plays /// behind, and those indexes are re-assigned by first use -- lane by lane, /// in display order -- so the same project always emits the same bytes. /// /// Every lane comes out, in display order and with its kind: that is what a /// v6 `.edith` holds (empty lanes included) and what [`Project::from_parts`] /// takes back. /// /// The equalizer and colour tables are pruned the same way and for the same /// reason: an undone [`set_eq`](Project::set_eq) or /// [`set_color`](Project::set_color) leaves settings nothing plays behind, /// and this is the one moment they can go -- the indexes that survive it are /// only the ones a clip names. /// /// Keeping every source (rather than only the ones a clip names) means an /// emptied timeline still has the file it is scaffolded from on reopen -- /// the frame rate is written nowhere else -- and a source a clip no longer /// plays stays a library entry at the index a reload expects. Source 0 is /// whatever was imported first, which may be a still or a song; /// `PlaybackSession::open_project` picks its rate and its audio reference by /// what a source *is*, never by where it sits. pub fn without_orphan_sources(&self) -> Parts { // Every source comes out, in session order: an import is not an undo // step, so a source nothing plays is a deliberate library entry, and // its index is the one a reload must find it at again. let sources = self.sources.clone(); let mut moved_eq = vec![None; self.eq.len()]; let mut eq = Vec::new(); let mut moved_color = vec![None; self.color.len()]; let mut color = Vec::new(); let mut moved_transform = vec![None; self.transform.len()]; let mut transform = Vec::new(); let mut lanes = self.lanes.clone(); for c in lanes.iter_mut().flat_map(|l| &mut l.clips) { if let Some(old) = c.eq.map(usize::from) { c.eq = Some(match moved_eq[old] { Some(new) => new, None => { eq.push(self.eq[old].clone()); let new = (eq.len() - 1) as u16; moved_eq[old] = Some(new); new } }); } if let Some(old) = c.color.map(usize::from) { c.color = Some(match moved_color[old] { Some(new) => new, None => { color.push(self.color[old]); let new = (color.len() - 1) as u16; moved_color[old] = Some(new); new } }); } if let Some(old) = c.transform.map(usize::from) { c.transform = Some(match moved_transform[old] { Some(new) => new, None => { transform.push(self.transform[old]); let new = (transform.len() - 1) as u16; moved_transform[old] = Some(new); new } }); } } ( sources, // Every lane, subtitle ones included: they hold no [`Clip`], so what // comes out for one is an empty list, and its *place* in the display // order is what a save writes it for. What is on it travels beside // this, in [`lane_subs`](Project::lane_subs), as the gains do. lanes.into_iter().map(|l| (l.kind, l.clips)).collect(), eq, color, transform, ) } /// How loud `lane` plays, in dB: `0.0` for one nobody has touched, and for /// a lane that is not there. Whole-lane and whole-band -- every clip on the /// track moves by it, which is what makes it a different control from a /// clip's equalizer (one frequency range of one take) and from the master /// volume (what this machine monitors at, which no file carries). pub fn lane_gain_db(&self, lane: Lane) -> f32 { self.index(lane).map_or(0.0, |i| self.lanes[i].gain_db) } /// The first auto-duck on `lane`, if it has one: the one /// [`lane_duck_list`](Project::lane_duck_list) starts with. `None` for a lane /// without one and for a lane that is not there. A lane with several ducks /// has them all in `lane_duck_list`; this names only the first. pub fn lane_duck(&self, lane: Lane) -> Option<Duck> { self.lane_duck_list(lane).first().copied() } /// Every auto-duck on `lane`, one per key lane, in the order they were set. /// Empty for a lane without one and for a lane that is not there. pub fn lane_duck_list(&self, lane: Lane) -> Vec<Duck> { self.index(lane) .map_or_else(Vec::new, |i| self.lanes[i].ducks.clone()) } /// Sets an auto-duck on an audio lane: while the key lane `duck.under` is /// loud, this lane drops by `depth_db` ([`crate::duck`]). A lane can be /// ducked under several keys at once: a duck under a key this lane already /// has replaces that one, and a duck under a new key is added beside the /// rest. `None` clears every duck on the lane. One undo step, like a volume. /// /// `false`, and no history, for a lane that is not an audio lane or is not /// there, for a key that is not another audio lane of this project (`under` /// is the key's [`Lane::ord`]), for a number that is not finite, and for a /// duck already set exactly so. The numbers are clamped as the duck's own /// ranges clamp them. pub fn set_lane_duck(&mut self, lane: Lane, duck: Option<Duck>) -> bool { let Some(i) = self.index(lane) else { return false; }; if lane.kind != LaneKind::Audio { return false; } let next = match duck { Some(d) => { let Some(d) = d.clamped() else { return false; }; let key = Lane::new(LaneKind::Audio, d.under); if key == lane || self.index(key).is_none() { return false; } let mut ducks = self.lanes[i].ducks.clone(); match ducks.iter_mut().find(|x| x.under == d.under) { Some(slot) if *slot == d => return false, Some(slot) => *slot = d, None => ducks.push(d), } ducks } None => { if self.lanes[i].ducks.is_empty() { return false; } Vec::new() } }; self.snapshot(); self.lanes[i].ducks = next; true } /// Removes the duck `lane` has under the key `under` (the key's /// [`Lane::ord`]), leaving its other ducks. One undo step. `false`, and no /// history, for a lane with no duck under that key. pub fn clear_lane_duck(&mut self, lane: Lane, under: usize) -> bool { let Some(i) = self.index(lane) else { return false; }; if !self.lanes[i].ducks.iter().any(|d| d.under == under) { return false; } self.snapshot(); self.lanes[i].ducks.retain(|d| d.under != under); true } /// The voice processors of `lane` ([`crate::voice`]): all off, and the /// default, for a lane that has none, is not an audio lane or is not there. pub fn lane_voice(&self, lane: Lane) -> VoiceFx { self.index(lane) .map_or_else(VoiceFx::default, |i| self.lanes[i].voice.clone()) } /// Sets the voice processors of an audio lane. One undo step. The numbers are /// clamped to their ranges. `false`, and no history, for a lane that is not an /// audio lane or is not there, for a number that is not finite, for a noise /// reduction switched on without a profile to subtract, and for a setting the /// lane already has. pub fn set_lane_voice(&mut self, lane: Lane, voice: VoiceFx) -> bool { let Some(i) = self.index(lane) else { return false; }; if lane.kind != LaneKind::Audio { return false; } let Some(voice) = voice.clamped() else { return false; }; if voice.nr.on && voice.nr.profile.is_empty() { return false; } if self.lanes[i].voice == voice { return false; } self.snapshot(); self.lanes[i].voice = voice; true } /// Sets that lane's volume, clamped to [`MIN_GAIN_DB`]..=[`MAX_GAIN_DB`]. /// One undo step like every other edit -- it is in the lane list, so an /// undo restores it with the rest. `false`, and no history, for a lane that /// is not there, for a value that is not finite, and for one already set. /// /// `false` too for a subtitle lane, and that one is a *refusal* rather than /// a value nobody reads: words have no loudness, so a slider that appeared /// to move one would be a control with no effect. (A video lane's gain is /// meaningless in the same way but is kept settable: it is the pair `A1` /// carries for a take, and mixing reads it off the audio lane alone.) pub fn set_lane_gain_db(&mut self, lane: Lane, db: f32) -> bool { let Some(i) = self.index(lane) else { return false; }; if !db.is_finite() || lane.kind == LaneKind::Subtitle { return false; } let db = db.clamp(MIN_GAIN_DB, MAX_GAIN_DB); if self.lanes[i].gain_db == db { return false; } self.snapshot(); self.lanes[i].gain_db = db; true } /// The pan of an audio lane ([`Self::set_lane_pan`]), `0.0` for the middle and for a lane /// that is not there. pub fn lane_pan(&self, lane: Lane) -> f32 { self.index(lane).map_or(0.0, |i| self.lanes[i].pan) } /// Give an audio lane this pan, clamped to `-1.0..=1.0`. One undo step; `false` for a lane /// that is not there or not audio, a value that is not finite, or one already set. pub fn set_lane_pan(&mut self, lane: Lane, pan: f32) -> bool { let Some(i) = self.index(lane) else { return false; }; if !pan.is_finite() || lane.kind != LaneKind::Audio { return false; } let pan = pan.clamp(-1.0, 1.0); if self.lanes[i].pan == pan { return false; } self.snapshot(); self.lanes[i].pan = pan; true } /// Whether `lane` is hidden: a video lane left out of the picture, or a /// subtitle lane whose words are left off the screen and the export. `false` /// for a lane that is not there. pub fn lane_hidden(&self, lane: Lane) -> bool { self.index(lane).is_some_and(|i| self.lanes[i].hidden) } /// Whether `lane` is muted: an audio lane silenced in the mix /// ([`audio_gains`](Project::audio_gains) reads it as 0). `false` for a lane /// that is not there. pub fn lane_muted(&self, lane: Lane) -> bool { self.index(lane).is_some_and(|i| self.lanes[i].muted) } /// Whether `lane` is locked. The engine does not look at it: an edit to a /// locked lane is refused by the front-end, which is where the toast that /// offers Unlock lives. pub fn lane_locked(&self, lane: Lane) -> bool { self.index(lane).is_some_and(|i| self.lanes[i].locked) } /// The name the user gave `lane`, `None` when it has none (or is not there). pub fn lane_name(&self, lane: Lane) -> Option<&str> { self.index(lane).map(|i| self.lanes[i].name.as_str()).filter(|n| !n.is_empty()) } /// What a header shows for `lane`: its name, else its address (`V1`). pub fn lane_title(&self, lane: Lane) -> String { self.lane_name(lane).map_or_else(|| lane.label(), str::to_string) } /// Names `lane`; an empty (or all-blank) name clears it. One undo step. /// `Ok(false)`, and no history, for a lane that is not there or no change. /// Refused: more than [`MAX_LANE_NAME_CHARS`] characters, control characters. pub fn set_lane_name(&mut self, lane: Lane, name: &str) -> Result<bool, String> { let name = name.trim(); if name.chars().count() > MAX_LANE_NAME_CHARS { return Err(format!("a lane name is at most {MAX_LANE_NAME_CHARS} characters")); } if name.chars().any(char::is_control) { return Err("a lane name holds no control characters".into()); } let Some(i) = self.index(lane) else { return Ok(false); }; if self.lanes[i].name == name { return Ok(false); } self.snapshot(); self.lanes[i].name = name.to_string(); Ok(true) } /// Every lane's name (empty for none), in display order and in step with /// [`lane_gains`](Project::lane_gains) -- what a save writes. pub fn lane_names(&self) -> Vec<String> { self.lanes.iter().map(|l| l.name.clone()).collect() } /// The names a *load* puts back, with no undo step (as /// [`with_voices`](Project::with_voices)). pub fn with_lane_names(mut self, names: &[String]) -> Self { for (data, name) in self.lanes.iter_mut().zip(names) { data.name = name.clone(); } self } /// Hides or shows a lane. One undo step. `false`, and no history, for a lane /// that is not there, for one already in that state, and for an audio lane: /// there is no picture or caption on one to leave out (the mute is its /// switch), so a hide on it would be a control with no effect. pub fn set_lane_hidden(&mut self, lane: Lane, hidden: bool) -> bool { self.set_flag(lane, hidden, |k| k != LaneKind::Audio, |l| &mut l.hidden) } /// Mutes or unmutes an audio lane. One undo step. `false`, and no history, /// for a lane that is not there, for one already in that state, and for any /// lane but an audio one -- the video lane's sound is never mixed. pub fn set_lane_muted(&mut self, lane: Lane, muted: bool) -> bool { self.set_flag(lane, muted, |k| k == LaneKind::Audio, |l| &mut l.muted) } /// Locks or unlocks a lane. One undo step. `false`, and no history, for a /// lane that is not there and for one already in that state. The engine /// accepts edits on a locked lane all the same; see /// [`lane_locked`](Project::lane_locked). pub fn set_lane_locked(&mut self, lane: Lane, locked: bool) -> bool { self.set_flag(lane, locked, |_| true, |l| &mut l.locked) } /// The one door the three switches go through: refuse what the lane kind /// cannot have, refuse a no-op, otherwise snapshot and set. fn set_flag( &mut self, lane: Lane, on: bool, allowed: fn(LaneKind) -> bool, field: fn(&mut LaneData) -> &mut bool, ) -> bool { let Some(i) = self.index(lane) else { return false; }; if !allowed(lane.kind) || *field(&mut self.lanes[i]) == on { return false; } self.snapshot(); *field(&mut self.lanes[i]) = on; true } /// Every lane's switches, in display order and in step with /// [`lane_gains`](Project::lane_gains) -- what a save writes beside them. pub fn lane_flags(&self) -> Vec<LaneFlags> { self.lanes .iter() .map(|l| LaneFlags { hidden: l.hidden, muted: l.muted, locked: l.locked, pan: l.pan, }) .collect() } /// The switches a *load* puts back, beside [`with_mix`](Project::with_mix) /// and for the same reason: no undo step. A short list leaves the rest /// clear, which is what a file from before the switches means. pub fn with_lane_flags(mut self, flags: &[LaneFlags]) -> Self { for (data, f) in self.lanes.iter_mut().zip(flags) { data.hidden = f.hidden; data.muted = f.muted; data.locked = f.locked; data.pan = f.pan; } self } /// Every lane's volume in dB, in display order -- what a save writes, /// beside the lanes [`Project::without_orphan_sources`] hands back. /// /// The *same* lanes as that call, subtitle ones included at the `0.0` they /// are pinned to ([`set_lane_gain_db`](Project::set_lane_gain_db) refuses /// one): a save zips the two lists, so a lane that is in one and not the /// other would hand every track below it the volume of the track above. pub fn lane_gains(&self) -> Vec<f32> { self.lanes.iter().map(|l| l.gain_db).collect() } /// Every lane's ducks, in step with [`lane_gains`](Project::lane_gains) -- /// what a save writes beside them. Empty for every lane but an audio lane /// with a duck. pub fn lane_ducks(&self) -> Vec<Vec<Duck>> { self.lanes.iter().map(|l| l.ducks.clone()).collect() } /// Every lane's voice processors, in step with [`lane_gains`](Project::lane_gains) /// -- what a save writes beside them. pub fn lane_voices(&self) -> Vec<VoiceFx> { self.lanes.iter().map(|l| l.voice.clone()).collect() } /// What is placed on every lane, in display order and in step with /// [`without_orphan_sources`](Project::without_orphan_sources) exactly as /// [`lane_gains`](Project::lane_gains) is -- what a save writes as its `sub` /// lines. Empty for every video and audio lane, which hold no [`SubClip`]. /// /// Nothing is renumbered on the way out: a [`SubClip`] names a *palette* /// row ([`Project::subtitles`]) and the palette is saved whole, so unlike a /// clip's source there is no orphan to prune and no index to move. pub fn lane_subs(&self) -> Vec<Vec<SubClip>> { self.lanes.iter().map(|l| l.subs.clone()).collect() } /// The placements a *load* puts back, one list per lane in display order -- /// the door [`with_mix`](Project::with_mix) is, and no undo step for the /// same reason. Called *after* /// [`with_subtitles`](Project::with_subtitles), whose palette a placement /// names. /// /// The subtitle half of [`from_parts`](Project::from_parts) and checked /// like it, by name and in release, because it is the same untrusted file: /// a placement on a lane that is not a subtitle lane, an empty span or an /// empty window, a track the palette does not have, a span running past the /// last frame there is, and a lane that is out of order or overlaps itself /// ([`place_sub`] refuses every one of those on the live timeline). /// /// [`place_sub`]: Project::place_sub pub fn with_subs(mut self, subs: Vec<Vec<SubClip>>) -> crate::Result<Self> { let names = handles(&self.lanes); let palette = self.subtitles.len(); for ((data, lane), list) in self.lanes.iter_mut().zip(names).zip(subs) { let name = lane.label(); if list.is_empty() { continue; } if data.kind != LaneKind::Subtitle { return Err(format!( "{name} is not a subtitle track and holds {} caption(s): words go on a \ subtitle track", list.len() ) .into()); } for s in &list { if s.frames == 0 || s.out_us <= s.in_us || s.in_us < 0 { return Err(format!( "{name} caption at {} is empty: {} frames of [{}, {})", s.start, s.frames, s.in_us, s.out_us ) .into()); } // The width of that window, which the check above cannot see: // [`edith`](crate::edith) asks the same question at the parse // and this repeats it, because `with_subs` is the door a // hand-built `Project` comes through and a caller is not more // trustworthy than a file. if s.out_us - s.in_us > MAX_WINDOW_US { return Err(format!( "{name} caption at {} is a {}us window, past the {}us a caption can hold", s.start, s.out_us - s.in_us, MAX_WINDOW_US ) .into()); } if s.track >= palette { return Err(format!( "{name} caption at {} names subtitle track {} of {palette}", s.start, s.track ) .into()); } if s.start.checked_add(s.frames).is_none() { return Err(format!( "{name} caption at {} runs past the last frame there is", s.start ) .into()); } } if !subs_sorted_disjoint(&list) { return Err(format!("the {name} lane is out of order or overlaps itself").into()); } data.subs = list; // The counter also has to clear the ids these captions carry: this // is the door a load's placements come through, *after* // [`from_parts`](Project::from_parts) seeded it from the clips, and // a hand-grouped caption may carry the highest id in the file. self.next_link = self.next_link.max( data.subs .iter() .filter_map(|s| s.link) .max() .map_or(0, |m| m.saturating_add(1)), ); } Ok(self) } /// The mix settings a *load* puts back: lane volumes in display order (a /// short list leaves the rest at unity) and the master limiter, both /// clamped as their setters clamp them. /// /// The load's own door, beside [`from_parts`](Project::from_parts): the /// setters push an undo step, and a project that arrives one undo away from /// a state it was never in is a project whose first ctrl+z is a surprise. pub fn with_mix(mut self, gains: &[f32], limiter: Limiter) -> Self { for (data, &db) in self.lanes.iter_mut().zip(gains) { if db.is_finite() { data.gain_db = db.clamp(MIN_GAIN_DB, MAX_GAIN_DB); } } self.settings.limiter = limiter.with_ceiling(limiter.ceiling_db); self } /// The ducks a *load* puts back, in display order and beside /// [`with_mix`](Project::with_mix), with no undo step for the same reason. /// A short list leaves the rest without one; a duck the file named is /// already validated by [`crate::edith`]. pub fn with_ducks(mut self, ducks: &[Vec<Duck>]) -> Self { for (data, lane_ducks) in self.lanes.iter_mut().zip(ducks) { data.ducks = lane_ducks.clone(); } self } /// The voice processors a *load* puts back, in display order, with no undo /// step for the reason [`with_mix`](Project::with_mix) gives. A short list /// leaves the rest with none. pub fn with_voices(mut self, voices: &[VoiceFx]) -> Self { for (data, voice) in self.lanes.iter_mut().zip(voices) { data.voice = voice.clone(); } self } /// The HDR rendition a *load* puts back -- the same door /// [`with_mix`](Project::with_mix) is, and no undo step for the same reason. pub fn with_tone(mut self, preset: crate::tonemap::Preset) -> Self { self.settings.tone = preset; self } /// The canvas and rate a *load* or a fresh open names -- the project /// resolution and the rate every clip is counted in. The same door /// [`with_mix`](Project::with_mix) is, and no undo step for the same /// reason: a project that arrives one undo away from a state it was never /// in is a project whose first ctrl+z is a surprise. pub fn with_canvas(mut self, width: u32, height: u32, frame_rate: f64) -> Self { self.settings.resolution = (width, height); self.settings.frame_rate = frame_rate; self } /// The saved switches a *load* puts back: whether the picture is cut on /// the stand-ins, whether new ones are made on arrival, which encoder an /// export opens, and the rate the mix was picked to run at. The load half /// of [`set_proxies`](Project::set_proxies) and its siblings, and no undo /// step for [`with_mix`](Project::with_mix)'s reason. pub fn with_switches( mut self, proxies: bool, auto_proxies: bool, encoder_seat: crate::export::EncoderSeat, sample_rate: Option<u32>, ) -> Self { self.settings.proxies = proxies; self.settings.auto_proxies = auto_proxies; self.settings.encoder_seat = encoder_seat; self.settings.sample_rate = sample_rate; self } /// The word times a *load* puts back, `(track, cue, words)`, after the cue edits and /// additions have made the cue list they index. No undo step, as [`with_tone`](Project::with_tone). pub(crate) fn with_cue_words(mut self, rows: &[CueWordRow]) -> Self { for (track, cue, words) in rows { if let Some(c) = self.subtitles.get_mut(*track).and_then(|t| t.cues.get_mut(*cue)) { c.words = words.clone(); } } self } /// Gives the cues of `track` the word times of `timed`, the cues they were written from: /// each track cue takes the next timed cue with the same text that starts within a /// millisecond of it (an SRT keeps milliseconds). A cue none matches keeps none. Not an undo /// step of its own: it completes the import that made the track. pub fn attach_cue_words(&mut self, track: usize, timed: &[crate::subtitle::Cue]) { let Some(t) = self.subtitles.get_mut(track) else { return; }; let norm = |s: &str| s.split_whitespace().collect::<Vec<_>>().join(" "); let mut from = 0; for cue in &mut t.cues { let hit = timed[from..] .iter() .position(|w| (w.start_us - cue.start_us).abs() <= 1000 && norm(&w.text) == norm(&cue.text)); if let Some(k) = hit { cue.words = timed[from + k].words.clone(); from += k + 1; } } } /// How burned captions animate in this project ([`crate::captionanim`]). pub fn caption_style(&self) -> crate::captionanim::CaptionStyle { self.settings.caption_style } /// Sets it (clamped). `false` for the style already in force; otherwise one undo step. pub fn set_caption_style(&mut self, style: crate::captionanim::CaptionStyle) -> bool { let style = style.clamped(); if self.settings.caption_style == style { return false; } self.snapshot(); self.settings.caption_style = style; true } /// The caption style a *load* puts back: no undo step, as [`with_tone`](Project::with_tone). pub(crate) fn with_caption_style(mut self, style: crate::captionanim::CaptionStyle) -> Self { self.settings.caption_style = style.clamped(); self } /// Which HDR-to-SDR rendition this project is watched and exported in. A /// project setting, not a clip's: one picture, one look /// ([`crate::tonemap::Preset`]). pub fn tone(&self) -> crate::tonemap::Preset { self.settings.tone } /// The project's notes: free text, several lines allowed, empty until one is written. pub fn notes(&self) -> &str { &self.settings.notes } /// Sets the notes. `false` for the text already in force; otherwise one undo step. pub fn set_notes(&mut self, notes: &str) -> bool { if self.settings.notes == notes { return false; } self.snapshot(); self.settings.notes = notes.to_string(); true } /// The notes a *load* puts back: no undo step, as [`with_tone`](Project::with_tone). pub(crate) fn with_notes(mut self, notes: String) -> Self { self.settings.notes = notes; self } /// How long a still image is placed for, in seconds: [`STILL_DEFAULT_SECS`] until the /// project picks another length. pub fn still_secs(&self) -> f64 { self.settings.still_secs } /// Sets it, clamped to [`STILL_MIN_SECS`]..[`STILL_MAX_SECS`]. `false` for the length already /// in force; otherwise one undo step. Only stills placed after this change take the new length. pub fn set_still_secs(&mut self, secs: f64) -> bool { let secs = if secs.is_finite() { secs.clamp(STILL_MIN_SECS, STILL_MAX_SECS) } else { STILL_DEFAULT_SECS }; if self.settings.still_secs == secs { return false; } self.snapshot(); self.settings.still_secs = secs; true } /// The still length a *load* puts back: no undo step, as [`with_tone`](Project::with_tone). pub(crate) fn with_still_secs(mut self, secs: Option<f64>) -> Self { self.settings.still_secs = secs.map_or(STILL_DEFAULT_SECS, |s| s.clamp(STILL_MIN_SECS, STILL_MAX_SECS)); self } /// Sets it. `false` for the one already in force, which is what keeps a /// re-pick of the current rendition from costing a reseek. One undo step /// like every other project-setting pick. pub fn set_tone(&mut self, preset: crate::tonemap::Preset) -> bool { if self.settings.tone == preset { return false; } self.snapshot(); self.settings.tone = preset; true } /// The subtitle tracks a *load* puts back, in the order they were saved -- /// the same door [`with_mix`](Project::with_mix) is, and no undo step for /// the same reason. pub fn with_subtitles(mut self, tracks: Vec<SubtitleTrack>) -> Self { self.subtitles = tracks; self } /// The languages a *load* puts back over the palette ([`with_subtitles`]( /// Project::with_subtitles) must come first): `(track, code)` pairs, the /// track by its position in the palette, as the `sublang` lines name it. /// No undo step, as with the palette itself. A pair naming no row is /// ignored; the reader has already refused a line that names one. pub fn with_sub_langs(mut self, langs: &[(usize, String)]) -> Self { for (track, code) in langs { if let Some(t) = self.subtitles.get_mut(*track) { crate::subtitle::set_language(t, code); } } self } /// Names the language of subtitle track `track`: an ISO 639-2 code as /// [`crate::subtitle::language_code`] gives it, or `und`. One undo step, and /// `false` (no step) for a track that is not there or already reads that. /// Kept in the project file (`sublang` lines), and an export writes it as /// the track's language. pub fn set_subtitle_language(&mut self, track: usize, code: &str) -> bool { match self.subtitles.get(track) { Some(t) if t.language != code => {} _ => return false, } self.snapshot(); crate::subtitle::set_language(&mut self.subtitles[track], code); true } /// The title cards and text overlays, in draw order: sorted by start, and /// where two share a start, in the order they were added. pub fn titles(&self) -> &[Title] { &self.titles } /// Adds a title and returns its index in [`titles`](Project::titles). It is /// kept sorted by start, so a title lands after the ones that start no /// later than it. Overlaps are allowed; the later one draws over the earlier. /// /// Refused, changing nothing and taking no undo step, when [`check_title`] /// refuses the title. A title may start past the last clip: the timeline /// runs on to cover it, over black where no clip plays. pub fn add_title(&mut self, title: Title) -> crate::Result<usize> { check_title(&title)?; self.snapshot(); let idx = self.titles.partition_point(|t| t.start <= title.start); self.titles.insert(idx, title); Ok(idx) } /// Replaces the title at `idx` with `title`, then re-sorts the list by start /// -- so the title may move, and a caller that holds its index looks it up /// again. `false`, changing nothing, for an index that is not there, a title /// [`check_title`] refuses, or a title the same as the one it replaces. pub fn set_title(&mut self, idx: usize, title: Title) -> bool { if idx >= self.titles.len() || check_title(&title).is_err() || self.titles[idx] == title { return false; } self.snapshot(); self.titles[idx] = title; self.titles.sort_by_key(|t| t.start); true } /// Takes the title at `idx` off the list. `false`, changing nothing, for an /// index that is not there. pub fn remove_title(&mut self, idx: usize) -> bool { if idx >= self.titles.len() { return false; } self.snapshot(); self.titles.remove(idx); true } /// The frame the last title ends on, or 0 with none: what the timeline /// runs on to, past its media. fn title_end(&self) -> u32 { self.titles.iter().map(Title::end).max().unwrap_or(0) } /// The chapter markers, sorted by frame, at most one per frame. pub fn chapters(&self) -> &[Chapter] { &self.chapters } /// Adds a chapter and returns its index in [`chapters`](Project::chapters). /// Refused, changing nothing and taking no undo step, when [`check_chapter`] /// refuses it or another chapter already sits on its frame. pub fn add_chapter(&mut self, chapter: Chapter) -> crate::Result<usize> { check_chapter(&chapter)?; if let Some(other) = self.chapters.iter().find(|c| c.frame == chapter.frame) { return Err(format!("chapter {:?} already starts at frame {}", other.title, other.frame).into()); } self.snapshot(); let idx = self.chapters.partition_point(|c| c.frame < chapter.frame); self.chapters.insert(idx, chapter); Ok(idx) } /// Takes the chapter at `idx` off the list. `false`, changing nothing, for an /// index that is not there. pub fn remove_chapter(&mut self, idx: usize) -> bool { if idx >= self.chapters.len() { return false; } self.snapshot(); self.chapters.remove(idx); true } /// Renames the chapter at `idx`. `false`, changing nothing, for an index that /// is not there, a name [`check_chapter`] refuses, or the name it already has. pub fn set_chapter_title(&mut self, idx: usize, title: &str) -> bool { let Some(current) = self.chapters.get(idx) else { return false; }; let renamed = Chapter { frame: current.frame, title: title.to_string() }; if current.title == title || check_chapter(&renamed).is_err() { return false; } self.snapshot(); self.chapters[idx] = renamed; true } /// Moves the chapter at `idx` to `frame`, and returns its new index. Refused, /// changing nothing, for an index that is not there or a frame another chapter /// already holds; a move to the frame it is on is a no-op that takes no step. pub fn move_chapter(&mut self, idx: usize, frame: u32) -> crate::Result<usize> { let Some(current) = self.chapters.get(idx) else { return Err("no chapter there to move".into()); }; if current.frame == frame { return Ok(idx); } if let Some(other) = self.chapters.iter().find(|c| c.frame == frame) { return Err(format!("chapter {:?} already starts at frame {frame}", other.title).into()); } self.snapshot(); let mut chapter = self.chapters.remove(idx); chapter.frame = frame; let at = self.chapters.partition_point(|c| c.frame < frame); self.chapters.insert(at, chapter); Ok(at) } /// Maps every chapter's frame through `map` in one undo step, for an edit that /// moves time: a cut, a ripple, a rate change. Two chapters that land on one /// frame keep the first in the list and drop the other, which is why the /// count of chapters dropped is returned. `0`, changing nothing and taking no /// step, when no frame moves. pub fn remap_chapters(&mut self, map: impl Fn(u32) -> u32) -> usize { let before = self.chapters.len(); let mut mapped: Vec<Chapter> = self .chapters .iter() .map(|c| Chapter { frame: map(c.frame), title: c.title.clone() }) .collect(); if mapped == self.chapters { return 0; } self.snapshot(); // Stable sort keeps list order among chapters on one frame, so the first // one written keeps its frame. mapped.sort_by_key(|c| c.frame); mapped.dedup_by_key(|c| c.frame); let dropped = before - mapped.len(); self.chapters = mapped; dropped } /// Sets the whole chapter list at once, for a loader: sorted by frame, a /// second chapter on a frame dropped. Not an edit, so no undo step. pub(crate) fn with_chapters(mut self, mut chapters: Vec<Chapter>) -> Self { chapters.sort_by_key(|c| c.frame); chapters.dedup_by_key(|c| c.frame); self.chapters = chapters; self } /// The comment markers, sorted by frame (several may share one). pub fn markers(&self) -> &[Marker] { &self.markers } /// Adds a marker and returns its index in [`markers`](Project::markers); after any /// marker already on its frame. Refused, changing nothing and taking no undo step, /// when [`check_marker`] refuses it. pub fn add_marker(&mut self, marker: Marker) -> crate::Result<usize> { check_marker(&marker)?; self.snapshot(); let idx = self.markers.partition_point(|m| m.frame <= marker.frame); self.markers.insert(idx, marker); Ok(idx) } /// Takes the marker at `idx` off. `false`, changing nothing, for an index that is not there. pub fn remove_marker(&mut self, idx: usize) -> bool { if idx >= self.markers.len() { return false; } self.snapshot(); self.markers.remove(idx); true } /// Replaces the marker at `idx` and returns its new index (it re-sorts when its frame /// moves). Refused, changing nothing, for an index that is not there, a marker /// [`check_marker`] refuses, or one equal to what is there. pub fn set_marker(&mut self, idx: usize, marker: Marker) -> crate::Result<usize> { let Some(current) = self.markers.get(idx) else { return Err("no marker there to change".into()); }; check_marker(&marker)?; if *current == marker { return Err("the marker is already so".into()); } self.snapshot(); if self.markers[idx].frame == marker.frame { self.markers[idx] = marker; return Ok(idx); } self.markers.remove(idx); let at = self.markers.partition_point(|m| m.frame <= marker.frame); self.markers.insert(at, marker); Ok(at) } /// Maps every marker's start and end through `map` in one undo step, for an edit that /// moves time, as [`remap_chapters`](Project::remap_chapters) does. A range keeps at /// least a point of itself; markers never merge, so none is lost. `false`, changing /// nothing and taking no step, when nothing moves. pub fn remap_markers(&mut self, map: impl Fn(u32) -> u32) -> bool { let mut mapped: Vec<Marker> = self .markers .iter() .map(|m| { let frame = map(m.frame); let length = if m.length == 0 { 0 } else { map(m.end()).saturating_sub(frame) }; Marker { frame, length, ..m.clone() } }) .collect(); if mapped == self.markers { return false; } self.snapshot(); mapped.sort_by_key(|m| m.frame); self.markers = mapped; true } /// Sets the whole marker list at once, for a loader: sorted by frame. Not an edit, so no undo step. pub(crate) fn with_markers(mut self, mut markers: Vec<Marker>) -> Self { markers.sort_by_key(|m| m.frame); self.markers = markers; self } /// Sets the whole title list at once, for a loader: the file's own order is /// kept, sorted by start as [`add_title`](Project::add_title) keeps it. Not an /// edit, so no undo step. pub(crate) fn with_titles(mut self, mut titles: Vec<Title>) -> Self { titles.sort_by_key(|t| t.start); self.titles = titles; self } /// What this timeline shows over the picture, in the order they were added: /// what a front-end lists and what a save writes. pub fn subtitles(&self) -> &[SubtitleTrack] { &self.subtitles } /// Adds a track, unless the very same one -- same file, same track number /// inside it -- is already on the list. `false` for that repeat: importing /// the same `.srt` twice is one subtitle track, not two identical ones. /// Adds every track of an import that is not already on the list, as one undo /// step: a failed batch that imported a track rolls the import back with it. /// Returns how many were added (none, and no step, when every one is a repeat). pub fn add_subtitles_all(&mut self, tracks: &[SubtitleTrack]) -> usize { let fresh: Vec<&SubtitleTrack> = tracks .iter() .filter(|t| !self.subtitles.iter().any(|o| o.path == t.path && o.track == t.track)) .collect(); if fresh.is_empty() { return 0; } self.snapshot(); let n = fresh.len(); self.subtitles.extend(fresh.into_iter().cloned()); n } pub fn add_subtitles(&mut self, track: &SubtitleTrack) -> bool { if self .subtitles .iter() .any(|t| t.path == track.path && t.track == track.track) { return false; } self.subtitles.push(track.clone()); true } /// Takes the track at `idx` off the list -- what a row's own remove goes /// through, so whatever an import added can be taken back out. Refused by /// name for a row this project does not have, rather than a silent no-op: /// a front-end holding a stale index has picked the wrong row and needs to /// hear it. /// /// Every index past `idx` moves down by one, as [`remove_source`]( /// Project::remove_source)'s do: a caller holding a picked row (an export's /// [`crate::export::ExportSettings::subtitles`]) has to fix it up or drop /// it, or it names a different track afterwards. The placements on the /// lanes are *not* a caller's to fix: a [`SubClip`] names its track by /// index into this very list, so every one past `idx` is walked down with /// it here -- the reindex [`remove_source`](Project::remove_source) does /// for its clips, for its reason. /// /// Refused, changing nothing, while anything placed on a subtitle lane /// plays *this* row: the placement would have to be deleted or retargeted, /// and either one is an edit nobody asked for. The refusal names the lane /// and the frame it is at, up to three of them -- /// [`remove_source`](Project::remove_source) refuses the same way for the /// same silent corruption. /// /// One undo step, and the reindex with it: every placement names a track /// by index into the palette ([`crate::subtitle`]), so a removal that /// renumbers them can only come back together with the palette it took /// the row from -- which is what the history entry holds, beside the /// lanes it also carries ([`Snapshot`]). The inverse, for a caller that /// wants the file's tracks back, is [`crate::PlaybackSession::import_subtitles`] /// -- the panel's own door, which reads the subtitles of a file and /// nothing else. pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> { if idx >= self.subtitles.len() { return Err(format!("there is no subtitle track {idx} to remove").into()); } let placed: Vec<String> = handles(&self.lanes) .into_iter() .zip(&self.lanes) .flat_map(|(lane, data)| { data.subs .iter() .filter(move |s| s.track == idx) .map(move |s| format!("{} at frame {}", lane.label(), s.start)) }) .take(3) .collect(); if !placed.is_empty() { return Err(format!( "{} is on the timeline ({}): delete those clips first", self.track_name(idx), placed.join(", ") ) .into()); } self.snapshot(); self.subtitles.remove(idx); // The overrides follow their tracks down, as the placements do, and the // removed row's own go with it: a row added again later is a new read of // the file, and its words are the file's. self.cue_edits.retain(|(t, _, _)| *t != idx); for (t, _, _) in &mut self.cue_edits { if *t > idx { *t -= 1; } } self.cue_adds.retain(|a| a.track != idx); for a in &mut self.cue_adds { if a.track > idx { a.track -= 1; } } for s in self.lanes.iter_mut().flat_map(|l| &mut l.subs) { if s.track > idx { s.track -= 1; } } Ok(()) } /// What a refusal calls a palette row: the name the panel shows, and the /// index alone for a row that is not there. fn track_name(&self, track: usize) -> String { self.subtitles .get(track) .map_or_else(|| format!("subtitle track {track}"), |t| t.label.clone()) } /// The subtitle lanes in display order -- what a front-end lays out and /// what [`sub_lane_cues`](Project::sub_lane_cues) is asked of, the twin of /// [`audio_lanes`](Project::audio_lanes). pub fn subtitle_lanes(&self) -> Vec<Lane> { self.lanes() .into_iter() .filter(|l| l.kind == LaneKind::Subtitle) .collect() } /// What is placed on `lane`, in timeline order -- [`Project::lane`]'s twin /// for a subtitle track, and empty for every other lane and for one that is /// not there. pub fn sub_lane(&self, lane: Lane) -> &[SubClip] { self.index(lane).map_or(&[][..], |i| &self.lanes[i].subs) } /// Put `sub` on `lane` at timeline frame `at` -- [`place`](Project::place)'s /// twin, with one deliberate difference: it **refuses** an overlap instead /// of overwriting what it lands on. /// /// `at` is where it lands and [`sub.start`](SubClip::start) is ignored, /// exactly as [`place`](Project::place) ignores a [`Clip`]'s: what a drag /// carries is a *window of a track* and where the hand let go, and the /// second of those is the argument. So a caller builds the placement once /// (from the palette row it dragged) and drops it at as many frames as it /// likes without rewriting the field each time. /// /// A picture placed over a picture hides it and the hidden one is still /// there to drag back out; two captions in one frame are two lines nobody /// asked to stack, and the words that lost are gone from the screen with /// nothing to show for it. So the refusal names the placement in the way, /// and the caller picks another frame or another lane -- which is what a /// second subtitle lane is for. /// /// One undo step on success, and none on any refusal: an empty window or an /// empty span, a track the palette does not have /// ([`Project::subtitles`]), a lane that is not there, a lane that is not a /// subtitle lane, a span that would run past the last frame there is, and /// the overlap above. pub fn place_sub(&mut self, lane: Lane, at: u32, sub: SubClip) -> crate::Result<()> { if lane.kind != LaneKind::Subtitle { return Err(format!( "{} is not a subtitle track: words go on a subtitle track, pictures and sound on \ the others", lane.label() ) .into()); } let Some(i) = self.index(lane) else { return Err(format!("there is no {} to place on", lane.label()).into()); }; if sub.frames == 0 || sub.out_us <= sub.in_us || sub.in_us < 0 { return Err(format!( "that placement is empty: {} frames of [{}, {})", sub.frames, sub.in_us, sub.out_us ) .into()); } // The width of that window, which the check above cannot see: the same // bound the two file doors hold a placement to // ([`edith`](crate::edith)'s parse and [`with_subs`]), asked here // because this is the door a *dragged* caption comes through and the // hand that dragged it builds the `SubClip` itself. if sub.out_us - sub.in_us > MAX_WINDOW_US { return Err(format!( "that placement is a {}us window, past the {}us a caption can hold", sub.out_us - sub.in_us, MAX_WINDOW_US ) .into()); } if sub.track >= self.subtitles.len() { return Err(format!( "it names subtitle track {} of {}", sub.track, self.subtitles.len() ) .into()); } // The placement belongs to no group, for [`place`]'s reason: a caption // dragged out of the palette arrives alone, and grouping it with what // is under it is a hand's decision ([`Project::group_all`]). let sub = SubClip { start: at, link: None, ..sub }; if at.checked_add(sub.frames).is_none() { return Err( format!("a placement at frame {at} runs past the last frame there is").into(), ); } if let Some(other) = self.lanes[i] .subs .iter() .find(|o| o.end() > sub.start && o.start < sub.end()) { return Err(format!( "the {} subtitle at frame {} already covers [{}, {}): move it, or place this one \ on another subtitle track", lane.label(), other.start, other.start, other.end() ) .into()); } self.snapshot(); let subs = &mut self.lanes[i].subs; let idx = subs.partition_point(|o| o.start < sub.start); subs.insert(idx, sub); debug_assert!(subs_sorted_disjoint(subs)); Ok(()) } /// Move the subtitle at `idx` of `from` onto `to` with its head at `start` /// -- [`move_clip`](Project::move_clip)'s twin. A caption in no group is /// refused an overlap where that one clamps to it, for /// [`place_sub`](Project::place_sub)'s reason: there is no group to hold it /// still and no picture under it that a landing would hide, so "as far as /// it goes" would silently be a frame nobody named. /// /// A caption *in* a group drags the group: one delta for every member -- /// its clips and its other captions -- clamped to the room each of them /// has, exactly as a clip's drag always has. That is the point of putting /// a caption in a group: the words go where the picture goes. /// /// The window travels untouched: a caption dragged later says the same /// words later, exactly as a clip dragged later plays the same pictures /// later. /// /// One undo step on success; nothing changed and no step on a refusal -- /// an index that is not there, a lane that is not a subtitle lane or is not /// there, and (for a caption in no group) an overlap. A drop that changes /// neither lane nor frame is `Ok` and no step: a hand that picked a caption /// up and put it back has done nothing wrong, and a front-end showing every /// `Err` would say so. pub fn move_sub(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> crate::Result<()> { let Some(sub) = self.sub_lane(from).get(idx).copied() else { return Err(format!("there is no subtitle {idx} on {}", from.label()).into()); }; if to.kind != LaneKind::Subtitle { return Err(format!( "{} is not a subtitle track: a caption cannot play on it", to.label() ) .into()); } let (Some(here), Some(dest)) = (self.index(from), self.index(to)) else { return Err(format!("there is no {} to move onto", to.label()).into()); }; if (here, start) == (dest, sub.start) { // A pick-up-put-back: the drag ended where it began, which is not a // mistake to name -- `Ok`, and no undo step, because nothing // changed. A refusal here would toast a hand that did nothing. return Ok(()); } if start.checked_add(sub.frames).is_none() { return Err( format!("a placement at frame {start} runs past the last frame there is").into(), ); } // A grouped caption carries its group, clamped to the room the whole // group has rather than refused ([`Project::move_room]) -- one drag, one // delta, one undo step. let members = self.group_of(from, idx).expect("the subtitle was found"); if !members.alone() { if members .subs .iter() .any(|&(l, i)| l == dest && (l, i) != (here, idx)) { return Err(format!( "a group is one caption per track: {} already carries this group", to.label() ) .into()); } let want = i64::from(start) - i64::from(sub.start); let Some((lo, hi)) = self.move_room(&members, Some((here, idx, dest, start)), None) else { return Err(format!( "another placement already covers those frames on {}", to.label() ) .into()); }; let delta = want.clamp(lo, hi); self.snapshot(); for &(l, i) in &members.clips { let c = &mut self.lanes[l].clips[i]; c.start = (i64::from(c.start) + delta) as u32; } for &(l, i) in &members.subs { let s = &mut self.lanes[l].subs[i]; s.start = (i64::from(s.start) + delta) as u32; } let moved = self.lanes[here].subs.remove(idx); let subs = &mut self.lanes[dest].subs; let at = subs.partition_point(|o| o.start < moved.start); subs.insert(at, moved); debug_assert!(subs_sorted_disjoint(subs)); debug_assert!( members .clips .iter() .all(|&(l, _)| sorted_disjoint(&self.lanes[l].clips)) ); return Ok(()); } let moved = SubClip { start, ..sub }; if let Some(other) = self.lanes[dest] .subs .iter() .enumerate() .find(|&(j, o)| { !(dest == here && j == idx) && o.end() > moved.start && o.start < moved.end() }) .map(|(_, o)| *o) { return Err(format!( "the {} subtitle at frame {} already covers [{}, {})", to.label(), other.start, other.start, other.end() ) .into()); } self.snapshot(); self.lanes[here].subs.remove(idx); let subs = &mut self.lanes[dest].subs; let at = subs.partition_point(|o| o.start < moved.start); subs.insert(at, moved); debug_assert!(subs_sorted_disjoint(subs)); debug_assert!(subs_sorted_disjoint(&self.lanes[here].subs)); Ok(()) } /// Move one `edge` of the subtitle at `idx` of `lane` to timeline frame /// `to` -- [`trim`](Project::trim)'s twin, and clamped like it: a hand /// pulling an edge past what is legal means "as far as it goes". /// /// The window follows the edge, in the seconds the frames are worth at /// `fps`: a head pulled in starts the caption later *in its track* (the /// cues that were skipped stay skipped, exactly as a trimmed clip's frames /// stay behind), and a tail pulled out shows more of it. `fps` is the one /// thing a [`Project`] does not know -- it is the caller's timeline rate, /// the same one [`crate::export::timeline_cues`] is asked with -- and it is /// needed here and nowhere else in this file, because a trim is the only /// edit that changes a *duration*. /// /// The walls: one frame always survives, an edge never crosses the /// neighbouring subtitle on its own lane, a head never walks back past the /// track's own start (`in_us` of 0), and a tail never runs past the last /// cue the track has -- which is the source length a media trim needs a /// caller's table for, and which this one can simply read. /// /// `Err`, with nothing changed and no undo step, for an index that is not /// there and an unusable `fps`. An edge that ends up where it already was /// -- the hand did not move it, or the clamp stopped it at a wall it stood /// against -- is `Ok` and no step, for [`move_sub`](Project::move_sub)'s /// reason: a drag that stops at a wall is not a mistake to report. pub fn trim_sub( &mut self, lane: Lane, idx: usize, edge: Edge, to: u32, fps: f64, ) -> crate::Result<()> { if !(fps.is_finite() && fps > 0.0) { return Err(format!("{fps} is not a frame rate to trim against").into()); } let Some((lo, hi)) = self.trim_sub_room(lane, idx, edge, fps) else { return Err(format!("there is no subtitle {idx} on {}", lane.label()).into()); }; let Some(sub) = self.sub_lane(lane).get(idx).copied() else { return Err(format!("there is no subtitle {idx} on {}", lane.label()).into()); }; let to = to.clamp(lo, hi); let at = match edge { Edge::Start => sub.start, Edge::End => sub.end(), }; if to == at { // The edge is where it was asked to go -- either the hand did not // move it, or the clamp above stopped it at a wall it was already // standing against. Both are a drag that changed nothing: `Ok`, no // undo step, and no toast for a box that simply stopped moving. return Ok(()); } self.snapshot(); let members = self.group_of(lane, idx).expect("the subtitle was found"); let i = self.index(lane).expect("the subtitle was found on it"); let s = &mut self.lanes[i].subs[idx]; match edge { // The words that stay are the words that were there, so the window // moves with the head -- a trim, not a slip. Edge::Start => { // The head moves and the words that stay are the words that // were there, so `in_us` follows it -- a trim, not a slip. // Falling `in_us` makes the window *wider* (the ceiling bounds // a difference), which is the half of this nothing here can // see: `set_window` is what puts the width back inside. set_window( s, s.in_us .saturating_add(us_of(i64::from(to) - i64::from(s.start), fps)), s.out_us, ); s.frames = s.end() - to; s.start = to; } Edge::End => { // ...and the tail, for [`set_window`]'s reason: the End room // is walled at the ceiling, but `set_window` is what holds it // for every other composer too, including this one when a // caller hands `trim_sub` a `to` past its own room. set_window( s, s.in_us, s.out_us .saturating_add(us_of(i64::from(to) - i64::from(s.end()), fps)), ); s.frames = to - s.start; } } debug_assert!(s.frames >= 1 && s.out_us > s.in_us && s.in_us >= 0); debug_assert!(subs_sorted_disjoint(&self.lanes[i].subs)); // A caption in a group drags its group's edge with it, exactly as a // clip's trim does: one delta, each member clamped to its own room. // No source table to grow by, which only tightens how far a member may // follow -- never whether it may. self.follow_group( &members, &(i, idx), edge, i64::from(to) - i64::from(at), SourceWalls::same(&[]), ); Ok(()) } /// How far that edge may travel, `(first, last)` timeline frame inclusive /// -- [`trim_room`](Project::trim_room)'s twin, and what a front-end /// drawing the box during a drag asks. `None` for an index that is not /// there. pub fn trim_sub_room( &self, lane: Lane, idx: usize, edge: Edge, fps: f64, ) -> Option<(u32, u32)> { let subs = self.sub_lane(lane); let s = *subs.get(idx)?; // How many timeline frames a stretch of the track is worth, which is // what turns its two walls -- its own start, and its last cue -- into // frames. A rate that is not a rate leaves the window where it is. let frames_of = |us: i64| match fps.is_finite() && fps > 0.0 { true => ((us as f64) * fps / 1e6) .round() .max(0.0) .min(f64::from(u32::MAX)) as u32, false => 0, }; Some(match edge { Edge::Start => ( s.start .saturating_sub(frames_of(s.in_us)) .max(idx.checked_sub(1).map_or(0, |p| subs[p].end())), s.end() - 1, ), Edge::End => { // Out to the end of the track it reads, and never over the // subtitle behind it. A track the palette no longer has (or one // with no cues at all) may not grow, exactly as a source with no // entry in a media trim's table may not. let track_end = self .subtitles .get(s.track) .and_then(|t| t.cues.iter().map(|c| c.end_us).max()) .unwrap_or(s.out_us); // ...and never past [`grow_window`]'s ceiling, which is the // wall that matters here: a front-end asks this to draw the // drag, then trims to whatever it is told. An engine that // offers room its own trim would refuse is a drag that snaps // back at the end of the gesture -- so the room stops where // the window stops, and the two agree by construction. let wall_us = track_end.min(s.in_us.saturating_add(MAX_WINDOW_US)); ( s.start + 1, s.start .saturating_add(frames_of((wall_us - s.in_us).max(1))) .min(subs.get(idx + 1).map_or(u32::MAX, |n| n.start)), ) } }) } /// Take the subtitle at `idx` off `lane`, leaving a gap and moving nothing /// else -- [`lift`](Project::lift)'s twin, and one undo step like it. /// `false` for an index that is not there (which a lane that is not a /// subtitle lane always is). pub fn lift_sub(&mut self, lane: Lane, idx: usize) -> bool { let Some(i) = self.index(lane).filter(|_| idx < self.sub_lane(lane).len()) else { return false; }; self.snapshot(); self.lanes[i].subs.remove(idx); true } /// Retypes one cue's words: cue `cue` of subtitle track `track`. The words /// belong to the **track**, not to one placement, so every placement of that /// track shows the new text -- a subtitle file's line is one line however /// many times the film plays it, and that is why this is a palette edit and /// not a placement one. /// /// `false`, changing nothing and taking no undo step, for a row or a cue /// that is not there, for a picture cue (a display set has no words), and /// for text the cue already has. Empty text is not a blank cue: it deletes /// the cue ([`delete_cue`](Project::delete_cue)), which is the one way a cue /// leaves a track. The edit is kept in [`cue_edits`](Project::cue_edits), /// which a save writes and a reopen puts back over the file's own words /// ([`with_cue_edits`](Project::with_cue_edits)). pub fn set_cue_text(&mut self, track: usize, cue: usize, text: &str) -> bool { if text.is_empty() { return self.delete_cue(track, cue); } let Some(c) = self.subtitles.get(track).and_then(|t| t.cues.get(cue)) else { return false; }; if c.image.is_some() || c.text == text { return false; } let added = self.added_index(track, cue); let orig = if added.is_none() { self.original_cue_index(track, cue) } else { 0 }; self.snapshot(); self.subtitles[track].cues[cue].text = text.to_string(); match added { Some(i) => self.cue_adds[i].text = text.to_string(), None => self.record_cue_edit(track, orig, text), } true } /// Removes one cue from subtitle track `track`, as [`set_cue_text`](Project::set_cue_text) /// names it. The track is the palette every placement of it shows, so the /// cue is gone from each of them; no placement moves, since a cue's place is /// its own time in the file, and the cues after it keep theirs. One undo step. /// /// `false` for a track or cue that is not there. A picture cue can go too. /// The removal is kept in [`cue_edits`](Project::cue_edits) as an empty text /// on the cue's file index, so a reopen takes the same cue out again. pub fn delete_cue(&mut self, track: usize, cue: usize) -> bool { if self.subtitles.get(track).is_none_or(|t| cue >= t.cues.len()) { return false; } self.snapshot(); self.drop_cue(track, cue); true } /// Adds a cue to subtitle track `track`, on the track's own clock: `start_us` /// to `end_us` in microseconds, with `text` (`\n` between lines). It is kept /// in the track's cue list at its start, shows in every placement of the /// track, and is written by a save as a [`AddedCue`]. Returns its index in /// the track. /// /// Refused, changing nothing and taking no undo step, for a track that is /// not there, empty text, an end that is not after the start, a picture /// track, and a cue that overlaps one already there -- unless `overwrite`, /// which removes every overlapped cue first. Both the add and those removals /// are one undo step. pub fn add_cue(&mut self, track: usize, start_us: i64, end_us: i64, text: &str, overwrite: bool) -> crate::Result<usize> { let Some(t) = self.subtitles.get(track) else { return Err(format!("there is no subtitle track {track}").into()); }; if text.is_empty() { return Err("a cue needs words: empty text adds no cue".into()); } if end_us <= start_us { return Err(format!("a cue must end after it starts ({start_us} µs to {end_us} µs)").into()); } if t.cues.iter().any(|c| c.image.is_some()) { return Err(format!("track {track} is pictures, not text: a text cue cannot be added to it").into()); } let clash: Vec<usize> = t .cues .iter() .enumerate() .filter(|(_, c)| c.start_us < end_us && start_us < c.end_us) .map(|(i, _)| i) .collect(); if let (false, Some(&first)) = (overwrite, clash.first()) { return Err(format!("track {track} cue {first} overlaps the new cue: pass overwrite to replace it").into()); } self.snapshot(); // Highest first, so each position still names the cue it named. for &i in clash.iter().rev() { self.drop_cue(track, i); } let at = self.subtitles[track].cues.partition_point(|c| c.start_us <= start_us); self.subtitles[track].cues.insert( at, crate::subtitle::Cue { start_us, end_us, text: text.to_string(), image: None, words: Vec::new(), karaoke: Vec::new() }, ); self.cue_adds.push(AddedCue { track, start_us, end_us, text: text.to_string() }); Ok(at) } /// Divides every text cue of track `track` that straddles `t_us` (on the /// track's own clock) into two cues at `t_us`, one undo step. Each cue's /// words are shared out at the word boundary nearest the proportional point /// ([`split_words`]), so a window edge that falls inside a sentence leaves /// each side only the words it covers, and a piece with no words is dropped. /// /// `false`, changing nothing and taking no undo step, when no cue straddles /// `t_us`. A picture cue is never split. pub fn split_cues_at(&mut self, track: usize, t_us: i64) -> bool { let straddles = self .subtitles .get(track) .is_some_and(|t| { t.cues .iter() .any(|c| c.image.is_none() && c.start_us < t_us && t_us < c.end_us && split_words(c, t_us).is_some()) }); if !straddles { return false; } self.snapshot(); self.split_cue_raw(track, t_us, false); true } /// The room cue `cue` of track `track` has to move or trim in, on the track's /// own clock: from the end of the cue before it (or 0) to the start of the cue /// after it (or the end of time). `None` for a cue that is not there. pub fn cue_room(&self, track: usize, cue: usize) -> Option<(i64, i64)> { let t = self.subtitles.get(track)?; let c = t.cues.get(cue)?; let lo = cue.checked_sub(1).map_or(0, |p| t.cues[p].end_us.min(c.start_us)); let hi = t.cues.get(cue + 1).map_or(i64::MAX, |n| n.start_us.max(c.end_us)); Some((lo, hi)) } /// Where timeline time `tl_us` falls on the track's own clock, through the /// placement on `lane` that shows cue `cue` of `track` (extrapolated when it is /// outside the cue's shown part). `None` when no placement on `lane` shows it. pub fn cue_track_us_at(&self, lane: Lane, track: usize, cue: usize, tl_us: i64, fps: f64) -> Option<i64> { let s = self.sub_lane_cue_spans(lane, fps).into_iter().find(|s| s.track == track && s.cue == cue)?; let (shown, window) = ((s.end_us - s.start_us).max(1) as f64, (s.window.1 - s.window.0) as f64); Some(s.window.0 + ((tl_us - s.start_us) as f64 * window / shown).round() as i64) } /// Gives cue `cue` of `track` new times and words, kept the way a cue add keeps /// them: an added cue's entry is updated, a file cue is removed as /// [`drop_cue`](Project::drop_cue) does and re-added with its new times. The cue /// keeps its place in the track. No undo step of its own. fn rewrite_cue_raw(&mut self, track: usize, cue: usize, start_us: i64, end_us: i64, text: &str) { match self.added_index(track, cue) { Some(i) => { let a = &mut self.cue_adds[i]; (a.start_us, a.end_us, a.text) = (start_us, end_us, text.to_string()); } None => { let orig = self.original_cue_index(track, cue); self.cue_edits.retain(|(t, c, _)| !(*t == track && *c == orig)); self.cue_edits.push((track, orig, String::new())); self.cue_adds.push(AddedCue { track, start_us, end_us, text: text.to_string() }); } } let c = &mut self.subtitles[track].cues[cue]; (c.start_us, c.end_us, c.text) = (start_us, end_us, text.to_string()); } /// Moves or trims one cue: new `start_us` and `end_us` on the track's own clock, /// kept inside its neighbours ([`cue_room`](Project::cue_room)) and at least /// [`MIN_CUE_US`] long. One undo step. `Ok(false)`, with none, when the times are /// the cue's own; `Err` for a cue that is not there, a picture cue, or times /// that break those rules. pub fn set_cue_times(&mut self, track: usize, cue: usize, start_us: i64, end_us: i64) -> crate::Result<bool> { let Some((lo, hi)) = self.cue_room(track, cue) else { return Err(format!("track {track} has no cue {cue}").into()); }; let c = &self.subtitles[track].cues[cue]; if c.image.is_some() { return Err("a picture cue has no times to set".into()); } if (c.start_us, c.end_us) == (start_us, end_us) { return Ok(false); } if end_us - start_us < MIN_CUE_US { return Err(format!("a cue is at least {} ms long", MIN_CUE_US / 1000).into()); } if start_us < lo || end_us > hi { return Err("the cue would run into its neighbour".into()); } let text = c.text.clone(); self.snapshot(); self.rewrite_cue_raw(track, cue, start_us, end_us, &text); Ok(true) } /// Joins cue `cue` of `track` with the one after it: one cue from the first's /// start to the second's end, the words one after the other (a space between), /// whatever lay between them now inside it. One undo step. `Err`, changing /// nothing, for a cue with none after it or a picture cue. pub fn merge_cue_next(&mut self, track: usize, cue: usize) -> crate::Result<()> { let Some(t) = self.subtitles.get(track) else { return Err(format!("there is no subtitle track {track}").into()); }; let (Some(a), Some(b)) = (t.cues.get(cue), t.cues.get(cue + 1)) else { return Err("there is no cue after it to merge with".into()); }; if a.image.is_some() || b.image.is_some() { return Err("a picture cue cannot be merged".into()); } let text = format!("{} {}", a.text.trim_end(), b.text.trim_start()); let (start_us, end_us) = (a.start_us, b.end_us.max(a.end_us)); let (words, karaoke) = (b.words.clone(), b.karaoke.clone()); self.snapshot(); self.drop_cue(track, cue + 1); self.rewrite_cue_raw(track, cue, start_us, end_us, &text); let c = &mut self.subtitles[track].cues[cue]; c.words.extend(words); c.karaoke.extend(karaoke); Ok(()) } /// Splits the cues of every subtitle lane in `on` at the window edges a cut /// at each of `frames` would make: a placement that strictly straddles a /// frame gets its window's point there ([`SubClip::window_at`]), and each /// cue crossing that point is divided. No undo step of its own; the caller /// has taken its snapshot. Called before the lanes move, so the windows are /// the ones the cut will split. /// /// `both_sides` keeps a one-word cue on both sides of a point /// ([`cut_pieces`]), for a cut: which side the cut leaves in a window is /// only known after the ripple, so the caller drops the other one with /// [`drop_unplaced`](Project::drop_unplaced). A split (`false`) keeps the /// word on one side only. Returns the pieces made, `(track, start_us, end_us)`. fn split_cues_across(&mut self, on: &[usize], frames: &[u32], both_sides: bool) -> Vec<(usize, i64, i64)> { let mut points: Vec<(usize, i64)> = Vec::new(); // A placement that both edges of a cut fall inside: its track and the two window points. let mut spans: Vec<(usize, i64, i64)> = Vec::new(); for &i in on { for s in &self.lanes[i].subs { let inside: Vec<i64> = frames .iter() .filter(|&&f| s.start < f && f < s.end()) .map(|&f| s.window_at(f)) .collect(); points.extend(inside.iter().map(|&t| (s.track, t))); if let [a, b] = inside[..] { spans.push((s.track, a, b)); } } } let mut pieces = Vec::new(); for (track, a, b) in spans { pieces.extend(self.keep_one_word_outside(track, a, b)); } for (track, t) in points { pieces.extend(self.split_cue_raw(track, t, both_sides)); } pieces } /// A cut from `a` to `b` through the middle of a one-word cue: the word is /// kept on whichever side the cut leaves longer (ties: the side before), and /// the removed middle and the other side are dropped, as [`split_words`] /// does for a single edge. A cue with more words is left for /// [`split_cue_raw`](Project::split_cue_raw) to divide at each edge. fn keep_one_word_outside(&mut self, track: usize, a: i64, b: i64) -> Vec<(usize, i64, i64)> { let Some(t) = self.subtitles.get(track) else { return Vec::new(); }; let crossing: Vec<usize> = t .cues .iter() .enumerate() .filter(|(_, c)| { c.image.is_none() && c.start_us < a && b < c.end_us && c.text.split_whitespace().count() == 1 }) .map(|(i, _)| i) .collect(); let mut pieces = Vec::new(); for &i in crossing.iter().rev() { let cue = self.subtitles[track].cues[i].clone(); let text = cue.text.trim().to_string(); let (start_us, end_us) = if a - cue.start_us >= cue.end_us - b { (cue.start_us, a) } else { (b, cue.end_us) }; self.drop_cue(track, i); self.add_piece(track, start_us, end_us, text); pieces.push((track, start_us, end_us)); } pieces } /// Puts one piece of a divided cue into `track` at its start time, and keeps /// it the way a cue add keeps it (an [`AddedCue`]), so a save and a reopen /// give back the same cues. fn add_piece(&mut self, track: usize, start_us: i64, end_us: i64, text: String) { let at = self.subtitles[track].cues.partition_point(|c| c.start_us <= start_us); self.subtitles[track] .cues .insert(at, crate::subtitle::Cue { start_us, end_us, text: text.clone(), image: None, words: Vec::new(), karaoke: Vec::new() }); self.cue_adds.push(AddedCue { track, start_us, end_us, text }); } /// [`split_cues_at`](Project::split_cues_at) with no snapshot and no check: /// each cue of `track` straddling `t_us` is replaced by its pieces, cut by /// [`cut_pieces`] with `both_sides`. The pieces are kept the way a cue add /// keeps them -- the file's cue is removed as [`drop_cue`](Project::drop_cue) /// does and each piece is an [`AddedCue`] -- so a save and a reopen give back /// the same cues. Returns the pieces made, `(track, start_us, end_us)`. fn split_cue_raw(&mut self, track: usize, t_us: i64, both_sides: bool) -> Vec<(usize, i64, i64)> { let Some(t) = self.subtitles.get(track) else { return Vec::new(); }; let crossing: Vec<usize> = t .cues .iter() .enumerate() .filter(|(_, c)| c.image.is_none() && c.start_us < t_us && t_us < c.end_us) .map(|(i, _)| i) .collect(); let mut made = Vec::new(); // Highest first, so each index still names the cue it named. for &i in crossing.iter().rev() { let cue = self.subtitles[track].cues[i].clone(); let Some(pieces) = cut_pieces(&cue, t_us, both_sides) else { continue; }; self.drop_cue(track, i); for (start_us, end_us, text) in pieces { self.add_piece(track, start_us, end_us, text); made.push((track, start_us, end_us)); } } made } /// The tracks a cut over the lanes `on` can change the windows of: the /// tracks their placements show, each once. fn caption_tracks_on(&self, on: &[usize]) -> Vec<usize> { let mut tracks: Vec<usize> = on.iter().flat_map(|&i| self.lanes[i].subs.iter().map(|s| s.track)).collect(); tracks.sort_unstable(); tracks.dedup(); tracks } /// Every placement window of every lane, as `(track, in_us, out_us)`. fn windows(&self) -> Vec<(usize, i64, i64)> { self.lanes .iter() .flat_map(|l| l.subs.iter()) .map(|s| (s.track, s.in_us, s.out_us)) .collect() } /// The cues of `tracks` that some placement window covers now (overlaps /// it), as `(track, start_us, end_us)`. fn placed_cues(&self, tracks: &[usize]) -> Vec<(usize, i64, i64)> { let windows = self.windows(); let mut out = Vec::new(); for &t in tracks { let Some(track) = self.subtitles.get(t) else { continue; }; for c in &track.cues { if windows.iter().any(|&(w, a, b)| w == t && c.start_us < b && a < c.end_us) { out.push((t, c.start_us, c.end_us)); } } } out } /// After a cut, drops the track cues it left outside every placement window /// of their track. A cue is dropped only if it is one the cut touched: /// `placed` is the cues a window covered before the cut, and `pieces` the /// ones the cut made ([`split_cues_across`](Project::split_cues_across)). A /// cue no placement ever covered (a caption placed over a part of its track, /// say) is not the cut's to remove, so it stays for a later placement. /// /// The cut leaves the words it removed, or the side of a divided cue it /// cut away, outside every window -- those are the orphans this drops. The /// piece a window still covers stays, with its words. fn drop_unplaced(&mut self, tracks: &[usize], placed: &[(usize, i64, i64)], pieces: &[(usize, i64, i64)]) { let windows = self.windows(); for &t in tracks { let Some(mut i) = self.subtitles.get(t).map(|tr| tr.cues.len()) else { continue; }; // Last first, so dropping one leaves the indices before it true. while i > 0 { i -= 1; let Some(c) = self.subtitles.get(t).and_then(|tr| tr.cues.get(i)) else { continue; }; let key = (t, c.start_us, c.end_us); let touched = placed.contains(&key) || pieces.contains(&key); let covered = windows.iter().any(|&(w, a, b)| w == t && c.start_us < b && a < c.end_us); if touched && !covered { self.drop_cue(t, i); } } } } /// Takes cue `cue` out of track `track` with no undo step of its own. An /// added cue leaves the added list; a file cue's removal is kept as an /// empty override on its file index. fn drop_cue(&mut self, track: usize, cue: usize) { match self.added_index(track, cue) { Some(i) => { self.cue_adds.remove(i); } None => { let orig = self.original_cue_index(track, cue); self.cue_edits.retain(|(t, c, _)| !(*t == track && *c == orig)); self.cue_edits.push((track, orig, String::new())); } } self.subtitles[track].cues.remove(cue); } /// Which entry of [`cue_adds`](Project::cue_adds) cue `cue` of track `track` /// is, if it was added rather than read from the file. An added cue is found /// by its times: no file cue overlaps one, since adding it refused that. fn added_index(&self, track: usize, cue: usize) -> Option<usize> { let c = self.subtitles.get(track)?.cues.get(cue)?; self.cue_adds .iter() .position(|a| a.track == track && a.start_us == c.start_us && a.end_us == c.end_us) } /// The file's index of cue `cue` of track `track`, which is not an added /// one. The cue overrides name cues by where the file has them, so a cue /// after a removed one keeps its index there while its position in the /// track moves down: this counts the file's cues before `cue` in the track /// (added ones have no file index), then walks the file past the removed /// ones to that survivor. fn original_cue_index(&self, track: usize, cue: usize) -> usize { let removed: Vec<usize> = self .cue_edits .iter() .filter(|(t, _, text)| *t == track && text.is_empty()) .map(|(_, c, _)| *c) .collect(); let before = self.subtitles[track] .cues .iter() .take(cue) .filter(|c| !self.cue_adds.iter().any(|a| a.track == track && a.start_us == c.start_us && a.end_us == c.end_us)) .count(); let (mut orig, mut seen) = (0usize, 0usize); loop { if !removed.contains(&orig) { if seen == before { return orig; } seen += 1; } orig += 1; } } /// Keeps one override in the save list, replacing an earlier one for the /// same cue so a cue is written once however often it was retyped. `cue` is /// the file's index ([`original_cue_index`](Project::original_cue_index)). fn record_cue_edit(&mut self, track: usize, cue: usize, text: &str) { match self .cue_edits .iter_mut() .find(|(t, c, _)| *t == track && *c == cue) { Some(edit) => edit.2 = text.to_string(), None => self.cue_edits.push((track, cue, text.to_string())), } } /// The cue overrides a save writes, `(track, cue, text)` each, the cue by /// its index in the file it came from; empty text is a cue removed -- see /// [`set_cue_text`](Project::set_cue_text) and [`delete_cue`](Project::delete_cue). pub fn cue_edits(&self) -> &[(usize, usize, String)] { &self.cue_edits } /// The cues added to tracks ([`AddedCue`]), in the order they were added: what /// a save writes after [`cue_edits`](Project::cue_edits). pub fn cue_adds(&self) -> &[AddedCue] { &self.cue_adds } /// Puts saved added cues back into their tracks, after the file's cues and /// the overrides ([`with_cue_edits`](Project::with_cue_edits)), each at its /// start. An added cue naming a track the file no longer has is kept, so a /// re-save does not lose it, and is not shown. pub fn with_cue_adds(mut self, adds: &[AddedCue]) -> Self { for a in adds { if let Some(t) = self.subtitles.get_mut(a.track) { let at = t.cues.partition_point(|c| c.start_us <= a.start_us); t.cues.insert( at, crate::subtitle::Cue { start_us: a.start_us, end_us: a.end_us, text: a.text.clone(), image: None, words: Vec::new(), karaoke: Vec::new() }, ); } self.cue_adds.push(a.clone()); } self } /// Puts saved cue overrides back over the tracks a load read -- the door a /// load uses, like [`with_subtitles`](Project::with_subtitles), and no undo /// step for the same reason. Retyped words go on first, then the removed /// cues come out, last index first, so each still names the file's cue. An /// override naming a track or cue the file no longer has is kept, so a /// re-save does not lose it, and changes nothing. pub fn with_cue_edits(mut self, edits: &[(usize, usize, String)]) -> Self { let mut removed: Vec<(usize, usize)> = Vec::new(); for (track, cue, text) in edits { if text.is_empty() { removed.push((*track, *cue)); } else if let Some(c) = self .subtitles .get_mut(*track) .and_then(|t| t.cues.get_mut(*cue)) .filter(|c| c.image.is_none()) { c.text = text.clone(); } self.record_cue_edit(*track, *cue, text); } removed.sort_unstable_by(|a, b| b.cmp(a)); removed.dedup(); for (track, cue) in removed { if let Some(t) = self.subtitles.get_mut(track).filter(|t| cue < t.cues.len()) { t.cues.remove(cue); } } self } /// What `lane` shows, as cues on the **timeline's** clock: every placement's /// window of its track, clipped to that window and shifted to where the /// placement sits. The map [`crate::export::timeline_cues`] is for a track /// carried through the picture's spans -- this is its twin for a track that /// is placed on a lane of its own, and it is what a preview draws and what /// an export of the lane would write. /// /// Cut, not stretched: a cue half inside the window keeps the half that is /// inside, and a cue wholly outside it is gone -- the same rule the media /// spans follow. `fps` is the timeline's rate, needed here because a /// placement's *position* is in frames while its words are in microseconds. /// /// Pure and cheap enough to ask per repaint (a walk of the placements and /// of their cues, no file opened), which is how a front-end asks it. pub fn sub_lane_cues(&self, lane: Lane, fps: f64) -> Vec<SubtitleCue> { self.sub_lane_cue_spans(lane, fps) .into_iter() .filter_map(|s| { let cue = self.subtitles.get(s.track)?.cues.get(s.cue)?; Some(SubtitleCue { start_us: s.start_us, end_us: s.end_us, text: cue.text.clone(), image: cue.image.clone(), words: crate::subtitle::map_words(&cue.words, s.window, (s.start_us, s.end_us)), karaoke: crate::subtitle::map_karaoke(&cue.karaoke, s.window, (s.start_us, s.end_us)), }) }) .collect() } /// [`sub_lane_cues`](Project::sub_lane_cues) with the cue each entry is: the /// same entries in the same order, as `(start_us, end_us, track, cue)`, so a /// front-end can hit-test a drawn cue and name the words it would edit /// ([`set_cue_text`](Project::set_cue_text)). Built by the same walk, so the /// two cannot disagree about which cue is where. pub fn sub_lane_cue_refs(&self, lane: Lane, fps: f64) -> Vec<(i64, i64, usize, usize)> { self.sub_lane_cue_spans(lane, fps) .into_iter() .map(|s| (s.start_us, s.end_us, s.track, s.cue)) .collect() } /// [`sub_lane_cue_refs`](Project::sub_lane_cue_refs) with the stretch of the cue's own clock /// each entry shows, which is what its words are moved by ([`crate::subtitle::map_words`]). pub fn sub_lane_cue_spans(&self, lane: Lane, fps: f64) -> Vec<CueSpan> { // A hidden lane has no cues at all: the screen draws none and the // export writes no track for it (its empty cue list is what drops it). if !(fps.is_finite() && fps > 0.0) || self.lane_hidden(lane) { return Vec::new(); } let mut out = Vec::new(); for s in self.sub_lane(lane) { let Some(track) = self.subtitles.get(s.track) else { // A placement whose track was taken off the palette // ([`Project::remove_subtitles`]) shows nothing rather than // showing another track's words. continue; }; // Where the placement's own first microsecond lands on the // 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 (i, cue) in track.cues.iter().enumerate() { let (a, b) = (cue.start_us.max(s.in_us), cue.end_us.min(s.out_us)); if b <= a { continue; // wholly outside the window this placement keeps } let (from, to) = (onto(a), onto(b)); // Frames the cue keeps on the timeline (the +1 is the microsecond // rounding gives back). let kept = ((to - from) as f64 + 1.0) * fps / 1e6; // Shorter than one timeline frame: a timeline cannot show it. A // cue a window edge shortened (a cut met it, or a trim) is also // a sliver under [`CUT_SLIVER_FRAMES`]: the words a cut leaves // at the very edge of a cue flash past too quickly to read, so // they are not a cue. A cue short in the file itself keeps the // one-frame floor, as it always had. let clipped = a != cue.start_us || b != cue.end_us; if kept < 1.0 || (clipped && kept < CUT_SLIVER_FRAMES) { continue; } out.push(CueSpan { start_us: from, end_us: to, track: s.track, cue: i, window: (a, b) }); } } // The placements come in timeline order, but two lanes' worth (or a // track whose cues a window reordered) do not. out.sort_by_key(|c| c.start_us); out } /// What each of [`audio_segments_from`](Project::audio_segments_from)'s /// lanes is multiplied by on its way into the mix: the same lanes in the /// same order, as *amplitudes*, so the mixer never sees a decibel. `1.0` /// for a lane nobody has turned, which is a multiply f32 leaves alone -- /// the bit-exact path. /// /// A muted lane is `0.0`: silence in the same sum, so the mix, the single /// stream test ([`AudioSession::is_mixed`]) and the export all see the one /// answer. Its clips and its volume are left as they are. pub fn audio_gains(&self) -> Vec<f32> { self.audio_lanes() .into_iter() .map(|lane| match self.lane_muted(lane) { true => 0.0, false => db_to_linear(self.lane_gain_db(lane)), }) .collect() } /// The ducks, in the same order and as long as /// [`audio_gains`](Project::audio_gains): what the mixer reads beside the /// gains. A duck names its key by the key's [`Lane::ord`], which is stable /// while lanes are emptied and refilled; here it is rewritten to the key's /// position in the mix, and dropped when that key is not in the mix at all /// (an empty audio lane is not one), which leaves the lane unducked rather /// than ducked under silence it could not hear. pub fn audio_ducks(&self) -> Vec<Vec<Duck>> { let mixed = self.audio_lanes(); mixed .iter() .map(|&lane| { self.lane_duck_list(lane) .into_iter() .filter_map(|duck| { let key = Lane::new(LaneKind::Audio, duck.under); let at = mixed.iter().position(|&l| l == key)?; Some(Duck { under: at, ..duck }) }) .collect() }) .collect() } /// The voice processors, in the same order and as long as /// [`audio_gains`](Project::audio_gains): what the mix opens each lane /// through. All off for a project nobody has cleaned, which is the path /// that decodes the same samples it always did. pub fn audio_voices(&self) -> Vec<VoiceFx> { self.audio_lanes() .into_iter() .map(|lane| self.lane_voice(lane)) .collect() } /// The master limiter the mix is summed through. A project setting, not a /// clip's and not a lane's: there is one mix. pub fn limiter(&self) -> Limiter { self.settings.limiter } /// Sets it, with the ceiling clamped to what [`Limiter`] allows. `false` /// for a setting already in force. One undo step like every other /// project-setting pick. pub fn set_limiter(&mut self, limiter: Limiter) -> bool { let limiter = limiter.with_ceiling(limiter.ceiling_db); if self.settings.limiter == limiter { return false; } self.snapshot(); self.settings.limiter = limiter; true } /// What the clip at `idx` of `lane` plays through, or `None` for one that /// plays flat (and for an index that is not there) -- what a feeder and an /// export ask before they build an [`crate::eq::EqState`]. pub fn eq_of(&self, lane: Lane, idx: usize) -> Option<&EqParams> { self.eq.get(usize::from(self.lane(lane).get(idx)?.eq?)) } /// Give the clip at `idx` of `lane` these equalizer settings, or `None` to /// take them off. One undo step, like every other edit. `false` -- and no /// history -- for an index that is not there and for params holding a value /// that is not finite, which is the one thing the file format could not /// write and read back. /// /// Settings equal to ones already in the table share their entry, so a /// project that puts the same curve on twenty clips writes it once. pub fn set_eq(&mut self, lane: Lane, idx: usize, params: Option<EqParams>) -> bool { if idx >= self.lane(lane).len() { return false; } let slot = match params { None => None, Some(params) => { if !finite(¶ms) { return false; } Some(match self.eq.iter().position(|p| *p == params) { Some(at) => at as u16, // corner-cut: the table is append-only within a session (see // the module docs) and `Clip::eq` is a u16, so the 65535th // *distinct* setting of one session is refused rather than // silently aliased. Upgrade path if a UI ever drags a slider // that far: widen `Clip::eq` to u32 (Clip has the room) or // snapshot the table into the history and compact it here. None if self.eq.len() >= usize::from(u16::MAX) => return false, None => { self.eq.push(params); (self.eq.len() - 1) as u16 } }) } }; self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].eq = slot; true } /// The clip's own gain in dB ([`Clip::gain_db`]) at `idx` of `lane`, or /// `None` for an index that is not there. `0.0` is unity. pub fn clip_gain(&self, lane: Lane, idx: usize) -> Option<f32> { self.lane(lane).get(idx).map(|c| c.gain_db) } /// Give the clip at `idx` of `lane` this gain in dB, clamped to /// [`MIN_CLIP_GAIN_DB`]..=[`MAX_CLIP_GAIN_DB`]. One undo step, like every /// other edit. `false` -- and no history -- for an index that is not there, /// a value that is not finite, a video lane (a picture has no volume of its /// own to set: the gain is on the audio clip the take was split into, not /// relayed to it), or a value already set exactly so. pub fn set_clip_gain(&mut self, lane: Lane, idx: usize, db: f32) -> bool { if lane.kind != LaneKind::Audio || !db.is_finite() { return false; } let Some(current) = self.lane(lane).get(idx).map(|c| c.gain_db) else { return false; }; let db = db.clamp(MIN_CLIP_GAIN_DB, MAX_CLIP_GAIN_DB); if current == db { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].gain_db = db; true } /// The clip's pan ([`Clip::pan`]) at `idx` of `lane`, or `None` for an index that is not there. pub fn clip_pan(&self, lane: Lane, idx: usize) -> Option<f32> { self.lane(lane).get(idx).map(Clip::pan_value) } /// Give the clip at `idx` of `lane` this pan, clamped to `-1.0..=1.0`. One undo step. /// `false` -- and no history -- for an index that is not there, a value that is not /// finite, a non-audio lane, or a value already set. pub fn set_clip_pan(&mut self, lane: Lane, idx: usize, pan: f32) -> bool { if lane.kind != LaneKind::Audio || !pan.is_finite() { return false; } let Some(current) = self.lane(lane).get(idx).map(|c| c.pan) else { return false; }; let pan = (pan.clamp(-1.0, 1.0) * 100.0).round() as i8; if current == pan { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].pan = pan; true } /// Give the clip at `idx` of `lane` this pitch in semitones, clamped to `-12..=12`. /// One undo step. `false` -- and no history -- for an index that is not there, a /// non-audio lane, or a value already set. pub fn set_clip_pitch(&mut self, lane: Lane, idx: usize, semitones: i32) -> bool { if lane.kind != LaneKind::Audio { return false; } let Some(current) = self.lane(lane).get(idx).map(|c| c.pitch) else { return false; }; let semitones = semitones.clamp(i32::from(MIN_CLIP_PITCH), i32::from(MAX_CLIP_PITCH)) as i8; if current == semitones { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].pitch = semitones; true } /// The pitch in semitones of the clip at `idx` of `lane`, or `None` for an index that /// is not there. pub fn clip_pitch(&self, lane: Lane, idx: usize) -> Option<i8> { self.lane(lane).get(idx).map(|c| c.pitch) } /// The placement animation of the clip at `idx` of `lane`, sorted by source /// frame ([`Clip::transform_keys`]). Empty for no keys and for an index /// that is not there. pub fn transform_keys_of(&self, lane: Lane, idx: usize) -> &[TransformKey] { self.lane(lane) .get(idx) .map_or(&[][..], |c| c.transform_keys.as_slice()) } /// Replace the whole placement animation of the clip at `idx` of `lane`; an /// empty list clears it back to [`Clip::transform`]. Sorted here, one undo /// step. `false` -- and no history -- for an audio lane, an index that is not /// there, more than [`MAX_TRANSFORM_KEYS`] keys, a key whose placement is not /// finite, two keys at one source frame, or a list already exactly so. An /// opacity is clamped to `0..=1`, as [`TransformParams::opacity`] requires. pub fn set_transform_keys( &mut self, lane: Lane, idx: usize, mut keys: Vec<TransformKey>, ) -> bool { if lane.kind != LaneKind::Video || keys.len() > MAX_TRANSFORM_KEYS { return false; } if keys.iter().any(|k| !transform_finite(&k.params)) { return false; } keys.sort_by_key(|k| k.at); if keys.windows(2).any(|w| w[0].at == w[1].at) { return false; } for k in &mut keys { k.params.opacity = k.params.opacity.clamp(0.0, 1.0); } let Some(current) = self.lane(lane).get(idx).map(|c| &c.transform_keys) else { return false; }; if *current == keys { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].transform_keys = keys; true } /// The opacity animation of the clip at `idx` of `lane`, sorted by source /// frame ([`Clip::opacity_keys`]). Empty for no keys and for an index that /// is not there. pub fn opacity_keys_of(&self, lane: Lane, idx: usize) -> &[OpacityKey] { self.lane(lane) .get(idx) .map_or(&[][..], |c| c.opacity_keys.as_slice()) } /// Replace the whole opacity animation of the clip at `idx` of `lane`; an /// empty list clears it. Sorted here, one undo step; opacities are clamped to /// `0..=1`. `false` -- and no history -- for an audio lane, an index that is /// not there, more than [`MAX_TRANSFORM_KEYS`] keys, a non-finite opacity, two /// keys at one source frame, or a list already exactly so. pub fn set_opacity_keys(&mut self, lane: Lane, idx: usize, mut keys: Vec<OpacityKey>) -> bool { if lane.kind != LaneKind::Video || keys.len() > MAX_TRANSFORM_KEYS { return false; } if keys.iter().any(|k| !k.opacity.is_finite()) { return false; } keys.sort_by_key(|k| k.at); if keys.windows(2).any(|w| w[0].at == w[1].at) { return false; } for k in &mut keys { k.opacity = k.opacity.clamp(0.0, 1.0); } let Some(current) = self.lane(lane).get(idx).map(|c| &c.opacity_keys) else { return false; }; if *current == keys { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].opacity_keys = keys; true } /// The colour animation of the clip at `idx` of `lane`, sorted by source /// frame ([`Clip::color_keys`]). Empty for no keys and for an index that is /// not there. pub fn color_keys_of(&self, lane: Lane, idx: usize) -> &[ColorKey] { self.lane(lane) .get(idx) .map_or(&[][..], |c| c.color_keys.as_slice()) } /// The effects stack of the clip at `idx` of `lane` ([`Clip::effects`]). /// Empty for none and for an index that is not there. pub fn effects_of(&self, lane: Lane, idx: usize) -> &[crate::effects::Effect] { self.lane(lane).get(idx).map_or(&[], |c| &c.effects) } /// Replaces the effects stack of the clip at `idx` of `lane` (an empty list /// clears it), each effect's values clamped into range. One undo step; `false` /// for a lane that is not a video lane, a clip that is not there, more than /// [`crate::effects::MAX_EFFECTS`] effects, and a stack equal to the current one. pub fn set_effects(&mut self, lane: Lane, idx: usize, effects: Vec<crate::effects::Effect>) -> bool { if lane.kind != LaneKind::Video || effects.len() > crate::effects::MAX_EFFECTS { return false; } let effects: Vec<_> = effects.into_iter().map(crate::effects::Effect::clamped).collect(); let Some(current) = self.lane(lane).get(idx).map(|c| &c.effects) else { return false; }; if *current == effects { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].effects = effects; true } /// The keys of the effect at stack position `slot` of the clip at `idx` of `lane` /// ([`crate::effects::Effect::keys`]); empty for an effect with none. pub fn effect_keys_of(&self, lane: Lane, idx: usize, slot: usize) -> &[crate::effects::EffectKey] { self.lane(lane).get(idx).and_then(|c| c.effects.get(slot)).map_or(&[], |e| &e.keys) } /// Replaces the keys of the effect at stack position `slot` of the clip at `idx` of /// `lane`; an empty list clears them. Keys are source-frame keyed and sorted here with /// no two alike. Each value is clamped into its parameter's range, a parameter that is /// not keyable (a switch or a choice) takes the effect's static value, and the unused /// tail is zeroed. Refused for a clip that is not a picture clip on a video lane, more /// than [`MAX_TRANSFORM_KEYS`] keys, a non-finite value, or a list equal to the current /// one. One undo step. pub fn set_effect_keys(&mut self, lane: Lane, idx: usize, slot: usize, mut keys: Vec<crate::effects::EffectKey>) -> bool { if lane.kind != LaneKind::Video || keys.len() > MAX_TRANSFORM_KEYS { return false; } keys.sort_by_key(|k| k.at); if keys.windows(2).any(|w| w[0].at == w[1].at) { return false; } let Some(current) = self.lane(lane).get(idx).and_then(|c| c.effects.get(slot)) else { return false; }; let specs = current.kind.params(); if keys.iter().any(|k| k.values[..specs.len()].iter().any(|v| !v.is_finite())) { return false; } for k in &mut keys { for (i, v) in k.values.iter_mut().enumerate() { *v = match specs.get(i) { Some(s) if s.keyable() => v.clamp(s.min, s.max), Some(_) => current.values[i], None => 0.0, }; } } if current.keys == keys { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].effects[slot].keys = keys; true } /// Replace the whole colour animation of the clip at `idx` of `lane`; an /// empty list clears it. Sorted here, one undo step; each field is clamped to /// its range (brightness and tint `-1..=1`, contrast and saturation `0..=2`). /// `false` -- and no history -- for an audio lane, an index that is not there, /// more than [`MAX_COLOR_KEYS`] keys, a non-finite value, two keys at one /// source frame, or a list already exactly so. pub fn set_color_keys(&mut self, lane: Lane, idx: usize, mut keys: Vec<ColorKey>) -> bool { if lane.kind != LaneKind::Video || keys.len() > MAX_COLOR_KEYS { return false; } if keys.iter().any(|k| !color_finite(&k.params)) { return false; } keys.sort_by_key(|k| k.at); if keys.windows(2).any(|w| w[0].at == w[1].at) { return false; } for k in &mut keys { let p = &mut k.params; p.brightness = p.brightness.clamp(-1.0, 1.0); p.contrast = p.contrast.clamp(0.0, 2.0); p.saturation = p.saturation.clamp(0.0, 2.0); p.tint = p.tint.clamp(-1.0, 1.0); } let Some(current) = self.lane(lane).get(idx).map(|c| &c.color_keys) else { return false; }; if *current == keys { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].color_keys = keys; true } /// The source frame the keys of the clip at `idx` of `lane` are read at for /// timeline `frame`, rounded to the nearest whole frame: where a key set "at /// the playhead" lands. `None` for an index that is not there, a frame /// outside the clip and a lane that is not a video lane. pub fn key_source_frame(&self, lane: Lane, idx: usize, timeline_frame: u32) -> Option<u32> { let c = self.lane(lane).get(idx)?; if lane.kind != LaneKind::Video || timeline_frame < c.start || timeline_frame >= c.end() { return None; } Some(key_pos(c, timeline_frame).round() as u32) } /// Whether the clip has any key of `kind` at all. pub fn is_keyed(&self, lane: Lane, idx: usize, kind: KeyKind) -> bool { self.lane(lane).get(idx).is_some_and(|c| match kind { KeyKind::Placement => !c.transform_keys.is_empty(), KeyKind::Opacity => !c.opacity_keys.is_empty(), KeyKind::Color => !c.color_keys.is_empty(), }) } /// The ease of the key of `kind` that sits exactly on timeline `frame` /// ([`key_source_frame`](Self::key_source_frame)), `None` with no key there. pub fn key_ease_at(&self, lane: Lane, idx: usize, kind: KeyKind, timeline_frame: u32) -> Option<KeyEase> { let at = self.key_source_frame(lane, idx, timeline_frame)?; let c = self.lane(lane).get(idx)?; match kind { KeyKind::Placement => c.transform_keys.iter().find(|k| k.at == at).map(|k| k.ease), KeyKind::Opacity => c.opacity_keys.iter().find(|k| k.at == at).map(|k| k.ease), KeyKind::Color => c.color_keys.iter().find(|k| k.at == at).map(|k| k.ease), } } /// The stopwatch: puts a key of `kind` on timeline `frame` holding the value the /// clip shows there (so the picture does not change), or takes the key that is /// there off. `Some(true)` for a key added, `Some(false)` for one removed, `None` /// -- and no history -- where no key can go (see /// [`key_source_frame`](Self::key_source_frame)) or the keys are full. One undo /// step. Removing the last key puts the clip back on its single value. pub fn toggle_key(&mut self, lane: Lane, idx: usize, kind: KeyKind, timeline_frame: u32) -> Option<bool> { let at = self.key_source_frame(lane, idx, timeline_frame)?; let had = self.key_ease_at(lane, idx, kind, timeline_frame).is_some(); let c = self.lane(lane).get(idx)?.clone(); let ok = match kind { KeyKind::Placement => { let mut keys = c.transform_keys.clone(); if had { keys.retain(|k| k.at != at); } else { let params = self.transform_at(lane, idx, timeline_frame).unwrap_or_default(); keys.push(TransformKey { at, params, ease: KeyEase::Linear }); } self.set_transform_keys(lane, idx, keys) } KeyKind::Opacity => { let mut keys = c.opacity_keys.clone(); if had { keys.retain(|k| k.at != at); } else { let opacity = self.opacity_at(lane, idx, timeline_frame); keys.push(OpacityKey { at, opacity, ease: KeyEase::Linear }); } self.set_opacity_keys(lane, idx, keys) } KeyKind::Color => { let mut keys = c.color_keys.clone(); if had { keys.retain(|k| k.at != at); } else { let params = self.color_at(lane, idx, timeline_frame).unwrap_or_default(); keys.push(ColorKey { at, params, ease: KeyEase::Linear }); } self.set_color_keys(lane, idx, keys) } }; ok.then_some(!had) } /// Sets the ease of the key of `kind` on timeline `frame`. `false` -- and no /// history -- with no key there or one already so. One undo step. pub fn set_key_ease_at(&mut self, lane: Lane, idx: usize, kind: KeyKind, timeline_frame: u32, ease: KeyEase) -> bool { let Some(at) = self.key_source_frame(lane, idx, timeline_frame) else { return false; }; if self.key_ease_at(lane, idx, kind, timeline_frame).is_none_or(|e| e == ease) { return false; } let Some(c) = self.lane(lane).get(idx) else { return false; }; match kind { KeyKind::Placement => { let mut keys = c.transform_keys.clone(); keys.iter_mut().filter(|k| k.at == at).for_each(|k| k.ease = ease); self.set_transform_keys(lane, idx, keys) } KeyKind::Opacity => { let mut keys = c.opacity_keys.clone(); keys.iter_mut().filter(|k| k.at == at).for_each(|k| k.ease = ease); self.set_opacity_keys(lane, idx, keys) } KeyKind::Color => { let mut keys = c.color_keys.clone(); keys.iter_mut().filter(|k| k.at == at).for_each(|k| k.ease = ease); self.set_color_keys(lane, idx, keys) } } } /// Writes `keys` as the clip's keys of one kind with the undo step only when /// `snapshot`: the shape of a drag, whose first sample is a step and the /// rest are not. The lists were validated by the caller. fn put_key<K: Keyed>( &mut self, lane: Lane, idx: usize, snapshot: bool, key: K, slot: impl Fn(&mut Clip) -> &mut Vec<K>, ) -> bool { if lane.kind != LaneKind::Video { return false; } let Some(clip) = self.lane_mut(lane).and_then(|c| c.get_mut(idx)) else { return false; }; let mut keys = slot(clip).clone(); match keys.iter_mut().find(|k| k.at() == key.at()) { Some(k) => *k = key.placed(key.at(), k.ease()), None => { keys.push(key); keys.sort_by_key(|k| k.at()); } } if keys.len() > MAX_TRANSFORM_KEYS { return false; } if snapshot { self.snapshot(); } *slot(&mut self.lane_mut(lane).expect("checked")[idx]) = keys; true } /// Sets the placement at timeline `frame` as a key there: a new key, or the /// existing key's value with its ease kept. The write a slider makes on a clip /// whose placement is keyed. `snapshot` is the undo step /// ([`set_transform_live`](Self::set_transform_live)'s rule). `false` where no /// key can go ([`key_source_frame`](Self::key_source_frame)) or a value is not finite. pub fn put_transform_key_at(&mut self, lane: Lane, idx: usize, timeline_frame: u32, params: TransformParams, snapshot: bool) -> bool { self.flat.clear(); let Some(at) = self.key_source_frame(lane, idx, timeline_frame) else { return false; }; if !transform_finite(¶ms) { return false; } let params = TransformParams { opacity: params.opacity.clamp(0.0, 1.0), ..params }; self.put_key(lane, idx, snapshot, TransformKey { at, params, ease: KeyEase::Linear }, |c| &mut c.transform_keys) } /// [`put_transform_key_at`](Self::put_transform_key_at) for the opacity. pub fn put_opacity_key_at(&mut self, lane: Lane, idx: usize, timeline_frame: u32, opacity: f32, snapshot: bool) -> bool { self.flat.clear(); let Some(at) = self.key_source_frame(lane, idx, timeline_frame) else { return false; }; if !opacity.is_finite() { return false; } let opacity = opacity.clamp(0.0, 1.0); self.put_key(lane, idx, snapshot, OpacityKey { at, opacity, ease: KeyEase::Linear }, |c| &mut c.opacity_keys) } /// [`put_transform_key_at`](Self::put_transform_key_at) for the grade. pub fn put_color_key_at(&mut self, lane: Lane, idx: usize, timeline_frame: u32, params: ColorParams, snapshot: bool) -> bool { self.flat.clear(); let Some(at) = self.key_source_frame(lane, idx, timeline_frame) else { return false; }; if !color_finite(¶ms) { return false; } let params = ColorParams { brightness: params.brightness.clamp(-1.0, 1.0), contrast: params.contrast.clamp(0.0, 2.0), saturation: params.saturation.clamp(0.0, 2.0), tint: params.tint.clamp(-1.0, 1.0), // Keys animate the sliders; the look is the clip's base grade's. look: crate::color::Look::default(), }; self.put_key(lane, idx, snapshot, ColorKey { at, params, ease: KeyEase::Linear }, |c| &mut c.color_keys) } /// The grade the clip at `idx` of `lane` shows at timeline `frame`: its /// colour keys evaluated at the source frame that frame plays, or its single /// grade when it has none. `None` for an index that is not there or a clip /// with no grade at all and no keys. pub fn color_at(&self, lane: Lane, idx: usize, timeline_frame: u32) -> Option<ColorParams> { let c = self.lane(lane).get(idx)?; if c.color_keys.is_empty() { return self.color_of(lane, idx).copied(); } Some(clip_color_at(c, self.color_of(lane, idx).copied().unwrap_or_default(), timeline_frame)) } /// The opacity the clip at `idx` of `lane` shows at timeline `frame`, the /// fades not counted: its opacity keys evaluated at the source frame that /// frame plays, else its placement's (keyed or not). `1.0` for an index /// that is not there. pub fn opacity_at(&self, lane: Lane, idx: usize, timeline_frame: u32) -> f32 { let Some(c) = self.lane(lane).get(idx) else { return 1.0; }; let base = self .transform_at(lane, idx, timeline_frame) .map_or(1.0, |t| t.opacity); clip_opacity_at(c, base, timeline_frame) } /// The placement the clip at `idx` of `lane` shows at timeline `frame`: its /// keys evaluated at the source frame that frame plays /// ([`keyed_transform`]), or its single [`Clip::transform`] when it has no /// keys. `None` for an index that is not there or a clip with no /// transform at all, which is the identity. pub fn transform_at(&self, lane: Lane, idx: usize, timeline_frame: u32) -> Option<TransformParams> { let c = self.lane(lane).get(idx)?; if c.transform_keys.is_empty() { return self.transform_of(lane, idx).copied(); } Some(clip_transform_at(c, self.transform_of(lane, idx).copied().unwrap_or_default(), timeline_frame)) } /// The volume envelope of the clip at `idx` of `lane`, sorted by source /// frame ([`Clip::gain_keys`]). Empty for a clip with no points, and for an /// index that is not there. pub fn gain_keys_of(&self, lane: Lane, idx: usize) -> &[GainKey] { self.lane(lane) .get(idx) .map_or(&[][..], |c| c.gain_keys.as_slice()) } /// Replace the whole volume envelope of the clip at `idx` of `lane`; an /// empty list clears it. The points are sorted here, and each dB is clamped /// to [`MIN_CLIP_GAIN_DB`]..=[`MAX_CLIP_GAIN_DB`]. One undo step. /// /// `false` -- and no history -- for a video lane, an index that is not /// there, more than [`MAX_GAIN_KEYS`] points, a point that is not finite, /// two points at one source frame, or an envelope already exactly so. pub fn set_gain_keys(&mut self, lane: Lane, idx: usize, mut keys: Vec<GainKey>) -> bool { if lane.kind != LaneKind::Audio || keys.len() > MAX_GAIN_KEYS { return false; } if keys.iter().any(|k| !k.db.is_finite()) { return false; } keys.sort_by_key(|k| k.at); if keys.windows(2).any(|w| w[0].at == w[1].at) { return false; } for k in &mut keys { k.db = k.db.clamp(MIN_CLIP_GAIN_DB, MAX_CLIP_GAIN_DB); } let Some(current) = self.lane(lane).get(idx).map(|c| &c.gain_keys) else { return false; }; if *current == keys { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].gain_keys = keys; true } /// Put one point into the volume envelope of the clip at `idx` of `lane`: /// it replaces the point already at `key.at`, or is added in source order. /// The dB is clamped as [`set_gain_keys`](Self::set_gain_keys) clamps it. /// One undo step. /// /// `false` -- and no history -- for a video lane, an index that is not /// there, a dB that is not finite, a new point past /// [`MAX_GAIN_KEYS`], or a point already exactly as it is. pub fn add_gain_key(&mut self, lane: Lane, idx: usize, key: GainKey) -> bool { if lane.kind != LaneKind::Audio || !key.db.is_finite() { return false; } let key = GainKey { db: key.db.clamp(MIN_CLIP_GAIN_DB, MAX_CLIP_GAIN_DB), ..key }; let Some(current) = self.lane(lane).get(idx).map(|c| &c.gain_keys) else { return false; }; let mut keys = current.clone(); match keys.binary_search_by_key(&key.at, |k| k.at) { Ok(at) => { if keys[at] == key { return false; } keys[at] = key; } Err(at) => { if keys.len() >= MAX_GAIN_KEYS { return false; } keys.insert(at, key); } } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].gain_keys = keys; true } /// Take the point at source frame `at` out of the volume envelope of the /// clip at `idx` of `lane`. One undo step. /// /// `false` -- and no history -- for a video lane, an index that is not /// there, or no point at `at`. pub fn remove_gain_key(&mut self, lane: Lane, idx: usize, at: u32) -> bool { if lane.kind != LaneKind::Audio { return false; } let Some(current) = self.lane(lane).get(idx).map(|c| &c.gain_keys) else { return false; }; let Ok(pos) = current.binary_search_by_key(&at, |k| k.at) else { return false; }; let mut keys = current.clone(); keys.remove(pos); self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].gain_keys = keys; true } /// The envelope's effective dB at timeline frame `timeline_frame` of the /// clip at `idx` of `lane`: its [`Clip::gain_db`] plus its keyed envelope /// at the source frame that timeline frame plays. The fades are not in it. /// `None` for a video lane or an index that is not there. Timeline frames /// before the clip read as its first frame. pub fn gain_db_at(&self, lane: Lane, idx: usize, timeline_frame: u32) -> Option<f32> { if lane.kind != LaneKind::Audio { return None; } let c = self.lane(lane).get(idx)?; Some(c.gain_db + gain_keys_db_at(&c.gain_keys, key_pos(c, timeline_frame))) } /// How the clip at `idx` of `lane` is graded, or `None` for one that plays /// ungraded (and for an index that is not there) -- what the renderer and /// the export ask before they call [`crate::color::apply_yuv`]. pub fn color_of(&self, lane: Lane, idx: usize) -> Option<&ColorParams> { self.color .get(usize::from(self.lane(lane).get(idx)?.color?)) } /// Give the clip at `idx` of `lane` this colour grade, or `None` to take it /// off. [`Project::set_eq`]'s twin in every respect: one undo step, `false` /// (and no history) for an index that is not there or a value that is not /// finite, and equal settings share a table entry. pub fn set_color(&mut self, lane: Lane, idx: usize, params: Option<ColorParams>) -> bool { self.write_color(lane, idx, params, true) } /// [`set_color`](Self::set_color) without the undo step: the samples *inside* /// one pointer drag, whose first write (a plain `set_color`) already took the /// snapshot the gesture rolls back to. A drag across a slider is one undo, /// not one per pixel -- and undoing it lands where the hand picked it up. pub fn set_color_live(&mut self, lane: Lane, idx: usize, params: Option<ColorParams>) -> bool { self.flat.clear(); self.write_color(lane, idx, params, false) } fn write_color( &mut self, lane: Lane, idx: usize, params: Option<ColorParams>, snapshot: bool, ) -> bool { if idx >= self.lane(lane).len() { return false; } let slot = match params { None => None, Some(params) => { if !color_finite(¶ms) { return false; } Some(match self.color.iter().position(|p| *p == params) { Some(at) => at as u16, // corner-cut: the 65535th *distinct* grade of one session is // refused rather than silently aliased, exactly as `set_eq` // refuses the 65535th curve; same upgrade path. None if self.color.len() >= usize::from(u16::MAX) => return false, None => { self.color.push(params); (self.color.len() - 1) as u16 } }) } }; if snapshot { self.snapshot(); } self.lane_mut(lane).expect("checked above")[idx].color = slot; true } /// Every `.cube` file a clip's grade names, each once, in table order. pub fn luts(&self) -> Vec<crate::cube::LutRef> { let used: std::collections::HashSet<u16> = self.lanes.iter().flat_map(|l| l.clips.iter().filter_map(|c| c.color)).collect(); let mut out = Vec::new(); for (i, p) in self.color.iter().enumerate() { if let Some(r) = p.look.lut && used.contains(&(i as u16)) && !out.contains(&r) { out.push(r); } } out } /// The `.cube` files clips name that cannot be used, and why -- a missing /// file or a malformed one. Such a clip plays and exports without its LUT /// (the rest of its grade still applies); this is the warning that says so. pub fn lut_problems(&self) -> Vec<(PathBuf, String)> { self.luts().into_iter().filter_map(|r| r.problem().map(|e| (r.path(), e))).collect() } /// Points every clip whose grade names the `.cube` file at `from` to `to` /// instead (re-reading `to` even if it is the same path, so an edited file is /// picked up). One undo step; the number of clips changed, 0 (and no history) /// when none names `from`. The colour table only grows, as every grade edit /// leaves it: the clips are re-pointed, which is what undo restores. pub fn relink_lut(&mut self, from: &Path, to: &Path) -> usize { let named = |p: &ColorParams| p.look.lut.is_some_and(|r| r.path() == from); let hits: Vec<(usize, usize)> = self .lanes .iter() .enumerate() .flat_map(|(l, lane)| { lane.clips.iter().enumerate().filter_map(move |(i, c)| c.color.map(|slot| (l, i, slot))) }) .filter(|&(_, _, slot)| self.color.get(usize::from(slot)).is_some_and(named)) .map(|(l, i, _)| (l, i)) .collect(); if hits.is_empty() { return 0; } let fresh = crate::cube::LutRef::reload(to); self.snapshot(); for &(l, i) in &hits { let slot = usize::from(self.lanes[l].clips[i].color.expect("graded")); let mut params = self.color[slot]; params.look.lut = Some(fresh); let at = match self.color.iter().position(|p| *p == params) { Some(at) => at, None => { self.color.push(params); self.color.len() - 1 } }; self.lanes[l].clips[i].color = Some(at as u16); } hits.len() } /// How the clip at `idx` of `lane` is placed, or `None` for one at its fit /// policy's own placement (and for an index that is not there) -- what the /// renderer and the export ask before they place its picture. pub fn transform_of(&self, lane: Lane, idx: usize) -> Option<&TransformParams> { self.transform .get(usize::from(self.lane(lane).get(idx)?.transform?)) } /// [`set_color`](Self::set_color)'s twin for [`Clip::transform`]: one undo /// step, `false` (and no history) for an index that is not there or a /// value that is not finite, and equal settings share a table entry. pub fn set_transform( &mut self, lane: Lane, idx: usize, params: Option<TransformParams>, ) -> bool { self.write_transform(lane, idx, params, true) } /// [`set_transform`](Self::set_transform) without the undo step, for /// [`set_color_live`](Self::set_color_live)'s reason: the samples inside /// one drag on a position/scale/rotate/crop control are one undo, not one /// per pixel. pub fn set_transform_live( &mut self, lane: Lane, idx: usize, params: Option<TransformParams>, ) -> bool { self.flat.clear(); self.write_transform(lane, idx, params, false) } fn write_transform( &mut self, lane: Lane, idx: usize, params: Option<TransformParams>, snapshot: bool, ) -> bool { if idx >= self.lane(lane).len() { return false; } let slot = match params { None => None, Some(params) => { if !transform_finite(¶ms) { return false; } Some(match self.transform.iter().position(|p| *p == params) { Some(at) => at as u16, // corner-cut: the 65535th *distinct* transform of one // session is refused rather than silently aliased, // exactly as `write_color` refuses the 65535th grade; // same upgrade path. None if self.transform.len() >= usize::from(u16::MAX) => return false, None => { self.transform.push(params); (self.transform.len() - 1) as u16 } }) } }; if snapshot { self.snapshot(); } self.lane_mut(lane).expect("checked above")[idx].transform = slot; true } /// How fast the clip at `idx` of `lane` plays. [`Speed::NORMAL`] for an /// index that is not there, which is what every clip starts at anyway. pub fn speed_of(&self, lane: Lane, idx: usize) -> Speed { self.lane(lane).get(idx).map_or(Speed::NORMAL, |c| c.speed) } /// Sets it, for the clip **and its whole group**: a link is one span on /// however many lanes ([`links_are_consistent`]), so a picture sped up away /// from its sound would be a group no save could load. One snapshot, so the /// group's change is one [`Project::undo`]. /// /// The source range is untouched -- a speed says how many timeline frames /// that range is spread over ([`Clip::frames`]), which is why a trim and a /// split still address the file afterwards. /// /// Refused, changing nothing and costing no undo step, when the clip would /// grow into the one after it on its own lane: slowing a clip down makes it /// wider, and a lane may not overlap itself. The refusal *names* the clip in /// the way -- by lane and timeline frame -- because "it did not fit" is not /// something a user can go and fix. Also for an index that is not there. pub fn set_speed(&mut self, lane: Lane, idx: usize, speed: Speed) -> crate::Result<()> { self.write_speed(lane, idx, speed, true) } /// [`set_speed`](Self::set_speed) without the undo step: the samples *inside* /// one pointer drag, whose first write (a plain `set_speed`) already took the /// snapshot the gesture rolls back to. A drag across the bar is one undo, /// not one per rate it passed through -- exactly as /// [`set_color_live`](Self::set_color_live) is for a slider. pub fn set_speed_live(&mut self, lane: Lane, idx: usize, speed: Speed) -> crate::Result<()> { self.write_speed(lane, idx, speed, false) } /// Gives the clip at `idx` of `lane` (and the clips linked to it) a speed ramp /// `keys` ([`Clip::speed_keys`]); empty keys clear it, and the clip takes its /// constant [`Clip::speed`] again. Its source range stays, so its footprint is /// recomputed from the ramp. Nothing ripples: as a constant rate change, the new /// length must still fit before the next clip on its lane, or the edit is /// refused, and the caller is told how far it would have run. pub fn set_speed_ramp(&mut self, lane: Lane, idx: usize, keys: &[SpeedKey]) -> crate::Result<()> { if !keys.is_empty() && !valid_speed_keys(keys) { return Err("a speed ramp takes 1 to 8 keys, in strictly increasing frames".into()); } let Some(members) = self.group_of(lane, idx) else { return Err(format!("there is no clip {idx} on {}", lane.label()).into()); }; if !members.subs.is_empty() { return Err("this clip is linked to captions, which a speed ramp cannot retime yet".into()); } if members.clips.iter().any(|&(l, i)| is_sequence_source(self.lanes[l].clips[i].source)) { return Err("a compound clip plays forward at normal speed: change the speed of the clips inside it".into()); } let labels = handles(&self.lanes); for &(l, i) in &members.clips { let clips = &self.lanes[l].clips; let moved = Clip { speed_keys: keys.to_vec(), ..clips[i].clone() }; if let Some(next) = clips.get(i + 1) && moved.end() > next.start { return Err(format!( "with that ramp the clip would run to frame {} and the next {} clip starts at {}: \ move it along, or trim this one first", moved.end(), labels[l].label(), next.start ) .into()); } } self.snapshot(); for (l, i) in members.clips { self.lanes[l].clips[i].speed_keys = keys.to_vec(); debug_assert!(sorted_disjoint(&self.lanes[l].clips)); } debug_assert!(links_are_consistent(&self.lanes).is_ok()); Ok(()) } fn write_speed( &mut self, lane: Lane, idx: usize, speed: Speed, snapshot: bool, ) -> crate::Result<()> { let Some(members) = self.group_of(lane, idx) else { return Err(format!("there is no clip {idx} on {}", lane.label()).into()); }; if members.clips.iter().any(|&(l, i)| is_sequence_source(self.lanes[l].clips[i].source)) { return Err("a compound clip plays forward at normal speed: change the speed of the clips inside it".into()); } if self.lane(lane).get(idx).is_some_and(|c| !c.speed_keys.is_empty()) { return Err("this clip has a speed ramp: clear the ramp before setting a constant speed".into()); } let labels = handles(&self.lanes); // 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).cloned() else { return Err(format!("there is no clip {idx} on {}", lane.label()).into()); }; // The same per-member piece `speeded_playhead` builds for this op, // asked here instead of read there -- a caption's length comes from // the map's own ends (`end - start`), never `round(len * ratio)` // separately, which is what let the two answers drift apart by a // frame. let (held_old, new) = (held.speed.as_f64(), speed.as_f64()); let scale = |f: u32| scaled_caption_frame(held.start, held_old, new, f); let retimed = |s: SubClip| { let map = TimelineMap::piece((s.start, s.end()), (scale(s.start), scale(s.end()))); let start = map.apply(s.start); let end = map.apply(s.end()).max(start.saturating_add(1)); SubClip { start, frames: end - start, ..s } }; for &(l, i) in &members.clips { let clips = &self.lanes[l].clips; let mut moved = clips[i].clone(); moved.speed = speed; if let Some(next) = clips.get(i + 1) && moved.end() > next.start { let name = labels[l].label(); return Err(format!( "at {speed} that clip would run to frame {} and the next {name} clip starts at {}: \ move it along, or trim this one first", moved.end(), next.start ) .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(); } for (l, i) in members.clips { 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(()) } /// Whether the clip at `idx` of `lane` plays backwards ([`Clip::reversed`]); /// `false` for an index that is not there. pub fn reversed_of(&self, lane: Lane, idx: usize) -> bool { self.lane(lane).get(idx).is_some_and(|c| c.reversed) } /// Plays the clip at `idx` of `lane` backwards (`on`) or forwards again, for the /// clip **and its whole group**, as [`set_speed`](Self::set_speed) does: a picture /// run backwards against its sound would be a take nobody recorded. One snapshot, /// so the group's change is one [`Project::undo`]. Nothing moves: a clip's /// footprint and window are the same either way, which is why this can never be /// refused for room. /// /// The keys read from the footage -- placement and volume points are source-keyed /// -- are mirrored across the window as the direction flips, so every animation /// still plays on the same timeline frames it did, and only the footage under it /// runs the other way. Flipping twice puts every key back on the frame it was on. /// /// Refused, changing nothing, for a still (it has no direction), and for an index /// that is not there. A group already in the asked state is `Ok` and costs no undo /// step. pub fn set_reverse(&mut self, lane: Lane, idx: usize, on: bool) -> crate::Result<()> { let Some(members) = self.group_of(lane, idx) else { return Err(format!("there is no clip {idx} on {}", lane.label()).into()); }; if members.clips.iter().any(|&(l, i)| self.is_still(&self.lanes[l].clips[i])) { return Err("a still picture has no direction to reverse".into()); } if members.clips.iter().any(|&(l, i)| is_sequence_source(self.lanes[l].clips[i].source)) { return Err("a compound clip plays forward at normal speed: change the speed of the clips inside it".into()); } if members.clips.iter().all(|&(l, i)| self.lanes[l].clips[i].reversed == on) { return Ok(()); } self.snapshot(); for (l, i) in members.clips { let c = &mut self.lanes[l].clips[i]; if c.reversed != on { c.reversed = on; mirror_keys(c); } } debug_assert!(links_are_consistent(&self.lanes).is_ok()); Ok(()) } /// Where a re-rate of the clip at `idx` of `lane` to `speed` puts a /// playhead standing at `at` -- read off the geometry **before** the /// write, because the old ends are gone after it. The question a card /// asks as its bar moves: the picture under the cursor has to stay the /// picture under the cursor, or the scene changes with the rate. /// /// The answer is piecewise, exactly as [`write_speed`] re-times, and it /// is asked of the same [`TimelineMap`] the write itself is a shape of: /// a clip member keeps its own start and re-fits its length at the new /// rate -- its **old** rate against the new one, which is what the bar's /// live samples ask about as much as a first press does; a caption /// member scales about the held clip's start by the held clip's own /// proportion; and a playhead outside every member's old extent stays /// exactly where it was -- nothing ripples, so the gap a shrink leaves /// (or the room a stretch takes) absorbs the difference, not the /// playhead. `None` for an index that is not there. pub fn speeded_playhead(&self, lane: Lane, idx: usize, speed: Speed, at: u32) -> Option<u32> { // A ramped clip has no single rate to re-fit against: no answer, not a wrong one. if self.lane(lane).get(idx).is_some_and(|c| !c.speed_keys.is_empty()) { return None; } let members = self.group_of(lane, idx)?; let held = self.lane(lane).get(idx).cloned()?; let (held_old, new) = (held.speed.as_f64(), speed.as_f64()); for &(l, i) in members.clips.iter().chain(&members.subs) { let (span, map) = match self.lanes[l] .clips .get(i) .filter(|_| members.clips.contains(&(l, i))) { // The clip's own span, re-fitted at its own old rate: one // piece of the map, anchored where the clip itself is. The // new length is the same *source* frames at the new rate -- // len divided by it, never multiplied. Some(c) => ( (c.start, c.start + c.frames()), TimelineMap::piece( (c.start, c.start + c.frames()), (c.start, c.start + (c.len() as f64 / new).round() as u32), ), ), // The caption's span, scaled about the held clip's head by // the held clip's proportion -- the offset the group keeps. None => { let s = self.lanes[l].subs.get(i)?; let scale = |f: u32| scaled_caption_frame(held.start, held_old, new, f); ( (s.start, s.end()), TimelineMap::piece((s.start, s.end()), (scale(s.start), scale(s.end()))), ) } }; // The piece owns only its own old span: outside it the playhead // is another member's question, or nobody's. if (span.0..span.1).contains(&at) { return Some(map.apply(at)); } } Some(at) } /// How the clip at `idx` of `lane` meets a project canvas of another shape. /// [`FitPolicy::Fit`] for an index that is not there, which is the default /// every clip starts at anyway. pub fn fit_of(&self, lane: Lane, idx: usize) -> FitPolicy { self.lane(lane) .get(idx) .map_or(FitPolicy::default(), |c| c.fit) } /// Sets it. One undo step like every other edit, and `false` (with no /// history) for an index that is not there. Unlike an eq or a grade there is /// no "off": every clip has a policy, and `Fit` is what "off" would mean. pub fn set_fit(&mut self, lane: Lane, idx: usize, fit: FitPolicy) -> bool { if idx >= self.lane(lane).len() { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].fit = fit; true } /// Turns the stabilisation of the clip at `idx` of a video `lane` on (with its /// settings) or off (`None`). One undo step; `false` (no history) for an index /// that is not there, a lane that is not video, or no change. pub fn set_stabilise(&mut self, lane: Lane, idx: usize, st: Option<crate::stabilise::Stabilise>) -> bool { if lane.kind != LaneKind::Video || self.lane(lane).get(idx).is_none_or(|c| c.stabilise == st) { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].stabilise = st; true } /// Sets how a video clip's interlaced source is deinterlaced /// ([`Clip::deinterlace`]). One undo step; `false`, and no history, for a /// lane that is not video, a clip that is not there, or no change. pub fn set_deinterlace(&mut self, lane: Lane, idx: usize, mode: Deinterlace) -> bool { if lane.kind != LaneKind::Video || self.lane(lane).get(idx).is_none_or(|c| c.deinterlace == mode) { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].deinterlace = mode; true } /// Points the clip at `idx` of a video `lane` at another source -- how a generator's /// parameter is edited (a new spelling is a new source). The window is kept. One undo step; /// `false` and no history for a clip that is not there, a source that is not, or no change. pub fn set_clip_source(&mut self, lane: Lane, idx: usize, source: usize) -> bool { if lane.kind != LaneKind::Video || source >= self.sources.len() || self.lane(lane).get(idx).is_none_or(|c| c.source == source) { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].source = source; true } /// Sets the colour label of the clip at `idx` of `lane` (0 for none). One undo step; `false` /// and no history for a clip that is not there, a label outside the palette, or no change. pub fn set_label(&mut self, lane: Lane, idx: usize, label: u8) -> bool { if label > LABEL_COUNT || self.lane(lane).get(idx).is_none_or(|c| c.mark.label == label) { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].mark.label = label; true } /// Switches the clip at `idx` of `lane` on or off ([`ClipMark::off`]). One undo step; `false` /// and no history for a clip that is not there or no change. pub fn set_enabled(&mut self, lane: Lane, idx: usize, enabled: bool) -> bool { if self.lane(lane).get(idx).is_none_or(|c| c.mark.off != enabled) { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].mark.off = !enabled; true } /// The clip's mark, the default for no clip. pub fn mark_of(&self, lane: Lane, idx: usize) -> ClipMark { self.lane(lane).get(idx).map_or_else(ClipMark::default, |c| c.mark) } /// The clip's deinterlace setting, `Frame` for no clip. pub fn deinterlace_of(&self, lane: Lane, idx: usize) -> Deinterlace { self.lane(lane).get(idx).map_or_else(Deinterlace::default, |c| c.deinterlace) } /// The clip's stabilisation ([`Clip::stabilise`]), `None` for off or no clip. pub fn stabilise_of(&self, lane: Lane, idx: usize) -> Option<crate::stabilise::Stabilise> { self.lane(lane).get(idx)?.stabilise } /// Waveform style flags on the clip at `idx` of `lane`. Bit 0 marks it /// a visualizer; the rest are outline/smooth/fast/tint. `false` (no /// history) for an index that is not there. pub fn set_visualizer(&mut self, lane: Lane, idx: usize, flags: u8) -> bool { if idx >= self.lane(lane).len() { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].visualizer = flags; true } /// Give the clip at `idx` of `lane` this packed look: one undo step, and /// `false` (with no history) for an index that is not there, the way every /// other setter refuses. pub fn set_viz_paint(&mut self, lane: Lane, idx: usize, paint: u64) -> bool { self.write_viz_paint(lane, idx, paint, true) } /// [`set_viz_paint`](Self::set_viz_paint) without the undo step: the /// samples *inside* one pointer drag across the colour wheel, whose first /// write (a plain `set_viz_paint`) already took the snapshot the gesture /// rolls back to. One drag is one undo, not one per sample. pub fn set_viz_paint_live(&mut self, lane: Lane, idx: usize, paint: u64) -> bool { self.flat.clear(); self.write_viz_paint(lane, idx, paint, false) } fn write_viz_paint(&mut self, lane: Lane, idx: usize, paint: u64, snapshot: bool) -> bool { if idx >= self.lane(lane).len() { return false; } if snapshot { self.snapshot(); } self.lane_mut(lane).expect("checked above")[idx].viz_paint = paint; true } /// Timeline frames of ramp-up from silence at the start of the clip at /// `idx` of `lane`. `0` for an index that is not there, same as a clip /// that has none. pub fn fade_in_of(&self, lane: Lane, idx: usize) -> u32 { self.lane(lane).get(idx).map_or(0, |c| c.fade_in) } /// Sets it, clamped to the clip's own length ([`Clip::frames`]) -- a fade /// can shrink a clip's audible middle to nothing but never ask for more /// ramp than the clip has frames. One undo step like [`Project::set_fit`], /// and `false` (no history) for an index that is not there. pub fn set_fade_in(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { if idx >= self.lane(lane).len() { return false; } self.snapshot(); let clip = &mut self.lane_mut(lane).expect("checked above")[idx]; clip.fade_in = frames.min(clip.frames()); true } /// Timeline frames of ramp-down to silence at the end of the clip at /// `idx` of `lane`. Same promise as [`Project::fade_in_of`]. pub fn fade_out_of(&self, lane: Lane, idx: usize) -> u32 { self.lane(lane).get(idx).map_or(0, |c| c.fade_out) } /// Sets it. Same promises as [`Project::set_fade_in`]. pub fn set_fade_out(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { if idx >= self.lane(lane).len() { return false; } self.snapshot(); let clip = &mut self.lane_mut(lane).expect("checked above")[idx]; clip.fade_out = frames.min(clip.frames()); true } /// Timeline frames of cross-dissolve the clip at `idx` of `lane` plays /// into its successor, at its own end. `0` for an index that is not /// there, same as a clip that has none. pub fn transition_out_of(&self, lane: Lane, idx: usize) -> u32 { self.lane(lane).get(idx).map_or(0, |c| c.transition_out) } /// Sets it, clamped to the clip's own length and to how many frames its /// successor on `lane` actually offers -- a dissolve can shrink a clip's /// visible middle to nothing but never ask for more than the two clips /// between them have. `false` -- no history -- unless `lane` is video, /// `idx + 1` is in bounds, and the two clips are adjacent: the second's /// [`Clip::start`] is exactly the first's [`Clip::end`], no gap between /// them. One undo step like [`Project::set_fade_out`]. pub fn set_transition_out(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { if lane.kind != LaneKind::Video { return false; } let clips = self.lane(lane); let (Some(a), Some(b)) = (clips.get(idx), clips.get(idx + 1)) else { return false; }; if a.end() != b.start { return false; } let cap = a.frames().min(b.frames()); self.snapshot(); self.lane_mut(lane).expect("checked above")[idx].transition_out = frames.min(cap); true } /// The kind of the transition the clip at `idx` of `lane` plays into its /// successor; a dissolve for an index that is not there. pub fn transition_kind_of(&self, lane: Lane, idx: usize) -> crate::transitions::TransitionKind { self.lane(lane).get(idx).map_or_else(Default::default, |c| c.transition_kind) } /// [`Project::set_transition_out`] with a kind: sets both in one undo step. pub fn set_transition( &mut self, lane: Lane, idx: usize, kind: crate::transitions::TransitionKind, frames: u32, ) -> bool { if lane.kind != LaneKind::Video { return false; } let clips = self.lane(lane); let (Some(a), Some(b)) = (clips.get(idx), clips.get(idx + 1)) else { return false; }; if a.end() != b.start { return false; } let cap = a.frames().min(b.frames()); self.snapshot(); let a = &mut self.lane_mut(lane).expect("checked above")[idx]; a.transition_out = frames.min(cap); a.transition_kind = kind; true } /// Crossfades the audio clip at `idx` of `lane` into the one immediately /// after it, over `frames` timeline frames: sets `idx`'s /// [`Clip::fade_out`] and its neighbour's [`Clip::fade_in`] to `frames`, /// each clamped to its own clip's length. `false` -- no history -- unless /// `lane` is audio, `idx + 1` is in bounds, and the two clips are /// adjacent: the second's [`Clip::start`] is exactly the first's /// [`Clip::end`], no gap between them. /// /// This is the whole mechanism, not a stand-in for one: a *real* /// cross-fade -- both takes heard at once, blending -- is what two clips /// **overlapping** on separate lanes already get for free from the /// mixer's own per-lane sum ([`crate::audio::open_mixed_streams_master`]). /// This call only shapes the edges of two clips already sitting /// end-to-end on *one* lane, so the join between them fades like one did. pub fn crossfade(&mut self, lane: Lane, idx: usize, frames: u32) -> bool { if lane.kind != LaneKind::Audio { return false; } let clips = self.lane(lane); let (Some(a), Some(b)) = (clips.get(idx), clips.get(idx + 1)) else { return false; }; if a.end() != b.start { return false; } self.snapshot(); let clips = self.lane_mut(lane).expect("checked above"); clips[idx].fade_out = frames.min(clips[idx].frames()); clips[idx + 1].fade_in = frames.min(clips[idx + 1].frames()); true } /// Every frame number on every lane rewritten onto a timeline counted at /// another rate: `k` is old timeline frames per new one, `counts` is how /// long each source is on the *new* timeline (indexed as /// [`Project::sources`] is), and the edit survives as the same seconds of /// the same footage. What [`crate::PlaybackSession::set_frame_rate`] does to /// the edit list, and the only thing that touches the frame numbers without /// being an edit anyone made. /// /// Every position -- a clip's start and its end alike -- goes through the /// one map [`Rate::timeline_at`], so two clips that met still meet, a gap /// stays a gap, and a take's picture and its sound land on the same frame on /// their two lanes: no lane can drift against another. A clip is then only /// ever made to *fit* the room its own start and end mapped to, never to /// outgrow it, which is what keeps the lane sorted and disjoint (the cursor /// below is the backstop for the one case that could: a slow clip so short /// that no source range fits its new room at all). /// /// Rounded to the nearest frame of the new grid, every boundary, length and /// envelope ([`Rate::timeline_nearest`]), so no round trip drifts: a second /// trip lands where the first did. Not exact both ways in general: a frame /// of one grid can fall between two of the other, and a rate change keeps /// only the frame it rounds to. An edit whose frames sit on both grids (on /// a whole second at 30, 25 and 24 fps) comes back exactly; any other /// boundary comes back within one frame. The exact fix is to hold the edit /// in seconds rather than frames, which every frame number in this module /// is measured in. pub fn retime(&mut self, k: Rate, counts: &[u32]) { for lane in &mut self.lanes { let mut cursor = 0; for clip in &mut lane.clips { let start = k.timeline_nearest(clip.start).max(cursor); // The room its own end mapped to -- never zero, since a clip is // never empty even on a grid too coarse to hold it. let room = k.timeline_nearest(clip.end()).saturating_sub(start).max(1); let count = counts.get(clip.source).copied().unwrap_or(u32::MAX).max(1); // Its footage, held inside the file's new length: a clip whose // out-point was the last frame of its source must not come out // of this naming a frame past the end -- that is a project that // would not open again ([`crate::PlaybackSession::open_project`] // refuses one by name). let in_frame = k.timeline_nearest(clip.in_frame).min(count - 1); let len = clip.speed.fit(room).unwrap_or(1).clamp(1, count - in_frame); // The envelopes are frame counts measured from the clip's own // start (fades in), its own end (fades out and a dissolve into // the next clip). Each is its own length in seconds, rounded to the // nearest frame of the new grid, not a difference of two rounded // boundaries, which is what let a dissolve drift a frame per trip. let fade_in = k.timeline_nearest(clip.fade_in); let fade_out = k.timeline_nearest(clip.fade_out); let transition = k.timeline_nearest(clip.transition_out); clip.start = start; clip.in_frame = in_frame; clip.out_frame = in_frame + len; let frames = clip.frames(); clip.fade_in = fade_in.min(frames); clip.fade_out = fade_out.min(frames); clip.transition_out = transition.min(frames); // A volume point is a source frame, numbered the way `in_frame` // is, so it moves with the footage. Two points that land on one // frame keep the first, since the envelope holds one per frame. let mut last = None; clip.gain_keys.retain_mut(|key| { key.at = k.timeline_nearest(key.at); let keep = last != Some(key.at); last = Some(key.at); keep }); // The placement keys are source frames too, so they follow the // same grid, one point per frame as the envelope keeps them. let mut last = None; clip.transform_keys.retain_mut(|key| { key.at = k.timeline_nearest(key.at); let keep = last != Some(key.at); last = Some(key.at); keep }); let mut last = None; clip.opacity_keys.retain_mut(|key| { key.at = k.timeline_nearest(key.at); let keep = last != Some(key.at); last = Some(key.at); keep }); let mut last = None; clip.color_keys.retain_mut(|key| { key.at = k.timeline_nearest(key.at); let keep = last != Some(key.at); last = Some(key.at); keep }); cursor = clip.end(); } // A dissolve is only as long as the clip it dissolves into allows // too, which the rescale above cannot see one clip at a time. for i in 1..lane.clips.len() { let (before, after) = lane.clips.split_at_mut(i); let (a, b) = (&mut before[i - 1], &after[0]); if a.end() == b.start { a.transition_out = a.transition_out.min(b.frames()); } } // A subtitle's *window* is in microseconds and a rate change moves // no seconds, so only where it sits is remapped -- through the same // [`Rate::timeline_at`] every clip boundary goes through, so a // caption still starts on the frame its picture does. let mut cursor = 0; for sub in &mut lane.subs { let start = k.timeline_nearest(sub.start).max(cursor); sub.frames = k.timeline_nearest(sub.end()).saturating_sub(start).max(1); sub.start = start; cursor = sub.end(); } } // A title is placed and sized in frames like a clip and its captions, // so it takes the same grid: its window maps start to end, and its // fades are re-measured from the boundary each one is counted from. // Held inside the largest frame a title may end on, which is the check // a load makes of every title. for title in &mut self.titles { let (old_start, old_end) = (title.start, title.end()); let start = k.timeline_nearest(old_start); let end = k.timeline_nearest(old_end).min(MAX_TITLE_END).max(start + 1); let frames = end - start; title.fade_in = k.timeline_nearest(title.fade_in).min(frames); title.fade_out = k.timeline_nearest(title.fade_out).min(frames); title.start = start; title.frames = frames; } // A chapter is a single frame, so it takes the grid as a title's start // does; two that land on one frame keep the first. for chapter in &mut self.chapters { chapter.frame = k.timeline_nearest(chapter.frame); } self.chapters.dedup_by_key(|c| c.frame); // A marker's start and end take the grid the same way. for m in &mut self.markers { let end = k.timeline_nearest(m.frame + m.length); m.frame = k.timeline_nearest(m.frame); m.length = end.saturating_sub(m.frame); } self.markers.sort_by_key(|m| m.frame); } /// Length of the timeline in frames: where the *last* lane runs out. A lane /// that ends early is a trailing gap in that lane, not a shorter timeline. /// /// A subtitle lane counts like any other: a caption placed past the last /// picture holds the timeline open under it, exactly as an `A2` running /// past `V1` does, rather than being silently clipped away by the length /// ([`crate::export::timeline_cues`] clips cues to this). pub fn timeline_frames(&self) -> u32 { self.lanes .iter() .filter_map(|l| match l.kind { LaneKind::Subtitle => l.subs.last().map(SubClip::end), _ => l.clips.last().map(Clip::end), }) .max() .unwrap_or(0) .max(self.title_end()) } /// The same, counting the *media* alone: where the last picture or sound /// runs out, with the captions over them ignored. /// /// What an export actually writes ends here -- the picture loop walks the /// composite spans and the file stops with them -- so this is the length a /// cue has to be inside of to be written at all (what /// [`crate::export::planned_subtitles`] clips a lane's cues to). Equal to /// [`timeline_frames`](Self::timeline_frames) on every timeline whose last /// thing is a picture or a sound, which is every timeline that has no /// caption hanging past the end of both. pub fn media_frames(&self) -> u32 { self.lanes .iter() .filter(|l| l.kind != LaneKind::Subtitle) .filter_map(|l| l.clips.last().map(Clip::end)) .max() .unwrap_or(0) // A title is picture too: one past the last clip runs on over black, // so the export and the picture cover it. .max(self.title_end()) } /// `(timeline_start, len)` per `V1` clip, in order -- what a UI lane needs /// to lay clips out. Gaps show up as the holes between consecutive entries. pub fn clip_spans(&self) -> Vec<(u32, u32)> { self.lane_spans(Lane::V1) } pub fn lane_spans(&self, lane: Lane) -> Vec<(u32, u32)> { // Timeline frames, not source ones: a box on a lane is as wide as the // clip is long *there*, which a speed halves or quadruples. self.lane(lane) .iter() .map(|c| (c.start, c.frames())) .collect() } /// Timeline frame -> `(clip index, source frame)` in `lane`. `None` in a gap /// and past the end -- [`Project::span_at`] is the version that tells those /// two apart. pub fn map(&self, lane: Lane, timeline_frame: u32) -> Option<(usize, u32)> { let clips = self.lane(lane); let idx = at(clips, timeline_frame)?; Some((idx, source_frame(&clips[idx], timeline_frame))) } /// [`map`](Project::map) on `V1` -- the mapping a decoder follows. pub fn map_timeline(&self, timeline_frame: u32) -> Option<(usize, u32)> { self.map(Lane::V1, timeline_frame) } /// What `lane` holds from `timeline_frame` on: the rest of the clip covering /// it, or the gap running to the next clip (or to the end of the timeline, /// which is where a lane shorter than its neighbour keeps showing black). /// `None` at or past the end of the timeline, which is the only "nothing /// left" there is. pub fn span_at(&self, lane: Lane, timeline_frame: u32) -> Option<Span> { let total = self.timeline_frames(); if timeline_frame >= total { return None; } let clips = self.lane(lane); Some(match at(clips, timeline_frame) { Some(idx) => { let (speed, ramp) = span_speed_of(&clips[idx], timeline_frame); Span { start: timeline_frame, len: clips[idx].end() - timeline_frame, from: Some((clips[idx].source, source_frame(&clips[idx], timeline_frame))), speed, ramp, reverse: clips[idx].reversed, pitch: clips[idx].pitch, } } None => Span { start: timeline_frame, speed: Speed::NORMAL, ramp: None, reverse: false, pitch: 0, // The next placement, or the end of the timeline: a gap is // bounded by what comes after it, never by its own bookkeeping. len: clips .iter() .find(|c| c.start > timeline_frame) .map_or(total, |c| c.start) - timeline_frame, from: None, }, }) } /// Every span of `lane` from `timeline_frame` to the end of the timeline, /// gaps included -- the whole play list, which is what an export walks. pub fn spans_from(&self, lane: Lane, timeline_frame: u32) -> Vec<Span> { let mut out = Vec::new(); let mut t = timeline_frame; while let Some(span) = self.span_at(lane, t) { t = span.end(); out.push(span); } out } /// What the *composite* shows from `timeline_frame` on -- the answer /// [`span_at`](Project::span_at) gives for one lane, resolved across every /// video lane by one rule: **the topmost lane with a clip there wins**, and /// topmost is the *last* video lane in display order, so `V2` covers `V1` /// (the usual overlay convention). A frame no video lane covers is a gap, /// which is black however many lanes are above or below it. /// /// No blending, so exactly one lane is visible at a time and the composite /// is itself a span list -- which is why one decoder plays it, the same one /// a single-lane timeline uses, rather than N running at once. /// /// The span is cut short wherever a *higher* lane's clip starts, because /// from that frame on a different lane wins; that is what makes walking /// [`composite_spans_from`](Project::composite_spans_from) correct. /// /// corner-cut: alpha, opacity or any blend mode makes this untrue -- two /// lanes would then be visible in one frame and the caller needs a decoder /// per lane plus a compositor. Upgrade path is a `Vec<Span>` per frame /// (this same walk, per lane) feeding N decoders. pub fn composite_span_at(&self, timeline_frame: u32) -> Option<Span> { let total = self.timeline_frames(); if timeline_frame >= total { return None; } let video = self.video_lanes(); // The last covering lane is the winner; only the lanes above it can // interrupt what it shows. let winner = video .iter() .rposition(|&lane| at(self.lane(lane), timeline_frame).is_some()); let (from, speed, ramp, until, reverse) = match winner { Some(i) => { let clips = self.lane(video[i]); let idx = at(clips, timeline_frame).expect("just found above"); let (speed, ramp) = span_speed_of(&clips[idx], timeline_frame); ( Some((clips[idx].source, source_frame(&clips[idx], timeline_frame))), speed, ramp, clips[idx].end(), clips[idx].reversed, ) } // Nothing covers it: black until something above the floor starts. None => (None, Speed::NORMAL, None, total, false), }; let takeover = video[winner.map_or(0, |i| i + 1)..] .iter() .filter_map(|&lane| self.lane(lane).iter().find(|c| c.start > timeline_frame)) .map(|c| c.start) .min(); Some(Span { start: timeline_frame, len: takeover.map_or(until, |t| t.min(until)) - timeline_frame, from, speed, ramp, reverse, pitch: 0, }) } /// The video lanes in display order, topmost last -- the order the composite /// rule counts in. A hidden one is not in it: the picture is composed from /// what is visible, so a hidden lane is black where it is the only one /// covering and the lane below shows through where it is not. fn video_lanes(&self) -> Vec<Lane> { self.lanes() .into_iter() .filter(|l| l.kind == LaneKind::Video && !self.lane_hidden(*l)) .collect() } /// *Which* clip the composite shows at `timeline_frame`, named: the winner /// of [`composite_span_at`](Project::composite_span_at)'s own rule and its /// index on that lane. `None` over a gap and past the end. What a front-end /// asks to act on "the clip on screen" without re-deriving the rule. pub fn composite_clip_at(&self, timeline_frame: u32) -> Option<(Lane, usize)> { let video = self.video_lanes(); let winner = video .iter() .rposition(|&lane| at(self.lane(lane), timeline_frame).is_some())?; Some((video[winner], at(self.lane(video[winner]), timeline_frame)?)) } /// Which clip of *one* lane `timeline_frame` falls on, or `None` over a gap /// and past that lane's end. [`composite_clip_at`](Project::composite_clip_at) /// answers "the clip on screen"; this answers it per lane, which is what /// walking the lanes for something to select needs. pub fn lane_clip_at(&self, lane: Lane, timeline_frame: u32) -> Option<usize> { at(self.lane(lane), timeline_frame) } /// How the composite is graded at `timeline_frame`, so the picture playback /// converts and the one an export encodes are graded from one answer. /// `None` over a gap (black is black) and for a clip nobody has graded, /// which is the byte-identical path everywhere. pub fn composite_color_at(&self, timeline_frame: u32) -> Option<&ColorParams> { let (lane, idx) = self.composite_clip_at(timeline_frame)?; self.color_of(lane, idx) } /// [`composite_color_at`](Self::composite_color_at)'s twin for /// [`Clip::transform`]: `None` over a gap and for a clip at its fit /// policy's own placement. pub fn composite_transform_at(&self, timeline_frame: u32) -> Option<&TransformParams> { let (lane, idx) = self.composite_clip_at(timeline_frame)?; self.transform_of(lane, idx) } /// How the composite meets the project canvas at `timeline_frame`, so the /// picture playback composes and the one an export encodes are placed from /// one answer. `Fit` over a gap, where black is already the canvas. pub fn composite_fit_at(&self, timeline_frame: u32) -> FitPolicy { self.composite_clip_at(timeline_frame) .map_or(FitPolicy::default(), |(lane, idx)| self.fit_of(lane, idx)) } /// Waveform flags of the topmost video clip at `timeline_frame`, or 0 /// over a gap / a picture that is not a visualizer. pub fn composite_visualizer_at(&self, timeline_frame: u32) -> u8 { self.composite_clip_at(timeline_frame) .map(|(lane, idx)| self.lane(lane)[idx].visualizer) .unwrap_or(0) } /// Packed look ([`Clip::viz_paint`]) of the topmost video clip at /// `timeline_frame`, or zero over a gap / a picture that is not a /// visualizer. pub fn composite_viz_paint_at(&self, timeline_frame: u32) -> u64 { self.composite_clip_at(timeline_frame) .map(|(lane, idx)| self.lane(lane)[idx].viz_paint) .unwrap_or(0) } /// [`spans_from`](Project::spans_from) over the composite: every stretch the /// viewer sees from `timeline_frame` to the end, in order. What an export /// encodes and what playback walks at a clip boundary. pub fn composite_spans_from(&self, timeline_frame: u32) -> Vec<Span> { let mut out = Vec::new(); let mut t = timeline_frame; while let Some(span) = self.composite_span_at(t) { t = span.end(); out.push(span); } out } /// The frame nearest `frame`, within `reach` frames either side, at which a cut lands /// cleanly on every lane -- `frame` itself when it does. `None` when no such frame is /// that near, which is what a cut or a freeze there is refused for. pub fn cut_frame_near(&self, frame: u32, reach: u32) -> Option<u32> { let clean = |f: u32| { self.lanes.iter().all(|l| { l.clips .iter() .all(|c| !(c.start < f && f < c.end()) || split_source(c, f).is_some()) }) }; (0..=reach) .flat_map(|d| [frame.checked_add(d), frame.checked_sub(d)]) .flatten() .find(|&f| clean(f)) } /// Split every lane at `timeline_frame`, so that frame becomes the first /// frame of a new clip, and hand the two sides two fresh group ids. Refused /// (`false`, no change) when no lane has a clip to split there -- at a /// placement's own start, in a gap, and past the end -- all of which would /// produce an empty clip or nothing at all. /// /// Lanes' halves share an id only where they are the *same* span: the lanes /// are edited apart, so the clip being cut here may start (or end) /// somewhere else on another lane, and a group id whose clips disagree /// about their span is not one take (see [`links_are_consistent`], which /// refuses to load one). /// /// Metadata only: no mapping changes, in any lane. pub fn split(&mut self, timeline_frame: u32) -> bool { self.write_split(timeline_frame, true, &all_lanes(&self.lanes)) } /// [`split`](Project::split) on the lanes in `scope` only: the lanes outside /// it keep their clip across the frame. `false`, no change, as `split`. pub fn split_on(&mut self, timeline_frame: u32, scope: &[Lane]) -> bool { let on: Vec<usize> = scope.iter().filter_map(|&l| self.index(l)).collect(); self.write_split(timeline_frame, true, &on) } /// [`split`](Project::split), with the snapshot optional and the lanes /// named: a batch that has already taken a snapshot /// ([`speed_regions`](Project::speed_regions)) splits at two frames per /// region and must still be one undo press, and a *scoped* batch must not /// cut a lane it was told to leave alone. fn write_split(&mut self, timeline_frame: u32, snapshot: bool, on: &[usize]) -> bool { let cut: Vec<Option<Clip>> = self .lanes .iter() .enumerate() .map(|(i, l)| { on.contains(&i) .then(|| splittable(&l.clips, timeline_frame).map(|idx| l.clips[idx].clone())) .flatten() }) .collect(); // The same question on the subtitle lanes, in their own arithmetic: a // razor cuts the captions with the picture, and a frame where only a // caption can be cut is still a cut. let cut_subs: Vec<bool> = self .lanes .iter() .enumerate() .map(|(i, l)| { on.contains(&i) && l.subs .iter() .any(|s| s.start < timeline_frame && timeline_frame < s.end()) }) .collect(); // The links the captions carry, read before the room is opened below // -- `sub_open_room` takes the id off both halves, exactly as // `open_room` does for a clip, and the split hands the halves theirs // back afterwards. let sub_orig: Vec<Option<u32>> = self .lanes .iter() .zip(&cut_subs) .map(|(l, &cut)| { cut.then(|| { l.subs .iter() .find(|s| s.start < timeline_frame && timeline_frame < s.end()) .and_then(|s| s.link) }) .flatten() }) .collect(); if cut.iter().all(Option::is_none) && !cut_subs.contains(&true) { return false; } if snapshot { self.snapshot(); } // The captions' words are divided before their windows are cut, so // the words on the far side of the split stay with the far side. self.split_cues_across(on, &[timeline_frame], false); for (data, _) in self .lanes .iter_mut() .zip(&cut_subs) .filter(|&(_, &cut)| cut) { // A split is room of no width: the halves keep their windows by // proportion and nothing moves. sub_open_room(&mut data.subs, timeline_frame, 0); debug_assert!(subs_sorted_disjoint(&data.subs)); } // Each side is its own question, and the answer is the group the clip // already carried: the left half *keeps* its take's id -- the take the // frames before the cut still are -- and the right halves of one take // share one fresh id of their own, clips and captions alike. A lane // holding no group (a `place`ment, a caption nobody grouped) comes out // of the cut holding none, which is what it went in with. The captions' // own links were drawn above, before the room opened. let orig: Vec<Option<u32>> = cut .iter() .map(|c| c.as_ref().and_then(|c| c.link)) .collect(); let mut distinct: Vec<u32> = Vec::new(); for id in orig.iter().chain(&sub_orig).flatten() { if !distinct.contains(id) { distinct.push(*id); } } // A cut on a *subset* of the lanes leaves the take's other members whole // on lanes that were not cut: the cut halves no longer agree with them // about their span, so the take is unlinked at the cut -- every member, // in this same snapshot (one undo puts it back). let detach = distinct.iter().any(|&id| { self.lanes.iter().enumerate().any(|(j, d)| { !on.contains(&j) && (d.clips.iter().any(|c| c.link == Some(id)) || d.subs.iter().any(|s| s.link == Some(id))) }) }); let fresh: Vec<u32> = (0..distinct.len()).map(|_| self.new_link()).collect(); let right_of = |link: Option<u32>| { link.and_then(|id| distinct.iter().position(|&d| d == id)) .map(|at| fresh[at]) }; for (l, data) in self.lanes.iter_mut().enumerate() { if !cut_subs[l] { continue; } // The pair the razor just made: they touch at the frame it cut. if let Some(idx) = data .subs .iter() .position(|s| s.end() == timeline_frame) .filter(|&idx| { data.subs .get(idx + 1) .is_some_and(|n| n.start == timeline_frame) }) { data.subs[idx].link = sub_orig[l]; data.subs[idx + 1].link = right_of(sub_orig[l]); } } for (data_i, data) in self.lanes.iter_mut().enumerate() { // The lanes this call was not scoped to are not cut, whatever their // own clips would allow. let Some(idx) = splittable(&data.clips, timeline_frame).filter(|_| cut[data_i].is_some()) else { continue; }; let mut tail = data.clips[idx].clone(); // Where the cut lands *in the file*, which is the clip's own rate // away from where it lands on the timeline. Both halves keep the // speed, and `splittable` has already refused any frame at which // the two would not add up to the one being cut. let cut_at = split_source(&tail, timeline_frame).expect("splittable said so"); let (head_speed, tail_speed) = split_keys(&tail, timeline_frame); tail.speed_keys = tail_speed; data.clips[idx].speed_keys = head_speed; let reversed = tail.reversed; match reversed { true => tail.out_frame = cut_at, false => tail.in_frame = cut_at, } tail.start = timeline_frame; // The placement animation is divided, not copied: each half keeps // its own keys and a key at the cut (see `split_transform_keys`). The // lower keys go to the tail of a reversed clip, which plays them last. let (below, above) = split_transform_keys(&data.clips[idx].transform_keys, cut_at); let (head_keys, tail_keys) = if reversed { (above, below) } else { (below, above) }; tail.transform_keys = tail_keys; data.clips[idx].transform_keys = head_keys; let (head_keys, tail_keys) = split_transform_keys(&data.clips[idx].opacity_keys, tail.in_frame); tail.opacity_keys = tail_keys; data.clips[idx].opacity_keys = head_keys; let (head_keys, tail_keys) = split_transform_keys(&data.clips[idx].color_keys, tail.in_frame); tail.color_keys = tail_keys; data.clips[idx].color_keys = head_keys; // Each mask's keys divide the same way as the placement's, per stack position. let masks: Vec<_> = data.clips[idx] .effects .iter() .map(|e| split_transform_keys(&e.keys, cut_at)) .collect(); for ((tail_e, head_e), (below, above)) in tail.effects.iter_mut().zip(data.clips[idx].effects.iter_mut()).zip(masks) { let (head_keys, tail_keys) = if reversed { (above, below) } else { (below, above) }; tail_e.keys = tail_keys; head_e.keys = head_keys; } tail.link = right_of(orig[data_i]); // A split makes a new edge for each half, and a fade is a promise // about an edge: only the half that *kept* the original edge // keeps that fade, re-clamped to its own now-shorter length (a // cut inside the ramp shortens it, never leaves it pointing past // the clip it is on); the edge the cut itself made starts, or // ends, flat -- there was no silence there to ramp out of. tail.fade_in = 0; tail.fade_out = tail.fade_out.min(tail.frames()); // A dissolve is a promise about the *end* edge, same as // `fade_out`: only the half that kept the clip's original end // (the tail) keeps it, re-clamped to its own now-shorter length; // the head's new end is the cut the razor just made, which starts // flat -- there was no successor to dissolve into there. tail.transition_out = tail.transition_out.min(tail.frames()); match reversed { true => data.clips[idx].in_frame = cut_at, false => data.clips[idx].out_frame = cut_at, } data.clips[idx].link = orig[data_i]; data.clips[idx].fade_out = 0; data.clips[idx].transition_out = 0; data.clips[idx].transition_kind = Default::default(); data.clips[idx].fade_in = data.clips[idx].fade_in.min(data.clips[idx].frames()); data.clips.insert(idx + 1, tail); } if detach { // Every member of the take gets an id of its own, as `ungroup` gives // them: each half is detached (a lone id), never a whole take. let gone: Vec<u32> = distinct.iter().chain(&fresh).copied().collect(); let count: usize = self .lanes .iter() .map(|d| { d.clips.iter().filter(|c| c.link.is_some_and(|id| gone.contains(&id))).count() + d.subs.iter().filter(|s| s.link.is_some_and(|id| gone.contains(&id))).count() }) .sum(); let mut own = (0..count).map(|_| self.new_link()).collect::<Vec<_>>(); for data in &mut self.lanes { for c in data.clips.iter_mut().filter(|c| c.link.is_some_and(|id| gone.contains(&id))) { c.link = own.pop(); } for s in data.subs.iter_mut().filter(|s| s.link.is_some_and(|id| gone.contains(&id))) { s.link = own.pop(); } } } true } /// The inverse of [`split`](Project::split): rejoin the placements that meet /// at `timeline_frame` in every lane and put the result back in one group. /// Only what a split could have produced is rejoined -- the two sides must /// touch on the timeline *and* be consecutive frames of the same source -- /// so the clip list comes back exactly as it was and traversal with it. /// `false` when no lane has such a pair. The rejoined placement keeps the /// **left** half's id, which is the take the split left on it: rejoining a /// cut take puts the take back, id and all. pub fn regroup(&mut self, timeline_frame: u32) -> bool { // What each lane would end up covering, if it joins here at all. let joined: Vec<Option<(u32, u32)>> = self .lanes .iter() .map(|l| { joinable(&l.clips, timeline_frame) .map(|idx| (l.clips[idx].start, l.clips[idx + 1].end())) }) .collect(); // ...and the subtitle lanes' own halves, by [`sub_joinable`]'s rule. let join_subs: Vec<Option<usize>> = self .lanes .iter() .map(|l| sub_joinable(&l.subs, timeline_frame)) .collect(); if joined.iter().all(Option::is_none) && join_subs.iter().all(Option::is_none) { return false; } self.snapshot(); for (data, sub) in self.lanes.iter_mut().zip(join_subs) { if let Some(idx) = sub { data.subs[idx].frames += data.subs[idx + 1].frames; // The left half's `in_us`, the right half's `out_us` -- which // is the SUM of two legal widths, because `sub_joinable` has // just established that the two halves are consecutive in the // *source* (`next.in_us == subs[idx].out_us`). Neither half // broke the ceiling and the whole does, so this is a composer // a per-arm check on one writer could never have found: // `set_window` is what makes the closed set closed. let (in_us, out_us) = (data.subs[idx].in_us, data.subs[idx + 1].out_us); set_window(&mut data.subs[idx], in_us, out_us); data.subs.remove(idx + 1); debug_assert!(subs_sorted_disjoint(&data.subs)); } let Some(idx) = joinable(&data.clips, timeline_frame) else { continue; }; match data.clips[idx].reversed { true => data.clips[idx].in_frame = data.clips[idx + 1].in_frame, false => data.clips[idx].out_frame = data.clips[idx + 1].out_frame, } data.clips.remove(idx + 1); } true } /// Take the placement at `idx` of `lane` out of its group: every clip and /// every caption carrying its id -- on however many lanes -- is handed an /// id of its own, so from here on each half moves, trims and is deleted /// alone. The music video whose sound is to be cut against its picture /// starts here, and so does the caption that has to come off the picture it /// was pinned to. /// /// A group that holds more than one take gives each take back whole: a /// [`group`](Project::group_all) with a take member merges that take into /// the group, and one id cannot say which halves were a take before, so /// the halves are matched by what a take is -- clips with the same source, /// start and in/out points -- and each match keeps one id of its own. So /// ungrouping the picture of a take that was grouped with another clip /// leaves the take linked and detaches only the other clip. A group of one /// take, or one with a caption, is taken apart whole, as before. /// /// An id of its own rather than none at all: a half no other lane is grouped /// with is exactly what a [`lift`](Project::lift) leaves behind and is legal /// (see [`links_are_consistent`]), and it is what a front-end already draws /// as detached. `None` would instead say "was never part of a take", which /// is what a one-lane [`place`](Project::place) means. /// /// Metadata only, like a [`split`](Project::split): no mapping changes, so /// nothing has to be reseeked. One snapshot, so one [`Project::undo`] puts /// the group back. Refused (`false`, nothing changed) for an index that is /// not there, a placement in no group at all, and one whose group has no /// other half -- that one is already detached, and a refusal must not cost /// an undo step. pub fn ungroup(&mut self, lane: Lane, idx: usize) -> bool { let Some(members) = self.group_of(lane, idx) else { return false; }; if members.alone() { return false; } let Some(id) = self .lane(lane) .get(idx) .and_then(|c| c.link) .or_else(|| self.sub_lane(lane).get(idx).and_then(|s| s.link)) else { return false; }; self.snapshot(); // The takes the group holds, by what a take is: its halves share these. let mut takes: Vec<TakeKey> = Vec::new(); for data in &self.lanes { for c in data.clips.iter().filter(|c| c.link == Some(id)) { let k = take_key(c); if !takes.contains(&k) { takes.push(k); } } } let whole = takes.len() == 1 && members.subs.is_empty(); // Drawn before the walk: `new_link` takes the whole project, and the // walk holds the lanes. One id per take (or per clip, when the group is // one take that comes apart), then one per caption. let clip_ids = if whole { members.clips.len() } else { takes.len() }; let mut fresh = (0..clip_ids + members.subs.len()).map(|_| self.new_link()).collect::<Vec<_>>(); let mut sub_fresh = fresh.split_off(clip_ids).into_iter(); let mut next = 0; for data in &mut self.lanes { for c in data.clips.iter_mut().filter(|c| c.link == Some(id)) { c.link = Some(if whole { next += 1; fresh[next - 1] } else { let k = take_key(c); fresh[takes.iter().position(|t| *t == k).expect("collected above")] }); } for s in data.subs.iter_mut().filter(|s| s.link == Some(id)) { s.link = sub_fresh.next(); } } debug_assert!(links_are_consistent(&self.lanes).is_ok()); true } /// Put two clips on two lanes into one group, so what moves one moves the /// other again -- the undo of a [`ungroup`](Project::ungroup) by hand, and /// how a picture is regrouped with sound it was never opened with. Whatever /// either of them was grouped with comes along: those clips cover this same /// span already, so the result is one group and not two overlapping ones. /// /// Same frames or nothing: a group id names **one span** on however many /// lanes ([`links_are_consistent`] refuses to load anything else), so two /// clips that do not cover the same frames cannot be one take, and the /// refusal says which bounds to trim to. Kinds are *not* checked: a take may /// run on `V1` and `V2` at once, and picture-with-sound is the case people /// mean, not the rule. /// /// Metadata only, one snapshot. The error says what is wrong, because /// `false` would not: a bad index, one lane twice (a group is at most one /// clip per lane), spans that disagree, and a pair that is already one take /// -- nothing changes, and none of them costs an undo step. pub fn group(&mut self, a: Lane, a_idx: usize, b: Lane, b_idx: usize) -> crate::Result<()> { if a == b { return Err(format!( "a group is one clip per lane: pick the clip to group with on another track, not a second one on {}", a.label() ) .into()); } let clip = |p: &Self, lane: Lane, idx: usize| -> crate::Result<Clip> { p.lane(lane) .get(idx) .cloned() .ok_or_else(|| format!("there is no clip {idx} on {}", lane.label()).into()) }; let (x, y) = (clip(self, a, a_idx)?, clip(self, b, b_idx)?); if (x.start, x.end()) != (y.start, y.end()) { return Err(format!( "{} covers [{}, {}) and {} covers [{}, {}): trim them to matching bounds first", a.label(), x.start, x.end(), b.label(), y.start, y.end() ) .into()); } if x.link.is_some() && x.link == y.link { return Err("those two are one take already".into()); } self.snapshot(); let id = self.new_link(); // Every clip either of them was grouped with, by the same rule: all of // them cover this span, so one id over the lot stays consistent. let old = [x.link, y.link]; for data in &mut self.lanes { for c in data.clips.iter_mut().filter(|c| c.link.is_some()) { if old.contains(&c.link) { c.link = Some(id); } } } // And the two themselves, which a `place` may have left in no group. self.lane_mut(a).expect("read above")[a_idx].link = Some(id); self.lane_mut(b).expect("read above")[b_idx].link = Some(id); debug_assert!(links_are_consistent(&self.lanes).is_ok()); Ok(()) } /// Put every placement the picks name into one group -- the group a hand /// builds with ctrl-click and a menu: the picture, its sound and the /// caption over it, on as many lanes as the picks name. What any of them /// was already grouped with rides along (one group, not two sharing a /// member), and the members keep their own spans: a group is one id over /// one placement per lane, and the offsets between them are what the group /// preserves -- a drag, a trim and a delete all move the members *by the /// same distance*, not to the same frames. /// /// No two picks may name one lane -- that is the one clip-per-lane rule, /// and the refusal says which lane -- and a pick that names nothing is /// refused with its own words. At least two picks, or there is nothing to /// group. /// /// Metadata only, one snapshot, so one [`Project::undo`] takes the group /// apart again. None of the refusals changes anything or costs an undo /// step. pub fn group_all(&mut self, picks: &[(Lane, usize)]) -> crate::Result<()> { if picks.len() < 2 { return Err("a group is two placements or more: pick another one first".into()); } let mut seen: Vec<Lane> = Vec::with_capacity(picks.len()); for &(lane, idx) in picks { if seen.contains(&lane) { return Err(format!( "a group is one placement per lane: {} is picked twice -- keep one clip per \ track", lane.label() ) .into()); } seen.push(lane); let there = if lane.kind == LaneKind::Subtitle { idx < self.sub_lane(lane).len() } else { idx < self.lane(lane).len() }; if !there { let what = if lane.kind == LaneKind::Subtitle { "subtitle" } else { "clip" }; return Err(format!("there is no {what} {idx} on {}", lane.label()).into()); } } let links: Vec<Option<u32>> = picks .iter() .map(|&(lane, idx)| { if lane.kind == LaneKind::Subtitle { self.sub_lane(lane)[idx].link } else { self.lane(lane)[idx].link } }) .collect(); // The picks are already one per lane (`seen`, above), but a pick's // *existing* group can carry a second member onto a lane another // pick (or another existing group in the merge) already occupies -- // two captions on one sub lane, say, each grouped with a different // clip before this call. Merging both in would leave that lane with // two members of one id, which `links_are_consistent` may never see: // refused by name, before anything moves, rather than a group closed // broken. let labels = handles(&self.lanes); let mut per_lane: std::collections::HashMap<usize, u32> = std::collections::HashMap::new(); for (li, data) in self.lanes.iter().enumerate() { let n = data .clips .iter() .filter(|c| c.link.is_some() && links.contains(&c.link)) .count() + data .subs .iter() .filter(|s| s.link.is_some() && links.contains(&s.link)) .count(); if n > 0 { per_lane.insert(li, n as u32); } } // A pick that carries no link of its own is invisible to the scan // above (there is no `Some` to match): it still claims its lane. for &(lane, _) in picks { let li = self.index(lane).expect("checked above"); per_lane.entry(li).or_insert(0); if !self.lanes[li] .clips .iter() .any(|c| c.link.is_some() && links.contains(&c.link)) && !self.lanes[li] .subs .iter() .any(|s| s.link.is_some() && links.contains(&s.link)) { *per_lane.get_mut(&li).expect("just inserted") += 1; } } if let Some((&li, _)) = per_lane.iter().find(|&(_, &n)| n > 1) { return Err(format!( "a group is one placement per lane: merging these groups would put two on {}", labels[li].label() ) .into()); } self.snapshot(); let id = self.new_link(); // Whatever any pick was grouped with comes along, for [`group`]'s // reason -- one id over the lot, not two groups sharing a member. for data in &mut self.lanes { for c in data.clips.iter_mut() { if c.link.is_some() && links.contains(&c.link) { c.link = Some(id); } } for s in data.subs.iter_mut() { if s.link.is_some() && links.contains(&s.link) { s.link = Some(id); } } } // And the picks themselves, which a `place` may have left in no group. // The lane seats are read before the walk, which holds the lanes. let seats: Vec<(usize, bool)> = picks .iter() .map(|&(lane, _)| { ( self.index(lane).expect("checked above"), lane.kind == LaneKind::Subtitle, ) }) .collect(); for (&(lane, idx), &(at, sub)) in picks.iter().zip(&seats) { let _ = lane; if sub { self.lanes[at].subs[idx].link = Some(id); } else { self.lanes[at].clips[idx].link = Some(id); } } debug_assert!(links_are_consistent(&self.lanes).is_ok()); Ok(()) } /// Place `clip` in one lane at `timeline_frame`, overwriting whatever it /// lands on and leaving every other clip exactly where it is -- the /// per-lane paste. Anything already there is trimmed away (and split in two /// if `clip` lands inside it), which is what keeps the lane sorted and /// disjoint; nothing ripples, so a gap before the placement simply stays a /// gap. Refused only for an empty `clip`. /// /// The placement belongs to no group: a clipboard clip carries the link id /// of the take it was copied from, and placing it twice -- or placing a copy /// back over the clip it came from -- would put that id on two clips of one /// lane, which is what a link may never mean (see [`links_are_consistent`]). /// `None` rather than a fresh id because a one-lane placement has nothing on /// another lane to be grouped *with*; [`Project::regroup`] is how clips /// become a group again. /// /// Refused for an empty `clip`, for a lane that is not there, and for a /// subtitle lane, which carries no picture and no sound /// ([`Project::place_sub`] is its door). pub fn place(&mut self, lane: Lane, timeline_frame: u32, clip: Clip) -> bool { if clip.out_frame <= clip.in_frame || lane.kind == LaneKind::Subtitle || self.index(lane).is_none() || self.cycles(&clip) { return false; } self.snapshot(); let clip = Clip { fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), start: timeline_frame, link: None, ..clip }; self.insert_over(lane, clip, &[]); true } /// Place `clip` on `lane` **and its sound on that lane's own audio row** as /// one grouped take -- what a picture let go over a further video track /// means: `V2`'s picture plays on `V2` and its sound on `A2`, which is added /// if it is not there yet (the `+ V` button adds a video row alone). Without /// this half a file with sound landed on a layer silent, and the only way to /// hear it was to drop it a second time on an audio lane. /// /// Both halves overwrite what they land on and neither ripples, exactly as /// [`place`](Project::place) does -- a layer is laid *over* the timeline, so /// nothing under it moves. One history snapshot for the pair, the added lane /// included, so one [`Project::undo`] takes the whole placement back. The two /// carry one link, which is what makes them one take to a drag, a trim and a /// save. /// /// Refused, changing nothing, for an empty `clip` and for a lane that is not /// there or is not a video lane: sound placed alone is /// [`place`](Project::place)'s door, and a still has no sound to pair. pub fn place_take(&mut self, lane: Lane, timeline_frame: u32, clip: Clip) -> bool { if clip.out_frame <= clip.in_frame || lane.kind != LaneKind::Video || self.index(lane).is_none() || self.cycles(&clip) { return false; } self.snapshot(); // `A2` for `V2` -- and the rows between it and the last audio lane, since // an ord names a lane only while every ord below it does. while self.lane_count(LaneKind::Audio) <= lane.ord { self.lanes.push(LaneData::new(LaneKind::Audio, Vec::new())); } let clip = Clip { fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), start: timeline_frame, link: Some(self.new_link()), ..clip }; for half in [lane, Lane::new(LaneKind::Audio, lane.ord)] { self.insert_over(half, clip.clone(), &[]); } debug_assert!(links_are_consistent(&self.lanes).is_ok()); true } /// A source range onto the timeline at `at`, the way an editor's /// three-point edit puts it: `items` are the `(lane, clip)` halves of one /// take (a picture on a video lane, its sound on an audio lane, or either /// alone), each landing with its head on `at` whatever `clip.start` says. /// /// * [`SourceEdit::Overwrite`] replaces what is there for the take's length: /// a clip it covers is trimmed or split around it, and nothing moves. /// * [`SourceEdit::Insert`] opens room first: on the lanes the items land /// on, a clip `at` falls inside is cut there and everything from `at` on /// moves later by the take's length. Lanes that are not item lanes do not /// move and are never cut -- so, as in a scoped ripple /// ([`Project::scope_holds_takes_whole`]), a take that would be pulled /// apart by it (one half on an item lane, the other not) refuses the /// insert by name. Captions keep their own clock, as a paste leaves them. /// /// The halves share one fresh link when there are several and run the same /// length; a lone half belongs to no group. Exactly one history snapshot, /// so one [`Project::undo`] takes the whole edit back. Changes the /// timeline->source mapping: the caller must reseek. /// /// Refused, changing nothing and costing no undo step, for no items, a lane /// that is not there, is locked or is a caption lane, a lane twice, a source /// that is not there or cannot play on the lane (a song on a video lane, a /// still on an audio one), an empty range, a start or ripple that would pass /// the last frame there is, and an insert inside a speeded clip at a frame /// its rate cannot cut. pub fn edit_from_source(&mut self, kind: SourceEdit, at: u32, items: &[(Lane, Clip)]) -> crate::Result<()> { if items.is_empty() { return Err("nothing to place".into()); } let labels = handles(&self.lanes); let mut on: Vec<usize> = Vec::with_capacity(items.len()); for (lane, clip) in items { let Some(i) = self.index(*lane) else { return Err(format!("no {} track on this timeline", lane.label()).into()); }; if lane.kind == LaneKind::Subtitle { return Err(format!("{} is a caption track: a source cannot go on it", labels[i].label()).into()); } if on.contains(&i) { return Err(format!("{} is named twice", labels[i].label()).into()); } if self.lanes[i].locked { return Err(format!("{} is locked: unlock it to edit it", labels[i].label()).into()); } if is_sequence_source(clip.source) { return Err("a sequence is placed with the sequence ops, not as a source".into()); } let Some(source) = self.sources.get(clip.source) else { return Err("that source is not in the project".into()); }; if clip.out_frame <= clip.in_frame { return Err("the range to place is empty".into()); } if lane.kind == LaneKind::Video && crate::is_audio(&source.path) { return Err(format!("that source has no picture: it cannot go on {}", labels[i].label()).into()); } if lane.kind == LaneKind::Audio && crate::is_image(&source.path) { return Err(format!("a still has no sound: it cannot go on {}", labels[i].label()).into()); } on.push(i); } let len = items.iter().map(|(_, c)| c.frames()).max().unwrap_or(0); if len == 0 || at.checked_add(len).is_none() { return Err("the range to place is empty or runs past the last frame there is".into()); } if kind == SourceEdit::Insert { for &i in &on { let clips = &self.lanes[i].clips; if clips.iter().any(|c| c.end() > at && c.end().checked_add(len).is_none()) { return Err("the insert would push a clip past the last frame there is".into()); } if clips.iter().any(|c| c.start < at && at < c.end()) && splittable(clips, at).is_none() { return Err(format!( "{} cannot be cut at frame {at}: the clip there plays at a rate that has no source frame at it; pick a neighbouring frame", labels[i].label() ) .into()); } } self.scope_holds_takes_whole(&on, at)?; } self.snapshot(); if kind == SourceEdit::Insert { self.write_split(at, false, &on); for &i in &on { for c in self.lanes[i].clips.iter_mut().filter(|c| c.start >= at) { c.start += len; } } } let link = (items.len() > 1 && items.iter().all(|(_, c)| c.frames() == len)).then(|| self.new_link()); // A take the overwrite cuts through keeps its take, when every member of it sits on a lane // this edit writes: each member is then cut at the same frames, so the head keeps the id and // the tail takes one fresh id shared by them all. A take with a member on a lane left alone // would be cut out of step with it, so it is detached as a delete detaches it. let end = at + len; let mut straddled: Vec<u32> = Vec::new(); for &i in &on { for c in &self.lanes[i].clips { if let Some(id) = c.link.filter(|_| c.start < end && c.end() > at) && !straddled.contains(&id) { straddled.push(id); } } } let mut keep: Vec<(u32, u32)> = Vec::new(); for id in straddled { let whole = self .lanes .iter() .enumerate() .all(|(j, d)| on.contains(&j) || !d.clips.iter().any(|c| c.link == Some(id))); if whole { keep.push((id, self.new_link())); } } for (lane, clip) in items { let clip = Clip { fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), start: at, link, ..clip.clone() }; self.insert_over(*lane, clip, &keep); } debug_assert!(links_are_consistent(&self.lanes).is_ok()); Ok(()) } /// `clip` into `lane` at its own start, over whatever it lands on: the /// insert both placement doors make, which is what keeps a lane sorted and /// disjoint. The caller has checked that the lane is there. fn insert_over(&mut self, lane: Lane, clip: Clip, keep: &[(u32, u32)]) { let clips = self.lane_mut(lane).expect("the caller checked the lane"); // The pieces a cut leaves keep their take where `keep` says so (see `edit_from_source`); // everywhere else a straddled clip's pieces are detached. clear_with(clips, clip.start, clip.end(), &mut |link| match link.and_then(|id| keep.iter().find(|k| k.0 == id)) { Some(&(head, tail)) => (Some(head), Some(tail)), None => (None, None), }); let idx = clips.partition_point(|c| c.start < clip.start); clips.insert(idx, clip); debug_assert!(sorted_disjoint(clips)); } /// Move the clip at `idx` of `from` onto `to` with its head at timeline /// frame `start`: the drag that rearranges takes, along a track and across /// them alike. One snapshot, so one [`Project::undo`] puts it back where it /// was. `start` at the clip's own is the pure lane change. /// /// The whole group travels the same distance, each half on the lane it /// already sits on -- a link means "these cover one span on however many /// lanes" ([`links_are_consistent`]), so a picture that slid away from its /// sound would be a group no save could load. Only the dragged half changes /// lane, and that is not a desync: a link names no lane at all. /// /// `start` is **clamped**, never refused, to the room the group has: no /// member crosses a neighbour on the lane it lands on, exactly as /// [`trim`](Project::trim) stops an edge at the clip in front of it. A hand /// dragging past a neighbour means "as far as it goes", and butting up /// against the take next door is how clips are laid end to end. The /// tightest member's wall wins, so a sound half boxed in between two others /// holds its picture still. /// /// Refused, changing nothing and costing no undo step, for a lane that is /// not there, an index that is not there, a move across *kinds* (a picture /// cannot play on an audio lane, and the save it wrote would not open /// again), a head let go *inside* another clip or into a gap too narrow to /// hold this one -- a move that overwrote what it landed on would destroy a /// take the pointer never named, which is what [`place`](Project::place) /// may do and a drag may not -- a half let go onto the lane its own partner /// is on, and a drop that changes neither lane nor frame, which is a clip /// picked up and put back down, i.e. a click. pub fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> bool { self.move_selection(&[(from, idx)], from, idx, to, start) } /// [`move_clip`](Self::move_clip)'s general form: every pick in `picks` /// (each with its own link group, exactly as [`group_of`](Self::group_of) /// reads it) travels the same distance as the clip at `idx` of `from`, /// which alone changes lane, onto `to`. One clamp across the whole set /// ([`move_room`](Self::move_room)) is what makes this all-or-nothing: a /// neighbour that boxes in *any* member narrows the delta every member /// moves by, and a member with no room at all (`lo > hi`) refuses the /// whole set rather than moving the rest and leaving that one behind. /// `move_clip` is this with a selection of one -- the dragged clip's own /// group and nothing else -- so a drag on a clip outside the selection is /// byte-identical to before this existed. pub fn move_selection( &mut self, picks: &[(Lane, usize)], from: Lane, idx: usize, to: Lane, start: u32, ) -> bool { let (Some(dest), Some(clip)) = (self.index(to), self.lane(from).get(idx).cloned()) else { return false; }; if from.kind != to.kind { return false; } let held = self.index(from).expect("the clip was found on it"); // Every pick's own group, unioned: the whole selection moves as one, // and a pick's link partners (not themselves picked) come along with // it exactly as a lone drag's do. let mut members = Members::default(); for &(lane, i) in picks { let Some(group) = self.group_of(lane, i) else { continue; }; for m in group.clips { if !members.clips.contains(&m) { members.clips.push(m); } } for m in group.subs { if !members.subs.contains(&m) { members.subs.push(m); } } } if !members.clips.contains(&(held, idx)) { members.clips.push((held, idx)); } // The two halves of *the dragged clip's own* group would land on one // span of one lane, which is the one thing a link may never mean -- // checked against that group alone, not the whole moving set: an // unlinked fellow pick already sitting on `dest` is no conflict at // all, since it travels by the same delta and `move_room` below // leaves it the room to. Refused rather than clamped: the partner // moves the same distance, so there is no room for it anywhere on // that lane. let own_group = self.group_of(from, idx).expect("the clip was found"); if dest != held && own_group .clips .iter() .chain(&own_group.subs) .any(|&(l, _)| l == dest) { return false; } let want = i64::from(start) - i64::from(clip.start); // First the placement as asked: every member's span shifted by the one // delta the pointer named, each read against the gap it would land in. // A drop on empty frames lands exactly there -- jumping over whatever // clips lie between, on its lane and its linked partners' -- and only // a member whose new span would meet another clip refuses it. let as_asked = self .move_room(&members, Some((held, idx, dest, start)), Some(want)) .filter(|&(lo, hi)| lo <= want && want <= hi); // Otherwise the drag as it always was: how far the group may travel, // in timeline frames and signed, with every member narrowing it to the // gap it stands in (its own lane, or the lane the pointer let it go // over), and what is left is what the pointer is clamped to. let Some((lo, hi)) = as_asked.or_else(|| { self.move_room(&members, Some((held, idx, dest, start)), None) }) else { return false; }; if lo > hi { return false; } let delta = want.clamp(lo, hi); if delta == 0 && dest == held { return false; } self.snapshot(); for &(l, i) in &members.clips { let c = &mut self.lanes[l].clips[i]; // In range by the walls above, which are `u32` frames throughout. c.start = (i64::from(c.start) + delta) as u32; } for &(l, i) in &members.subs { let s = &mut self.lanes[l].subs[i]; s.start = (i64::from(s.start) + delta) as u32; } if dest != held { let clip = self.lanes[held].clips.remove(idx); let clips = &mut self.lanes[dest].clips; let at = clips.partition_point(|c| c.start < clip.start); clips.insert(at, clip); } // A jump can carry a member past clips it did not pass before, on its // own lane as much as onto another, so every lane the set touched is // put back in order. The members keep their spacing among themselves // (one delta), so the order is all that can have changed. let mut touched: Vec<usize> = members .clips .iter() .chain(&members.subs) .map(|&(l, _)| l) .chain([dest]) .collect(); touched.sort_unstable(); touched.dedup(); for l in touched { self.lanes[l].clips.sort_by_key(|c| c.start); self.lanes[l].subs.sort_by_key(|s| s.start); } debug_assert!(sorted_disjoint(&self.lanes[dest].clips)); debug_assert!(members.clips.iter().chain(&members.subs).all(|&(l, _)| { sorted_disjoint(&self.lanes[l].clips) && subs_sorted_disjoint(&self.lanes[l].subs) })); true } /// How far a group drag may travel, `(least, most)` in timeline frames and /// signed: every member narrows the range to the gap it is landing in -- /// the clips against their lanes' clips and the captions against their /// lanes' captions -- and what is left is what the pointer is clamped to. /// `travelling` names the one member that changes lane, if any: the clip a /// hand is carrying onto another track, whose walls are read on the lane it /// is let go over at the frame the pointer named. `None` when a member has /// no gap to land in at all. /// /// `requested`, when given, reads every member's walls where it would sit /// with the whole group shifted by that many frames, rather than where it /// stands; a member whose requested spot is inside another placement, or /// has no gap wide enough, is `None`. fn move_room( &self, members: &Members, travelling: Option<(usize, usize, usize, u32)>, requested: Option<i64>, ) -> Option<(i64, i64)> { let (mut lo, mut hi) = (i64::MIN, i64::MAX); for &(l, i) in members.clips.iter().chain(&members.subs) { let sub = members.subs.contains(&(l, i)); let (start, frames) = match self.lanes[l] .clips .get(i) .filter(|_| !sub) .map(|c| (c.start, c.frames())) { Some(pair) => pair, None => { let s = self.lanes[l].subs.get(i)?; (s.start, s.frames) } }; let land = travelling .filter(|&(fl, fi, ..)| (fl, fi) == (l, i)) .map_or(l, |(_, _, dest, _)| dest); // Which gap this member's walls are read off: its own place on the // lane it stays on, and the frame the pointer named on the lane it // is let go over. A clip's neighbours are the clips of the lane it // lands on and a caption's are its captions -- the two lists of one // lane never meet. let at = match (requested, travelling) { (Some(shift), _) => match u32::try_from(i64::from(start) + shift) { Ok(at) => at, Err(_) => return None, }, (None, Some((fl, fi, _, asked))) if (fl, fi) == (l, i) && land != l => asked, (None, _) => start, }; let (mut wall_lo, mut wall_hi) = (0, u32::MAX); let neighbours: Vec<(u32, u32)> = if sub { self.lanes[land] .subs .iter() .map(|s| (s.start, s.end())) .collect() } else { self.lanes[land] .clips .iter() .map(|c| (c.start, c.end())) .collect() }; for (j, (other_start, other_end)) in neighbours.iter().enumerate() { if members.clips.contains(&(land, j)) || members.subs.contains(&(land, j)) { continue; } if *other_start <= at && at < *other_end { // A head let go inside another placement, which a clamp // has no answer for: the drop is refused. return None; } match other_end <= &at { true => wall_lo = wall_lo.max(*other_end), false => wall_hi = wall_hi.min(*other_start), } } if wall_hi - wall_lo < frames { return None; } lo = lo.max(i64::from(wall_lo) - i64::from(start)); hi = hi.min(i64::from(wall_hi - frames) - i64::from(start)); } Some((lo, hi)) } /// Move one `edge` of the clip at `idx` of `lane` to timeline frame `to` -- /// the drag on a clip's end that makes it play more or less of its source. /// The rest of the lane stays exactly where it is (nothing ripples), so what /// a shortened clip leaves behind is a gap and what a lengthened one takes /// is room that was already empty. One snapshot, so a whole drag is one /// [`Project::undo`] -- commit once, at the release, rather than per pointer /// sample. Changes the timeline->source mapping: the caller must reseek. /// /// [`Edge::Start`] moves the in-point with it: the frames that stay play /// exactly what they played before, which is what makes this a trim and not /// a slip. /// /// `to` is **clamped**, never refused -- a hand pulling an edge past what is /// legal means "as far as it goes", and stopping the box there is the /// affordance. The walls are: one frame of clip always survives, an edge /// never crosses the neighbouring clip on its own lane, an in-point never /// walks back past the source's first frame, and an out-point never runs /// past `source_frames[clip.source]` -- the caller's table of how long each /// source actually is ([`Project`] does not know, and a clip ending past its /// file's last frame is a save that will not open again; see /// `PlaybackSession::trim_clip`, which fills it in). A source with no entry /// there may not grow at all. /// /// Linked placements trim *together*, by one delta: every member's same /// edge moves the distance the dragged one did, clamped to the room its /// own walls leave -- offsets between the members of a hand-built group /// survive the trim, exactly as they survive a drag. /// /// `false`, changing nothing and costing no undo step, for an index that is /// not there and for an edge that is already where it was asked to go. pub fn trim(&mut self, lane: Lane, idx: usize, edge: Edge, to: u32, source_frames: &[u32]) -> bool { self.trim_with_walls(lane, idx, edge, to, SourceWalls::same(source_frames)) } /// [`trim`](Project::trim) measured against each lane's own table /// ([`SourceWalls`]): what a linked take needs, since its audio and picture /// members end on different frames. pub fn trim_with_walls( &mut self, lane: Lane, idx: usize, edge: Edge, to: u32, walls: SourceWalls<'_>, ) -> bool { let (Some((lo, hi)), Some(clip)) = ( self.trim_room_with_walls(lane, idx, edge, walls), self.lane(lane).get(idx).cloned(), ) else { return false; }; let to = to.clamp(lo, hi); let at = match edge { Edge::Start => clip.start, Edge::End => clip.end(), }; if to == at { return false; } // Worked out before anything moves: at a slow rate a room can be too // narrow to hold even one source frame ([`Speed::fit`]), and the // dragged clip refusing to fit must refuse the whole gesture rather // than be given a range wider than its room -- which is the overlap // the lane invariant exists to forbid. The walls below leave that room, // so this is the backstop for a project that came in through another // door. // A ramp's head is not moved yet: its keys are measured from the clip's own // start, so a head trim would have to rebase them. Refused, as a split is. if matches!(edge, Edge::Start) && !clip.speed_keys.is_empty() { return false; } let room = match edge { // The frames that stay play what they played, so what survives is // measured from the *end*. Edge::Start => clip.end() - to, Edge::End => to - clip.start, }; let Some(keep) = clip.fit_source(room) else { return false; }; // An end edge never keeps more source than the source has past the // in-point: [`Speed::fit`] rounds by the same footprint the wall does, so // at a fast rate it can name one frame past the source's end, one the wall // (`clip_walls`) never offers. Held to the wall, the edit it commits is // the edit a save will accept. let keep = keep.min(keep_cap(&clip, edge, walls.for_kind(lane.kind))); let members = self.group_of(lane, idx).expect("checked above"); self.snapshot(); let still = self.is_still(&clip); let held = self.index(lane).expect("checked above"); write_trim(&mut self.lanes[held].clips[idx], edge, to, keep, still); rekey_trimmed(&mut self.lanes[held].clips[idx], &clip, edge, still); debug_assert!(sorted_disjoint(&self.lanes[held].clips)); self.follow_group( &members, &(held, idx), edge, i64::from(to) - i64::from(at), walls, ); true } /// **Ripple trim**: [`trim`](Project::trim), except that the hole the trim leaves is closed /// (or opened) and what comes after moves with the edge, as Premiere's ripple trim does. A /// start edge keeps the clip where it began and cuts its head out, so the clips behind it /// move up by as much; an end edge moves what follows by the length it gained or lost. A /// linked member moves the same way on its own lane. The room an end edge may grow into is /// still the room its neighbour leaves, as a plain trim's is. One undo step. pub fn ripple_trim(&mut self, lane: Lane, idx: usize, edge: Edge, to: u32, source_frames: &[u32]) -> bool { self.ripple_trim_with_walls(lane, idx, edge, to, SourceWalls::same(source_frames)) } /// [`ripple_trim`](Project::ripple_trim) measured against each lane's own table /// ([`SourceWalls`]), as [`trim_with_walls`](Project::trim_with_walls) is. pub fn ripple_trim_with_walls(&mut self, lane: Lane, idx: usize, edge: Edge, to: u32, walls: SourceWalls<'_>) -> bool { let Some(members) = self.group_of(lane, idx) else { return false; }; let before: Vec<(usize, usize, u32, u32)> = members .clips .iter() .filter_map(|&(l, i)| self.lanes[l].clips.get(i).map(|c| (l, i, c.start, c.end()))) .collect(); if !self.trim_with_walls(lane, idx, edge, to, walls) { return false; } for (l, i, old_start, old_end) in before { let clip = &self.lanes[l].clips[i]; let (new_start, new_end) = (clip.start, clip.end()); match edge { Edge::Start => { // The head went (or came back): the clip starts where it did, and the // hole between it and what it was cut from closes behind it. let g = i64::from(new_start) - i64::from(old_start); if g != 0 { // Back in place first: `shift_behind` checks the whole lane, and the clip // still starting at the trimmed head would overlap what moves up. self.lanes[l].clips[i].start = old_start; shift_behind(&mut self.lanes[l], old_end, -g); } } Edge::End => { let g = i64::from(new_end) - i64::from(old_end); if g != 0 { shift_behind(&mut self.lanes[l], old_end, g); } } } } true } /// The rest of a group after one member's `edge` has moved by `delta`: /// every other member's same edge moves the same distance, clamped to the /// room its own walls leave -- a clip's against its lane and its source, /// a caption's against its own lane, its window following by the /// placement's own proportion. The undo step is the caller's: this is the // second half of one edit, not an edit of its own. fn follow_group( &mut self, members: &Members, dragged: &(usize, usize), edge: Edge, delta: i64, walls: SourceWalls<'_>, ) { for &(l, i) in &members.clips { if (l, i) == *dragged { continue; } let old = self.lanes[l].clips[i].clone(); let c = &old; let at = match edge { Edge::Start => c.start, Edge::End => c.end(), }; let (lo, hi) = self.clip_room(l, i, edge, walls); let to = (i64::from(at) + delta).clamp(i64::from(lo), i64::from(hi)) as u32; if to == at { continue; } let room = match edge { Edge::Start => c.end() - to, Edge::End => to - c.start, }; if matches!(edge, Edge::Start) && !c.speed_keys.is_empty() { continue; } let Some(keep) = c.fit_source(room) else { continue; }; // Held to the member's own source, as the dragged clip is. let keep = keep.min(keep_cap(c, edge, walls.for_kind(self.lanes[l].kind))); let still = self.is_still(&self.lanes[l].clips[i]); write_trim(&mut self.lanes[l].clips[i], edge, to, keep, still); rekey_trimmed(&mut self.lanes[l].clips[i], &old, edge, still); debug_assert!(sorted_disjoint(&self.lanes[l].clips)); } for &(l, i) in &members.subs { if (l, i) == *dragged { continue; } let s = self.lanes[l].subs[i]; let at = match edge { Edge::Start => s.start, Edge::End => s.end(), }; let (lo, hi) = sub_edge_room(&self.lanes[l].subs, i, edge); let to = (i64::from(at) + delta).clamp(i64::from(lo), i64::from(hi)) as u32; if to == at { continue; } write_sub_edge(&mut self.lanes[l].subs[i], edge, to); debug_assert!(subs_sorted_disjoint(&self.lanes[l].subs)); } } /// How far that edge may travel, `(first, last)` timeline frame inclusive -- /// the walls [`trim`](Project::trim) clamps to, without moving anything. /// What a front-end drawing the box *during* a drag asks, so the live width /// is the width the release will commit and an edge stops under the pointer /// rather than snapping back. The **clip's own** walls: the rest of its /// group follows by the same delta within their own ([`follow_group`]), so /// what is drawn for the dragged edge is what its own clip will commit. /// `None` for an index that is not there. pub fn trim_room( &self, lane: Lane, idx: usize, edge: Edge, source_frames: &[u32], ) -> Option<(u32, u32)> { self.trim_room_with_walls(lane, idx, edge, SourceWalls::same(source_frames)) } /// [`trim_room`](Project::trim_room) against each lane's own table. pub fn trim_room_with_walls( &self, lane: Lane, idx: usize, edge: Edge, walls: SourceWalls<'_>, ) -> Option<(u32, u32)> { let l = self.index(lane)?; (idx < self.lanes[l].clips.len()).then(|| self.clip_room(l, idx, edge, walls)) } /// [`trim_room`](Project::trim_room) with the same two walls, and for each /// one whether it is the source's media limit (head for a start edge, tail /// for an end edge) rather than a neighbouring clip. Stills never report /// media. `None` for an index that is not there. pub fn trim_walls( &self, lane: Lane, idx: usize, edge: Edge, source_frames: &[u32], ) -> Option<TrimWalls> { self.trim_walls_with_walls(lane, idx, edge, SourceWalls::same(source_frames)) } /// [`trim_walls`](Project::trim_walls) against each lane's own table. pub fn trim_walls_with_walls( &self, lane: Lane, idx: usize, edge: Edge, walls: SourceWalls<'_>, ) -> Option<TrimWalls> { let l = self.index(lane)?; (idx < self.lanes[l].clips.len()).then(|| self.clip_walls(l, idx, edge, walls).0) } /// [`trim_walls`](Project::trim_walls) with each wall's bound named: what /// sets the low wall and what the high one, as a [`WallBound`]. The same /// walls, so a trim to one of them lands on the bound named for it. Asked /// before the trim: a clip committed at a wall sits on it, and the name is /// the one for the place it was asked to go. `None` for a bad index. pub fn trim_bounds( &self, lane: Lane, idx: usize, edge: Edge, walls: SourceWalls<'_>, ) -> Option<(WallBound, WallBound)> { let l = self.index(lane)?; (idx < self.lanes[l].clips.len()).then(|| { let (_, lo, hi) = self.clip_walls(l, idx, edge, walls); (lo, hi) }) } // ---- roll, slip, slide ------------------------------------------------ // // The three trims that move *two* things at once, so no single-edge // [`trim`](Project::trim) can do them: the pair of edges at a cut, the // window a clip reads, the clip between its neighbours. Each is one undo // step, carries the clip's linked take along by the same delta, and is // measured against the same source tables a trim is. The engine ignores // locks, as everywhere: the front-end refuses first. // // Each plans the whole edit on clones and commits only a plan every member // can take *exactly*: at a rate that does not divide the frame grid a // timeline length can be unreachable for a source range ([`Speed::fit`]), // and an edit that lands one frame off would open a gap or an overlap and // move the timeline's length. The delta therefore backs off toward zero // until the plan is exact; a delta of zero is "no change" and costs no step. /// `c` with its `edge` at timeline frame `to`, as [`trim`](Project::trim) /// would leave it, ignoring the neighbours (the caller owns them). `None` /// where a trim would refuse: a ramped head, no room for a source frame. fn edge_moved(&self, c: &Clip, edge: Edge, to: u32, table: &[u32]) -> Option<Clip> { if matches!(edge, Edge::Start) && !c.speed_keys.is_empty() { return None; } let still = self.is_still(c); let room = match edge { Edge::Start => c.end().checked_sub(to)?, Edge::End => to.checked_sub(c.start)?, }; let keep = c.fit_source(room)?; let mut n = c.clone(); match edge { Edge::End => n.out_frame = c.in_frame + keep.min(source_left(c, table)), Edge::Start if still => { n.in_frame = 0; n.out_frame = keep.min(table.get(c.source).copied().unwrap_or(c.len())).max(1); n.start = to; } Edge::Start => { n.in_frame = c.out_frame - keep.min(c.out_frame); n.start = to; } } rekey_trimmed(&mut n, c, edge, still); Some(n) } /// The lane slot and index of every cut a roll at `(lane, idx)` moves: the /// clip before the cut first, then the same cut of each other member of its /// take that has a clip abutting behind it. `None` when `idx` has no clip /// abutting behind it. fn roll_pairs(&self, lane: Lane, idx: usize) -> Option<Vec<(usize, usize)>> { let l = self.index(lane)?; let abuts = |l: usize, i: usize| { let clips = &self.lanes[l].clips; matches!((clips.get(i), clips.get(i + 1)), (Some(a), Some(b)) if a.end() == b.start) }; if lane.kind == LaneKind::Subtitle || !abuts(l, idx) { return None; } let members = self.group_of(lane, idx)?; let mut out = vec![(l, idx)]; out.extend(members.clips.into_iter().filter(|&(ml, mi)| (ml, mi) != (l, idx) && abuts(ml, mi))); Some(out) } /// How far the cut after `(l, i)` may travel on its media alone, as the /// delta range `(back, forward)` around where it is: the clip before it can /// play on to the end of its source, the one behind it back to the start of /// its own, and each keeps a frame. fn roll_span(&self, l: usize, i: usize, walls: SourceWalls<'_>) -> (i64, i64) { let (a, b) = (&self.lanes[l].clips[i], &self.lanes[l].clips[i + 1]); let t = walls.for_kind(self.lanes[l].kind); let cut = i64::from(a.end()); if !b.speed_keys.is_empty() { return (0, 0); } let tail = i64::from(a.start) + i64::from(footprint(a, t.get(a.source).copied().unwrap_or(a.out_frame).saturating_sub(a.in_frame))); let head = if self.is_still(b) { i64::from(b.end()) - i64::from(b.speed.room(t.get(b.source).copied().unwrap_or(b.len()))) } else { i64::from(b.start) - i64::from(footprint(b, b.in_frame)) }; let lo = head.max(0).max(i64::from(a.start) + i64::from(footprint(a, 1))); let hi = tail.min(i64::from(b.end()) - i64::from(footprint(b, 1))); ((lo - cut).min(0), (hi - cut).max(0)) } /// The two clips of the cut after `(l, i)` once it sits at `to`, or `None` /// when either cannot take it *exactly* (see the section note). fn rolled(&self, l: usize, i: usize, to: u32, walls: SourceWalls<'_>) -> Option<(Clip, Clip)> { let (a, b) = (&self.lanes[l].clips[i], &self.lanes[l].clips[i + 1]); let t = walls.for_kind(self.lanes[l].kind); let na = self.edge_moved(a, Edge::End, to, t)?; let nb = self.edge_moved(b, Edge::Start, to, t)?; (na.end() == to && nb.start == to && nb.end() == b.end()).then_some((na, nb)) } /// Where the cut at the end of the clip at `idx` of `lane` may travel, /// `(first, last)` timeline frame inclusive: the walls [`roll`](Project::roll) /// clamps to -- the media of both clips of every member of the take, and a /// frame of each. `None` for a clip with nothing abutting behind it. pub fn roll_room(&self, lane: Lane, idx: usize, walls: SourceWalls<'_>) -> Option<(u32, u32)> { let pairs = self.roll_pairs(lane, idx)?; let (back, fwd) = self.roll_delta_span(&pairs, walls); let cut = i64::from(self.lanes[pairs[0].0].clips[pairs[0].1].end()); Some(((cut + back) as u32, (cut + fwd) as u32)) } fn roll_delta_span(&self, pairs: &[(usize, usize)], walls: SourceWalls<'_>) -> (i64, i64) { pairs.iter().fold((i64::MIN, i64::MAX), |(back, fwd), &(l, i)| { let (b, f) = self.roll_span(l, i, walls); (back.max(b), fwd.min(f)) }) } /// **Roll**: moves the cut at the end of the clip at `idx` of `lane` to /// timeline frame `to`. The out-point of that clip and the in-point of the /// clip abutting behind it move together, so the pair keeps its joint span /// and nothing else on the lane moves: the timeline's length does not /// change. The frames that stay play what they played. /// /// Like [`trim`](Project::trim), `to` is clamped to the walls /// ([`roll_room`](Project::roll_room)), never refused. The rest of the take /// rolls its own cut by the same delta -- the ones that have a cut there -- /// and the whole take is held to the tightest member. At a rate where the /// exact frame is unreachable the cut stops on the nearest one that is. /// /// `Some(cut)`: where the cut is now (the old place when nothing moved, at /// no cost in undo steps; one step otherwise). `None`, changing nothing: /// no clip there, or none abutting behind it -- a roll needs a cut. A /// speed-ramped clip behind the cut cannot take a new in-point (as a trim /// refuses a ramped head), which pins the cut. pub fn roll(&mut self, lane: Lane, idx: usize, to: u32, walls: SourceWalls<'_>) -> Option<u32> { let pairs = self.roll_pairs(lane, idx)?; let cut = i64::from(self.lanes[pairs[0].0].clips[pairs[0].1].end()); let (back, fwd) = self.roll_delta_span(&pairs, walls); let mut d = (i64::from(to) - cut).clamp(back, fwd); let plan = loop { if d == 0 { return Some(cut as u32); } let plan: Option<Vec<_>> = pairs .iter() .map(|&(l, i)| { let at = i64::from(self.lanes[l].clips[i].end()) + d; let at = u32::try_from(at).ok()?; self.rolled(l, i, at, walls).map(|p| (l, i, p)) }) .collect(); if let Some(plan) = plan { break plan; } d -= d.signum(); }; self.snapshot(); for (l, i, (a, b)) in plan { self.lanes[l].clips[i] = a; self.lanes[l].clips[i + 1] = b; debug_assert!(sorted_disjoint(&self.lanes[l].clips)); } Some((cut + d) as u32) } /// The clips a slide at `(lane, idx)` moves, as lane slot and index: the /// clip itself first, then the rest of its take. `None` unless every one of /// them has a clip on both sides -- sliding needs neighbours to give to. fn slide_members(&self, lane: Lane, idx: usize) -> Option<Vec<(usize, usize)>> { let l = self.index(lane)?; if lane.kind == LaneKind::Subtitle || self.lanes[l].clips.get(idx).is_none() { return None; } let members = self.group_of(lane, idx)?; let mut out = vec![(l, idx)]; out.extend(members.clips.into_iter().filter(|&m| m != (l, idx))); out.iter() .all(|&(ml, mi)| mi > 0 && mi + 1 < self.lanes[ml].clips.len()) .then_some(out) } /// How far the clip at `(l, i)` may slide on its neighbours' media alone, as /// the delta range `(back, forward)`. A neighbour it abuts gives or takes /// frames at its cut, up to its own media and a frame of itself; a /// neighbour across a gap only bounds the move where it begins. fn slide_span(&self, l: usize, i: usize, walls: SourceWalls<'_>) -> (i64, i64) { let clips = &self.lanes[l].clips; let (p, c, n) = (&clips[i - 1], &clips[i], &clips[i + 1]); let t = walls.for_kind(self.lanes[l].kind); let (start, end) = (i64::from(c.start), i64::from(c.end())); let (mut back, mut fwd) = (i64::MIN, i64::MAX); if p.end() == c.start { let tail = i64::from(p.start) + i64::from(footprint(p, t.get(p.source).copied().unwrap_or(p.out_frame).saturating_sub(p.in_frame))); back = back.max(i64::from(p.start) + i64::from(footprint(p, 1)) - start); fwd = fwd.min(tail - start); } else { back = back.max(i64::from(p.end()) - start); } if n.start == c.end() { if n.speed_keys.is_empty() { let head = if self.is_still(n) { i64::from(n.end()) - i64::from(n.speed.room(t.get(n.source).copied().unwrap_or(n.len()))) } else { i64::from(n.start) - i64::from(footprint(n, n.in_frame)) }; back = back.max(head - end); fwd = fwd.min(i64::from(n.end()) - i64::from(footprint(n, 1)) - end); } else { back = back.max(0); fwd = fwd.min(0); } } else { fwd = fwd.min(i64::from(n.start) - end); } (back.min(0), fwd.max(0)) } /// The three clips of the slide at `(l, i)` once the middle one starts /// `d` frames later: the previous clip and the next one `None` when a gap /// (not a cut) lies on that side and so nothing there changes. fn slid(&self, l: usize, i: usize, d: i64, walls: SourceWalls<'_>) -> Option<(Option<Clip>, Clip, Option<Clip>)> { let clips = &self.lanes[l].clips; let (p, c, n) = (&clips[i - 1], &clips[i], &clips[i + 1]); let t = walls.for_kind(self.lanes[l].kind); let start = u32::try_from(i64::from(c.start) + d).ok()?; let end = start.checked_add(c.frames())?; let np = if p.end() == c.start { let np = self.edge_moved(p, Edge::End, start, t)?; (np.end() == start).then_some(())?; Some(np) } else { (start >= p.end()).then_some(())?; None }; let nn = if n.start == c.end() { let nn = self.edge_moved(n, Edge::Start, end, t)?; (nn.start == end && nn.end() == n.end()).then_some(())?; Some(nn) } else { (end <= n.start).then_some(())?; None }; Some((np, Clip { start, ..c.clone() }, nn)) } /// Where the clip at `idx` of `lane` may start when slid, `(first, last)` /// timeline frame inclusive: the walls [`slide`](Project::slide) clamps to. /// `None` for a clip that lacks a neighbour on either side (or any member /// of its take does). pub fn slide_room(&self, lane: Lane, idx: usize, walls: SourceWalls<'_>) -> Option<(u32, u32)> { let members = self.slide_members(lane, idx)?; let (back, fwd) = self.slide_delta_span(&members, walls); let start = i64::from(self.lanes[members[0].0].clips[members[0].1].start); Some(((start + back).max(0) as u32, (start + fwd).min(i64::from(u32::MAX)) as u32)) } fn slide_delta_span(&self, members: &[(usize, usize)], walls: SourceWalls<'_>) -> (i64, i64) { members.iter().fold((i64::MIN, i64::MAX), |(back, fwd), &(l, i)| { let (b, f) = self.slide_span(l, i, walls); (back.max(b), fwd.min(f)) }) } /// **Slide**: starts the clip at `idx` of `lane` at timeline frame `to`, /// between its neighbours. Its content and length stay; the out-point of the /// clip before it and the in-point of the clip after it absorb the move /// (each gives or takes the frames at its cut), so the timeline's length /// does not change. A neighbour across a gap is not edited: the clip just /// may not run into it. /// /// Clamped like [`trim`](Project::trim) to [`slide_room`](Project::slide_room); /// the take slides by the same delta, held to its tightest member, and at a /// rate where the exact frame is unreachable stops on the nearest one that /// is. `Some(start)`: where the clip starts now (the old place when nothing /// moved, costing no undo step; one step otherwise). `None`, changing /// nothing: no clip, or no neighbour on a side -- the first and last clip /// of a lane have nothing to give or take. A speed-ramped clip *after* the /// slid one cannot take a new in-point, which pins the slide. pub fn slide(&mut self, lane: Lane, idx: usize, to: u32, walls: SourceWalls<'_>) -> Option<u32> { let members = self.slide_members(lane, idx)?; let start = i64::from(self.lanes[members[0].0].clips[members[0].1].start); let (back, fwd) = self.slide_delta_span(&members, walls); let mut d = (i64::from(to) - start).clamp(back, fwd); let plan = loop { if d == 0 { return Some(start as u32); } let plan: Option<Vec<_>> = members .iter() .map(|&(l, i)| self.slid(l, i, d, walls).map(|p| (l, i, p))) .collect(); if let Some(plan) = plan { break plan; } d -= d.signum(); }; self.snapshot(); for (l, i, (p, c, n)) in plan { if let Some(p) = p { self.lanes[l].clips[i - 1] = p; } self.lanes[l].clips[i] = c; if let Some(n) = n { self.lanes[l].clips[i + 1] = n; } debug_assert!(sorted_disjoint(&self.lanes[l].clips)); } Some((start + d) as u32) } /// The source frames that `by` timeline frames are worth for the clip at /// `idx` of `lane`: `by` itself at real time, `by * speed` at a constant /// rate and at the ramp's average rate for a ramped clip. Rounded to the /// nearest frame, so a tiny `by` on a slow clip can be zero. What a /// front-end turns a pointer or a typed timeline distance into before /// [`slip`](Project::slip), which works in the media's own frames. pub fn slip_source_delta(&self, lane: Lane, idx: usize, by: i64) -> Option<i64> { let c = self.lane(lane).get(idx)?; let (len, frames) = (i128::from(c.len()), i128::from(c.frames())); let n = i128::from(by) * len; // Round half away from zero. let r = (n.abs() * 2 + frames) / (frames * 2) * n.signum(); Some(r as i64) } /// How far the clip at `idx` of `lane` may slip, `(back, forward)` in /// source frames (`back <= 0 <= forward`): back to the first frame of the /// source and forward to its last, which is where the clip's own window /// stops. `None` for a bad index and for a still, which has no frames to /// slip through. pub fn slip_room(&self, lane: Lane, idx: usize, walls: SourceWalls<'_>) -> Option<(i64, i64)> { let l = self.index(lane)?; let c = self.lanes[l].clips.get(idx)?; if self.is_still(c) { return None; } let t = walls.for_kind(self.lanes[l].kind); let limit = i64::from(t.get(c.source).copied().unwrap_or(c.out_frame)); Some((-i64::from(c.in_frame), (limit - i64::from(c.out_frame)).max(0))) } /// **Slip**: shifts the source window of the clip at `idx` of `lane` by /// `by` source frames (see [`slip_source_delta`](Project::slip_source_delta) /// for timeline frames), later in the file for positive. Its place on the /// timeline and its length -- at its speed or ramp -- are unchanged: the /// same slot shows different content. Gain and placement keys stay on the /// footage they were set on. /// /// Clamped to [`slip_room`](Project::slip_room), never refused; the rest of /// the take slips by the same source frames, each within its own media. A /// source with no known length may not slip forward, as it may not grow in /// a trim. `Some(shift)`: the source frames the clip actually moved (`0` /// when pinned at a limit or asked for nothing, costing no undo step; one /// step otherwise). `None`, changing nothing: no clip, or a still. pub fn slip(&mut self, lane: Lane, idx: usize, by: i64, walls: SourceWalls<'_>) -> Option<i64> { let (lo, hi) = self.slip_room(lane, idx, walls)?; let d = by.clamp(lo, hi); if d == 0 { return Some(0); } let l = self.index(lane)?; let members = self.group_of(lane, idx)?; let mut plan: Vec<(usize, usize, Clip)> = Vec::new(); for &(ml, mi) in &members.clips { let c = &self.lanes[ml].clips[mi]; let t = walls.for_kind(self.lanes[ml].kind); let limit = i64::from(t.get(c.source).copied().unwrap_or(c.out_frame)); let own = if (ml, mi) == (l, idx) { d } else if self.is_still(c) { continue; } else { d.clamp(-i64::from(c.in_frame), (limit - i64::from(c.out_frame)).max(0)) }; if own == 0 { continue; } let mut n = c.clone(); n.in_frame = (i64::from(c.in_frame) + own) as u32; n.out_frame = (i64::from(c.out_frame) + own) as u32; if !c.transform_keys.is_empty() { n.transform_keys = keys_within(&c.transform_keys, n.in_frame, n.out_frame); } for (ne, ce) in n.effects.iter_mut().zip(&c.effects) { if !ce.keys.is_empty() { ne.keys = keys_within(&ce.keys, n.in_frame, n.out_frame); } } plan.push((ml, mi, n)); } self.snapshot(); for (ml, mi, n) in plan { self.lanes[ml].clips[mi] = n; } Some(d) } /// [`trim_room`](Project::trim_room) for a clip the caller already holds by /// its lane's storage index: the one wall computation, asked for the clip /// itself and for each member a group trim carries. fn clip_room(&self, l: usize, i: usize, edge: Edge, walls: SourceWalls<'_>) -> (u32, u32) { let (w, _, _) = self.clip_walls(l, i, edge, walls); (w.lo, w.hi) } /// The walls [`clip_room`](Self::clip_room) measures, with the media flags /// [`trim_walls`](Project::trim_walls) reports. Every wall is computed here /// once; the flags are read off the same comparisons that pick the wall. fn clip_walls( &self, l: usize, i: usize, edge: Edge, walls: SourceWalls<'_>, ) -> (TrimWalls, WallBound, WallBound) { // The lane's own table: a sound's length on an audio lane, the picture's // everywhere else. let source_frames = walls.for_kind(self.lanes[l].kind); let clips = &self.lanes[l].clips; let c = clips[i].clone(); // A still has no media limit to report: its head and tail reach as far // as the caller's table says, which is a cap, not a source's end. let still = self.is_still(&c); let prev_end = i.checked_sub(1).map_or(0, |p| clips[p].end()); let (lo, hi, lo_media, hi_media, lo_bound, hi_bound) = match edge { Edge::Start => { // Back to the source's own first frame -- as many *timeline* // frames as that head is worth at the clip's rate, which at // real time is the head itself -- and never over the clip in // front of it. Saturating because a clip may hold *more* // head than the timeline has room for -- a ripple delete // slides a clip back to frame 0 with its in-point wherever // the cut left it -- and frame 0 is the other wall. // // A still is measured from its *tail* instead: it has no // in-point worth the name (every frame the same picture), so // its head reaches exactly as far as its tail does -- out to // the length the caller's table gives it, whole. Measured // from the in-point it would have no head room at all, which // is a placed picture whose left edge cannot be dragged out. // No entry in the table means no growth, as it does at the // other end. let head = if still { c.end().saturating_sub( c.speed .room(source_frames.get(c.source).copied().unwrap_or(c.len())), ) } else if c.reversed { // The head is the window's far end: what it can still grow into // is what the file has past the out-point. c.start.saturating_sub(c.speed.room( source_frames .get(c.source) .copied() .unwrap_or(c.out_frame) .saturating_sub(c.out_frame), )) } else { c.start.saturating_sub(c.speed.room(c.in_frame)) }; ( head.max(prev_end), // One *frame of clip* always survives, and at a rate below // real time one frame of clip is several frames of timeline // ([`Speed::room`]): an edge dragged closer than that would // ask for a range no source frame fits in. At real time this // is `end - 1`, unchanged. c.end() - c.speed.room(1), // The head is the binding wall when it is not behind the // neighbour; equal walls count as media. With no neighbour // the wall is frame 0, which the head always meets or beats. !still && head >= prev_end, // The clip's own minimum length, not a neighbour or media. false, // A neighbour that sits on the same frame as the source's // head is named: the two walls coincide, and the neighbour is // the one a caller can move, so a tie is worded as it. if i > 0 && head <= prev_end { WallBound::Neighbour(prev_end) } else if still { WallBound::Cap } else { WallBound::Source }, WallBound::MinLength, ) } Edge::End => { // Out to whatever is left of the source -- again in timeline // frames, at this clip's rate -- and never over the clip // behind it. let tail = c.start.saturating_add(c.speed.room(match c.reversed { // The tail is the window's near end, which grows down to the // file's first frame. true => c.out_frame, false => source_frames .get(c.source) .copied() .unwrap_or(c.out_frame) .saturating_sub(c.in_frame), })); let next = clips.get(i + 1).map_or(u32::MAX, |n| n.start); ( // ...and the same wall from the other end: the shortest this // clip can be is the room one source frame of it takes. c.start + c.speed.room(1), tail.min(next), false, // The tail is the binding wall when it is not past the // neighbour; equal walls count as media. !still && tail <= next, WallBound::MinLength, // A tie with the neighbour's start is named as the neighbour // too, for the same reason as the start edge's. if clips.get(i + 1).is_some() && tail >= next { WallBound::Neighbour(next) } else if still { WallBound::Cap } else { WallBound::Source }, ) } }; // `hi.max(lo)`: for a clip the invariants hold for, the range always // contains the edge's own place, and a caller's wrong `source_frames` // must not become an empty range (or a panicking `clamp`) here. ( TrimWalls { lo, hi: hi.max(lo), lo_media, hi_media, }, lo_bound, hi_bound, ) } /// Whether `clip` plays a still image ([`crate::is_image`]): a file whose /// every frame is the same picture, so which frame of it a clip's in-point /// names is not a question -- what [`Project::trim`] lets grow at either /// end. `false` for a source that is not there. fn is_still(&self, clip: &Clip) -> bool { self.sources .get(clip.source) .is_some_and(|s| crate::is_image(&s.path)) } /// The placements that move as one with the one at `idx` of `lane` -- /// itself and whatever carries its link, on the media lanes and the /// subtitle ones alike. `None` for an index that is not there. /// Whether the clip at `idx` is in a take: its link is shared with another clip. A lone link /// (what [`ungroup`](Self::ungroup) leaves) is not a take, so the clip moves on its own. fn in_take(&self, lane: Lane, idx: usize) -> bool { self.group_of(lane, idx).is_some_and(|m| !m.alone()) } fn group_of(&self, lane: Lane, idx: usize) -> Option<Members> { let sub = lane.kind == LaneKind::Subtitle; let link = if sub { self.sub_lane(lane).get(idx)?.link } else { self.lane(lane).get(idx)?.link }; let mut members = Members::default(); match link { Some(link) => { for (l, data) in self.lanes.iter().enumerate() { if let Some(i) = data.clips.iter().position(|c| c.link == Some(link)) { members.clips.push((l, i)); } if let Some(i) = data.subs.iter().position(|s| s.link == Some(link)) { members.subs.push((l, i)); } } } None => { let l = self.index(lane).expect("the placement was found on it"); if sub { members.subs.push((l, idx)); } else { members.clips.push((l, idx)); } } } Some(members) } /// Insert `clip` into the first lane of each kind at `timeline_frame` as one /// new group, pushing everything from there on later by its length in /// *every* lane -- the grouped, rippling paste a clipboard does. Mid-clip /// the clip it lands in is split around it; past the end of the timeline it /// goes down where it was asked for, with black in front of it -- which is /// what a library row let go on the open bed means, and what the ghost under /// the pointer promised. A clipboard means "put it here", not "here and /// black in front", and clamps to the end at its own door /// ([`crate::PlaybackSession::paste_at`]). /// Use [`place`](Project::place) to paste into one lane, or to make a gap. /// /// `V1` and `A1` and no other lane, because a take is one picture and one /// sound: copying it onto every lane there is would play the same audio /// twice over (and leave an mp4 export with two tracks to copy). A clip of a /// source with no picture ([`crate::is_audio`]) reaches `A1` only -- on a /// video lane it is a clip that decodes to nothing, and a save carrying one /// does not open again -- and a still image ([`crate::is_image`]) reaches /// `V1` only, for the mirror of that reason. The room is still opened on /// every media lane, or the lanes it was not inserted into would slide out /// of step with the ones it was -- and the subtitle lanes are left alone: /// the words keep their own clock, and a caption that should travel with a /// clip is what a group is for. /// /// Exactly one history snapshot, so one [`Project::undo`] takes it back. /// Changes the timeline->source mapping: the caller must reseek. Refused for /// an empty `clip` and for one naming a source that is not there (both of /// which the public fields let a caller build), and for a paste whose ripple /// would push a clip past the last frame there is -- nothing is changed by /// any of them. pub fn paste(&mut self, timeline_frame: u32, clip: Clip) -> bool { let source = self.sources.get(clip.source); let compound = is_sequence_source(clip.source); if source.is_none() && (!compound || self.compound_refusal(&clip).is_some()) { return false; } if clip.out_frame <= clip.in_frame { return false; } let audio_only = source.is_some_and(|s| crate::is_audio(&s.path)); // ...and its mirror: a still is a picture and no sound, so a copy of one // reaches `V1` only. Pasted onto `A1` as well it is a PNG the audio // worker tries to demux -- which silences the *whole session*, not just // that clip -- and a save the engine's own `open_project` then refuses. let picture_only = source.is_some_and(|s| crate::is_image(&s.path)); // The frame it was asked for and no other: a row let go on the empty // bed past the last clip lands *there*, under the ghost that promised // it, with black in front. Clamping to the end here made every drop on // open bed an append -- the clipboard's rule, applied to a hand that // had named a place. let at = timeline_frame; // The room it takes on the timeline, which its speed decides -- not the // source range it reads. let len = clip.frames(); // Room for it: `open_room` adds `len` to every start from `at` on, and a // hand-written file may hold a clip that ends at the very last frame // (`edith::check` permits `start + len == u32::MAX`). Refused here // rather than wrapped there -- a wrapped start lands *behind* the clip // it belongs after, which is an overlap no save will read back. if at.checked_add(len).is_none() || self .lanes .iter() .flat_map(|l| &l.clips) .any(|c| c.end() > at && c.end().checked_add(len).is_none()) { return false; } // ...and room *to open*: the paste splits whatever it lands inside of, // and a speeded clip can only be cut where its own rate has a source // frame ([`Speed::split_at`]). Refused rather than pasted over the top // of one, which is the overlap a lane may never hold. At real time every // frame answers, so this cannot refuse an unspeeded paste. let lands_inside = |clips: &[Clip]| clips.iter().any(|c| c.start < at && at < c.end()); if self .lanes .iter() .any(|l| lands_inside(&l.clips) && splittable(&l.clips, at).is_none()) { return false; } self.snapshot(); let clip = Clip { fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), start: at, link: Some(self.new_link()), ..clip }; // Only lanes that can play the source: a file with no picture belongs on // an audio lane and nowhere else, exactly as `place_stream_at` decides // it for an import. On a video lane it would decode to nothing, and the // save it wrote would not open again. let kinds: &[LaneKind] = match (audio_only, picture_only) { (true, _) => &[LaneKind::Audio], (_, true) => &[LaneKind::Video], _ => &[LaneKind::Video, LaneKind::Audio], }; let takes: Vec<usize> = kinds .iter() .filter_map(|&kind| self.index(Lane::new(kind, 0))) .collect(); for (i, data) in self.lanes.iter_mut().enumerate() { open_room(&mut data.clips, at, len); // The subtitle lanes are NOT touched: a paste inserts media, and // the words keep their own clock. Opening room here used to split // a caption the paste landed inside and slide the rest behind it // -- "a caption that stayed put while the picture moved on no // longer says the same thing" -- but a caption pinned to nothing // (and a video arriving on a timeline of words alone) has no // picture to follow, and the split did to the words what no hand // asked for. A caption that should travel with a clip is what a // group is for, and a group moves as one. if takes.contains(&i) { let idx = data.clips.partition_point(|c| c.start < at); data.clips.insert(idx, clip.clone()); } debug_assert!(sorted_disjoint(&data.clips)); debug_assert!(subs_sorted_disjoint(&data.subs)); } true } /// [`paste`](Project::paste)'s general form: every `(lane, clip)` in /// `items` lands on the lane it names, its head moved by the offset every /// other item moves by too -- `at` less the earliest `start` among them, /// so the set's own gaps and lane assignment survive the trip from /// clipboard to bed unchanged, rather than collapsing onto `V1`/`A1` the /// way a lone clip's copy does. /// /// All-or-nothing, and **refused** rather than opening room or splitting /// a neighbour the way [`paste`](Project::paste) does for one clip: /// a set-paste never ripples the bed, so a landing that overlaps so much /// as one existing clip on its own lane refuses the whole set, changing /// nothing -- the paste's own [`move_selection`](Project::move_selection) /// clamps instead of refusing, but a paste has no drag to clamp against, /// and a landing moved off the frame asked for is not what a paste means. /// `items` empty, naming a lane or a source that is not there, or a clip /// with `out_frame <= in_frame`, refuses too, before anything is touched. /// /// Exactly one history snapshot for the whole set, so one /// [`Project::undo`] takes it back. Changes the timeline->source mapping /// on every lane an item lands on: the caller must reseek. pub fn paste_set(&mut self, at: u32, items: &[(Lane, Clip)]) -> bool { let Some(anchor) = items.iter().map(|(_, c)| c.start).min() else { return false; }; // Resolved once, up front, off the lanes as they stand now: a lane or // source that is not there, or a landing that would overlap a // neighbour, refuses the whole set before any of it is written. let mut landings: Vec<(usize, Clip)> = Vec::with_capacity(items.len()); for (lane, clip) in items.iter().cloned() { let Some(li) = self.index(lane) else { return false; }; let known = self.sources.get(clip.source).is_some() || (is_sequence_source(clip.source) && self.compound_refusal(&clip).is_none()); if !known || clip.out_frame <= clip.in_frame { return false; } let delta = i64::from(clip.start) - i64::from(anchor); let Ok(start) = u32::try_from(i64::from(at) + delta) else { return false; }; let Some(end) = start.checked_add(clip.frames()) else { return false; }; if self.lanes[li] .clips .iter() .any(|c| c.start < end && start < c.end()) { return false; } landings.push((li, Clip { start, ..clip })); } self.snapshot(); for (li, mut clip) in landings { clip.link = Some(self.new_link()); clip.fade_in = 0; clip.fade_out = 0; clip.transition_out = 0; let clips = &mut self.lanes[li].clips; let idx = clips.partition_point(|c| c.start < clip.start); clips.insert(idx, clip); } true } /// Lift the clip at `idx` out of `lane`, leaving a gap: black frames or /// silence, and nothing else moves. Refused only for an out-of-range index /// (which a lane that is not there always is) -- lifting the last placement /// there is leaves an *empty* timeline, which is a state the project holds /// like any other and an undo brings back. /// Swaps the placements at `a` and `b` of `lane`: `b` takes `a`'s start, the /// placements between them move by the difference in their lengths (the /// ripple within this one lane), and `a` lands where `b` was, moved by the /// same difference. The span the two cover keeps its length, so nothing /// after `b` moves. One snapshot. /// /// `false`, nothing changed, for an index that is not there, `a == b`, and a /// placement in a group: a take is moved as a take, never swapped apart. pub fn swap(&mut self, lane: Lane, a: usize, b: usize) -> bool { let (a, b) = if a < b { (a, b) } else { (b, a) }; let Some(new) = (|| { let clips = self.lane(lane); if a == b || b >= clips.len() || self.in_take(lane, a) || self.in_take(lane, b) { return None; } let span = &clips[a..=b]; let (sa, sb) = (span[0].start, span[span.len() - 1].start); let d = i64::from(span[span.len() - 1].frames()) - i64::from(span[0].frames()); let shift = |x: u32, by: i64| (i64::from(x) + by) as u32; let last = span.len() - 1; let mut out = Vec::with_capacity(span.len()); let mut first_in = span[last].clone(); first_in.start = sa; out.push(first_in); for c in &span[1..last] { let mut m = c.clone(); m.start = shift(m.start, d); out.push(m); } let mut back = span[0].clone(); back.start = shift(sb, d); out.push(back); Some(out) })() else { return false; }; self.snapshot(); self.lane_mut(lane).expect("checked above").splice(a..=b, new); true } /// Exchanges the positions of two unlinked clips on `lane` without rippling /// anything: each takes the other's head, and whatever lies between them stays /// where it is. When the two differ in length the shorter leaves a gap where it /// was and the longer must fit its new place -- the one that does not fit is /// refused, naming the clip it would overlap, and nothing changes. /// /// Both new footprints are checked against every clip that stays put, at once, /// so a clip moving into the slot the other one vacates is not refused for /// overlapping a slot that is about to be free (which is what sequencing three /// single moves did). One undo step. pub fn swap_in_place(&mut self, lane: Lane, a: usize, b: usize) -> crate::Result<()> { let label = lane.label(); let clips = self.lane(lane); if a == b { return Err("the same clip twice: name two different clips".into()); } let (Some(ca), Some(cb)) = (clips.get(a).cloned(), clips.get(b).cloned()) else { return Err(format!("there is no clip {} or {} on {label}", a.max(b), a.min(b)).into()); }; if self.in_take(lane, a) || self.in_take(lane, b) { return Err(format!( "clip {a} and clip {b} on {label} are grouped with other lanes: a take moves as a take, not swapped apart" ) .into()); } let mut moved_a = ca.clone(); moved_a.start = cb.start; let mut moved_b = cb.clone(); moved_b.start = ca.start; let placed = [(a, &moved_a), (b, &moved_b)]; for (idx, moved) in placed { for (i, other) in clips.iter().enumerate() { if i == a || i == b { continue; } if moved.start < other.end() && other.start < moved.end() { return Err(format!( "swapping {a} and {b} on {label} would put clip {idx} at {}f..{}f, which overlaps clip {i} at {}f..{}f: move that clip or trim this one first", moved.start, moved.end(), other.start, other.end() ) .into()); } } } if moved_a.start < moved_b.end() && moved_b.start < moved_a.end() { return Err(format!( "swapping {a} and {b} on {label} would overlap the two clips with each other ({}f..{}f and {}f..{}f): the longer one is wider than the space between them", moved_b.start, moved_b.end(), moved_a.start, moved_a.end() ) .into()); } self.snapshot(); let list = self.lane_mut(lane).expect("checked above"); list[a] = moved_a; list[b] = moved_b; list.sort_by_key(|c| c.start); debug_assert!(sorted_disjoint(list)); Ok(()) } pub fn lift(&mut self, lane: Lane, idx: usize) -> bool { if idx >= self.lane(lane).len() { return false; } self.snapshot(); self.lane_mut(lane).expect("checked above").remove(idx); true } /// Cut the timeline frames `[at, at + len)` out of *every* lane and close /// the hole: everything after slides back by `len`. The rippling delete -- /// [`lift`](Project::lift) is the one that leaves a gap. Refused only for an /// empty range; a delete that leaves nothing behind empties the timeline, /// which is a state like any other and one undo away. pub fn ripple_delete(&mut self, at: u32, len: u32) -> bool { if len == 0 { return false; } // Nothing reaches past `at`: there is nothing to cut and nothing to // slide back, and a delete that changes nothing must not cost an undo // step (see [`snapshot`](Project::snapshot)). if !self .lanes .iter() .flat_map(|l| &l.clips) .any(|c| c.end() > at) { return false; } self.snapshot(); let on = all_lanes(&self.lanes); let tracks = self.caption_tracks_on(&on); let placed = self.placed_cues(&tracks); let pieces = self.split_cues_across(&on, &[at, at.saturating_add(len)], true); ripple(&mut self.lanes, &on, at, len); self.drop_unplaced(&tracks, &placed, &pieces); true } /// The empty stretch of `lane` covering `frame`, as `(start, len)` -- what /// a right-click on empty bench space names before offering to close it. /// `None` when `frame` sits inside a placement, or past the last one: the /// open end of a lane is not a gap, since there is nothing after it to slide /// back ([`gap`]). Subtitle lanes are asked of their [`SubClip`] list, not /// the media [`Clip`] list they deliberately never hold. pub fn gap_at(&self, lane: Lane, frame: u32) -> Option<(u32, u32)> { let data = self.index(lane).map(|i| &self.lanes[i])?; match data.kind { LaneKind::Subtitle => gap(&data.subs, frame), LaneKind::Video | LaneKind::Audio => gap(&data.clips, frame), } } /// How many bounded holes `lane` holds. The open end after the last /// placement is not counted, like [`gap_at`](Self::gap_at), because closing /// it would move nothing. pub fn gap_count(&self, lane: Lane) -> usize { let Some(data) = self.index(lane).map(|i| &self.lanes[i]) else { return 0; }; match data.kind { LaneKind::Subtitle => gaps(&data.subs).len(), LaneKind::Video | LaneKind::Audio => gaps(&data.clips).len(), } } /// The lanes closing the gap `(start, frames)` on `lane` must ripple /// together to keep sync -- what a right-click on empty bench space asks /// before the menu offers to close it, and what widens /// [`cut_regions`](Project::cut_regions)'s scope from the one lane a hand /// named to the whole take the gap might be half of. /// /// A clip bordering the gap on `lane` -- the one ending where it starts, /// the one starting where it ends -- carries a link when it is one take /// with a clip on another lane. That lane joins the scope only when the /// *exact same* stretch is empty there too (same start, same length): the /// ripple then removes nothing but silence, on every lane it touches, and /// both halves stay the length they always were. A link whose partner /// lane is not empty there -- a gap of a different length, or none at all /// -- is named back to the caller rather than silently widened past /// (which would cut real media out of a lane nothing asked to touch) or /// silently left out (which is the defect this closes: a per-lane ripple /// would leave [`scope_holds_takes_whole`](Project::scope_holds_takes_whole) /// to refuse it after the fact, on the very half this could have carried /// along). /// /// `Ok(vec![lane])` alone for a gap that borders no take, or none whose /// partner clip sits on another lane -- the unlinked case, untouched. /// /// Subtitle lanes have no media take halves to inspect. A caption can be /// hand-grouped with clips, but it is not the imported A/V take this method /// widens for, so a subtitle gap stays scoped to its own lane; the later /// scoped cut still refuses an explicit group split. pub fn gap_take_scope(&self, lane: Lane, start: u32, frames: u32) -> crate::Result<Vec<Lane>> { let Some(li) = self.index(lane) else { return Err("no track to work on".into()); }; let labels = handles(&self.lanes); let end = start + frames; let mut scope = vec![lane]; for c in self.lanes[li] .clips .iter() .filter(|c| c.end() == start || c.start == end) { let Some(id) = c.link else { continue }; for (j, data) in self.lanes.iter().enumerate() { if j == li { continue; } if !data.clips.iter().any(|o| o.link == Some(id)) { continue; } match gap(&data.clips, start) { Some((s, f)) if s == start && f == frames => { let other = labels[j]; if !scope.contains(&other) { scope.push(other); } } _ => { // One plain line: the partner by name, and the reason in a word. return Err(format!("Can't close this gap: it would put {} out of sync.", labels[j].label()).into()); } } } } Ok(scope) } /// Close every bounded gap on `lane`, as one undo step, without making the /// lane's scope global. Media lanes use the same take-safety widening /// [`gap_take_scope`](Self::gap_take_scope) gives a single close: linked /// halves ride along only when their matching lane is empty over that exact /// stretch; mismatches are skipped and reported. Subtitle lanes close their /// own [`SubClip`] list only: captions may be hand-grouped, but they are not /// halves of an imported A/V take, so the media take-widening rule has /// nothing to add there. /// /// The walk is back-to-front. Closing a gap shifts everything after it, so /// later holes must be consumed while their measured starts are still true; /// earlier starts are unchanged by cuts to their right. A snapshot is taken /// only before the first successful close, so an all-skipped sweep is not an /// undo step. pub fn close_all_gaps_on_lane(&mut self, lane: Lane) -> crate::Result<GapSweep> { let Some(i) = self.index(lane) else { return Err("no track to work on".into()); }; if self.lanes[i].kind == LaneKind::Subtitle { return Ok(self.close_all_subtitle_gaps_on_lane(i)); } let mut report = GapSweep::default(); let mut snapshot = false; for (start, frames) in gaps(self.lane(lane)).into_iter().rev() { match self.gap_take_scope(lane, start, frames).and_then(|scope| { let on = self.scoped(&scope)?; self.scope_holds_takes_whole(&on, start)?; Ok(on) }) { Ok(on) => { if !snapshot { self.snapshot(); snapshot = true; } ripple(&mut self.lanes, &on, start, frames); report.closed += 1; } Err(e) => report.skipped.push(GapSkip { start, reason: e.to_string(), }), } } report.skipped.reverse(); debug_assert!(links_are_consistent(&self.lanes).is_ok()); Ok(report) } /// Subtitle lanes have the same bounded-gap sweep as media lanes, but no /// take-scope widening: a caption is not one half of an A/V take. Explicit /// hand groups still obey [`scope_holds_takes_whole`](Self::scope_holds_takes_whole), /// so a grouped caption is refused rather than pulled away from its group. fn close_all_subtitle_gaps_on_lane(&mut self, i: usize) -> GapSweep { let mut report = GapSweep::default(); let mut snapshot = false; let on = [i]; for (start, frames) in gaps(&self.lanes[i].subs).into_iter().rev() { match self.scope_holds_takes_whole(&on, start) { Ok(()) => { if !snapshot { self.snapshot(); snapshot = true; } ripple(&mut self.lanes, &on, start, frames); report.closed += 1; } Err(e) => report.skipped.push(GapSkip { start, reason: e.to_string(), }), } } report.skipped.reverse(); debug_assert!(links_are_consistent(&self.lanes).is_ok()); report } /// Cut **every** one of `regions` -- `(start, len)` in timeline frames -- /// out of the lanes in `scope` and close each hole, as one edit: the /// jumpcut a silence scan asks for ([`crate::silence`]). /// /// A lane that is not in `scope` **does not move**, which is the whole /// point of the parameter: the silences of a podcast track come out /// without the music track under it sliding. `scope` is an arbitrary set of /// lanes and not a kind or a range, so a caller with its own idea of what /// is selected (a take, a track, everything) says it in lanes and nothing /// here has to learn about selection. /// /// One [`snapshot`](Project::snapshot) for the lot, which is the whole /// reason this exists rather than a caller's loop over /// [`ripple_delete`](Project::ripple_delete): forty silences must be one /// undo press, not forty. Cut back to front, so a region's frames still /// mean what the caller measured while the ones before it are cut. /// /// The list is sorted and overlapping entries are merged before anything is /// cut ([`tidy`]) -- two regions that overlap would otherwise cut each /// other's frames. Refused, with no undo step, for an empty list, for a /// scope naming no lane that is there, and -- by name -- when the scope /// would take one half of a take with it /// ([`scope_holds_takes_whole`](Project::scope_holds_takes_whole)). pub fn cut_regions(&mut self, regions: &[(u32, u32)], scope: &[Lane]) -> crate::Result<()> { let regions = tidy(regions); let on = self.scoped(scope)?; let Some(&(first, _)) = regions.first() else { return Err("nothing to cut".into()); }; self.scope_holds_takes_whole(&on, first)?; self.snapshot(); for &(at, len) in regions.iter().rev() { let tracks = self.caption_tracks_on(&on); let placed = self.placed_cues(&tracks); let pieces = self.split_cues_across(&on, &[at, at.saturating_add(len)], true); ripple_keeping_takes(&mut self.lanes, &on, at, len, &mut self.next_link); self.drop_unplaced(&tracks, &placed, &pieces); } debug_assert!(links_are_consistent(&self.lanes).is_ok()); self.settle(); Ok(()) } /// The storage indices of `scope`, which is what every batch below walks. /// `Err` for a scope that names nothing there rather than a silent no-op: /// an edit asked for on a lane that is not there did not happen, and the /// caller has to hear so. fn scoped(&self, scope: &[Lane]) -> crate::Result<Vec<usize>> { let on: Vec<usize> = scope.iter().filter_map(|&l| self.index(l)).collect(); match on.is_empty() { true => Err("no track to work on".into()), false => Ok(on), } } /// The law a scoped ripple lives under: **a group travels as one**, so a /// batch that moves one half of a take and not the other would leave the /// halves disagreeing about where they stand -- an offset no hand asked /// for, and the reason the take's lanes have to be scoped together. /// /// Refused by name rather than widened silently. Widening would edit a /// track the user did not scope, which is exactly the surprise scoping /// exists to end; a front-end that wants the take is free to *say* the /// take's lanes, and to name them in what it tells the user afterwards. /// /// Only from `from` on: a take sitting entirely before the first cut does /// 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<u32>, 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) { 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(()) } /// Play every one of `regions` at `speed` instead of cutting it: each is /// split out of whatever covers it, re-rated, and the room it no longer /// needs closed up behind it -- [`set_speed`](Project::set_speed) alone /// does not ripple, and a hole where a silence shrank is the one thing this /// must not leave. One snapshot for the lot, like /// [`cut_regions`](Project::cut_regions). /// /// The rate is **absolute**, not a multiplier on what is there: running it /// twice over the same stretch leaves it at `speed`, not at `speed` /// squared, so a second pass over an already-cut timeline changes nothing. /// /// Scoped exactly as [`cut_regions`](Project::cut_regions) is, and under /// the same take law: a lane outside `scope` is neither re-rated nor moved. /// /// Refused by name, with nothing changed and no undo step, when: /// /// * the scope would take one half of a take with it /// ([`scope_holds_takes_whole`](Project::scope_holds_takes_whole)); /// * a clip on a scoped lane covers only *part* of a region -- the lanes /// shrink by different amounts and the ripple would pull them apart, /// which is what cutting does not suffer and why delete mode has no such /// refusal; /// * a rate cannot address a region's edge ([`Speed::split_at`]), so the /// piece could not be split out exactly; /// * two lanes' pieces would end up different lengths, which is a group /// whose halves disagree about their span (the refusal /// [`write_speed`](Project::write_speed) already makes, in its words); /// * the region would grow rather than shrink, i.e. it already plays faster /// than `speed`. pub fn speed_regions( &mut self, regions: &[(u32, u32)], speed: Speed, scope: &[Lane], ) -> crate::Result<()> { let regions = tidy(regions); let on = self.scoped(scope)?; let Some(&(first, _)) = regions.first() else { return Err("nothing to speed up".into()); }; self.scope_holds_takes_whole(&on, first)?; if self.lanes.iter().any(|l| { l.clips.iter().any(|c| { is_sequence_source(c.source) && regions.iter().any(|&(a, b)| c.start < b && a < c.end()) }) }) { return Err("a compound clip plays forward at normal speed: change the speed of the clips inside it".into()); } if self.lanes.iter().any(|l| { l.clips.iter().any(|c| { !c.speed_keys.is_empty() && regions.iter().any(|&(a, b)| c.start < b && a < c.end()) }) }) { return Err("a clip under the region has a speed ramp: clear the ramp first".into()); } let labels = handles(&self.lanes); // Before anything is touched, because this is the refusal a person can // act on: a clip that laps over the edge of a silence has to be trimmed // (or the silences cut instead), and saying so after a rollback would // be saying it about a timeline that already looks different. for &(at, len) in ®ions { for &l in &on { for c in &self.lanes[l].clips { if c.end() > at && c.start < at + len && !(c.start <= at && c.end() >= at + len) { return Err(format!( "the {} clip at frame {} covers only part of the silence at [{at}, {}): \ speeding it up would pull the lanes apart -- trim it first, or cut the \ silences out instead", labels[l].label(), c.start, at + len ) .into()); } } } } // The rest is checked as it is built: a split's own arithmetic is what // says whether a rate can address an edge. So the snapshot is taken // first and *rolled back* on a refusal -- one undo either way, and a // refusal still costs no step (`roll_back_snapshot` takes back the one just pushed). self.snapshot(); for &(at, len) in regions.iter().rev() { self.write_split(at, false, &on); self.write_split(at + len, false, &on); let mut room: Option<(Lane, u32)> = None; let mut refused = None; for &l in &on { let clips = &self.lanes[l].clips; for c in clips.iter().filter(|c| c.end() > at && c.start < at + len) { if (c.start, c.end()) != (at, at + len) { // *Which* edge did not come off: a rate that cannot // address the end of the silence is not a rate that // cannot address its start, and naming the wrong frame // sends the user looking in the wrong place. let edge = match c.start == at { true => at + len, false => at, }; refused = Some(format!( "the {} clip at frame {} plays at {} and cannot be cut at frame {edge}: \ detach it, or put it back to 1.00x first", labels[l].label(), c.start, c.speed )); continue; } let after = speed.frames(c.len()); match room { None => room = Some((labels[l], after)), Some((first, n)) if n != after => { refused = Some(format!( "at {speed} the {} half of the silence at frame {at} would cover \ {after} frames and the {} half {n}: they are one take and a take \ is one span -- detach them first", labels[l].label(), first.label() )); } Some(_) => {} } } } let shrunk = match (refused, room) { (Some(e), _) => { self.roll_back_snapshot(); return Err(e.into()); } // A gap on every lane: nothing to re-rate, and nothing to close. (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.roll_back_snapshot(); return Err(format!( "the silence at frame {at} already plays faster than {speed}: \ at that rate it would be {shrunk} frames instead of {len}" ) .into()); }; for &l in &on { for c in &mut self.lanes[l].clips { if c.start == at && c.end() == at + len { c.speed = speed; } else if c.start >= at + len { c.start -= delta; } } 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. // // The pieces inside re-time by their *boundaries*, not each // on its own: rounding one piece's start and its neighbour's // start apart can land both on the same frame (two one-frame // cues of a thirty-frame region at 2x both want frame 15), so // the mapped boundaries are walked in order and forced apart // -- each piece at least one frame, none past the region's own // new end, where the first follower lands exactly. A rounding // artifact is clamped, not refused: nothing about the words // asked for an overlap. let share = f64::from(after) / f64::from(len).max(1.); let map = |bound: u32| at + (f64::from(bound - at) * share).round() as u32; let mut prev = at; let subs = &mut self.lanes[l].subs; for sub in subs.iter_mut() { let s = *sub; if s.start >= at + len { sub.start = s.start - delta; } else if s.start >= at && s.end() <= at + len { let mut start = map(s.start).max(prev); let end = map(s.end()).max(start + 1).min(at + after); start = start.min(end - 1); sub.start = start; sub.frames = end - start; prev = end; } } debug_assert!(subs_sorted_disjoint(&self.lanes[l].subs)); } } debug_assert!(links_are_consistent(&self.lanes).is_ok()); self.settle(); Ok(()) } /// Remove the `V1` clip at `idx` and everything under it, closing the gap /// -- the whole-group delete a single-lane front-end means. `false` for a /// bad index. Changes the mapping: the caller must reseek. pub fn delete(&mut self, idx: usize) -> bool { self.delete_in(Lane::V1, idx) } /// [`delete`](Project::delete) for the lane the clip was picked on. A clip /// in no group (or a group of one, which is the same thing to a delete) is /// what it always was: its own span cut out of *every* lane, so the /// timeline closes up under it. A clip in a group takes the group with it, /// each member by its **own** span and on its **own** lane -- the members /// keep their offsets, so their spans may disagree, and the lanes may end /// up out of step with each other, which is inherent to a hand-built group /// and one undo away. Every member -- clips and captions alike -- goes by /// the same law: its own span out of its own lane, hole closed; a caption /// in no group keeps the lift that leaves a gap, which is its own door's /// law ([`delete_sub_in`](Project::delete_sub_in)). One snapshot for the /// whole group, so one [`Project::undo`] puts it back. `false` for a bad /// index, and for a lane that is not there. Changes the mapping: the /// caller must reseek. pub fn delete_in(&mut self, lane: Lane, idx: usize) -> bool { let Some(members) = self.group_of(lane, idx) else { return false; }; if !members.alone() { return self.delete_members(&members); } let Some(clip) = self.lane(lane).get(idx).cloned() else { return false; }; self.ripple_delete(clip.start, clip.frames()) } /// [`delete_in`](Project::delete_in) for the caption the pick named: a /// caption in no group lifts, exactly as its own key always made it /// ([`Project::lift_sub`]), and a caption in a group takes the group with /// it by [`delete_in`](Project::delete_in)'s rule -- every member's own /// span out of its own lane, hole closed, captions and clips alike. /// `false` for a bad index. pub fn delete_sub_in(&mut self, lane: Lane, idx: usize) -> bool { let Some(members) = self.group_of(lane, idx) else { return false; }; if !members.alone() { return self.delete_members(&members); } self.lift_sub(lane, idx) } /// The one whole-group delete both doors land in: every member's own span /// cut out of its own lane and the hole closed there -- clips and /// captions by the same law -- and nothing else on another lane moves, /// which is what keeps one member's ripple from dragging another /// member's lane out from under it. One snapshot, one undo step. fn delete_members(&mut self, members: &Members) -> bool { self.snapshot(); for &(l, i) in &members.clips { let c = self.lanes[l].clips[i].clone(); ripple(&mut self.lanes, &[l], c.start, c.frames()); } // The captions go by the same law their clip siblings do -- the span // out of its own lane, the hole closed -- and not by the lift a // *lone* caption's delete is: deleted with its group, the words after // it slide up with everything else, or the lane the group emptied is // the one lane the delete left a hole in. `ripple` is the one // mechanism (it clears the span and shifts the rest, clips and // captions alike); a sub lane holds no clips, so its other list is // untouched. for &(l, i) in &members.subs { let s = self.lanes[l].subs[i]; ripple(&mut self.lanes, &[l], s.start, s.frames); } debug_assert!(links_are_consistent(&self.lanes).is_ok()); true } /// The clips a lane holds, for the sweep's order law: `lane`'s own list. #[cfg(test)] pub(crate) fn lane_clips_pub(&self, lane: Lane) -> &[Clip] { self.lane(lane) } /// The mark an action takes before it runs, for [`Project::name_step`]: the count /// of every step pushed so far. Settles a no-op first, so the mark is exact. pub(crate) fn step_mark(&mut self) -> u64 { self.settle(); self.pushes } /// Names the newest undo step, but only when the action that took `mark` pushed one: /// an action that changed nothing, or only undid, names nothing. The name is what /// Undo and Redo say about this step ([`Project::undo_label`]) and it leaves with /// the step, so a settled no-op or a capped-out step takes its name with it. pub(crate) fn name_step(&mut self, mark: u64, label: &str) { self.settle(); if self.pushes > mark && let Some(top) = self.history.last_mut() { top.label = Some(label.to_string()); } } /// The name of the step [`Project::undo`] would take back; `None` when that step /// was never named, or when there is nothing to undo. pub fn undo_label(&self) -> Option<&str> { let top = match self.settling_no_op() { // The no-op is the newest step; settling it leaves the one under it on top. Some(_) => self.history.len().checked_sub(2).and_then(|i| self.history.get(i)), None => self.history.last(), }; top.and_then(|s| s.label.as_deref()) } /// The name of the step [`Project::redo`] would walk forward to; `None` when that /// step was never named, or when there is nothing to redo. pub fn redo_label(&self) -> Option<&str> { let stack = match self.settling_no_op() { Some(pending) => &pending.redo, None => &self.redo, }; stack.last().and_then(|s| s.label.as_deref()) } /// How many undo steps are stacked: what the sweep asks so "one op, one /// snapshot" is a count and not a guess. /// The name of every step [`Project::undo`] can take back, oldest first; `None` for a step /// nobody named. The last one is the step the project is on. pub fn undo_labels(&self) -> Vec<Option<String>> { let n = self.history_len(); self.history.iter().take(n).map(|s| s.label.clone()).collect() } /// The name of every step [`Project::redo`] can walk forward to, the next one last (the /// mirror of [`undo_labels`](Project::undo_labels)). pub fn redo_labels(&self) -> Vec<Option<String>> { let stack = match self.settling_no_op() { Some(pending) => &pending.redo, None => &self.redo, }; stack.iter().map(|s| s.label.clone()).collect() } pub(crate) fn history_len(&self) -> usize { match self.settling_no_op() { // The no-op's step goes, and the one it evicted comes back. Some(pending) => self.history.len() - 1 + usize::from(pending.evicted.is_some()), None => self.history.len(), } } /// Every snapshot pushed so far, including those the cap has since dropped. pub(crate) fn history_pushes(&self) -> u64 { match self.settling_no_op() { Some(_) => self.pushes.saturating_sub(1), None => self.pushes, } } /// Which state the project is in: a fresh id after every edit, and the id the /// state had before it after an undo, so equal revisions mean the same /// timeline. Never reused, so a revision names one state for good. pub fn revision(&self) -> u64 { match (self.settling_no_op(), self.history.last()) { (Some(_), Some(top)) => top.revision, _ => self.revision, } } /// Drops the redo stack without a step: for a caller whose own trial edits /// were rolled back, which left their undone states here to redo. pub(crate) fn clear_redo(&mut self) { self.settle(); self.redo.clear(); } /// Moves the redo stack out, leaving it empty, for a caller whose trial edits /// are about to run and may be rolled back. [`Project::restore_redo`] puts it /// back exactly, replacing whatever the trial's own undo steps left on it. pub(crate) fn take_redo(&mut self) -> RedoCheckpoint { self.settle(); RedoCheckpoint(std::mem::take(&mut self.redo)) } /// Puts back a stack [`Project::take_redo`] took out. pub(crate) fn restore_redo(&mut self, cp: RedoCheckpoint) { self.settle(); self.redo = cp.0; } /// Lets the undo history run past [`HISTORY_CAP`] while a trial batch is open, /// so the trial's pushes evict nothing; closing it (`false`) trims the history /// back to the cap, dropping the oldest steps as an edit would. pub(crate) fn hold_history(&mut self, hold: bool) { self.settle(); self.hold_cap = hold; if !hold { let over = self.history.len().saturating_sub(HISTORY_CAP); self.history.drain(..over); } } /// How many redo steps are stacked: what the sweep asks to check `undo` /// left a branch behind, and that a fresh edit clears it. pub(crate) fn redo_len(&self) -> usize { match self.settling_no_op() { Some(pending) => pending.redo.len(), None => self.redo.len(), } } /// The project's own settings as they stand ([`ProjectSettings`]) -- what an /// undo entry holds beside the lanes, and what the session reads back after /// one is restored ([`crate::PlaybackSession::undo`]). pub(crate) fn settings_now(&self) -> ProjectSettings { self.settings.clone() } /// One undo step for a change to the project's own settings: `from` is the /// state the change is from and `now` the one it leaves behind. The entry /// pushed holds `from`, while the copy kept here is `now` -- so the next /// lane edit's own entry carries the canvas and the rate really in force /// rather than the ones before the pick. The lanes the entry holds are the /// ones in place at the call: a settings door leaves them alone and may /// take this step after its change, and one that moves them /// ([`Project::retime`] on a rate change) takes it before, with `now` equal /// to `from`, mirroring the new state afterwards /// ([`Project::mirror_settings`]). pub(crate) fn push_settings_step(&mut self, from: ProjectSettings, now: ProjectSettings) { self.settle(); self.settings = from; self.snapshot(); self.settings = now; } /// Keeps `now` as this project's copy of its own settings, without a step: /// the second half of a settings door that had to take its step before the /// change. pub(crate) fn mirror_settings(&mut self, now: ProjectSettings) { self.settle(); self.settings = now; } /// Every part of the project that an edit can change, as comparable /// values: what the sweep's undo round-trip asks "byte identical?" of. #[cfg(test)] pub(crate) fn parts(&self) -> SweepParts { SweepParts( self.sources.len(), self.lanes .iter() .map(|l| (l.kind, l.clips.clone())) .collect(), self.lanes.iter().map(|l| l.subs.clone()).collect(), self.subtitles.clone(), self.settings.clone(), ) } /// Restore everything from before the last successful edit -- the lanes, /// the subtitle palette, the project's own settings and the sources (a /// relink is undone by the old path coming back). `false` when there /// is nothing left to undo. Pushes what was just left onto the redo stack, /// so [`Project::redo`] can walk forward again. pub fn undo(&mut self) -> bool { self.settle(); match self.history.pop() { Some(prev) => { self.batch_snapped = false; self.pending = None; let displaced = self.swap_state(prev); self.redo.push(displaced); true } None => false, } } /// Restore everything from before the last [`Project::undo`] -- the /// mirror of `undo`, walking the redo stack it fills. `false` when there /// is nothing left to redo, which is also true after any fresh edit: a /// new [`Project::snapshot`] clears the branch `undo` left behind. pub fn redo(&mut self) -> bool { self.settle(); match self.redo.pop() { Some(next) => { self.batch_snapped = false; self.pending = None; let displaced = self.swap_state(next); self.history.push(displaced); true } None => false, } } /// Puts `to` back as the project's state and returns the state it replaced, /// as the snapshot an undo or redo of that replacement restores. fn swap_state(&mut self, to: Snapshot) -> Snapshot { let sources = restored_sources(to.sources, &self.sources); let displaced = Snapshot { revision: std::mem::replace(&mut self.revision, to.revision), label: to.label.clone(), lanes: std::mem::replace(&mut self.lanes, to.lanes), subtitles: self.subtitles.clone(), cue_edits: self.cue_edits.clone(), cue_adds: self.cue_adds.clone(), titles: self.titles.clone(), chapters: self.chapters.clone(), markers: self.markers.clone(), settings: self.settings.clone(), sources: std::mem::replace(&mut self.sources, sources), bins: std::mem::replace(&mut self.bins, to.bins), seq: std::mem::replace(&mut self.seq, to.seq), }; self.flat.clear(); self.subtitles = to.subtitles; self.cue_edits = to.cue_edits; self.cue_adds = to.cue_adds; self.titles = to.titles; self.chapters = to.chapters; self.markers = to.markers; self.settings = to.settings; displaced } /// Half-open `(source, start, end)` segments in *source* seconds, from /// `timeline_frame` to the end of the timeline -- the play list for the /// audio worker, read off **`A1`**. A `None` source is a gap: the /// worker synthesises that many seconds of silence, which is what keeps the /// audio master clock counting across a hole instead of stalling on it. /// The first entry is partial when the position is mid-clip. Empty when the /// position is past the end or `fps` is not usable. pub fn segments_from(&self, timeline_frame: u32, fps: f64) -> Vec<(Option<usize>, f64, f64)> { self.lane_segments_from(Lane::A1, timeline_frame, fps) } /// [`segments_from`](Project::segments_from) for a named lane. pub fn lane_segments_from( &self, lane: Lane, timeline_frame: u32, fps: f64, ) -> Vec<(Option<usize>, f64, f64)> { if !(fps.is_finite() && fps > 0.0) { return Vec::new(); } self.spans_from(lane, timeline_frame) .iter() .map(|span| match span.from { // A clip's window is in *source* seconds: where in the file it // reads from, which a delete before it never shifts. How many // source frames that is, is the span's own arithmetic -- at a // speed it is not the timeline length ([`Span::source_len`]). Some(_) => { let (source, lo, hi) = span.source_window().expect("a clip's span"); (Some(source), f64::from(lo) / fps, f64::from(hi) / fps) } // A gap has no file, so all it can say is how long it is. None => (None, 0.0, f64::from(span.len) / fps), }) .collect() } /// One play list per audio lane that *holds* something, in display order -- /// what the mixer sums ([`crate::AudioSession::open_mixed_streams`]) and /// what an export asks how many lanes it would have to mix. /// /// Never empty: a timeline whose audio lanes are all empty (or which has no /// audio lane at all) still yields one all-gap list, so it plays silence /// against a master clock exactly as it did before there were lanes to /// count. Lanes with nothing on them are left out rather than mixed in as /// silence: they would cost a decode thread each and add zero. pub fn audio_segments_from( &self, timeline_frame: u32, fps: f64, ) -> Vec<Vec<(Option<usize>, f64, f64)>> { self.audio_lanes() .into_iter() .map(|lane| self.lane_segments_from(lane, timeline_frame, fps)) .collect() } /// *Which* lanes [`audio_segments_from`](Project::audio_segments_from) /// builds a play list for, in the same order. Public because everything /// asked per audio lane -- which clip a segment's equalizer belongs to, /// which lane an mp4 export would copy -- has to read the very list the /// sound comes off, or the two answers drift apart (which is exactly how an /// `A2`-only project once exported silently, ledger). pub fn audio_lanes(&self) -> Vec<Lane> { let lanes: Vec<Lane> = self .lanes() .into_iter() .filter(|l| l.kind == LaneKind::Audio && !self.lane(*l).is_empty()) .collect(); match lanes.is_empty() { // The all-gap fallback: `A1`'s own (empty) list, so a timeline with // no sound still plays silence against the master clock. true => vec![Lane::A1], false => lanes, } } /// The rate each of [`audio_segments_from`](Project::audio_segments_from)'s /// segments plays at, and how long the timeline gives it: the same lanes in /// the same order, one entry per segment, `None` for a segment at real time /// -- which is every segment of a project nobody has speeded, and the path /// the audio worker leaves bit-identical. /// /// A parallel list for [`audio_eqs_from`](Project::audio_eqs_from)'s reason: /// the segment tuple is what every opener and a dozen tests speak, and a /// rate is not part of *which samples* a segment names. /// /// The seconds are what makes a resample exact: a segment's own window /// resolves to whole source frames, and dividing that by the rate would /// leave each clip a fraction of a frame short or long -- a drift that grows /// clip by clip. Told how much timeline it owes, the worker pads or trims /// the last few samples of each segment instead, so the sound stays locked /// to the picture however many speeded cuts it walks through. pub fn audio_speeds_from(&self, timeline_frame: u32, fps: f64) -> Vec<Vec<Option<Stretch>>> { self.audio_lanes() .into_iter() .map(|lane| self.lane_speeds_from(lane, timeline_frame, fps)) .collect() } /// [`audio_speeds_from`](Project::audio_speeds_from) for one lane, matching /// [`lane_segments_from`](Project::lane_segments_from) entry for entry -- /// same walk, same `fps` refusal. pub fn lane_speeds_from( &self, lane: Lane, timeline_frame: u32, fps: f64, ) -> Vec<Option<Stretch>> { if !(fps.is_finite() && fps > 0.0) { return Vec::new(); } self.spans_from(lane, timeline_frame) .iter() .map(|span| { // A ramp's `step` is its mean rate over the span (the source it reads // over the timeline it fills), the fallback; the stretch follows the // ramp itself through `ramp`. let ramped = span.ramp.is_some(); let step = match span.ramp { Some(_) => f64::from(span.source_len()) / f64::from(span.len), None => span.speed.as_f64(), }; (ramped || !span.speed.is_normal() || span.reverse || span.pitch != 0).then(|| Stretch { step, timeline_secs: f64::from(span.len) / fps, reverse: span.reverse, ramp: span.ramp, fps, pitch: span.pitch, }) }) .collect() } /// The equalizer each of [`audio_segments_from`](Project::audio_segments_from)'s /// segments plays through: the same lanes in the same order, one entry per /// segment, `None` where the segment is a gap or its clip plays flat. /// /// A parallel list rather than a fourth element of the segment tuple: that /// tuple is what every opener, the packet-copy path and a dozen tests speak, /// and an effect is not part of *which samples* a segment names. A short /// list therefore means "flat from here on" rather than a length mismatch -- /// see [`crate::AudioSession::open_mixed_streams_eq`]. pub fn audio_eqs_from(&self, timeline_frame: u32, fps: f64) -> Vec<Vec<Option<EqParams>>> { self.audio_lanes() .into_iter() .map(|lane| self.lane_eqs_from(lane, timeline_frame, fps)) .collect() } /// [`audio_eqs_from`](Project::audio_eqs_from) for one lane, matching /// [`lane_segments_from`](Project::lane_segments_from) entry for entry -- /// same walk, same `fps` refusal, so the two lists cannot come out different /// lengths. pub fn lane_eqs_from( &self, lane: Lane, timeline_frame: u32, fps: f64, ) -> Vec<Option<EqParams>> { if !(fps.is_finite() && fps > 0.0) { return Vec::new(); } self.spans_from(lane, timeline_frame) .iter() // A span off a clip covers that clip's frames and no others, so its // own start names the clip -- the index a [`Span`] does not carry. .map(|span| { self.map(lane, span.start) .and_then(|(idx, _)| self.eq_of(lane, idx).cloned()) }) .collect() } /// The fade envelope each of [`audio_segments_from`](Project::audio_segments_from)'s /// segments plays through: the same lanes in the same order, one entry per /// segment, `None` where the segment is a gap or its clip has neither a /// [`Clip::fade_in`] nor a [`Clip::fade_out`]. Parallel list for /// [`audio_eqs_from`](Project::audio_eqs_from)'s reason. pub fn audio_fades_from(&self, timeline_frame: u32, fps: f64) -> Vec<Vec<Option<Fade>>> { self.audio_lanes() .into_iter() .map(|lane| self.lane_fades_from(lane, timeline_frame, fps)) .collect() } /// [`audio_fades_from`](Project::audio_fades_from) for one lane, matching /// [`lane_segments_from`](Project::lane_segments_from) entry for entry. pub fn lane_fades_from(&self, lane: Lane, timeline_frame: u32, fps: f64) -> Vec<Option<Fade>> { if !(fps.is_finite() && fps > 0.0) { return Vec::new(); } let lane_pan = self.lane_pan(lane); self.spans_from(lane, timeline_frame) .iter() .map(|span| { let (idx, _) = self.map(lane, span.start)?; let clip = self.lane(lane).get(idx)?; (clip.fade_in > 0 || clip.fade_out > 0 || clip.gain_db != 0.0 || clip.pan != 0 || lane_pan != 0.0 || !clip.gain_keys.is_empty()) .then(|| Fade { // `span.len` is the clip's own remaining frames from // `span.start` on -- shorter than `clip.frames()` only // when this is the first span of a seek mid-clip. elapsed: clip.frames().saturating_sub(span.len), fade_in: clip.fade_in, fade_out: clip.fade_out, total: clip.frames(), gain: db_to_linear(clip.gain_db), pan: pan_gains(clip.pan_value(), lane_pan), curve: clip_curve(clip), }) }) .collect() } /// Pushes the undo snapshot. Every mutating method calls this once, *after* /// it has decided it will succeed -- a refusal must not cost an undo step. /// Clears the redo stack: a fresh edit branches off from here, so whatever /// `undo` had left to redo is no longer where this edit leads back to. fn snapshot(&mut self) { self.flat.clear(); if self.batching && self.batch_snapped { // The open batch already holds its one step, taken before its first edit. self.redo.clear(); self.revision = next_revision(); return; } // A no-op left on top is dropped first, so it never stacks as a step of its own, // evicts one, or clears a redo branch it did not displace. self.settle(); let evicted = match !self.hold_cap && self.history.len() == HISTORY_CAP { true => Some(self.history.remove(0)), false => None, }; let redo = std::mem::take(&mut self.redo); self.pending = Some(PendingStep { redo, evicted }); self.pushes += 1; self.history.push(Snapshot { revision: self.revision, label: None, lanes: self.lanes.clone(), subtitles: self.subtitles.clone(), cue_edits: self.cue_edits.clone(), cue_adds: self.cue_adds.clone(), titles: self.titles.clone(), chapters: self.chapters.clone(), markers: self.markers.clone(), settings: self.settings.clone(), sources: self.sources.clone(), bins: self.bins.clone(), seq: self.seq.clone(), }); self.batch_snapped = self.batching; self.revision = next_revision(); } /// Whether the project is exactly the state the newest undo step restores. /// Every field a step holds is compared, the sources in full (their own `==` /// ignores the library's star and bin, which an edit may change). fn matches_newest_step(&self) -> bool { let Some(top) = self.history.last() else { return false; }; let same_sources = top.sources.len() == self.sources.len() && top.sources.iter().zip(&self.sources).all(|(a, b)| { a == b && a.fav == b.fav && a.bin == b.bin }); top.lanes == self.lanes && top.subtitles == self.subtitles && top.cue_edits == self.cue_edits && top.cue_adds == self.cue_adds && top.titles == self.titles && top.chapters == self.chapters && top.markers == self.markers && top.settings == self.settings && same_sources && top.bins == self.bins && top.seq == self.seq } /// Takes back the step this method's own [`Project::snapshot`] pushed, when /// the method refuses after it has already changed things. Unlike /// [`Project::undo`] it does not settle first: a refused edit that changed /// nothing must still take back its own step, not an earlier real one. The /// redo branch the snapshot displaced comes back exactly as it was. fn roll_back_snapshot(&mut self) { let Some(pending) = self.pending.take() else { self.undo(); return; }; let Some(prev) = self.history.pop() else { return; }; self.swap_state(prev); self.redo = pending.redo; if let Some(old) = pending.evicted { self.history.insert(0, old); } self.pushes = self.pushes.saturating_sub(1); self.batch_snapped = false; } /// The pending step, when it is a no-op that [`Project::settle`] would drop. /// Every reader looks through it, so a no-op never shows as a step, a new /// revision or a cleared redo branch, whether or not anything has settled it yet. fn settling_no_op(&self) -> Option<&PendingStep> { self.pending.as_ref().filter(|_| self.matches_newest_step()) } /// Takes back the step the last edit pushed when that edit left the project /// as it was: no undo step, the revision it started from, and the redo branch /// it displaced put back. Nothing is dropped unless the step is the newest /// one this project pushed and the state matches it exactly. Outside a batch /// the step is then settled, so a later edit cannot pick it up. Every snapshot, /// undo and redo settles first, so a no-op is gone before anything builds on it. pub(crate) fn settle(&mut self) { if self.pending.is_some() && self.matches_newest_step() { let (Some(prev), Some(pending)) = (self.history.pop(), self.pending.take()) else { return; }; self.revision = prev.revision; self.redo = pending.redo; if let Some(old) = pending.evicted { self.history.insert(0, old); } self.pushes = self.pushes.saturating_sub(1); self.batch_snapped = false; self.flat.clear(); } if !self.batching { self.pending = None; } } /// Starts a batch: until [`Project::end_batch`], every edit shares one undo /// step, the snapshot taken before the batch's first edit. Idempotent while open. pub fn begin_batch(&mut self) { if !self.batching { self.batching = true; self.batch_snapped = false; } } /// Ends the batch [`Project::begin_batch`] opened. The next edit takes its own step. pub fn end_batch(&mut self) { if self.batching { self.settle(); } self.batching = false; self.batch_snapped = false; self.pending = None; } fn new_link(&mut self) -> u32 { let id = self.next_link; self.next_link = self.next_link.saturating_add(1); id } } /// The handle of every lane, in storage order -- the one definition of what /// [`Lane::ord`] counts. fn handles(lanes: &[LaneData]) -> Vec<Lane> { let (mut video, mut audio, mut subtitle) = (0, 0, 0); lanes .iter() .map(|l| match l.kind { LaneKind::Video => { video += 1; Lane::new(LaneKind::Video, video - 1) } LaneKind::Audio => { audio += 1; Lane::new(LaneKind::Audio, audio - 1) } LaneKind::Subtitle => { subtitle += 1; Lane::new(LaneKind::Subtitle, subtitle - 1) } }) .collect() } /// The one law a caption re-time scales by: frame `f` moves about the held /// clip's own start by the held clip's old-to-new rate ratio. [`write_speed`] /// commits it, [`Project::speeded_playhead`] previews it -- both build their /// [`TimelineMap`] piece from this single answer so the two cannot drift /// apart by a frame the way they once did. fn scaled_caption_frame(held_start: u32, held_old: f64, new: f64, f: u32) -> u32 { (f64::from(held_start) + (f64::from(f) - f64::from(held_start)) * held_old / new) .round() .clamp(0., f64::from(u32::MAX)) as u32 } /// Where in the source a clip's `clip_frame`th timeline frame plays, fractional: /// its in-point plus the speed's integral up to that frame. The one place the two /// frame spaces meet, so a speed cannot be applied twice or forgotten in one of /// them. At a constant speed the position is whole (the floored /// [`Speed::source_at`]), so `floor` of it is that path's frame exactly; with a /// ramp ([`Clip::speed_keys`]) it is the continuous integral and the frame is its /// floor. pub fn source_pos(clip: &Clip, clip_frame: u32) -> f64 { let offset = if clip.speed_keys.is_empty() { f64::from(clip.speed.source_at(clip_frame)) } else { ramp_source_at(&clip.speed_keys, f64::from(clip_frame)) }; match clip.reversed { // Walked from the window's last frame down. The whole frames travelled are // what is mirrored (`out - 1 - floor`), so a ramp's fraction cannot lift the // floor of the frame shown, and the first frame is the last one of the window. true => f64::from(clip.out_frame - 1) - offset.floor() + offset.fract(), false => f64::from(clip.in_frame) + offset, } } /// Where the source-keyed things of a clip (its placement and volume keys) are read /// at `timeline_frame`: the playing source position, fractional. At a constant /// speed this is its own expression, so an unramped, unreversed clip is bit-identical /// to what it was before there were ramps. /// The source position a key is sampled at: [`key_pos`] rounded to a whole frame, /// the frame [`Project::key_source_frame`] writes a key at. A key is read back /// exactly where it was written; sampling the fraction would blend it with its /// neighbour (a typed 40% on a 1.5x clip reads 41%). pub(crate) fn key_sample_pos(clip: &Clip, timeline_frame: u32) -> f64 { key_pos(clip, timeline_frame).round() } fn key_pos(clip: &Clip, timeline_frame: u32) -> f64 { let offset = timeline_frame.saturating_sub(clip.start); match (clip.speed_keys.is_empty(), clip.reversed) { (true, false) => f64::from(clip.in_frame) + f64::from(offset) * clip.speed.as_f64(), (true, true) => f64::from(clip.out_frame - 1) - f64::from(offset) * clip.speed.as_f64(), (false, false) => source_pos(clip, offset), // Continuous, unlike [`source_pos`]'s floor-exact walk: a key is interpolated, // and a saw-toothed position would make it jump. (false, true) => { f64::from(clip.out_frame - 1) - ramp_source_at(&clip.speed_keys, f64::from(offset)) } } } /// Which source frame a clip plays at `timeline_frame` -- [`source_pos`] floored. /// Clamped inside the clip's own range: rounding at a slow rate can put the last /// timeline frame of a clip a frame past its out-point, and a source frame outside /// the range is one the next clip owns. fn source_frame(c: &Clip, timeline_frame: u32) -> u32 { (source_pos(c, timeline_frame - c.start).floor() as u32).clamp(c.in_frame, c.out_frame - 1) } /// The span a clip contributes from `timeline_frame` on: its speed field, and its ramp /// when it has one, based at the span's own start. pub(crate) fn span_speed_of(c: &Clip, timeline_frame: u32) -> (Speed, Option<SpeedRamp>) { if c.speed_keys.is_empty() { (c.speed, None) } else { let base = timeline_frame - c.start; (Speed::NORMAL, Some(SpeedRamp::new(&c.speed_keys, base))) } } /// The tiny interface `Clip` and `SubClip` share for gap maths. Keeping the gap /// finder generic makes the subtitle path use the same bounded-hole rules as /// media lanes without teaching the media-specific take logic about captions. trait GapPlacement { fn start_frame(&self) -> u32; fn end_frame(&self) -> u32; } impl GapPlacement for Clip { fn start_frame(&self) -> u32 { self.start } fn end_frame(&self) -> u32 { self.end() } } impl GapPlacement for SubClip { fn start_frame(&self) -> u32 { self.start } fn end_frame(&self) -> u32 { self.end() } } /// Index of the placement covering `frame`, or `None` for a gap or past the /// end. Binary search: the sorted-disjoint invariant is what makes it legal. fn at<T: GapPlacement>(items: &[T], frame: u32) -> Option<usize> { let idx = items .partition_point(|item| item.start_frame() <= frame) .checked_sub(1)?; (frame < items[idx].end_frame()).then_some(idx) } /// The empty stretch of `items` covering `frame`, as `(start, len)` -- `None` /// when `frame` is inside a placement, or past the last one. Sorted-disjoint /// invariant like [`at`]: the first placement whose start is past `frame` is /// the far wall, and the last one that ends at or before `frame` (or the /// timeline's own head, `0`, when there is none) is the near one. fn gap<T: GapPlacement>(items: &[T], frame: u32) -> Option<(u32, u32)> { if at(items, frame).is_some() { return None; } let idx = items.partition_point(|item| item.start_frame() <= frame); let next = items.get(idx)?; let start = idx.checked_sub(1).map_or(0, |i| items[i].end_frame()); Some((start, next.start_frame() - start)) } /// Every bounded hole in `items`, left to right. The tail after the last /// placement is deliberately absent: there is no later placement for a ripple /// to bring back. fn gaps<T: GapPlacement>(items: &[T]) -> Vec<(u32, u32)> { let mut out = Vec::new(); let mut next = 0; for item in items { if item.start_frame() > next { out.push((next, item.start_frame() - next)); } next = item.end_frame(); } out } /// The clip's gain envelope in clip-relative timeline frames, or `None` when it /// has no points. Source frame `s` is reached `(s - in_frame) / speed` timeline /// frames in, which is exact because a speed is one linear rate. fn clip_curve(clip: &Clip) -> Option<std::sync::Arc<[(f64, f32, KeyEase)]>> { if clip.gain_keys.is_empty() { return None; } let step = clip.speed.as_f64(); let base = f64::from(clip.in_frame); let mut curve: Vec<(f64, f32, KeyEase)> = match clip.reversed { // The far end of the window is the clip's head, so the points are walked from // there and come out in time order the other way about. true => { let top = f64::from(clip.out_frame - 1); clip.gain_keys .iter() .enumerate() .rev() .map(|(n, k)| { // The segment that arrived at a point is the one that now leaves it. let ease = if n > 0 { clip.gain_keys[n - 1].ease } else { k.ease }; ((top - f64::from(k.at)) / step, k.db, ease) }) .collect() } false => clip .gain_keys .iter() .map(|k| ((f64::from(k.at) - base) / step, k.db, k.ease)) .collect(), }; curve.shrink_to_fit(); Some(curve.into()) } /// `keys` walked from the far end of a window whose last frame is `top`, in the /// order a reversed clip plays them: each point at source frame `p` moves to /// `top - p`, and each segment keeps the easing it had, now leaving the point at /// its other end. A point the mirror would put before frame 0 is dropped -- it lay /// far outside the window, where nothing reads it. fn mirrored<K: Clone>( keys: &[K], top: i64, at: impl Fn(&K) -> u32, ease: impl Fn(&K) -> KeyEase, with: impl Fn(&K, u32, KeyEase) -> K, ) -> Vec<K> { keys.iter() .enumerate() .rev() .filter_map(|(n, k)| { // The segment that arrived at this point is the one that now leaves it. let e = if n > 0 { ease(&keys[n - 1]) } else { ease(k) }; Some(with(k, u32::try_from(top - i64::from(at(k))).ok()?, e)) }) .collect() } /// Re-keys `c`'s source-keyed animation after its direction flipped: each point at /// source frame `p` moves to `in + out - 1 - p`, the same footage frame seen from the /// other end of the window, so the point stays on the timeline frame it was on. fn mirror_keys(c: &mut Clip) { let top = i64::from(c.in_frame) + i64::from(c.out_frame) - 1; c.gain_keys = mirrored(&c.gain_keys, top, |k| k.at, |k| k.ease, |k, at, ease| GainKey { at, ease, ..*k }); c.transform_keys = mirrored(&c.transform_keys, top, |k| k.at, |k| k.ease, |k, at, ease| TransformKey { at, ease, ..*k }); c.opacity_keys = mirrored(&c.opacity_keys, top, |k| k.at, |k| k.ease, |k, at, ease| OpacityKey { at, ease, ..*k }); c.color_keys = mirrored(&c.color_keys, top, |k| k.at, |k| k.ease, |k, at, ease| ColorKey { at, ease, ..*k }); for e in &mut c.effects { e.keys = mirrored(&e.keys, top, |k| k.at, |k| k.ease, |k, at, ease| crate::effects::EffectKey { at, ease, ..*k }); } } /// Index of the clip `frame` falls *strictly inside*, i.e. the one a split /// there would cut in two. A placement's own first frame is not inside it. fn splittable(clips: &[Clip], frame: u32) -> Option<usize> { let idx = at(clips, frame)?; // ...and, on a speeded clip, one its rate can actually address // ([`Speed::split_at`]): a cut between two showings of one source frame // would leave halves that no longer add up to the clip that was cut. (frame > clips[idx].start) .then_some(idx) .filter(|&idx| split_source(&clips[idx], frame).is_some()) } /// Where in the source a split of `c` at `frame` cuts, or `None` when the clip's /// rate cannot address that frame. fn split_source(c: &Clip, frame: u32) -> Option<u32> { if !c.speed_keys.is_empty() { // A reversed ramp is not cut yet: its far end would need the ramp mirrored. if c.reversed { return None; } return ramp_split(&c.speed_keys, c.len(), frame - c.start).map(|(src, _, _)| c.in_frame + src); } c.speed.split_at(c.len(), frame - c.start).map(|src| match c.reversed { // The head of a reversed clip is the window's far end, so the cut is that // far down from the out-point. true => c.out_frame - src, false => c.in_frame + src, }) } /// The speed keys each half of `c` keeps when it is cut at `frame`. Both empty for a /// constant rate, which keeps its own [`Speed`] on both halves. fn split_keys(c: &Clip, frame: u32) -> (Vec<SpeedKey>, Vec<SpeedKey>) { if c.speed_keys.is_empty() { return (Vec::new(), Vec::new()); } match ramp_split(&c.speed_keys, c.len(), frame - c.start) { Some((_, head, tail)) => (head, tail), None => (Vec::new(), Vec::new()), } } /// Where a ramped clip of `src_len` source frames can be cut at its clip-relative timeline /// frame `off`, and the keys each half keeps. The source frame the tail starts at is /// relative to the clip's in-point. `None` where the cut would not add up in whole frames /// on both halves -- the ramp's own version of what [`Speed::split_at`] asks of a constant /// rate. Reads at which the ramp lands near a frame boundary answer; the others do not. pub fn ramp_split(keys: &[SpeedKey], src_len: u32, off: u32) -> Option<(u32, Vec<SpeedKey>, Vec<SpeedKey>)> { let len = ramp_frames(keys, src_len); if off == 0 || off >= len { return None; } let src = ramp_source_at(keys, f64::from(off)).round(); if src < 1. || src >= f64::from(src_len) { return None; } let src = src as u32; let head = ramp_keys_until(keys, off); let tail = ramp_keys_from(keys, off); let whole = valid_speed_keys(&head) && valid_speed_keys(&tail) && ramp_frames(&head, src) == off && ramp_frames(&tail, src_len - src) == len - off; whole.then_some((src, head, tail)) } /// The keys of the part of a ramped clip up to clip-relative frame `cut`: the keys before /// it, and the speed there as the last one when the ramp goes on past it. The ramp is held /// to `cut` exactly as the whole ramp plays there, so the head's length comes back as `cut`. pub fn ramp_keys_until(keys: &[SpeedKey], cut: u32) -> Vec<SpeedKey> { let mut out: Vec<SpeedKey> = keys.iter().copied().filter(|k| k.at < cut).collect(); if keys.iter().any(|k| k.at > cut) { out.push(SpeedKey { at: cut, speed: ramp_speed_at(keys, cut) }); } out } /// Index of the first of the two clips a [`Project::regroup`] at `frame` would /// rejoin: they must touch there, and be consecutive frames of one source. fn joinable(clips: &[Clip], frame: u32) -> Option<usize> { let idx = clips.iter().position(|c| c.end() == frame)?; let next = clips.get(idx + 1)?; let joined = clips[idx].len() + next.len(); (next.start == frame && next.source == clips[idx].source && next.reversed == clips[idx].reversed && match clips[idx].reversed { // The halves of a reversed cut swap sides: the head holds the window's // upper part. true => clips[idx].in_frame == next.out_frame, false => next.in_frame == clips[idx].out_frame, } // ...at one rate, and one that puts the rejoined clip back in exactly // the room the two took up: what a split could have produced, which is // all this undoes. && next.speed == clips[idx].speed && clips[idx].speed.frames(joined) == clips[idx].frames() + next.frames()) .then_some(idx) } /// The lane invariant: sorted by `start`, no two placements overlapping, no /// empty placement. Checked at every constructor and asserted after every /// mutation -- the offset model is only tractable while it holds. /// The one thing an equalizer setting may not be: a value the text format /// cannot write and read back as itself. Checked wherever params come in, so /// what an edit produces is always what a save writes and a load reads. fn finite(params: &EqParams) -> bool { params .bands .iter() .all(|b| b.freq_hz.is_finite() && b.gain_db.is_finite() && b.q.is_finite()) } /// The same for a colour grade: [`crate::color`] itself falls back to identity /// on a non-finite value, but a value that cannot be written and read back as /// itself has no business reaching the model. fn color_finite(p: &ColorParams) -> bool { let w = &p.look.wheels; w.lift.iter().chain(&w.gamma).chain(&w.gain).all(|v| v.is_finite()) && p.look.lut_mix.is_finite() && p.brightness.is_finite() && p.contrast.is_finite() && p.saturation.is_finite() && p.tint.is_finite() } /// The same for a transform: [`crate::scale`] clamps a non-finite value to /// identity at read time, but a value that cannot round-trip has no business /// reaching the model. fn transform_finite(p: &TransformParams) -> bool { p.pos_x.is_finite() && p.pos_y.is_finite() && p.scale.is_finite() && p.rotate.is_finite() && p.crop_l.is_finite() && p.crop_r.is_finite() && p.crop_t.is_finite() && p.crop_b.is_finite() && (0.0..=1.0).contains(&p.opacity) } fn sorted_disjoint(clips: &[Clip]) -> bool { clips.iter().all(|c| c.out_frame > c.in_frame) && clips.windows(2).all(|w| w[0].end() <= w[1].start) } /// The very same invariant on a subtitle lane, in that lane's own two units: /// sorted by `start`, no two placements overlapping, no empty placement at /// either end. Asserted after every mutation, exactly as its twin is. fn subs_sorted_disjoint(subs: &[SubClip]) -> bool { subs.iter() .all(|s| s.frames >= 1 && s.out_us > s.in_us && s.in_us >= 0) && subs.windows(2).all(|w| w[0].end() <= w[1].start) } /// What `frames` timeline frames are worth in microseconds at `fps`, signed -- /// the one conversion between a lane's clock and a cue's, and `0` for a rate /// that is not one (every caller of this has already refused such a rate). fn us_of(frames: i64, fps: f64) -> i64 { match fps.is_finite() && fps > 0.0 { true => ((frames as f64) / fps * 1e6).round() as i64, false => 0, } } /// **The one function in this file that writes a caption's window.** Every /// composer -- a trim, a split, a ripple, a group follow, a rejoin -- routes /// through it, and the invariant it holds is total: /// /// ```text /// 0 <= in_us and in_us < out_us and out_us - in_us <= MAX_WINDOW_US /// ``` /// /// which is the never-empty invariant plus the width ceiling, restated as one /// shape both ends of it come from. Note that the ceiling bounds a /// **difference**, which is why a per-arm check is not enough and this is: /// /// - Growing `out_us` (a tail trim) makes the width larger, so the *upper* /// wall bites. /// - Falling `in_us` (a head trim) makes the width larger too, from the other /// side -- so bounding `in_us` alone, which is what a head trim's own clamp /// (`in_us <= out_us - 1`) does, says nothing at all about the ceiling. /// - A rejoin takes the left half's `in_us` and the right half's `out_us`, and /// the two halves are consecutive in the *source* (that is what /// [`sub_joinable`] requires), so the joined width is the **sum** of two /// legal widths. Each half passes its own door; the whole does not. /// /// Three rounds of review each found one more composer this way, so the /// function is the fix rather than any single call site: clamping a new /// caller into shape is a two-line change, and forgetting one is silent. /// `every_window_write_goes_through_set_window` (in the tests below) is the /// other half of that promise -- it scans the file and fails if a write /// appears anywhere else. /// /// **Why it clamps where the doors refuse.** The doors -- the parser, /// [`Project::with_subs`], [`Project::place_sub`] -- see a value on the way /// IN, from a file or a foreign caller, where nobody was mid-gesture, so a /// value that wide is named and refused rather than quietly changed. Every /// composer below is an edit a person is watching happen, so the same value is /// clamped to the end of the window: a drag that stops, exactly like the frame /// walls these trims already apply. fn set_window(s: &mut SubClip, in_us: i64, out_us: i64) { let in_us = in_us.max(0); // The `+ 1` is the never-empty invariant: the window is open, always, so a // composer that would otherwise clamp it shut lands on one microsecond. let out_us = out_us.clamp(in_us + 1, in_us.saturating_add(MAX_WINDOW_US)); s.in_us = in_us; s.out_us = out_us; debug_assert!( s.in_us >= 0 && s.out_us > s.in_us && s.out_us - s.in_us <= MAX_WINDOW_US, "set_window left an illegal window: {:?}", (s.in_us, s.out_us) ); } /// The two pieces a cue becomes when a window edge at `t_us` falls inside it: /// `(start, end, text)` for the piece before the edge and the one after, the /// words shared between them at the word boundary nearest the point that is the /// same fraction of the words as `t_us` is of the time. A piece with no words is /// left out. A cue with one word has no boundary to divide at, so that word /// stays on the longer side of the edge (ties: the piece before it) and the /// other side is left out too. `None` for a cue with no words at all. fn split_words(c: &crate::subtitle::Cue, t_us: i64) -> Option<Vec<(i64, i64, String)>> { let text = c.text.as_str(); let mut starts: Vec<usize> = Vec::new(); let mut prev_space = true; for (i, ch) in text.char_indices() { let space = ch.is_whitespace(); if !space && prev_space { starts.push(i); } prev_space = space; } let n = starts.len(); if n == 0 { return None; } if n == 1 { let word = text.trim().to_string(); let before = t_us - c.start_us; let after = c.end_us - t_us; return Some(if before >= after { vec![(c.start_us, t_us, word)] } else { vec![(t_us, c.end_us, word)] }); } let p = (t_us - c.start_us) as f64 / (c.end_us - c.start_us) as f64; let k = ((p * n as f64).round() as usize).min(n); let mut out = Vec::with_capacity(2); if k >= 1 { let end = if k < n { starts[k] } else { text.len() }; out.push((c.start_us, t_us, text[..end].trim_end().to_string())); } if k < n { out.push((t_us, c.end_us, text[starts[k]..].to_string())); } Some(out) } /// [`split_words`] for one edge of a cut. With `both_sides` a one-word cue is /// kept whole on both sides of the point, so the side the cut leaves in its /// window survives: which side that is, only the ripple knows, and /// [`Project::drop_unplaced`] drops the other. Every other cue is /// [`split_words`]' division. fn cut_pieces(c: &crate::subtitle::Cue, t_us: i64, both_sides: bool) -> Option<Vec<(i64, i64, String)>> { if both_sides && c.text.split_whitespace().count() == 1 { let word = c.text.trim().to_string(); return Some(vec![(c.start_us, t_us, word.clone()), (t_us, c.end_us, word)]); } split_words(c, t_us) } /// [`clear`] for a subtitle lane: removes the timeline frames `[start, end)`, /// dropping what is inside the hole, splitting what straddles it and trimming /// what overlaps an edge. The windows follow the cut by proportion /// ([`SubClip::window_at`]), so what is left says exactly the words it said. fn sub_clear(subs: &mut Vec<SubClip>, start: u32, end: u32) { sub_clear_with(subs, start, end, &mut |_| (None, None)); } /// [`sub_clear`] with the (head, tail) links its pieces carry, as [`clear_with`] has for clips. fn sub_clear_with(subs: &mut Vec<SubClip>, start: u32, end: u32, links: &mut dyn FnMut(Option<u32>) -> (Option<u32>, Option<u32>)) { let mut out = Vec::with_capacity(subs.len() + 1); for s in subs.drain(..) { if s.end() <= start || s.start >= end { out.push(s); continue; } if s.start < start { let cut = s.window_at(start); let (head_link, _) = links(s.link); let mut head = SubClip { frames: start - s.start, link: head_link, ..s }; // `window_at` already answers inside the window, and `set_window` // is the shape that says so once, for every composer, rather than // leaving each of them to re-derive it. set_window(&mut head, s.in_us, cut); out.push(head); } if s.end() > end { let cut = s.window_at(end); let (_, tail_link) = links(s.link); let mut tail = SubClip { start: end, frames: s.end() - end, link: tail_link, ..s }; set_window(&mut tail, cut, s.out_us); out.push(tail); } } *subs = out; } /// [`open_room`] for a subtitle lane: slides everything from `at` on later by /// `len`, splitting a placement that straddles `at` so the two halves end up on /// either side of the hole. A `len` of 0 is that split alone, which is what a /// [`Project::split`] over a subtitle lane is. fn sub_open_room(subs: &mut Vec<SubClip>, at: u32, len: u32) { if let Some(idx) = subs.iter().position(|s| s.start < at && at < s.end()) { let s = subs[idx]; let cut = s.window_at(at); let mut head = SubClip { frames: at - s.start, link: None, ..s }; set_window(&mut head, s.in_us, cut); subs[idx] = head; let mut tail = SubClip { start: at, frames: s.end() - at, link: None, ..s }; set_window(&mut tail, cut, s.out_us); subs.insert(idx + 1, tail); } for s in subs.iter_mut().filter(|s| s.start >= at) { s.start += len; } } /// [`joinable`] for a subtitle lane: the first of the two placements a /// [`Project::regroup`] at `frame` would rejoin. What a split could have /// produced and nothing else -- they touch on the timeline, they read the same /// track, and the second one's window carries on where the first's stopped. fn sub_joinable(subs: &[SubClip], frame: u32) -> Option<usize> { let idx = subs.iter().position(|s| s.end() == frame)?; let next = subs.get(idx + 1)?; (next.start == frame && next.track == subs[idx].track && next.in_us == subs[idx].out_us) .then_some(idx) } /// How far one `edge` of the caption at `idx` may travel against its own lane's /// walls alone: the frame arithmetic of [`Project::trim_sub_room`] without the /// rate, which a caption *following* a group's trim does not have to be exact /// about -- its own walls, its own one surviving frame, and nothing that needs /// a clock. fn sub_edge_room(subs: &[SubClip], idx: usize, edge: Edge) -> (u32, u32) { let s = &subs[idx]; match edge { Edge::Start => (idx.checked_sub(1).map_or(0, |p| subs[p].end()), s.end() - 1), Edge::End => (s.start + 1, subs.get(idx + 1).map_or(u32::MAX, |n| n.start)), } } /// Move one `edge` of `s` to timeline frame `to`, its window following by the /// placement's own proportion -- [`SubClip::window_at`]'s arithmetic, run /// forwards and backwards. The caption a group's trim is carrying has no rate /// of its own and needs none: what one frame of it is worth in microseconds is /// the window it already shows over the frames it already takes. fn write_sub_edge(s: &mut SubClip, edge: Edge, to: u32) { let per_frame = s.window_us() / i64::from(s.frames.max(1)); // The `saturating_mul` keeps a `SubClip` that reached here without a door // behind it from wrapping the window negative; `set_window` is what keeps // the result legal rather than merely finite. This is the group-follow twin // of [`Project::trim_sub`], and it reaches the same field from both edges -- // so a fix that only walled the primary trim's End arm left the head (and // the whole group path) open. let by = |from: u32, to: u32| per_frame.saturating_mul(i64::from(to) - i64::from(from)); match edge { Edge::Start => { set_window(s, s.in_us.saturating_add(by(s.start, to)), s.out_us); s.frames = s.end() - to; s.start = to; } Edge::End => { set_window(s, s.in_us, s.out_us.saturating_add(by(s.end(), to))); s.frames = to - s.start; } } debug_assert!(s.frames >= 1 && s.out_us > s.in_us && s.in_us >= 0); } /// The grouping invariant, checked in release at [`Project::from_parts`] and /// asserted by the tests after every edit: a link id names **at most one** /// placement per lane -- a clip on a media lane or a caption on a subtitle /// one, whichever carries it. That is all "these move together" can mean now /// that a group is assembled by hand: the members keep their own spans and /// their own offsets, and what binds them is the id alone. /// /// Deliberately *not* a pairing of two lanes, and never a span rule: with N /// lanes a group may run on `V1`, `A1` and a subtitle lane at once, and two /// placements of one group may cover different frames -- the offset-preserving /// group a hand builds with [`Project::group_all`], which a drag moves by one /// delta rather than by one span. /// /// A link no other lane carries is legal and is not an error: lifting one half /// of a group ([`Project::lift`]) leaves exactly that, and a save of that /// timeline has to load again. /// [`links_are_consistent`](self::links_are_consistent) for a whole project, /// asked by the sweep suite's universal law. #[cfg(test)] pub(crate) fn links_are_consistent_pub(p: &Project) -> crate::Result<()> { links_are_consistent(&p.lanes) } /// What a take is, for [`Project::ungroup`]: the halves of one take read the /// same source over the same frames, so they share these four numbers. type TakeKey = (usize, u32, u32, u32); fn take_key(c: &Clip) -> TakeKey { (c.source, c.start, c.in_frame, c.out_frame) } fn links_are_consistent(lanes: &[LaneData]) -> crate::Result<()> { let handles = handles(lanes); for (data, lane) in lanes.iter().zip(&handles) { for (i, c) in data.clips.iter().enumerate() { let Some(id) = c.link else { continue }; if data.clips[..i].iter().any(|prev| prev.link == Some(id)) { return Err( format!("link {id} names two clips in the {} lane", lane.label()).into(), ); } } // ...and the same for the captions, in their own list on the same lane: // a subtitle lane holds no `Clip`, so this is the only list a caption's // id could be doubled on. for (i, s) in data.subs.iter().enumerate() { let Some(id) = s.link else { continue }; if data.subs[..i].iter().any(|prev| prev.link == Some(id)) { return Err( format!("link {id} names two captions in the {} lane", lane.label()).into(), ); } } } Ok(()) } /// Removes the timeline frames `[start, end)` from `clips`: placements inside /// it go, one straddling it is split in two, one overlapping an edge is /// trimmed. Source frames follow the trim, so what is left plays what it always /// played. Leaves the lane sorted and disjoint. /// /// A piece the hole cut is no longer the take its group id names -- and both /// halves of a straddled placement carrying that id would be the same id twice /// in one lane -- so what is cut loses its link (see [`links_are_consistent`]). fn clear(clips: &mut Vec<Clip>, start: u32, end: u32) { clear_with(clips, start, end, &mut |_| (None, None)); } /// [`clear`] with the links the pieces it leaves carry: `links` gives the (head, tail) ids of a clip /// that straddles the hole. `clear` itself gives both none, the detach rule of a delete. fn clear_with(clips: &mut Vec<Clip>, start: u32, end: u32, links: &mut dyn FnMut(Option<u32>) -> (Option<u32>, Option<u32>)) { let mut out = Vec::with_capacity(clips.len() + 1); for c in clips.drain(..) { // Disjoint from the hole: untouched. if c.end() <= start || c.start >= end { out.push(c); continue; } // The head that survives in front of the hole -- as much source as fits // in the room in front of it at this clip's own rate ([`Speed::fit`]), // which at real time is that room itself. Never *more*: a head that // outgrew its room would reach into the hole it is being cut out of, // which is exactly the overlap a slow clip punched at a frame its rate // cannot address used to leave (at 0.25x a single source frame is four // timeline frames, and three frames of room hold none of it). `None` // there means no remainder at all, and the piece simply goes. if let Some(keep) = c.fit_source(start.saturating_sub(c.start)) && c.start < start { let (head, _) = links(c.link); let mut piece = Clip { fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), link: head, ..c.clone() }; // What survives in front plays the window's far end when it is reversed. match c.reversed { true => piece.in_frame = c.out_frame - keep.min(c.len()), false => piece.out_frame = c.in_frame + keep.min(c.len()), } out.push(piece); } // ...and the tail behind it, which keeps reading up to where it would // have: measured from its out-point for the head's reason, so what it // occupies still ends where the whole clip did -- and dropped whole on // the same `None`. // A ramp's tail restarts at `end`, so its keys are rebased to that frame // ([`ramp_keys_from`]) and its room is measured with them. let tail_keys = if c.speed_keys.is_empty() { Vec::new() } else { ramp_keys_from(&c.speed_keys, end.saturating_sub(c.start)) }; let tail_probe = Clip { speed_keys: tail_keys.clone(), ..c.clone() }; if let Some(keep) = tail_probe.fit_source(c.end().saturating_sub(end)) && c.end() > end { let (_, tail) = links(c.link); let mut piece = Clip { fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), start: end, link: tail, speed_keys: tail_keys, ..c.clone() }; match c.reversed { true => piece.out_frame = c.in_frame + keep.min(c.len()), false => piece.in_frame = c.out_frame - keep.min(c.len()), } out.push(piece); } } *clips = out; } /// Cuts `[at, at + len)` out of the lanes at indices `on` and slides what is /// behind it back -- the body of [`Project::ripple_delete`] (which passes every /// lane) and of every cut a batch ([`Project::cut_regions`]) makes on the lanes /// it was scoped to. No snapshot of its own: whose undo step this is belongs to /// the caller. fn ripple(lanes: &mut [LaneData], on: &[usize], at: u32, len: u32) { for &i in on { let clips = &mut lanes[i].clips; clear(clips, at, at + len); for c in clips.iter_mut().filter(|c| c.start >= at) { c.start -= len; } debug_assert!(sorted_disjoint(clips)); // The subtitle lanes close the hole with everything else: a caption // left standing where the picture it belongs to was cut out is the // desync a lane model exists to make impossible. let subs = &mut lanes[i].subs; sub_clear(subs, at, at + len); for s in subs.iter_mut().filter(|s| s.start >= at) { s.start -= len; } debug_assert!(subs_sorted_disjoint(subs)); } } /// Moves every clip and caption on `lane` that starts at or after `from` by `delta` frames: the /// second half of a ripple trim ([`Project::ripple_trim`]), which closes or opens the hole an edge /// left. No snapshot of its own. fn shift_behind(lane: &mut LaneData, from: u32, delta: i64) { let step = |s: u32| (i64::from(s) + delta) as u32; for c in lane.clips.iter_mut().filter(|c| c.start >= from) { c.start = step(c.start); } debug_assert!(sorted_disjoint(&lane.clips)); for s in lane.subs.iter_mut().filter(|s| s.start >= from) { s.start = step(s.start); } debug_assert!(subs_sorted_disjoint(&lane.subs)); } /// [`ripple`] for a cut that keeps its takes together: every take it splits keeps its id on the /// piece before the cut, and the pieces after it share one fresh id per take, as the razor's /// split does ([`Project::split_on`]). Only [`Project::cut_regions`] calls it, and that refuses /// any scope that would take one half of a take with it, so every take it splits is whole in scope. fn ripple_keeping_takes(lanes: &mut [LaneData], on: &[usize], at: u32, len: u32, next_link: &mut u32) { let mut tails: Vec<(u32, u32)> = Vec::new(); let mut links = |old: Option<u32>| -> (Option<u32>, Option<u32>) { let Some(id) = old else { return (None, None) }; let tail = match tails.iter().find(|(o, _)| *o == id) { Some(&(_, t)) => t, None => { let t = *next_link; *next_link = next_link.saturating_add(1); tails.push((id, t)); t } }; (Some(id), Some(tail)) }; for &i in on { let clips = &mut lanes[i].clips; clear_with(clips, at, at + len, &mut links); for c in clips.iter_mut().filter(|c| c.start >= at) { c.start -= len; } debug_assert!(sorted_disjoint(clips)); let subs = &mut lanes[i].subs; sub_clear_with(subs, at, at + len, &mut links); for s in subs.iter_mut().filter(|s| s.start >= at) { s.start -= len; } debug_assert!(subs_sorted_disjoint(subs)); } } /// Every lane, as the index list the two above take: what "the whole timeline" /// is spelled as, and the scope [`Project::ripple_delete`] and /// [`Project::split`] have always had. fn all_lanes(lanes: &[LaneData]) -> Vec<usize> { (0..lanes.len()).collect() } /// A caller's region list as a batch may use it: sorted by start, empty entries /// dropped, and touching or overlapping entries merged into one. /// /// Overlapping regions are the one thing a batch cannot take -- cutting the /// second would cut frames the first already moved -- and a detector handing /// back two that touch means one silence, not two cuts. Merged rather than /// refused: this is arithmetic on a preview, not a user's mistake. fn tidy(regions: &[(u32, u32)]) -> Vec<(u32, u32)> { let mut sorted: Vec<(u32, u32)> = regions.iter().copied().filter(|&(_, l)| l > 0).collect(); sorted.sort_by_key(|&(at, _)| at); let mut out: Vec<(u32, u32)> = Vec::new(); for (at, len) in sorted { let end = at.saturating_add(len); match out.last_mut() { Some(last) if at <= last.0 + last.1 => last.1 = end.saturating_sub(last.0).max(last.1), _ => out.push((at, len)), } } out } /// Slides everything from `at` on later by `len`, splitting a placement that /// straddles `at` so the two halves end up on either side of the hole. The /// insert half of a rippling paste. The two halves lose the group id for the /// reason [`clear`] gives. fn open_room(clips: &mut Vec<Clip>, at: u32, len: u32) { if let Some(idx) = splittable(clips, at) { let mut tail = clips[idx].clone(); // The cut in *source* frames, at the clip's own rate. `splittable` has // already refused a frame the rate cannot address, which is why // [`Project::paste`] refuses one too rather than opening room inside a // clip that cannot be cut there. let cut_at = split_source(&tail, at).expect("splittable said so"); let (head_speed, tail_speed) = split_keys(&tail, at); tail.speed_keys = tail_speed; clips[idx].speed_keys = head_speed; match tail.reversed { true => { tail.out_frame = cut_at; clips[idx].in_frame = cut_at; } false => { tail.in_frame = cut_at; clips[idx].out_frame = cut_at; } } tail.start = at; tail.link = None; clips[idx].link = None; clips.insert(idx + 1, tail); } for c in clips.iter_mut().filter(|c| c.start >= at) { c.start += len; } } /// Absolute, symlink-resolved when the file is reachable; the path as given /// when it is not -- an unreadable path still deserves an index, it simply /// dedups by spelling. pub(crate) fn canonical(path: &Path) -> PathBuf { std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()) } #[cfg(test)] mod tests { use super::*; const FPS: f64 = 30.0; /// A caption track that ends between two frames places with a window of /// whole frames: 4.750 s at 30 fps is 143 frames, and its cues keep their /// times (0.5 s is frame 15, 4.5 s is frame 135) instead of being stretched /// by 143 / 142.5 as they were when the window was the track's own length. #[test] fn a_whole_track_drop_is_not_stretched_by_its_last_frame() { let sub = SubClip::whole_track(2, 4_750_000, FPS); assert_eq!((sub.track, sub.start, sub.in_us), (2, 0, 0)); assert_eq!(sub.frames, 143, "the fewest frames that hold the last cue"); assert_eq!(sub.out_us, 4_766_667, "the window is the frames' own length"); let frame_of = |us: i64| f64::from(sub.frames) * us as f64 / sub.window_us() as f64; for (us, want) in [(500_000i64, 15.0), (750_000, 22.5), (4_500_000, 135.0)] { assert!((frame_of(us) - want).abs() < 1e-3, "cue at {us} us lands on frame {}", frame_of(us)); } // The old window (the track's length) put 4.5 s on 135.47 frames. assert!((4_500_000f64 * 143.0 / 4_750_000.0 - 135.0).abs() > 0.4); // A track that ends on a frame is that many frames, with no extra one. let exact = SubClip::whole_track(0, 1_000_000, FPS); assert_eq!((exact.frames, exact.out_us), (30, 1_000_000)); } /// Never opened -- `Project` is pure data, so a path that does not exist is /// simply a path that dedups by spelling. const FILE: &str = "/nonexistent/a.mp4"; const FILE2: &str = "/nonexistent/b.mp4"; /// A batch is one undo step however many edits it makes, far past the cap; undo takes /// back the whole batch, and an edit after `end_batch` takes its own step. #[test] fn a_batch_is_one_undo_step_past_the_cap() { let mut p = Project::single(FILE, 1); let v = p.add_lane(LaneKind::Video); let base = p.history_len(); p.begin_batch(); for k in 0..150u32 { assert!(p.place(v, k * 3, clip(k * 3, 0, 3, 0))); } p.end_batch(); assert_eq!(p.history_len(), base + 1, "the batch is one step"); assert_eq!(p.lane_clips_pub(v).len(), 150); assert!(p.undo()); assert!(p.lane_clips_pub(v).is_empty(), "undo takes the whole batch back"); assert_eq!(p.history_len(), base); assert!(p.place(v, 0, clip(0, 0, 3, 0))); assert_eq!(p.history_len(), base + 1, "an edit outside a batch is its own step"); } /// Undo inside a batch starts a fresh step for what follows it, so the edits after it /// are not lost from the history. #[test] fn an_undo_inside_a_batch_opens_a_new_step() { let mut p = Project::single(FILE, 1); let v = p.add_lane(LaneKind::Video); let base = p.history_len(); p.begin_batch(); assert!(p.place(v, 0, clip(0, 0, 3, 0))); assert!(p.undo()); assert!(p.place(v, 0, clip(0, 0, 4, 0))); p.end_batch(); assert_eq!(p.history_len(), base + 1); assert!(p.undo()); assert!(p.lane_clips_pub(v).is_empty()); } /// A contiguous placement, group id ignored: what most assertions compare. fn clip(start: u32, in_frame: u32, out_frame: u32, source: usize) -> Clip { Clip { speed_keys: Vec::new(), reversed: false, stabilise: None, deinterlace: Default::default(), mark: Default::default(), angle: Default::default(), fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), visualizer: 0, viz_paint: 0, gain_db: 0.0, gain_keys: Vec::new(), pan: 0, pitch: 0, transform_keys: Vec::new(), opacity_keys: Vec::new(), color_keys: Vec::new(), effects: Vec::new(), start, in_frame, out_frame, source, link: None, eq: None, color: None, transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, } } /// A reversed clip's timeline head is its source out side: a split gives the /// head the late footage and the tail the early, a trim of the head eats the /// source's end, and joining the halves gives the one clip back. #[test] fn a_reversed_clip_splits_trims_and_joins_from_its_out_side() { let mut p = Project::single(FILE, 100); let v = p.add_lane(LaneKind::Video); let mut c = clip(0, 0, 100, 0); c.reversed = true; assert!(p.place(v, 0, c.clone())); assert!(p.split(30)); let halves = p.lane_clips_pub(v).to_vec(); assert_eq!((halves[0].start, halves[0].in_frame, halves[0].out_frame), (0, 70, 100)); assert_eq!((halves[1].start, halves[1].in_frame, halves[1].out_frame), (30, 0, 70)); assert!(halves.iter().all(|h| h.reversed)); assert!(p.regroup(30), "the halves are one reversed run again"); let one = &p.lane_clips_pub(v)[0]; assert_eq!((one.in_frame, one.out_frame, one.reversed), (0, 100, true)); // Trimming the head by 10 frames drops the source's last 10. assert!(p.trim(v, 0, Edge::Start, 10, &[100])); let t = &p.lane_clips_pub(v)[0]; assert_eq!((t.start, t.in_frame, t.out_frame), (10, 0, 90)); } /// Toggling is one undo step, mirrors a key anchored to the footage so the /// timeline behaviour does not change, and refuses a still. #[test] fn set_reverse_is_one_undoable_step_and_mirrors_footage_keys() { let mut p = Project::single(FILE, 100); let v = p.add_lane(LaneKind::Video); assert!(p.place(v, 0, clip(0, 0, 100, 0))); let steps = p.history_len(); p.set_reverse(v, 0, true).expect("reverse"); assert_eq!(p.history_len(), steps + 1); assert!(p.reversed_of(v, 0)); p.set_reverse(v, 0, true).expect("already so is a no-op"); assert_eq!(p.history_len(), steps + 1, "no second step"); assert_eq!(source_pos(&p.lane_clips_pub(v)[0], 0).floor() as u32, 99, "the first timeline frame shows the last footage"); assert!(p.undo()); assert!(!p.reversed_of(v, 0)); assert!(p.set_reverse(v, 7, true).is_err(), "no such clip"); } /// The shape `captions_place` makes: one placement covering a three-cue /// track, where cue 1 ("second") is exactly frames 93..176 at 30 fps. fn three_cue_captions() -> (Project, Lane) { let cue = |start_us: i64, end_us: i64, text: &str| crate::subtitle::Cue { start_us, end_us, text: text.into(), image: None, words: Vec::new(), karaoke: Vec::new(), }; let mut words = subtitle_track(); words.cues = vec![ cue(0, 3_100_000, "first"), cue(3_100_000, 5_866_667, "second"), cue(5_866_667, 9_000_000, "third"), ]; let mut p = Project::single(FILE, 270).with_subtitles(vec![words]); let s1 = p.add_lane(LaneKind::Subtitle); let sub = SubClip { start: 0, frames: 270, track: 0, in_us: 0, out_us: 9_000_000, link: None, }; p.place_sub(s1, 0, sub).expect("placed"); (p, s1) } /// A cut over a span nothing lies in changes nothing: it costs no undo step, /// keeps the revision, and pushes nothing the batch counter would count. #[test] fn a_cut_over_an_empty_span_costs_no_step_and_keeps_the_revision() { let mut p = Project::single(FILE, 270); let empty = p.add_lane(LaneKind::Audio); let (depth, pushes, rev) = (p.history_len(), p.history_pushes(), p.revision()); p.cut_regions(&[(10, 20)], &[empty]).expect("an empty lane is a legal scope"); assert_eq!(p.history_len(), depth, "no undo step"); assert_eq!(p.history_pushes(), pushes, "no snapshot counted"); assert_eq!(p.revision(), rev, "same state, same revision"); p.cut_regions(&[(400, 20)], &[Lane::V1, Lane::A1]).expect("a span past the last clip"); assert_eq!(p.history_len(), depth, "still no undo step"); assert_eq!(p.revision(), rev, "still the same revision"); } /// A cut that removes something still costs exactly one undo step and a new revision. #[test] fn a_real_cut_still_adds_one_step() { let mut p = Project::single(FILE, 270); let (depth, rev) = (p.history_len(), p.revision()); p.cut_regions(&[(30, 30)], &[Lane::V1, Lane::A1]).expect("cut"); assert_eq!(p.history_len(), depth + 1, "one undo step"); assert_ne!(p.revision(), rev, "a new state"); } /// A no-op after the save leaves the saved revision in place, and undoing the /// real edit back to that save reads as the saved state again. #[test] fn undo_back_to_the_saved_point_clears_the_dot_after_a_no_op() { let mut p = Project::single(FILE, 270); let empty = p.add_lane(LaneKind::Audio); p.cut_regions(&[(30, 30)], &[Lane::V1, Lane::A1]).expect("cut"); let saved = p.revision(); p.cut_regions(&[(10, 20)], &[empty]).expect("empty span"); assert_eq!(p.revision(), saved, "a no-op after the save leaves the dot clear"); p.cut_regions(&[(100, 10)], &[Lane::V1, Lane::A1]).expect("a real cut"); assert_ne!(p.revision(), saved, "the real cut moves off the saved state"); assert!(p.undo()); assert_eq!(p.revision(), saved, "undo back to the saved point clears the dot"); } /// The undo bookkeeping a no-op must leave exactly as it found it. fn undo_state(p: &Project) -> (usize, u64, usize, u64) { (p.history_len(), p.revision(), p.redo_len(), p.history_pushes()) } /// Mutators given arguments that change nothing (the value a setting already /// holds, an empty list, a move by 0) take no undo step, move no revision and /// keep the redo branch an edit would have cleared. #[test] fn no_op_mutators_leave_the_undo_state_alone() { let mut p = Project::single(FILE, 270); assert!(p.set_lane_gain_db(Lane::A1, -3.0)); assert!(p.set_enabled(Lane::V1, 0, false)); assert!(p.set_label(Lane::V1, 0, 2)); assert!(p.set_clip_gain(Lane::A1, 0, -6.0)); assert!(p.undo()); let before = undo_state(&p); assert_eq!(before.2, 1, "the undone gain waits on the redo stack"); // Each call below sets a value the project already holds (or nothing). p.set_lane_gain_db(Lane::A1, -3.0); assert_eq!(undo_state(&p), before, "set_lane_gain_db"); p.set_enabled(Lane::V1, 0, false); assert_eq!(undo_state(&p), before, "set_enabled"); p.set_label(Lane::V1, 0, 2); assert_eq!(undo_state(&p), before, "set_label"); p.set_clip_gain(Lane::A1, 0, 0.0); assert_eq!(undo_state(&p), before, "set_clip_gain"); p.set_fade_in(Lane::V1, 0, 0); assert_eq!(undo_state(&p), before, "set_fade_in"); p.set_fade_out(Lane::V1, 0, 0); assert_eq!(undo_state(&p), before, "set_fade_out"); let _ = p.set_reverse(Lane::V1, 0, false); assert_eq!(undo_state(&p), before, "set_reverse"); assert_eq!(p.slip(Lane::V1, 0, 0, SourceWalls::same(&[270])), Some(0)); assert_eq!(undo_state(&p), before, "slip by 0"); p.set_eq(Lane::A1, 0, None); assert_eq!(undo_state(&p), before, "set_eq"); p.set_color_keys(Lane::V1, 0, Vec::new()); assert_eq!(undo_state(&p), before, "set_color_keys"); p.set_gain_keys(Lane::A1, 0, Vec::new()); assert_eq!(undo_state(&p), before, "set_gain_keys"); p.set_opacity_keys(Lane::V1, 0, Vec::new()); assert_eq!(undo_state(&p), before, "set_opacity_keys"); let _ = p.set_lane_name(Lane::A1, ""); assert_eq!(undo_state(&p), before, "set_lane_name"); p.clear_lane_duck(Lane::A1, 0); assert_eq!(undo_state(&p), before, "clear_lane_duck"); p.remap_chapters(|f| f); assert_eq!(undo_state(&p), before, "remap_chapters"); p.remap_markers(|f| f); assert_eq!(undo_state(&p), before, "remap_markers"); p.add_subtitles_all(&[]); assert_eq!(undo_state(&p), before, "add_subtitles_all"); p.relink_sources(&[]); assert_eq!(undo_state(&p), before, "relink_sources"); let _ = p.cut_regions(&[(10, 0)], &[Lane::V1]); assert_eq!(undo_state(&p), before, "cut_regions of nothing"); // And the redo branch is still there to walk forward. let depth = p.history_len(); assert!(p.redo(), "the gain comes back"); assert_eq!(p.history_len(), depth + 1, "redo is one step forward"); } /// Real edits still cost exactly one undo step each, also right after a no-op. #[test] fn real_edits_still_add_exactly_one_step_after_no_ops() { let mut p = Project::single(FILE, 270); assert!(!p.set_enabled(Lane::V1, 0, true), "already on: nothing to do"); let steps = p.history_len(); let rev = p.revision(); assert!(p.set_lane_gain_db(Lane::A1, -3.0)); assert_eq!(p.history_len(), steps + 1, "one step for the gain"); assert_ne!(p.revision(), rev, "a new state"); assert!(p.set_enabled(Lane::V1, 0, false)); assert_eq!(p.history_len(), steps + 2, "one step for the switch"); assert!(p.set_label(Lane::V1, 0, 2)); assert_eq!(p.history_len(), steps + 3, "one step for the label"); assert!(p.set_clip_gain(Lane::A1, 0, -6.0)); assert_eq!(p.history_len(), steps + 4, "one step for the clip gain"); } /// A no-op right after a real edit does not hide that edit from undo: one undo /// takes back the real edit, and nothing is left behind to undo next. #[test] fn undo_right_after_a_no_op_takes_back_the_real_edit() { let mut p = Project::single(FILE, 270); let start = p.revision(); assert!(p.set_lane_gain_db(Lane::A1, -3.0)); let edited = p.revision(); p.set_lane_gain_db(Lane::A1, -3.0); assert_eq!(p.revision(), edited, "the no-op leaves the edited state's revision"); assert!(p.undo()); assert_eq!(p.revision(), start, "undo lands on the state before the edit"); assert_eq!(p.history_len(), 0, "and no phantom step is left"); assert!(!p.undo()); } /// A no-op between two edits leaves no step behind for the second to stack on. #[test] fn a_no_op_between_two_edits_is_not_a_step() { let mut p = Project::single(FILE, 270); assert!(p.set_lane_gain_db(Lane::A1, -3.0)); p.set_lane_gain_db(Lane::A1, -3.0); assert!(p.set_enabled(Lane::V1, 0, false)); assert_eq!(p.history_len(), 2, "the two edits, and no more"); assert!(p.undo()); assert!(p.undo()); assert!(!p.undo(), "the no-op is gone for good"); } /// A no-op never destroys a redo branch: after an undo, a no-op keeps it, and /// redo still walks forward to the edit that was undone. #[test] fn a_no_op_after_undo_keeps_the_redo_branch() { let mut p = Project::single(FILE, 270); assert!(p.set_lane_gain_db(Lane::A1, -3.0)); let edited = p.revision(); assert!(p.undo()); p.set_lane_gain_db(Lane::A1, 0.0); assert_eq!(p.redo_len(), 1, "the undone edit is still on the branch"); assert!(p.redo()); assert_eq!(p.revision(), edited, "redo walks back to the same state"); } /// Inside a batch a no-op adds no step, and a batch with a real edit and no-ops /// in it is one step. #[test] fn no_ops_inside_a_batch_add_no_step() { let mut p = Project::single(FILE, 270); let before = undo_state(&p); p.begin_batch(); p.set_lane_gain_db(Lane::A1, 0.0); p.set_enabled(Lane::V1, 0, true); p.end_batch(); assert_eq!(undo_state(&p), before, "an all-no-op batch is no step"); p.begin_batch(); p.set_lane_gain_db(Lane::A1, 0.0); assert!(p.set_lane_gain_db(Lane::A1, -3.0)); p.set_lane_gain_db(Lane::A1, -3.0); assert!(p.set_enabled(Lane::V1, 0, false)); p.end_batch(); assert_eq!(p.history_len(), before.0 + 1, "the batch is one step"); assert!(p.undo()); assert_eq!(p.revision(), before.1, "undo takes the whole batch back"); assert!(!p.undo()); } /// At the cap a no-op neither evicts the oldest step nor loses it: the next real /// edit evicts it as it would have, and every step but that one can be undone. #[test] fn a_no_op_at_the_cap_keeps_every_step_it_should() { let mut p = Project::single(FILE, 400); for frame in 1..=HISTORY_CAP as u32 { assert!(p.split(frame)); } let before = undo_state(&p); assert_eq!(before.0, HISTORY_CAP); p.set_enabled(Lane::V1, 0, true); assert_eq!(undo_state(&p), before, "a no-op at the cap"); assert!(p.split(HISTORY_CAP as u32 + 1)); assert_eq!(p.history_len(), HISTORY_CAP); let mut steps = 0; while p.undo() { steps += 1; } assert_eq!(steps, HISTORY_CAP, "every step but the evicted oldest one"); } /// The cues `s1` shows, as `(text, start_us, end_us)` in timeline order. fn shown_cues(p: &Project, s1: Lane) -> Vec<(String, i64, i64)> { p.sub_lane_cue_refs(s1, 30.0) .into_iter() .map(|(a, b, t, c)| (p.subtitles()[t].cues[c].text.clone(), a, b)) .collect() } /// A cut that covers a whole cue removes it: no zero-length copy, no /// duplicate, and the cues after it slide back. A cut that covers part of /// a cue trims it, and a cut spanning two cues trims both. #[test] fn a_cut_removes_or_trims_the_cues_it_covers() { let (mut p, s1) = three_cue_captions(); p.cut_regions(&[(93, 83)], &[s1]).expect("cut the second sentence"); let shown = shown_cues(&p, s1); assert_eq!( shown.iter().map(|c| c.0.as_str()).collect::<Vec<_>>(), ["first", "third"], "the covered cue is gone, once: {shown:?}" ); assert!(shown.iter().all(|c| c.2 > c.1), "no zero-length cue: {shown:?}"); assert!((shown[1].1 - 3_100_000).abs() <= 3, "the third cue slides back to the cut: {shown:?}"); assert!((shown[1].2 - shown[1].1 - 3_133_333).abs() <= 3, "third keeps its length: {shown:?}"); // Part of the second cue (frames 100..150) is cut out of the middle. let (mut p, s1) = three_cue_captions(); p.cut_regions(&[(100, 50)], &[s1]).expect("cut inside the second cue"); let shown = shown_cues(&p, s1); let total = |text: &str| -> i64 { shown.iter().filter(|c| c.0 == text).map(|c| c.2 - c.1).sum() }; // "second" is one word: it keeps the longer side the cut leaves (5.0 s to 5.87 s, 0.87 s), once. assert!((total("second") - 866_667).abs() <= 3, "second keeps its longer side, once: {shown:?}"); assert_eq!(shown.iter().filter(|c| c.0 == "second").count(), 1, "{shown:?}"); assert!((total("first") - 3_100_000).abs() <= 3, "first is untouched: {shown:?}"); assert!(shown.iter().all(|c| c.2 > c.1), "no zero-length cue: {shown:?}"); // A cut from inside the first cue to inside the second. let (mut p, s1) = three_cue_captions(); p.cut_regions(&[(60, 60)], &[s1]).expect("cut across two cues"); let shown = shown_cues(&p, s1); let total = |text: &str| -> i64 { shown.iter().filter(|c| c.0 == text).map(|c| c.2 - c.1).sum() }; assert!((total("first") - 2_000_000).abs() <= 3, "first keeps its first two seconds: {shown:?}"); assert!((total("second") - 1_866_667).abs() <= 3, "second keeps its last 1.87 s: {shown:?}"); assert!((total("third") - 3_133_333).abs() <= 3, "third is untouched: {shown:?}"); assert!(shown.iter().all(|c| c.2 > c.1), "no zero-length cue: {shown:?}"); } /// A sentence over one placement, nine seconds at 30 fps. fn sentence_captions() -> (Project, Lane) { let mut words = subtitle_track(); words.cues = vec![crate::subtitle::Cue { start_us: 0, end_us: 9_000_000, text: "alpha beta gamma delta".into(), image: None, words: Vec::new(), karaoke: Vec::new(), }]; let mut p = Project::single(FILE, 270).with_subtitles(vec![words]); let s1 = p.add_lane(LaneKind::Subtitle); let sub = SubClip { start: 0, frames: 270, track: 0, in_us: 0, out_us: 9_000_000, link: None }; p.place_sub(s1, 0, sub).expect("placed"); (p, s1) } /// Cutting the middle of a sentence keeps the words either side of the cut /// and drops the ones inside it: "beta gamma" lies in frames 90..180 (3 s /// to 6 s), so only "alpha" and "delta" are left to show. Undo brings the /// one sentence back as it was. #[test] fn a_cut_through_a_sentence_drops_the_words_inside_it() { let (mut p, s1) = sentence_captions(); p.cut_regions(&[(90, 90)], &[s1]).expect("cut the middle"); let shown: Vec<String> = shown_cues(&p, s1).into_iter().map(|c| c.0).collect(); assert_eq!(shown, ["alpha", "delta"], "the cut words are gone and the rest stays"); assert!(p.undo(), "the cut is one undo step"); assert_eq!(p.subtitles()[0].cues.len(), 1, "undo restores the one cue"); assert_eq!(p.subtitles()[0].cues[0].text, "alpha beta gamma delta"); } /// A ripple delete over the same span splits the same way, and so does a /// split at a point inside a sentence, which divides the words once. #[test] fn a_ripple_delete_and_a_split_divide_the_words_too() { let (mut p, s1) = sentence_captions(); assert!(p.ripple_delete(90, 90)); let shown: Vec<String> = shown_cues(&p, s1).into_iter().map(|c| c.0).collect(); assert_eq!(shown, ["alpha", "delta"], "{shown:?}"); let (mut p, _) = sentence_captions(); assert!(p.split(150), "a frame inside the sentence splits it"); let texts: Vec<String> = p.subtitles()[0].cues.iter().map(|c| c.text.clone()).collect(); assert_eq!(texts, ["alpha beta", "gamma delta"], "a split at the half divides the words"); } /// `split_cues_at` divides the cue at the word boundary nearest the point, /// keeps every piece's time inside the cue, and is one undo step. Where no /// cue straddles the point it changes nothing and takes no step. #[test] fn split_cues_at_divides_the_words_and_is_one_undo_step() { let (mut p, _) = sentence_captions(); assert!(!p.split_cues_at(0, 0), "a point at the cue's start splits nothing"); assert!(p.split_cues_at(0, 2_250_000)); let cues: Vec<(String, i64, i64)> = p.subtitles()[0].cues.iter().map(|c| (c.text.clone(), c.start_us, c.end_us)).collect(); assert_eq!( cues, [("alpha".to_string(), 0, 2_250_000), ("beta gamma delta".to_string(), 2_250_000, 9_000_000)], "the pieces meet at the point and keep the words either side" ); assert!(p.undo()); assert_eq!(p.subtitles()[0].cues.len(), 1, "undo restores the sentence"); // "second" is one word: it stays whole on the longer side of 4 s (the tail, 4 s to 5.87 s). let (mut p, _) = three_cue_captions(); assert!(p.split_cues_at(0, 4_000_000)); let cues: Vec<(String, i64, i64)> = p.subtitles()[0].cues.iter().map(|c| (c.text.clone(), c.start_us, c.end_us)).collect(); assert_eq!(cues.len(), 3, "a one-word cue is not duplicated: {cues:?}"); assert_eq!(cues[1], ("second".to_string(), 4_000_000, 5_866_667)); } /// A cue with one word cut by an edge keeps its text on the longer piece only /// (ties: the piece before the edge), so the word is never shown twice. #[test] fn a_one_word_cue_cut_by_an_edge_keeps_its_text_on_one_piece() { let cue = |start_us: i64, end_us: i64| crate::subtitle::Cue { start_us, end_us, text: "Onewordcue".into(), image: None, words: Vec::new(), karaoke: Vec::new() }; let before = cue(0, 4_000_000); assert_eq!( split_words(&before, 3_000_000), Some(vec![(0, 3_000_000, "Onewordcue".to_string())]), "the longer piece before the edge keeps the word" ); assert_eq!( split_words(&cue(0, 4_000_000), 1_000_000), Some(vec![(1_000_000, 4_000_000, "Onewordcue".to_string())]), "the longer piece after the edge keeps the word" ); assert_eq!( split_words(&cue(0, 4_000_000), 2_000_000), Some(vec![(0, 2_000_000, "Onewordcue".to_string())]), "a tie keeps the word on the piece before" ); assert_eq!(split_words(&crate::subtitle::Cue { start_us: 0, end_us: 1, text: " ".into(), image: None, words: Vec::new(), karaoke: Vec::new() }, 0), None); } /// A cut through a one-word caption ("first", 0 to 3.1 s) at 2 s keeps the /// word on the longer piece before the cut, and no piece after it shows it. #[test] fn a_cut_through_a_one_word_caption_shows_it_once() { let (mut p, _) = three_cue_captions(); assert!(p.split_cues_at(0, 2_000_000)); let cues: Vec<(String, i64, i64)> = p.subtitles()[0].cues.iter().map(|c| (c.text.clone(), c.start_us, c.end_us)).collect(); assert_eq!(cues[0], ("first".to_string(), 0, 2_000_000), "{cues:?}"); assert_eq!(cues.iter().filter(|c| c.0 == "first").count(), 1, "one copy of the word: {cues:?}"); } /// Divided cues are written by a save as cue edits and cue adds, so a reopen /// of the saved project shows the same words, not the sentence it began as. #[test] fn divided_cues_survive_a_save_and_reopen() { let (mut p, s1) = sentence_captions(); p.cut_regions(&[(90, 90)], &[s1]).expect("cut the middle"); let edits = p.cue_edits().to_vec(); let adds = p.cue_adds().to_vec(); let mut words = subtitle_track(); words.cues = vec![crate::subtitle::Cue { start_us: 0, end_us: 9_000_000, text: "alpha beta gamma delta".into(), image: None, words: Vec::new(), karaoke: Vec::new(), }]; let reopened = Project::single(FILE, 270) .with_subtitles(vec![words]) .with_cue_edits(&edits) .with_cue_adds(&adds); let texts = |p: &Project| -> Vec<(String, i64, i64)> { p.subtitles()[0].cues.iter().map(|c| (c.text.clone(), c.start_us, c.end_us)).collect() }; assert_eq!(texts(&reopened), texts(&p), "the reopened cues are the divided ones"); } /// The rippling delete is the same cut on every lane, so it must leave the /// same captions as the scoped one: no covered cue survives it either. #[test] fn a_ripple_delete_over_a_cue_leaves_no_copy_of_it() { let (mut p, s1) = three_cue_captions(); assert!(p.ripple_delete(93, 83)); let shown = shown_cues(&p, s1); assert_eq!( shown.iter().map(|c| c.0.as_str()).collect::<Vec<_>>(), ["first", "third"], "{shown:?}" ); assert!(shown.iter().all(|c| c.2 > c.1), "no zero-length cue: {shown:?}"); } /// How many of track `track`'s cues some placement window overlaps: a cue no /// window overlaps is an orphan, one nothing on the timeline can show. fn placed_cues_of(p: &Project, track: usize) -> usize { let windows: Vec<(i64, i64)> = p .lanes .iter() .flat_map(|l| l.subs.iter()) .filter(|s| s.track == track) .map(|s| (s.in_us, s.out_us)) .collect(); p.subtitles()[track] .cues .iter() .filter(|c| windows.iter().any(|&(a, b)| c.start_us < b && a < c.end_us)) .count() } /// A cut through a sentence leaves no orphan. "beta gamma" lies in the cut, /// so it is removed with its words: the track keeps only "alpha" and /// "delta", and each is under the placement window that is left. #[test] fn a_cut_through_a_sentence_leaves_no_orphan_piece() { let (mut p, s1) = sentence_captions(); p.cut_regions(&[(90, 90)], &[s1]).expect("cut the middle"); let texts: Vec<String> = p.subtitles()[0].cues.iter().map(|c| c.text.clone()).collect(); assert_eq!(texts, ["alpha", "delta"], "the removed middle is not a cue: {texts:?}"); assert_eq!(placed_cues_of(&p, 0), 2, "every cue is under a window"); } /// A cut that ends inside a one-word cue keeps the part the timeline still /// shows. "Over" runs 1.0 s to 1.7 s; cutting frames 1.0 s to 1.5 s leaves /// 1.5 s to 1.7 s on the timeline, so that is the cue that stays, with its /// word. The removed 1.0 s to 1.5 s piece is dropped. #[test] fn a_cut_ending_in_a_one_word_cue_keeps_the_visible_part() { let mut words = subtitle_track(); words.cues = vec![crate::subtitle::Cue { start_us: 1_000_000, end_us: 1_700_000, text: "Over".into(), image: None, words: Vec::new(), karaoke: Vec::new() }]; let mut p = Project::single(FILE, 270).with_subtitles(vec![words]); let s1 = p.add_lane(LaneKind::Subtitle); // The file's 1 s to 4 s, played from timeline frame 30 (1.0 s) for 90 frames. let sub = SubClip { start: 30, frames: 90, track: 0, in_us: 1_000_000, out_us: 4_000_000, link: None }; p.place_sub(s1, 30, sub).expect("placed"); p.cut_regions(&[(30, 15)], &[s1]).expect("cut 1.0 s to 1.5 s"); let cues: Vec<(String, i64, i64)> = p.subtitles()[0].cues.iter().map(|c| (c.text.clone(), c.start_us, c.end_us)).collect(); assert_eq!(cues, [("Over".to_string(), 1_500_000, 1_700_000)], "{cues:?}"); let shown = shown_cues(&p, s1); assert_eq!(shown.len(), 1, "{shown:?}"); assert_eq!(shown[0].0, "Over"); assert!((shown[0].1 - 1_000_000).abs() <= 3, "the word now starts where the cut ended: {shown:?}"); assert!((shown[0].2 - 1_200_000).abs() <= 3, "and keeps its 0.2 s: {shown:?}"); } /// Three cuts in a row over a captioned timeline keep the track to the cues /// the placement shows: after each one every cue is under a window, so the /// cue count is the placed count. #[test] fn three_cuts_keep_the_cue_count_at_the_placed_count() { let cue = |start_us: i64, end_us: i64, text: &str| crate::subtitle::Cue { start_us, end_us, text: text.into(), image: None, words: Vec::new(), karaoke: Vec::new() }; let mut words = subtitle_track(); words.cues = vec![ cue(0, 1_900_000, "a b"), cue(1_900_000, 4_200_000, "c d e"), cue(4_200_000, 6_000_000, "f g"), cue(6_000_000, 8_100_000, "h i j"), cue(8_100_000, 10_000_000, "k l"), cue(10_000_000, 12_000_000, "m n"), ]; let mut p = Project::single(FILE, 360).with_subtitles(vec![words]); let s1 = p.add_lane(LaneKind::Subtitle); let sub = SubClip { start: 0, frames: 360, track: 0, in_us: 0, out_us: 12_000_000, link: None }; p.place_sub(s1, 0, sub).expect("placed"); assert_eq!(placed_cues_of(&p, 0), 6); for (at, len) in [(45, 20), (100, 30), (200, 25)] { p.cut_regions(&[(at, len)], &[s1]).expect("cut"); let total = p.subtitles()[0].cues.len(); assert_eq!(placed_cues_of(&p, 0), total, "every cue is under a window after a cut: {total} cues"); } } /// A cue no placement ever covered is not the cut's to remove. A caption /// placed over the first part of its track leaves the rest of the cues in /// the track, so a cut through the placed part keeps them for later. #[test] fn a_cue_no_placement_covers_survives_a_cut() { let cue = |start_us: i64, end_us: i64, text: &str| crate::subtitle::Cue { start_us, end_us, text: text.into(), image: None, words: Vec::new(), karaoke: Vec::new() }; let mut words = subtitle_track(); words.cues = vec![cue(0, 2_000_000, "x y"), cue(5_000_000, 7_000_000, "z w")]; let mut p = Project::single(FILE, 270).with_subtitles(vec![words]); let s1 = p.add_lane(LaneKind::Subtitle); // Only the first two seconds are placed. let sub = SubClip { start: 0, frames: 60, track: 0, in_us: 0, out_us: 2_000_000, link: None }; p.place_sub(s1, 0, sub).expect("placed"); p.cut_regions(&[(30, 15)], &[s1]).expect("cut inside the placed part"); let kept = p.subtitles()[0] .cues .iter() .filter(|c| c.text == "z w" && c.start_us == 5_000_000 && c.end_us == 7_000_000) .count(); assert_eq!(kept, 1, "the unplaced cue stays in the track: {:?}", p.subtitles()[0].cues); } /// A caption placement for pure timeline tests: the track window is the /// same duration as the placement, so assertions can stay in frames. fn caption(start: u32, frames: u32) -> SubClip { SubClip { start, frames, track: 0, in_us: 0, out_us: i64::from(frames) * 1_000_000, link: None, } } /// A language retype is one undo step, refused (with no step) for a track /// that is not there or that already reads it, and a load puts a saved /// language back over the file's own. #[test] fn a_subtitle_language_is_one_undo_step_and_a_load_puts_it_back() { let mut p = Project::single(FILE, 300).with_subtitles(vec![subtitle_track()]); assert!(p.set_subtitle_language(0, "fra")); assert_eq!(p.subtitles()[0].language, "fra"); assert!(!p.set_subtitle_language(0, "fra"), "it already reads that: no step"); assert!(!p.set_subtitle_language(5, "fra"), "no such track"); assert!(p.undo()); assert_eq!(p.subtitles()[0].language, "eng", "one step takes it back"); let back = Project::single(FILE, 300) .with_subtitles(vec![subtitle_track()]) .with_sub_langs(&[(0, "deu".to_string()), (9, "xxx".to_string())]); assert_eq!(back.subtitles()[0].language, "deu"); } /// A subtitle palette row for tests that place captions on lanes. fn subtitle_track() -> SubtitleTrack { SubtitleTrack { path: FILE.into(), track: None, language: "eng".into(), name: String::new(), label: "eng".into(), cues: Vec::new(), bitmap: false, refused: None, } } /// A retyped cue is the track's words, so every placement of that track /// shows it; a retype that changes nothing, or names no cue, costs no undo /// step; one undo takes the words and the save list back together; and a /// removed row's overrides go with it while the others keep their place. #[test] fn a_cue_is_trimmed_moved_and_merged_inside_its_neighbours_one_undo_step_each() { let (mut p, s1) = three_cue_captions(); let times = |p: &Project| -> Vec<(i64, i64)> { p.subtitles()[0].cues.iter().map(|c| (c.start_us, c.end_us)).collect() }; assert_eq!(p.cue_room(0, 1), Some((3_100_000, 5_866_667))); assert!(p.set_cue_times(0, 1, 3_100_000, 5_866_667).is_ok_and(|changed| !changed), "same times, no edit"); assert!(p.set_cue_times(0, 1, 3_000_000, 5_000_000).is_err(), "into the cue before"); assert!(p.set_cue_times(0, 1, 3_100_000, 6_000_000).is_err(), "into the cue after"); assert!(p.set_cue_times(0, 1, 3_100_000, 3_150_000).is_err(), "too short"); assert!(matches!(p.set_cue_times(0, 1, 3_500_000, 5_000_000), Ok(true))); assert_eq!(times(&p)[1], (3_500_000, 5_000_000)); assert_eq!(p.subtitles()[0].cues[1].text, "second"); assert_eq!( shown_cues(&p, s1)[1], ("second".to_string(), 3_500_000, 5_000_000), "the timeline shows the moved cue" ); // The file cue leaves and its new times are an added cue: a save keeps both. assert_eq!(p.cue_adds().len(), 1); assert!(p.cue_edits().iter().any(|(_, _, t)| t.is_empty())); // A second trim of the same (now added) cue stays one added entry. assert!(matches!(p.set_cue_times(0, 1, 3_400_000, 5_000_000), Ok(true))); assert_eq!(p.cue_adds().len(), 1); assert_eq!(p.cue_adds()[0].start_us, 3_400_000); assert_eq!(p.cue_track_us_at(s1, 0, 1, 4_000_000, 30.0), Some(4_000_000)); assert!(p.merge_cue_next(2, 0).is_err(), "no such track"); assert!(p.merge_cue_next(0, 2).is_err(), "nothing after the last"); assert!(p.merge_cue_next(0, 0).is_ok()); assert_eq!(p.subtitles()[0].cues.len(), 2); assert_eq!(p.subtitles()[0].cues[0].text, "first second"); assert_eq!(times(&p)[0], (0, 5_000_000)); assert!(p.undo(), "the merge goes"); assert_eq!(p.subtitles()[0].cues.len(), 3); assert!(p.undo(), "the second trim goes"); assert_eq!(times(&p)[1], (3_500_000, 5_000_000)); assert!(p.undo(), "the first trim goes"); assert_eq!(times(&p)[1], (3_100_000, 5_866_667)); assert!(p.cue_adds().is_empty() && p.cue_edits().is_empty()); } #[test] fn a_retyped_cue_is_the_tracks_words_and_one_undo_step() { let cue = |start_us: i64, end_us: i64, text: &str| crate::subtitle::Cue { start_us, end_us, text: text.into(), image: None, words: Vec::new(), karaoke: Vec::new(), }; let mut words = subtitle_track(); words.cues = vec![cue(0, 1_000_000, "one"), cue(2_000_000, 3_000_000, "two")]; let mut p = Project::single(FILE, 300).with_subtitles(vec![subtitle_track(), words]); let s1 = p.add_lane(LaneKind::Subtitle); // A window of the track's first three seconds, played over three: one // timeline second is one of the file's, so the cues land where they are. for at in [0, 100] { let sub = SubClip { start: at, frames: 90, track: 1, in_us: 0, out_us: 3_000_000, link: None, }; p.place_sub(s1, at, sub).expect("placed"); } let texts = |p: &Project| -> Vec<String> { p.sub_lane_cues(s1, 30.0) .into_iter() .map(|c| c.text) .collect() }; assert_eq!(texts(&p), ["one", "two", "one", "two"]); assert!(!p.set_cue_text(1, 0, "one"), "the same words are no edit"); assert!(!p.set_cue_text(1, 9, "x"), "a cue that is not there"); assert!(!p.set_cue_text(7, 0, "x"), "a track that is not there"); assert!(p.set_cue_text(1, 1, "TWO\nlines")); assert_eq!( texts(&p), ["one", "TWO\nlines", "one", "TWO\nlines"], "both placements show the retyped words" ); assert_eq!(p.cue_edits(), [(1, 1, "TWO\nlines".to_string())]); let refs = p.sub_lane_cue_refs(s1, 30.0); let drawn = p.sub_lane_cues(s1, 30.0); assert_eq!(refs.len(), drawn.len(), "one ref for each cue drawn"); for (&(start, end, track, cue_idx), c) in refs.iter().zip(&drawn) { assert_eq!((start, end), (c.start_us, c.end_us)); assert_eq!(p.subtitles()[track].cues[cue_idx].text, c.text); } assert!(p.undo(), "one step back"); assert_eq!(texts(&p), ["one", "two", "one", "two"]); assert!( p.cue_edits().is_empty(), "the save list goes back with the words" ); assert!(p.redo()); assert_eq!(texts(&p)[1], "TWO\nlines"); // The row above the edited one comes off: the edit moves down with its // track, and still names the cue it changed. p.remove_subtitles(0).expect("nothing is placed on it"); assert_eq!(p.cue_edits(), [(0, 1, "TWO\nlines".to_string())]); assert_eq!(texts(&p)[1], "TWO\nlines"); } /// A deleted cue is gone from every placement of its track, and no placement /// moves. Deleting cue 1 and then cue 0 (by the indices a listing shows /// after each) leaves the file's third cue; a retype after the deletes lands /// on that cue's file index; and a reload of the same file, with the saved /// overrides put back, shows exactly what the session showed. #[test] fn a_deleted_cue_leaves_the_track_and_survives_a_reload() { let file_words = || { let cue = |start_us: i64, end_us: i64, text: &str| crate::subtitle::Cue { start_us, end_us, text: text.into(), image: None, words: Vec::new(), karaoke: Vec::new(), }; let mut words = subtitle_track(); words.cues = vec![ cue(0, 1_000_000, "one"), cue(2_000_000, 3_000_000, "two"), cue(4_000_000, 5_000_000, "three"), ]; words }; let mut p = Project::single(FILE, 300).with_subtitles(vec![subtitle_track(), file_words()]); let s1 = p.add_lane(LaneKind::Subtitle); let sub = SubClip { start: 0, frames: 150, track: 1, in_us: 0, out_us: 5_000_000, link: None }; p.place_sub(s1, 0, sub).expect("placed"); let texts = |p: &Project| -> Vec<String> { p.sub_lane_cues(s1, 30.0).into_iter().map(|c| c.text).collect() }; assert_eq!(texts(&p), ["one", "two", "three"]); assert!(p.delete_cue(1, 1), "cue 1 (\"two\") goes"); assert!(!p.delete_cue(1, 9), "a cue that is not there"); assert_eq!(texts(&p), ["one", "three"]); assert_eq!((p.sub_lane(s1)[0].start, p.sub_lane(s1)[0].frames), (0, 150), "nothing moves"); assert_eq!(p.cue_edits(), [(1, 1, String::new())]); // Cue 1 is now "three"; its file index is 2, which the save names. assert!(p.set_cue_text(1, 1, "THREE")); assert_eq!(texts(&p), ["one", "THREE"]); assert_eq!(p.cue_edits(), [(1, 1, String::new()), (1, 2, "THREE".to_string())]); // Empty text is a removal, not a blank cue: "one" goes. assert!(p.set_cue_text(1, 0, "")); assert_eq!(texts(&p), ["THREE"]); assert_eq!( p.cue_edits(), [(1, 1, String::new()), (1, 2, "THREE".to_string()), (1, 0, String::new())] ); // A reload: the file's three cues, the saved overrides put back. let saved = p.cue_edits().to_vec(); let mut reopened = Project::single(FILE, 300).with_subtitles(vec![subtitle_track(), file_words()]).with_cue_edits(&saved); let s1r = reopened.add_lane(LaneKind::Subtitle); reopened.place_sub(s1r, 0, SubClip { start: 0, frames: 150, track: 1, in_us: 0, out_us: 5_000_000, link: None }).expect("placed"); let got: Vec<String> = reopened.sub_lane_cues(s1r, 30.0).into_iter().map(|c| c.text).collect(); assert_eq!(got, ["THREE"], "the reload shows what the session did"); assert_eq!(reopened.cue_edits(), saved.as_slice(), "and saves the same list"); // Undo takes back the last removal, then the retype, then the first removal. assert!(p.undo()); assert_eq!(texts(&p), ["one", "THREE"]); assert!(p.undo()); assert_eq!(texts(&p), ["one", "three"]); assert!(p.undo()); assert_eq!(texts(&p), ["one", "two", "three"]); } /// A cue a window edge shortened to under [`CUT_SLIVER_FRAMES`] is no cue; /// a cue that was short in the file, and whole, stays one. #[test] fn a_cut_sliver_under_four_frames_is_dropped_and_a_short_cue_is_not() { let cue = |start_us: i64, end_us: i64, text: &str| crate::subtitle::Cue { start_us, end_us, text: text.into(), image: None, words: Vec::new(), karaoke: Vec::new(), }; let mut words = subtitle_track(); words.cues = vec![ cue(0, 1_000_000, "whole"), // Shortened by the window's end at 1.1 s: 0.1 s, three frames. cue(1_000_000, 2_000_000, "edge"), // Two frames long in the file itself, inside the window. cue(1_000_000, 1_066_667, "born"), ]; let mut p = Project::single(FILE, 300).with_subtitles(vec![subtitle_track(), words]); let s1 = p.add_lane(LaneKind::Subtitle); let sub = SubClip { start: 0, frames: 33, track: 1, in_us: 0, out_us: 1_100_000, link: None }; p.place_sub(s1, 0, sub).expect("placed"); let shown: Vec<String> = p.sub_lane_cues(s1, 30.0).into_iter().map(|c| c.text).collect(); assert_eq!(shown, ["whole", "born"], "the three-frame edge is no cue: {shown:?}"); } /// A recognisable equalizer setting: one peak band, `n` telling two of them /// apart -- which is what makes the table's dedup and its prune visible. fn band_at(n: u32) -> EqParams { EqParams { bands: vec![crate::eq::Band { freq_hz: 1000.0, gain_db: f64::from(n) as f32 * 1.5, q: 0.707, kind: crate::eq::BandKind::Peak, }], } } /// A recognisable colour grade, `n` telling two of them apart -- [`band_at`] /// for the colour table. fn grade_at(n: u32) -> ColorParams { ColorParams { brightness: f64::from(n) as f32 * 0.05, ..Default::default() } } /// A recognisable placement, `n` telling two of them apart -- [`grade_at`]'s /// twin for the transform table. fn transform_at(n: u32) -> TransformParams { TransformParams { pos_x: f64::from(n) as f32 * 0.05, ..Default::default() } } /// The `V1`, `A1` lane list a two-lane load hands [`Project::from_parts`]. fn two(video: Vec<Clip>, audio: Vec<Clip>) -> Vec<(LaneKind, Vec<Clip>)> { vec![(LaneKind::Video, video), (LaneKind::Audio, audio)] } /// Every lane with its group ids blanked, for comparing shape. fn shape(p: &Project) -> Vec<Vec<Clip>> { p.lanes() .into_iter() .map(|l| { p.lane(l) .iter() .map(|c| Clip { link: None, ..c.clone() }) .collect() }) .collect() } /// The *source frame* every timeline frame reads, per lane -- the traversal /// a player performs. Deliberately not the clip index: a split changes which /// clip a frame belongs to, and nothing else. fn traversal(p: &Project) -> Vec<Vec<Option<u32>>> { (0..p.timeline_frames() + 2) .map(|f| { p.lanes() .into_iter() .map(|l| p.map(l, f).map(|(_, source)| source)) .collect() }) .collect() } /// Source clips [0,3) [3,5) [5,9) on both lanes -- the ledger's off-by-one /// fixture. Built through `split` so the constructor path is exercised too. fn three() -> Project { let mut p = Project::single(FILE, 9); assert!(p.split(3)); assert!(p.split(5)); assert_eq!( shape(&p), [ vec![clip(0, 0, 3, 0), clip(3, 3, 5, 0), clip(5, 5, 9, 0)], vec![clip(0, 0, 3, 0), clip(3, 3, 5, 0), clip(5, 5, 9, 0)], ] ); p } #[test] fn single_is_the_whole_file_on_both_lanes() { let p = Project::single(FILE, 150); assert_eq!(p.clips().len(), 1); assert_eq!(p.lane(Lane::A1).len(), 1); assert_eq!(p.timeline_frames(), 150); assert_eq!(p.clip_spans(), vec![(0, 150)]); // grouped: video and audio carry the same link assert_eq!(p.clips()[0].link, p.lane(Lane::A1)[0].link); assert!(p.clips()[0].link.is_some()); // degenerate mapping: timeline == source assert_eq!(p.map_timeline(0), Some((0, 0))); assert_eq!(p.map_timeline(149), Some((0, 149))); assert_eq!(p.map_timeline(150), None); // never-empty invariant survives a bogus frame count assert_eq!(Project::single(FILE, 0).timeline_frames(), 1); } #[test] fn split_cuts_every_lane_and_regroups_it_back() { let mut p = Project::single(FILE, 9); let before = traversal(&p); let before_shape = shape(&p); assert!(p.split(4)); for l in p.lanes() { assert_eq!(p.lane(l).len(), 2, "{} split", l.label()); } // The two sides are two groups, one per side, matching across the lanes. let (v, a) = (p.lane(Lane::V1), p.lane(Lane::A1)); assert_eq!(v[0].link, a[0].link); assert_eq!(v[1].link, a[1].link); assert_ne!(v[0].link, v[1].link, "the halves are no longer one take"); // A split changes no mapping, in either lane. assert_eq!(traversal(&p), before, "a split moves nothing"); assert!(p.regroup(4), "the inverse rejoins them"); assert_eq!(shape(&p), before_shape, "bit-exact back to one clip"); assert_eq!(traversal(&p), before); assert_eq!( p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link, "and the rejoined clip is one group again" ); } /// [`Fade::gain_at`]'s curve at the edges and in the middle: silence at /// the very first frame of a fade-in, unity at its last, equal-power /// (`sin(pi/4)`) at its midpoint, and unity anywhere neither ramp reaches /// at all. #[test] fn clip_gain_set_clamps_refuses_and_undoes() { let mut p = Project::single(FILE, 150); let a1 = Lane::new(LaneKind::Audio, 0); assert_eq!(p.clip_gain(a1, 0), Some(0.0)); assert!(p.set_clip_gain(a1, 0, -6.0)); assert_eq!(p.clip_gain(a1, 0), Some(-6.0)); assert!(p.set_clip_gain(a1, 0, -100.0)); assert_eq!(p.clip_gain(a1, 0), Some(-60.0), "clamped to the floor"); assert!(!p.set_clip_gain(a1, 0, -60.0), "the same value is no step"); assert!(!p.set_clip_gain(a1, 0, f32::NAN), "not finite"); assert!( !p.set_clip_gain(Lane::V1, 0, -3.0), "a video clip takes no gain" ); assert!(!p.set_clip_gain(a1, 5, -3.0), "no clip there"); assert!(p.set_clip_gain(a1, 0, 30.0)); assert_eq!(p.clip_gain(a1, 0), Some(24.0), "clamped to the ceiling"); assert!(p.undo()); assert_eq!(p.clip_gain(a1, 0), Some(-60.0)); assert!(p.undo()); assert_eq!(p.clip_gain(a1, 0), Some(-6.0)); assert!(p.undo()); assert_eq!(p.clip_gain(a1, 0), Some(0.0)); } #[test] fn gain_rides_the_envelope_and_a_split_carries_it_to_both_halves() { let mut p = Project::single(FILE, 150); let a1 = Lane::new(LaneKind::Audio, 0); assert!(p.set_clip_gain(a1, 0, -6.0)); let fade = p.audio_fades_from(0, 30.0)[0][0] .clone() .expect("a gained clip has an envelope"); assert!((fade.gain - 0.5012).abs() < 1e-3, "-6 dB is a linear 0.501"); assert!( (fade.gain_at(75) - fade.gain).abs() < 1e-6, "no ramps: the gain alone" ); assert!(p.split(60)); assert_eq!(p.lane(a1).len(), 2); for c in p.lane(a1) { assert_eq!(c.gain_db, -6.0, "both halves keep the gain"); } } #[test] fn gain_envelope_is_linear_in_db_and_holds_at_both_ends() { let keys = [GainKey::new(10, -12.0), GainKey::new(20, 0.0)]; assert_eq!( gain_keys_db_at(&keys, 15.0), -6.0, "the midpoint is the midpoint of the dB" ); assert_eq!(gain_keys_db_at(&keys, 10.0), -12.0, "exact at a point"); assert_eq!(gain_keys_db_at(&keys, 20.0), 0.0, "exact at the last point"); assert_eq!( gain_keys_db_at(&keys, 0.0), -12.0, "held before the first point" ); assert_eq!( gain_keys_db_at(&keys, 99.0), 0.0, "held after the last point" ); assert_eq!(gain_keys_db_at(&[], 5.0), 0.0, "no points is unity"); let three = [ GainKey::new(0, 0.0), GainKey::new(10, -20.0), GainKey::new(30, -20.0), ]; assert_eq!(gain_keys_db_at(&three, 5.0), -10.0); assert_eq!( gain_keys_db_at(&three, 20.0), -20.0, "a flat run stays flat" ); } #[test] fn gain_keys_are_sorted_clamped_capped_and_refused_where_they_must_be() { let mut p = Project::single(FILE, 150); let a1 = Lane::new(LaneKind::Audio, 0); assert!(p.set_gain_keys( a1, 0, vec![GainKey::new(30, -20.0), GainKey::new(0, 0.0)] )); assert_eq!( p.gain_keys_of(a1, 0), &[GainKey::new(0, 0.0), GainKey::new(30, -20.0)], "sorted by source frame" ); assert!( !p.set_gain_keys( a1, 0, vec![GainKey::new(5, 1.0), GainKey::new(5, 2.0)] ), "two points at one source frame are refused" ); assert!( !p.set_gain_keys(Lane::V1, 0, vec![GainKey::new(0, 0.0)]), "a video clip takes no envelope" ); assert!(!p.set_gain_keys(a1, 9, Vec::new()), "no such clip"); assert!(p.set_gain_keys(a1, 0, vec![GainKey::new(0, 100.0)])); assert_eq!(p.gain_keys_of(a1, 0)[0].db, MAX_CLIP_GAIN_DB, "clamped"); assert!( !p.set_gain_keys(a1, 0, vec![GainKey::new(0, 100.0)]), "an envelope already exactly so changes nothing, and costs no undo" ); assert!(!p.set_gain_keys( a1, 0, vec![GainKey::new(0, f32::NAN)] )); let over: Vec<GainKey> = (0..=MAX_GAIN_KEYS as u32) .map(|at| GainKey::new(at, 0.0)) .collect(); assert!(!p.set_gain_keys(a1, 0, over), "257 points is over the cap"); let full: Vec<GainKey> = (0..MAX_GAIN_KEYS as u32) .map(|at| GainKey::new(at, 0.0)) .collect(); assert!(p.set_gain_keys(a1, 0, full), "256 is the cap itself"); let extra = GainKey::new(9_999, 0.0); assert!( !p.add_gain_key(a1, 0, extra), "a 257th point through add is refused" ); assert!( p.add_gain_key(a1, 0, GainKey::new(3, -3.0)), "a replacement still fits" ); } #[test] fn gain_keys_add_replace_remove_and_undo_each_take_one_step() { let mut p = Project::single(FILE, 150); let a1 = Lane::new(LaneKind::Audio, 0); assert!(p.add_gain_key(a1, 0, GainKey::new(0, 0.0))); assert!(p.add_gain_key(a1, 0, GainKey::new(30, -20.0))); assert!( p.add_gain_key(a1, 0, GainKey::new(30, -3.0)), "same frame replaces" ); assert_eq!( p.gain_keys_of(a1, 0), &[GainKey::new(0, 0.0), GainKey::new(30, -3.0)] ); assert!( !p.add_gain_key(a1, 0, GainKey::new(30, -3.0)), "the point already there changes nothing" ); assert!(p.remove_gain_key(a1, 0, 0)); assert!(!p.remove_gain_key(a1, 0, 0), "no point left at 0"); assert_eq!(p.gain_keys_of(a1, 0), &[GainKey::new(30, -3.0)]); assert!(p.undo()); assert_eq!(p.gain_keys_of(a1, 0).len(), 2, "the removal undone"); assert!(p.undo()); assert_eq!( p.gain_keys_of(a1, 0), &[GainKey::new(0, 0.0), GainKey::new(30, -20.0)], "the replacement undone" ); assert!(p.undo()); assert_eq!(p.gain_keys_of(a1, 0), &[GainKey::new(0, 0.0)]); assert!(p.undo()); assert!(p.gain_keys_of(a1, 0).is_empty(), "back to flat"); assert!(!p.undo(), "and no step is left to take"); } #[test] fn gain_db_at_maps_timeline_to_source_through_in_point_and_speed() { let mut p = Project::single(FILE, 150); let a1 = Lane::new(LaneKind::Audio, 0); assert!(p.set_gain_keys( a1, 0, vec![GainKey::new(0, 0.0), GainKey::new(100, -20.0)] )); assert!((p.gain_db_at(a1, 0, 50).unwrap() + 10.0).abs() < 1e-4); assert_eq!( p.gain_db_at(Lane::V1, 0, 50), None, "a video lane has no envelope" ); assert_eq!(p.gain_db_at(a1, 3, 50), None, "no such clip"); assert!(p.set_speed(a1, 0, Speed::from_permille(2000)).is_ok()); assert!( (p.gain_db_at(a1, 0, 20).unwrap() + 8.0).abs() < 1e-3, "at 2x, timeline frame 20 plays source 40" ); assert!(p.set_clip_gain(a1, 0, -6.0)); assert!( (p.gain_db_at(a1, 0, 20).unwrap() + 14.0).abs() < 1e-3, "clip gain adds" ); } #[test] fn split_keeps_the_gain_curve_on_both_halves() { let mut p = Project::single(FILE, 150); let a1 = Lane::new(LaneKind::Audio, 0); assert!(p.set_gain_keys( a1, 0, vec![GainKey::new(0, 0.0), GainKey::new(100, -20.0)] )); let before: Vec<f32> = (0..150).map(|t| p.gain_db_at(a1, 0, t).unwrap()).collect(); assert!(p.split(60)); assert_eq!(p.lane(a1).len(), 2); for c in p.lane(a1) { assert_eq!( c.gain_keys, vec![GainKey::new(0, 0.0), GainKey::new(100, -20.0)], "both halves keep every point" ); } let after: Vec<f32> = (0..150) .map(|t| p.gain_db_at(a1, usize::from(t >= 60), t).unwrap()) .collect(); assert_eq!(before, after, "the curve is unchanged by the cut"); } #[test] fn gain_envelope_rides_the_fade_in_timeline_frames_and_scales_to_audio() { let mut p = Project::single(FILE, 150); let a1 = Lane::new(LaneKind::Audio, 0); assert!(p.set_gain_keys( a1, 0, vec![GainKey::new(0, 0.0), GainKey::new(30, -20.0)] )); let fade = p.lane_fades_from(a1, 0, 30.0)[0] .clone() .expect("an enveloped clip has a fade"); let minus_ten = 10f32.powf(-10.0 / 20.0); assert!( (fade.gain_at(15) - minus_ten).abs() < 1e-4, "frame 15 is -10 dB, which is {}", fade.gain_at(15) ); assert!( (fade.gain_at(45) - 0.1).abs() < 1e-4, "held at -20 dB past the last point" ); let scaled = fade.clone().scaled(1000.0); assert!( (scaled.gain_at(15_000) - fade.gain_at(15)).abs() < 1e-4, "audio frames" ); } #[test] fn gain_at_is_silent_at_a_fades_edge_and_unity_in_the_body() { let f = Fade { elapsed: 0, fade_in: 10, fade_out: 10, total: 40, gain: 1.0, pan: (1.0, 1.0), curve: None, }; assert_eq!(f.gain_at(0), 0.0, "silence at the very first frame"); assert_eq!(f.gain_at(10), 1.0, "unity the frame the ramp ends"); let midpoint = std::f32::consts::FRAC_PI_2 * 0.5; assert!( (f.gain_at(5) - midpoint.sin()).abs() < 1e-6, "equal-power midpoint is sin(pi/4)" ); assert_eq!( f.gain_at(20), 1.0, "unchanged in the body, past either ramp" ); // The fade-out is the same curve, mirrored, counting down from the // clip's end: unity at the frame the ramp starts, the same // equal-power midpoint, and -- since `total` itself is one past the // clip's last valid frame -- the very last frame is one step short of // the silence a fade-in's own frame `0` lands on exactly. assert_eq!(f.gain_at(30), 1.0, "unity the frame the fade-out starts"); assert!( (f.gain_at(35) - midpoint.sin()).abs() < 1e-6, "the fade-out's own equal-power midpoint" ); assert!( (f.gain_at(39) - (std::f32::consts::FRAC_PI_2 * 0.1).sin()).abs() < 1e-6, "one step short of silence at the clip's last playable frame" ); } /// [`Project::crossfade`] refuses a non-audio lane and two clips that are /// not adjacent, and otherwise sets the shared edge's fades and nothing /// else. #[test] fn crossfade_only_joins_adjacent_audio_clips() { let mut p = Project::single(FILE, 20); assert!(p.split(10)); assert!(!p.crossfade(Lane::V1, 0, 5), "video is not an audio lane"); assert!(!p.crossfade(Lane::A1, 1, 5), "there is no clip after idx 1"); assert!(p.crossfade(Lane::A1, 0, 5), "the two halves are adjacent"); let a = p.lane(Lane::A1); assert_eq!(a[0].fade_out, 5); assert_eq!(a[1].fade_in, 5); assert_eq!(a[0].fade_in, 0); assert_eq!(a[1].fade_out, 0); // A gap between two clips is not adjacency, even on an audio lane. let mut p2 = Project::single(FILE, 1); let a2 = p2.add_lane(LaneKind::Audio); assert!(p2.place(a2, 0, clip(0, 0, 3, 0))); assert!(p2.place(a2, 5, clip(5, 3, 6, 0)), "a gap before this one"); assert!(!p2.crossfade(a2, 0, 5), "a gap is not adjacent"); } /// The rule [`Project::write_split`] follows for the edge the razor makes: /// the left half keeps its old [`Clip::fade_in`] (clamped to its new, /// shorter length) and loses its [`Clip::fade_out`]; the right half is the /// mirror image. #[test] fn a_split_clamps_the_kept_fade_and_zeroes_the_cut_edge() { let mut p = Project::single(FILE, 20); assert!(p.set_fade_in(Lane::V1, 0, 15)); assert!(p.set_fade_out(Lane::V1, 0, 15)); assert!(p.split(5)); let v = p.lane(Lane::V1); assert_eq!( v[0].fade_in, 5, "the left half's kept fade-in, clamped to 5 frames" ); assert_eq!(v[0].fade_out, 0, "the cut edge it made starts flat"); assert_eq!(v[1].fade_in, 0, "the cut edge it made starts flat"); assert_eq!( v[1].fade_out, 15, "the right half's kept fade-out, unclamped: 15 frames fit in 15" ); } /// [`Project::set_transition_out`] refuses a non-video lane, a clip with /// no successor, and two clips that do not touch, and otherwise clamps /// the dissolve to the shorter of the two clips it spans. #[test] fn set_transition_out_only_dissolves_adjacent_video_clips() { let mut p = Project::single(FILE, 20); assert!(p.split(15), "a short 5-frame tail to clamp against"); assert!( !p.set_transition_out(Lane::A1, 0, 5), "audio is not a video lane" ); assert!( !p.set_transition_out(Lane::V1, 1, 5), "there is no clip after idx 1" ); assert!( p.set_transition_out(Lane::V1, 0, 100), "the two halves are adjacent" ); assert_eq!( p.transition_out_of(Lane::V1, 0), 5, "clamped to the shorter neighbour's 5 frames" ); // A gap between two clips is not adjacency, even on the video lane. let mut p2 = Project::single(FILE, 1); let v2 = p2.add_lane(LaneKind::Video); assert!(p2.place(v2, 0, clip(0, 0, 3, 0))); assert!(p2.place(v2, 5, clip(5, 3, 6, 0)), "a gap before this one"); assert!(!p2.set_transition_out(v2, 0, 5), "a gap is not adjacent"); } /// The two transition doors' bool is the WEAK shape, stated here as /// behaviour rather than left as something a front-end has to know: both /// answer "these two clips are adjacent", and both snapshot and clamp -- /// so a write that lands the value the join already carries is still /// reported as a write. The step it pushed changed nothing, so it is settled /// away and costs no undo step. The front-end's before/after comparison /// (`Player::transition_edges`) still decides what it shows. #[test] fn a_transition_door_says_yes_to_a_write_that_moved_nothing() { let mut p = Project::single(FILE, 20); assert!(p.split(10), "two halves to join"); assert!(p.set_transition_out(Lane::V1, 0, 5), "the dissolve lands"); assert!(p.crossfade(Lane::A1, 0, 5), "the crossfade lands"); let history = p.history_len(); // The very same numbers again: nothing moved, and both doors report a // write anyway. assert!( p.set_transition_out(Lane::V1, 0, 5), "set_transition_out answers 'the clips are adjacent', not 'the \ dissolve moved', so a no-op reads as a write" ); assert!( p.crossfade(Lane::A1, 0, 5), "crossfade answers the same way for the same reason" ); assert_eq!( p.history_len(), history, "and neither spent an undo step: a write that changed nothing is settled away" ); // What did not move -- the comparison the front-end makes: both edges // a crossfade writes, and the dissolve itself. let a = p.lane(Lane::A1); assert_eq!((a[0].fade_out, a[1].fade_in), (5, 5), "unchanged"); assert_eq!(p.transition_out_of(Lane::V1, 0), 5, "unchanged"); // And a refusal still answers no, so the arm is not simply always on: // the last clip has no successor to dissolve into. assert!( !p.set_transition_out(Lane::V1, 1, 5), "no neighbour end to end" ); } /// The rule [`Project::write_split`] follows for [`Clip::transition_out`], /// same shape as [`a_split_clamps_the_kept_fade_and_zeroes_the_cut_edge`]: /// only the half that kept the clip's original *end* keeps the dissolve, /// re-clamped to its own now-shorter length; the head the cut just made /// has no successor to dissolve into and starts flat. #[test] fn a_split_clamps_the_tail_transition_and_zeroes_the_head() { let mut p = Project::single(FILE, 20); assert!( !p.set_transition_out(Lane::V1, 0, 0), "one clip on the lane, no successor to dissolve into yet" ); // Give the clip a dissolve by hand, then cut it in two. p.lane_mut(Lane::V1).unwrap()[0].transition_out = 15; assert!(p.split(5)); let v = p.lane(Lane::V1); assert_eq!( v[0].transition_out, 0, "the cut edge it made has no successor" ); assert_eq!( v[1].transition_out, 15, "the tail's kept dissolve, unclamped: 15 frames fit in 15" ); } /// The music video's own path: the take comes apart into halves that carry /// ids of their own, one undo puts it back, and two clips over the same /// frames become one take again by hand. #[test] fn a_take_comes_apart_and_goes_back_together() { let mut p = Project::single(FILE, 9); let one = p.clips()[0].link.expect("a fresh project is one take"); assert_eq!(p.lane(Lane::A1)[0].link, Some(one)); // A third lane in the same group -- and the merge case with it: a // placement is in no group, and grouping it in leaves *one* group. let v2 = p.add_lane(LaneKind::Video); assert!(p.place(v2, 0, clip(0, 0, 9, 0))); assert!(p.lane(v2)[0].link.is_none(), "a placement joins no group"); p.group(Lane::V1, 0, v2, 0) .expect("the same frames, one lane over"); let id = p.clips()[0].link.expect("still a group"); assert!( p.lanes().into_iter().all(|l| p.lane(l)[0].link == Some(id)), "one id over the three, not two groups sharing a span" ); links_are_consistent(&p.lanes).expect("one id, one span"); // Detached by the sound: every half of the group, not only the two. assert!(p.ungroup(Lane::A1, 0)); let ids: Vec<Option<u32>> = p.lanes().into_iter().map(|l| p.lane(l)[0].link).collect(); assert!( ids.iter().all(Option::is_some), "a half keeps an id of its own" ); assert!( ids.iter() .enumerate() .all(|(i, a)| ids[..i].iter().all(|b| a != b)), "and no two halves are the same group any more: {ids:?}" ); links_are_consistent(&p.lanes).expect("a lone id is legal"); // One snapshot for the whole detach. assert!(p.undo()); assert!( p.lanes().into_iter().all(|l| p.lane(l)[0].link == Some(id)), "one undo puts the whole group back" ); // And back together by hand: the two the pointer named, and only them. assert!(p.ungroup(Lane::V1, 0)); p.group(Lane::V1, 0, Lane::A1, 0) .expect("both cover [0, 9)"); assert_eq!(p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link); assert_ne!( p.lane(v2)[0].link, p.lane(Lane::V1)[0].link, "the third half stayed detached" ); links_are_consistent(&p.lanes).expect("one id, one span"); } /// What a group may not be, and what a detach has nothing to take apart -- /// none of which may cost an undo step. #[test] fn a_group_is_one_span_and_one_clip_per_lane() { let mut p = three(); let before = shape(&p); let why = |r: crate::Result<()>| r.expect_err("refused").to_string(); assert!( why(p.group(Lane::V1, 0, Lane::V1, 1)).contains("one clip per lane"), "two clips of one lane are never one take" ); assert!(why(p.group(Lane::V1, 0, Lane::A1, 9)).contains("there is no clip 9 on A1")); let spans = why(p.group(Lane::V1, 0, Lane::A1, 1)); assert!( spans.contains("V1 covers [0, 3) and A1 covers [3, 5)"), "{spans}" ); assert!( spans.contains("trim them to matching bounds first"), "{spans}" ); assert!(why(p.group(Lane::V1, 0, Lane::A1, 0)).contains("one take already")); // A half nothing else is grouped with is already detached, a placement // is in no group at all, and an index that is not there has nothing to // detach either. assert!(p.place(Lane::V1, 20, clip(20, 0, 3, 0))); assert!(!p.ungroup(Lane::V1, 3), "a placement is in no group"); assert!(p.lift(Lane::A1, 0)); assert!(!p.ungroup(Lane::V1, 0), "its group has no other half"); assert!(!p.ungroup(Lane::V1, 9), "no clip there"); // Two undos, one per edit: not one of the refusals pushed a snapshot. assert!(p.undo()); assert!(p.undo()); assert_eq!(shape(&p), before); } /// Where a re-rate puts a playhead: the scene under the cursor stays the /// scene under the cursor, measured the way a viewer would -- the source /// frame playing at the playhead is the same frame after the write, at /// every playhead position the re-rate can catch one. #[test] fn a_re_rate_keeps_the_frame_under_the_playhead() { 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, }; // A group of a 300-frame clip and a caption offset 30 frames into it. let mut p = Project::single(FILE, 300).with_subtitles(vec![track()]); let s1 = p.add_lane(LaneKind::Subtitle); p.place_sub(s1, 30, caption(30, 240)).expect("placed"); p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); // The frame playing at `at`: which clip, and which source frame of it. let playing = |p: &Project, at: u32| -> Option<(usize, u32)> { p.span_at(Lane::V1, at).and_then(|s| s.from) }; // Inside the held clip: the source frame under the playhead is the // same one after the re-rate, at 2x and at half speed both. for (what, speed) in [("2x", 2000u16), ("half", 500)] { let at = 120; let before = playing(&p, at).expect("a clip under the playhead"); let mapped = p.speeded_playhead(Lane::V1, 0, Speed::from_permille(speed), at); p.set_speed(Lane::V1, 0, Speed::from_permille(speed)) .expect("room for it"); let after = playing(&p, mapped.unwrap()).expect("still a clip there"); assert_eq!(before, after, "{what}: the same source frame plays"); // ...and the write itself is untouched by the question. assert!(p.undo(), "{what}: one step back"); } // Past the group -- the clip ends at 300 and the caption at 270, so // from 300 on nothing is re-timed under the playhead: unmoved. for at in [300u32, 301, 1_000] { assert_eq!( p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), at), Some(at), "at {at}: outside every member, unmoved" ); } // The boundary is exact: the held clip's own last frame (299) maps // to its new one (149 -- the last frame inside [0, 150), where the // shrunken clip's last source frame plays), and its first stays put. assert_eq!( p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), 299), Some(149) ); assert_eq!( p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), 0), Some(0) ); // A spot the caption shares with the clip maps by the clip (it is // what is *playing* there): the held clip's own proportion, which on // this fixture is also its head. assert_eq!( p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), 90), Some(45), "the clip under the caption owns the spot" ); } /// A playhead inside another clip member of the group: that member keeps /// its own head, so the mapping is by its proportion and not the held /// clip's -- and the frame under the cursor is still the frame that was. #[test] fn a_re_rate_maps_a_second_clip_member_about_its_own_head() { let mut p = Project::single(FILE, 300); // A second clip, grouped with the first but starting 300 later: the // head the group shares and its own differ by exactly that. let v2 = p.add_lane(LaneKind::Video); assert!(p.lift(Lane::A1, 0), "the sound half stands aside"); assert!(p.place(v2, 300, clip(300, 0, 300, 0)), "placed on v2"); p.group_all(&[(Lane::V1, 0), (v2, 0)]).expect("grouped"); let at = 450; // inside the second member, 150 past its head let before = p.span_at(v2, at).and_then(|s| s.from); let mapped = p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), at); p.set_speed(Lane::V1, 0, Speed::from_permille(2000)) .expect("room for it"); let after = p.span_at(v2, mapped.unwrap()).and_then(|s| s.from); assert_eq!(before, after, "the second member's own frame plays"); // ...and the mapping itself: 150 past its head halves, the head stays. assert_eq!(mapped, Some(375)); } /// The link counter a load seeds sits past every id the file names, /// captions included: a hand-grouped caption may carry the highest id there /// is, and a counter that read the clips alone would hand it to the next /// group that asked -- silently grouping the caption with clips it never /// met. #[test] fn the_link_counter_sits_past_a_caption_only_id() { 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, 9); p.add_lane(LaneKind::Subtitle); let caption = SubClip { start: 0, frames: 6, track: 0, in_us: 0, out_us: 6_000_000, // The highest id in the file, and only a caption carries it. link: Some(1), }; let mut p = p .with_subtitles(vec![track()]) .with_subs(vec![Vec::new(), Vec::new(), vec![caption]]) .expect("a caption may carry the file's highest id"); // A new group on lanes the caption is not on mints the next id -- which // must not be the caption's. let v2 = p.add_lane(LaneKind::Video); assert!(p.place(v2, 0, clip(0, 0, 9, 0))); p.group_all(&[(Lane::V1, 0), (v2, 0)]) .expect("the clips group"); let minted = p.lane(Lane::V1)[0].link.expect("grouped"); assert_ne!(minted, 1, "the caption's id is not re-issued to clips"); assert_eq!( p.sub_lane(Lane::new(LaneKind::Subtitle, 0))[0].link, Some(1), "the caption keeps its own group" ); 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)); } /// [`Project::speeded_playhead`]'s caption arm fires only when the /// playhead is inside a caption member and outside every clip member -- /// a shape every other fixture avoids by keeping its caption inside the /// held clip. Here the caption overhangs the clip's tail, and the /// playhead standing in that overhang is answered by the caption's own /// piece: it scales about the held clip's head by the held clip's own /// proportion, same as [`write_speed`](Project::write_speed) re-times it. #[test] fn the_caption_arm_answers_a_playhead_in_the_overhang_past_its_clip() { 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); // The caption spans [80, 100) -- ten frames past the clip's own end // at 90 -- grouped with the clip so it re-times by the clip's ratio. p.place_sub(s1, 80, caption(80, 20)).expect("placed"); p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); // At 2x the clip halves to 45 frames; the overhang frame 95, which // no clip member's old span reaches, is the caption's own question: // half of 95 is 47.5, rounded to 48. assert_eq!( p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), 95), Some(48), "the overhang frame answers through the caption's own piece" ); } /// `write_speed` and `speeded_playhead` are two questions of the SAME /// map ([`map`]'s architecture law): a caption's landed span comes from /// the map's own ends, and the playhead standing at the caption's own /// last frame before the write has to land exactly where the write put /// it -- across clip lengths and rates that round differently, and an /// odd-length caption straddling the clip's own tail edge, the corner /// where `write_speed` used to answer a frame short. #[test] fn write_speed_and_speeded_playhead_agree_at_a_captions_own_last_frame() { 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, }; for &len in &[90u32, 91, 177, 300, 301] { for &permille in &[500u16, 2000, 2500, 3000] { let mut p = Project::single(FILE, len).with_subtitles(vec![track()]); let s1 = p.add_lane(LaneKind::Subtitle); // An odd-length caption straddling the clip's tail edge: its // own last frame lands past the held clip. let c_start = len.saturating_sub(3); p.place_sub(s1, c_start, caption(c_start, 7)) .expect("placed"); p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); let speed = Speed::from_permille(permille); let last = c_start + 7 - 1; let mapped = p .speeded_playhead(Lane::V1, 0, speed, last) .expect("the caption is there"); if p.set_speed(Lane::V1, 0, speed).is_err() { continue; // a refusal changes nothing: not this law's cell } let landed = p.sub_lane(s1)[0]; if permille >= 1000 { // A speed-up compresses the timeline: many old frames // share one new frame, so the caption's own last old // frame lands flush with the write's own boundary -- // the exact law [`write_speed`]'s worked drift-fix case // is stated against. assert_eq!( mapped, landed.end() - 1, "len {len} permille {permille}: the playhead at the caption's own \ last frame lands where the write actually put it" ); } else { // A slow-down expands it: one old frame becomes several // new ones, and `apply` answers where that frame's own // block *starts*, not where the caption's last new // frame (the end of that same block) sits -- a // one-frame gap inherent to the piece's own semantics, // not a drift between the two callers. Both still have // to agree the answer sits inside the caption the write // actually landed. assert!( (landed.start..landed.end()).contains(&mapped), "len {len} permille {permille}: the playhead lands inside the \ caption the write actually put ({landed:?}), got {mapped}" ); } } } } /// 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, words: Vec::new(), karaoke: Vec::new(), }], 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::<Vec<_>>(), [(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::<Vec<_>>(), [(4_000_000, 4_500_000)], "the cue crosses with the clip's new rate" ); } /// A video arriving on a timeline of words mutates none of them: the /// import's rippling insert used to open room on the subtitle lanes too, /// splitting a caption it landed inside and sliding the rest behind the /// video -- the hand asked for an add, and got a cut it never made. The /// words keep their own clock; co-travel is what a group is for. #[test] fn an_arriving_clip_never_touches_the_captions() { let caption = |start: u32, frames: u32| SubClip { start, frames, track: 0, in_us: i64::from(start) * 1_000_000 / 30, out_us: i64::from(start + frames) * 1_000_000 / 30, 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, }; // A timeline of words alone: 60 frames of caption, no media clips. let mut p = Project::single(FILE, 60).with_subtitles(vec![track()]); for l in [Lane::V1, Lane::A1] { while !p.lane(l).is_empty() { p.lift(l, 0); } } let s1 = p.add_lane(LaneKind::Subtitle); p.place_sub(s1, 0, caption(0, 60)).expect("placed"); let before = p.sub_lane(s1).to_vec(); // The video lands BEFORE the caption's span: nothing moves. let vid = clip(0, 0, 30, 0); assert!(p.paste(0, vid.clone()), "the video goes down at the head"); assert_eq!(p.sub_lane(s1), &before[..], "the caption keeps its clock"); // ...and OVERLAPPING it, dead centre: no split, no shift, byte for // byte the placement the hand made. assert!(p.paste(15, vid), "a second video lands inside the caption"); assert_eq!(p.sub_lane(s1), &before[..], "still one caption, unmoved"); assert_eq!(p.sub_lane(s1).len(), 1, "and uncut"); assert_eq!(p.lane(Lane::V1).len(), 3, "the videos landed"); assert!(links_are_consistent(&p.lanes).is_ok()); } /// 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 /// region's new end -- never an overlap on the lane. #[test] fn a_regions_caption_pieces_never_round_onto_one_another() { 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 = |n| SubtitleTrack { path: FILE.into(), track: None, language: "eng".into(), name: String::new(), label: format!("eng{n}"), cues: Vec::new(), bitmap: false, refused: None, }; // Three lanes, one repro shape each: adjacent one-frame cues, a // three-frame pair, and the region's last cue against the first // caption behind it. The region [0, 30) plays at 2x: 15 frames. let mut p = Project::single(FILE, 60).with_subtitles(vec![track(0), track(1), track(2)]); let lanes: Vec<Lane> = (0..3).map(|_| p.add_lane(LaneKind::Subtitle)).collect(); p.place_sub(lanes[0], 9, caption(9, 1)).expect("placed"); p.place_sub(lanes[0], 10, caption(10, 1)).expect("placed"); p.place_sub(lanes[1], 5, caption(5, 3)).expect("placed"); p.place_sub(lanes[1], 8, caption(8, 3)).expect("placed"); p.place_sub(lanes[2], 29, caption(29, 1)).expect("placed"); p.place_sub(lanes[2], 30, caption(30, 15)).expect("placed"); let mut scope = vec![Lane::V1, Lane::A1]; scope.extend(lanes.iter().copied()); p.speed_regions(&[(0, 30)], Speed::from_permille(2000), &scope) .expect("the region speeds up"); // Adjacent one-frame cues: both map to frame 5, and are forced apart. let spans = |p: &Project, lane: Lane| -> Vec<(u32, u32)> { p.sub_lane(lane) .iter() .map(|s| (s.start, s.frames)) .collect() }; assert_eq!( spans(&p, lanes[0]), [(5, 1), (6, 1)], "one frame each, in order" ); // The three-frame pair: [5, 8) and [8, 11) map to [3, 5) and [4, 6), // and the walk pushes the second to where the first ends. assert_eq!( spans(&p, lanes[1]), [(3, 1), (4, 2)], "forced apart, kept in order" ); // The last cue of the region ends exactly where the region now does // (frame 15), and the caption behind it lands on that same frame: // touching, never overlapping. assert_eq!( spans(&p, lanes[2]), [(14, 1), (15, 15)], "the cue ends with the region" ); for &lane in &lanes { assert!(subs_sorted_disjoint(&p.lanes[p.index(lane).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::<Vec<_>>(), [(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] fn a_hand_built_group_holds_clips_and_a_caption() { 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, 9).with_subtitles(vec![track()]); let v2 = p.add_lane(LaneKind::Video); let s1 = p.add_lane(LaneKind::Subtitle); // A layer at its own offset, and a caption over the head of the take. assert!(p.place(v2, 2, clip(2, 0, 7, 0))); p.place_sub(s1, 0, caption(0, 6)).expect("placed"); // Too few, one lane twice, and an index that is not there: words, no // snapshot. let history = p.history.len(); assert!( p.group_all(&[]) .unwrap_err() .to_string() .contains("two placements or more") ); assert!( p.group_all(&[(Lane::V1, 0), (Lane::V1, 0)]) .unwrap_err() .to_string() .contains("V1 is picked twice") ); assert!( p.group_all(&[(Lane::V1, 0), (s1, 9)]) .unwrap_err() .to_string() .contains("no subtitle 9") ); assert_eq!(p.history.len(), history, "refusals cost no undo step"); // The group: the picture, the layer, and the caption -- and the sound // the picture was already grouped with comes along unasked. p.group_all(&[(Lane::V1, 0), (v2, 0), (s1, 0)]) .expect("three picks, three lanes"); let id = p.lane(Lane::V1)[0].link.expect("grouped"); for lane in [Lane::V1, Lane::A1, v2] { assert_eq!( p.lane(lane)[0].link, Some(id), "{} rides along", lane.label() ); } assert_eq!(p.sub_lane(s1)[0].link, Some(id), "the caption is a member"); assert_eq!(p.sub_lane(s1)[0].frames, 6, "its own span, kept"); assert_eq!(p.lane(v2)[0].start, 2, "the layer's offset, kept"); assert!(links_are_consistent(&p.lanes).is_ok()); assert_eq!(p.history.len(), history + 1, "one snapshot for the group"); // ...and one undo takes it apart again. assert!(p.undo()); assert_eq!(p.sub_lane(s1)[0].link, None, "the caption is loose again"); // Detach names a caption like it names a clip: every member an id of // its own, which is never `None` (see `ungroup`). p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("two picks"); assert!(p.ungroup(s1, 0)); let (loose_sub, loose_clip) = (p.sub_lane(s1)[0].link, p.lane(Lane::V1)[0].link); assert!(loose_sub.is_some() && loose_clip.is_some()); assert_ne!(loose_sub, loose_clip); assert!(links_are_consistent(&p.lanes).is_ok()); } /// Two captions on one sub lane, each already grouped with a *different* /// clip: `group_all` over both those clips would merge the two existing /// groups into one id and leave both captions on `s1` carrying it -- /// `links_are_consistent`'s one-member-per-lane law broken the moment the /// group closes. Refused by name, and nothing moves. #[test] fn group_all_refuses_a_merge_that_doubles_up_a_lane() { 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 v2 = p.add_lane(LaneKind::Video); let s1 = p.add_lane(LaneKind::Subtitle); assert!(p.place(v2, 0, clip(0, 0, 90, 0))); p.place_sub(s1, 0, caption(0, 30)).expect("placed"); p.place_sub(s1, 30, caption(30, 30)).expect("placed"); // V1 grouped with the first caption, v2 with the second -- two // separate groups, each already one member per lane. p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); p.group_all(&[(v2, 0), (s1, 1)]).expect("grouped"); let (v1_link, v2_link) = (p.lane(Lane::V1)[0].link, p.lane(v2)[0].link); let history = p.history.len(); let err = p .group_all(&[(Lane::V1, 0), (v2, 0)]) .unwrap_err() .to_string(); assert!(err.contains("one placement per lane"), "{err}"); assert_eq!(p.history.len(), history, "the refusal costs no undo step"); assert_eq!( p.lane(Lane::V1)[0].link, v1_link, "the first group untouched" ); assert_eq!(p.lane(v2)[0].link, v2_link, "the second group untouched"); assert!(links_are_consistent(&p.lanes).is_ok()); } /// A split hands the caption of a group the same ids it hands the clips: /// the left halves keep the group, the right halves share a fresh one. #[test] fn a_split_cuts_a_grouped_caption_with_its_clips() { 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, 9).with_subtitles(vec![track()]); let s1 = p.add_lane(LaneKind::Subtitle); p.place_sub(s1, 0, caption(0, 9)).expect("placed"); p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); let take = p.lane(Lane::V1)[0].link.expect("the group id"); assert!(p.split(4)); let (left, right) = (p.sub_lane(s1)[0], p.sub_lane(s1)[1]); assert_eq!(left.link, Some(take), "the caption's head keeps the group"); assert_eq!(left.frames, 4); assert_eq!( right.link, p.lane(Lane::V1)[1].link, "the caption's tail shares the clips' fresh id" ); assert_ne!(right.link, Some(take)); assert!(links_are_consistent(&p.lanes).is_ok()); // Regroup at the seam restores the take, caption included. assert!(p.regroup(4)); assert_eq!(p.sub_lane(s1).len(), 1, "the caption rejoined"); assert_eq!(p.sub_lane(s1)[0].link, Some(take)); } /// Deleting a member of a hand-built group takes the group: every /// member's own span out of its own lane, hole closed -- the lanes may /// end up out of step, which is what one undo restores together. #[test] fn deleting_a_grouped_clip_ripples_every_members_own_lane() { 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, 9).with_subtitles(vec![track()]); let s1 = p.add_lane(LaneKind::Subtitle); p.place_sub(s1, 2, caption(2, 4)).expect("placed"); p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); // A neighbour on a lane the group does not touch. let v2 = p.add_lane(LaneKind::Video); assert!(p.place(v2, 10, clip(10, 0, 3, 0))); // By the picture or by the caption, the same whole-group delete. assert!(p.delete_sub_in(s1, 0)); assert!(p.lane(Lane::V1).is_empty(), "the take's span left V1"); assert!( p.sub_lane(s1).is_empty(), "the caption's span left its lane" ); assert_eq!( p.lane(v2)[0].start, 10, "a lane the group does not touch stays" ); assert!(links_are_consistent(&p.lanes).is_ok()); assert!(p.undo(), "one step puts the group back"); assert_eq!(p.sub_lane(s1)[0].start, 2, "offsets and all"); } /// The delete law, one statement for every anchor: a caption deleted /// WITH its group ripples its lane exactly as its clip siblings do -- /// the hole closes and what follows slides up -- while a caption lifted /// alone leaves its gap, as it always has. And the delete of a grouped /// clip takes the group from whichever member was clicked. #[test] fn a_grouped_delete_closes_the_caption_lane_and_takes_the_group_from_every_anchor() { 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, }; // V+A+S grouped, with a second caption BEHIND the group's caption: // closing the hole is what the second caption's position says. let group = || { let mut p = Project::single(FILE, 90).with_subtitles(vec![track()]); let s1 = p.add_lane(LaneKind::Subtitle); p.place_sub(s1, 30, caption(30, 30)) .expect("the group's caption"); p.place_sub(s1, 90, caption(90, 30)) .expect("the caption behind"); p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); (p, s1) }; // Deleted by the picture: all three lanes close. let (mut p, s1) = group(); assert!(p.delete_in(Lane::V1, 0)); assert!(p.lane(Lane::V1).is_empty(), "the clip's span left V1"); assert!(p.lane(Lane::A1).is_empty(), "and the sound's"); assert_eq!( p.sub_lane(s1).iter().map(|s| s.start).collect::<Vec<_>>(), [60], "the caption's lane closed by its own span: the follower slid up" ); assert!(p.undo(), "one step"); assert_eq!( p.sub_lane(s1).len(), 2, "both captions back, offsets and all" ); // By the sound: the same whole-group delete. let (mut p, s1) = group(); assert!(p.delete_in(Lane::A1, 0)); assert!(p.lane(Lane::V1).is_empty() && p.lane(Lane::A1).is_empty()); assert_eq!(p.sub_lane(s1)[0].start, 60, "the caption lane closed too"); // By the caption itself: the same again. let (mut p, s1) = group(); assert!(p.delete_sub_in(s1, 0)); assert!(p.lane(Lane::V1).is_empty() && p.lane(Lane::A1).is_empty()); assert_eq!( p.sub_lane(s1)[0].start, 60, "closed from the caption's own door" ); assert!(links_are_consistent(&p.lanes).is_ok()); // A caption lifted ALONE still leaves its gap: the ungrouped law, // unchanged. let (mut p, s1) = group(); assert!(p.ungroup(s1, 0), "the caption stands alone"); assert!(p.lift_sub(s1, 0)); assert_eq!( p.sub_lane(s1)[0].start, 90, "the follower stayed put: a lone caption's lift is a gap" ); } /// Dragging a grouped caption carries its clips, one delta for all of them /// -- and a caption in no group refuses an overlap exactly as it always did. #[test] fn dragging_a_grouped_caption_carries_its_group() { 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, 9).with_subtitles(vec![track()]); let s1 = p.add_lane(LaneKind::Subtitle); p.place_sub(s1, 2, caption(2, 4)).expect("placed"); p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); // Same lane, further along: everyone moves by the same eight frames. p.move_sub(s1, 0, s1, 10).expect("room for all of it"); assert_eq!(p.sub_lane(s1)[0].start, 10); assert_eq!( p.lane(Lane::V1)[0].start, 8, "the clip follows by the delta" ); assert!(links_are_consistent(&p.lanes).is_ok()); // ...and an ungrouped caption is the caption it always was. assert!(p.ungroup(s1, 0)); let mut q = Project::single(FILE, 9).with_subtitles(vec![track()]); let s1 = q.add_lane(LaneKind::Subtitle); q.place_sub(s1, 0, caption(0, 4)).expect("placed"); q.place_sub(s1, 5, caption(5, 4)).expect("placed"); assert!( q.move_sub(s1, 0, s1, 3) .unwrap_err() .to_string() .contains("already covers") ); } /// A trim carries a group's caption by the same delta as its clips, clamped /// to the caption's own walls -- the offsets between the members survive. #[test] fn trimming_a_grouped_clip_carries_the_caption_by_the_same_delta() { 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, 9).with_subtitles(vec![track()]); let s1 = p.add_lane(LaneKind::Subtitle); // The caption sits two frames into the take and ends one early. The // source runs past the clip, or there is no tail to pull out. let src = &[20]; p.place_sub(s1, 2, caption(2, 6)).expect("placed"); p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped"); // The tail goes out by three: the clip to 12, the caption to 11. assert!(p.trim(Lane::V1, 0, Edge::End, 12, src)); assert_eq!(p.lane(Lane::V1)[0].end(), 12); assert_eq!( (p.sub_lane(s1)[0].start, p.sub_lane(s1)[0].end()), (2, 11), "the caption follows by the delta" ); assert!(p.sub_lane(s1)[0].out_us > p.sub_lane(s1)[0].in_us); // The head comes in by two: everyone's start moves two along. assert!(p.trim(Lane::V1, 0, Edge::Start, 2, src)); assert_eq!(p.sub_lane(s1)[0].start, 4, "the offset is kept"); assert!(links_are_consistent(&p.lanes).is_ok()); } #[test] fn split_refused_at_zero_boundary_end_and_in_a_gap() { let mut p = three(); let before = shape(&p); assert!(!p.split(0), "timeline 0 has nothing before it"); assert!(!p.split(3), "existing boundary"); assert!(!p.split(5), "existing boundary"); assert!(!p.split(9), "one past the last frame"); assert!(!p.split(1_000)); assert_eq!(shape(&p), before); // one undo lands before the *second* split, so the refusals pushed nothing assert!(p.undo()); assert_eq!(p.clips().len(), 2, "refused splits push no history"); // ...and a frame that is a gap in both lanes has nothing to split. let mut p = three(); assert!(p.lift(Lane::V1, 1)); assert!(p.lift(Lane::A1, 1)); assert!(!p.split(4), "a gap is not a clip"); } #[test] fn regroup_refuses_what_a_split_could_not_have_made() { let mut p = three(); // [3,5) and [5,9) are source-contiguous: that pair rejoins. assert!(p.regroup(5)); assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 9, 0)]); // A boundary with a gap on one side, and one where the sources do not // meet, are both refused. let mut p = three(); assert!(p.lift(Lane::V1, 1)); assert!(p.lift(Lane::A1, 1)); assert!(!p.regroup(3), "nothing starts at 3 any more"); let mut p = three(); for l in p.lanes() { assert!(p.place(l, 3, clip(0, 100, 102, 0))); } assert!(!p.regroup(3), "source 100 does not follow source 3"); assert!(!p.regroup(0), "nothing ends at 0"); assert!(!p.regroup(1_000)); } #[test] fn map_and_spans_sweep_every_boundary() { let p = three(); assert_eq!(p.timeline_frames(), 9); assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 4)]); // contiguous source here, so the clip index is what actually moves let expect = [ (0, (0, 0)), (1, (0, 1)), (2, (0, 2)), (3, (1, 3)), // half-open: the boundary frame belongs to the NEXT clip (4, (1, 4)), (5, (2, 5)), (6, (2, 6)), (7, (2, 7)), (8, (2, 8)), ]; for (t, want) in expect { assert_eq!(p.map_timeline(t), Some(want), "timeline frame {t}"); } assert_eq!(p.map_timeline(9), None); // A span is the rest of the clip from where it was asked about. assert_eq!( p.span_at(Lane::V1, 6), Some(Span { reverse: false, ramp: None, start: 6, len: 3, from: Some((0, 6)), speed: Speed::NORMAL, pitch: 0, }) ); assert_eq!(p.span_at(Lane::V1, 9), None); } /// The fit policy is one byte *in* the clip rather than an index into a /// table like the eq and the grade: there is nothing for a table to share. /// It costs the clip nothing at all -- the fields before it (a `usize` /// source and two `Option<u16>`s) already left the struct padded to 40 /// bytes, and the byte landed in that padding. `fade_in`/`fade_out` are two /// `u32`s after it (48 bytes), and `transition_out` is a third word -- /// 52 bytes of fields, but the struct's own alignment is 8 (from the /// `usize` source and the 8-byte `Option<u32>` link), so an odd number of /// words after it pads out to the next multiple of 8: 56 bytes total. /// The transform index is a third `Option<u16>`, same reason as the eq and /// colour ones before it -- it lands in bytes already spent on padding, so /// the struct still costs 56. /// The visualizer look is the first field whose own width outgrows the /// padding it lands in: it is a `u64` (a strand colour needs bytes above /// the clip's own four), so the words after it come to 60 and the /// alignment rounds them to 64 -- one word more than a clip used to cost. /// Since the gain envelope (`gain_db` plus the `gain_keys` `Vec`, 24 bytes /// of pointer, length and capacity) the clip is 96 bytes and no longer /// `Copy`; the `Vec` is what the copy-cost comment above no longer covers. /// The placement keys (`transform_keys`, a second `Vec`) take it to 120. /// The fit policy itself is still one byte: it sits in the padding among /// the other fields, and the total is the 224 (144 before the opacity and colour keys' `Vec`s, /// 192 before the effects stack's) /// rounded up to the alignment of 8, so removing it would not shrink the clip. /// This asserts the one-byte enum and the exact size, so any further growth /// shows up here first. #[test] fn a_fit_policy_costs_the_clip_no_word() { assert_eq!(std::mem::size_of::<FitPolicy>(), 1); assert_eq!( std::mem::size_of::<Clip>(), 224, "Clip changed size: {} bytes", std::mem::size_of::<Clip>() ); } /// The clip a copy would hand back: source `[100, 102)`, unrelated to /// anything in `three()` so it is recognisable wherever it lands. const PASTED: Clip = Clip { speed_keys: Vec::new(), reversed: false, stabilise: None, deinterlace: Deinterlace::Frame, mark: ClipMark { label: 0, off: false }, angle: Angle { video: 0, audio: None }, fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: crate::transitions::TransitionKind::Dissolve, visualizer: 0, viz_paint: 0, gain_db: 0.0, gain_keys: Vec::new(), pan: 0, transform_keys: Vec::new(), opacity_keys: Vec::new(), color_keys: Vec::new(), effects: Vec::new(), start: 0, in_frame: 100, out_frame: 102, source: 0, link: None, eq: None, color: None, transform: None, fit: FitPolicy::Fit, speed: Speed::NORMAL, pitch: 0, }; #[test] fn paste_mid_clip_splits_around_it() { let mut p = three(); assert!(p.paste(6, PASTED)); // inside [5,9), one frame in assert_eq!(p.timeline_frames(), 11); assert_eq!( shape(&p)[0], vec![ clip(0, 0, 3, 0), clip(3, 3, 5, 0), clip(5, 5, 6, 0), clip(6, 100, 102, 0), clip(8, 6, 9, 0), ] ); assert_eq!(shape(&p)[1], shape(&p)[0], "a paste lands on both lanes"); // The pasted frames are exactly where they were asked for, and the // split-off remainder resumes after them. assert_eq!(p.map_timeline(6), Some((3, 100))); assert_eq!(p.map_timeline(7), Some((3, 101))); assert_eq!(p.map_timeline(8), Some((4, 6))); } #[test] fn paste_at_boundary_zero_and_end() { let mut p = three(); assert!(p.paste(3, PASTED), "existing boundary: no split"); assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 2), (7, 4)]); assert_eq!(p.map_timeline(3), Some((1, 100))); let mut p = three(); assert!(p.paste(0, PASTED)); assert_eq!(p.map_timeline(0), Some((0, 100))); assert_eq!(p.map_timeline(2), Some((1, 0))); // At the end: appended, there being nothing in front to leave black. let mut p = three(); assert!(p.paste(9, PASTED)); assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 4), (9, 2)]); // Past it: down on the frame it was asked for, black in front of it -- // a row let go on the open bed lands under the ghost that promised it. // The clipboard's clamp lives in `PlaybackSession::paste_at`. assert!(p.paste(1_000, PASTED)); assert_eq!(p.timeline_frames(), 1_002); assert_eq!( p.lane(Lane::V1).last().expect("the pasted clip").start, 1_000 ); assert_eq!( p.lane(Lane::A1).last().expect("its other half").start, 1_000 ); // An empty clip is the one thing refused, and it pushes no history. let mut p = three(); assert!(!p.paste(4, clip(0, 7, 7, 0))); assert_eq!(p.clips().len(), 3); } /// The data-loss paste: a clip of a source with no picture used to land on /// `V1` as well as `A1`, where it decodes to nothing -- and the save that /// carried it was refused on the way back in ("a box with a larger size"). #[test] fn a_paste_of_a_source_with_no_picture_stays_off_the_video_lane() { let mut p = three(); let wav = p.import("/nonexistent/song.wav", 0); let copied = Clip { fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), visualizer: 0, viz_paint: 0, gain_db: 0.0, source: wav, ..PASTED }; assert!(p.paste(3, copied)); // The room is opened on every lane, or the two would slide apart... assert_eq!(p.timeline_frames(), 11); assert_eq!( shape(&p)[0], vec![clip(0, 0, 3, 0), clip(5, 3, 5, 0), clip(7, 5, 9, 0)], "the video lane got room and nothing else" ); // ...but the clip itself only reaches the lane that can play it. assert_eq!( shape(&p)[1], vec![ clip(0, 0, 3, 0), clip(3, 100, 102, wav), clip(5, 3, 5, 0), clip(7, 5, 9, 0) ] ); // A source with a picture is unchanged: still the grouped pair. let mut p = three(); assert!(p.paste(3, PASTED)); assert_eq!(shape(&p)[0], shape(&p)[1]); } /// A hand-written file may hold a clip that ends at the very last frame /// (`edith::check` permits `start + len == u32::MAX`), and the ripple used /// to wrap its start round to the front of the lane: an overlap in debug, a /// project no reload would accept in release. #[test] fn a_paste_that_would_run_past_the_last_frame_is_refused() { let far = clip(u32::MAX - 90, 0, 90, 0); let parts = |c: Clip| { Project::from_parts( vec![Source::new(FILE, 0)], two(vec![c.clone()], vec![c]), Vec::new(), Vec::new(), Vec::new(), ) .expect("valid parts") }; let mut p = parts(far.clone()); assert!(!p.paste(0, PASTED), "no room for the two frames it adds"); assert_eq!(shape(&p)[0], vec![far], "a refusal changes nothing"); assert!(!p.undo(), "...and pushes no history either"); assert!( !p.paste(u32::MAX, PASTED), "appending has nowhere to go either: the timeline ends at the last frame" ); // With room for exactly those frames the same paste is taken. let mut p = parts(clip(u32::MAX - 92, 0, 90, 0)); assert!(p.paste(0, PASTED)); assert_eq!(p.lane(Lane::V1)[1].end(), u32::MAX); } #[test] fn paste_undoes_in_one_step() { let mut p = three(); let before = shape(&p); assert!(p.paste(6, PASTED)); assert!(p.undo()); assert_eq!(shape(&p), before, "a paste is one undo step"); } #[test] fn a_copy_outlives_the_clip_it_came_from() { let mut p = three(); let copied = p.clips()[1].clone(); // [3,5) assert!(p.delete(1)); // Just a frame range: the source is still on disk either way. assert!(p.paste(0, copied)); assert_eq!(p.map_timeline(0), Some((0, 3))); assert_eq!(p.timeline_frames(), 9); } #[test] fn delete_closes_the_gap_on_both_lanes() { let mut p = three(); assert!(p.delete(1)); assert_eq!(p.timeline_frames(), 7); assert_eq!(p.clip_spans(), vec![(0, 3), (3, 4)]); assert_eq!(p.lane_spans(Lane::A1), vec![(0, 3), (3, 4)]); // timeline 3 used to be source 3; the gap closed so it is source 5 now assert_eq!(p.map_timeline(3), Some((1, 5))); assert_eq!(p.map_timeline(6), Some((1, 8))); assert_eq!(p.map_timeline(7), None); } #[test] fn delete_first_last_and_only() { let mut p = three(); assert!(p.delete(0)); assert_eq!(p.map_timeline(0), Some((0, 3))); assert_eq!(p.timeline_frames(), 6); let mut p = three(); assert!(p.delete(2)); assert_eq!(p.timeline_frames(), 5); assert_eq!(p.map_timeline(4), Some((1, 4))); assert!(!p.delete(2), "index past the end"); assert!(p.delete(1)); assert!(p.delete(0), "and the last remaining clip goes too"); assert_eq!(p.clips().len(), 0); assert_eq!(p.timeline_frames(), 0, "the timeline is emptiable"); assert!(!p.delete(0), "there is nothing left to delete"); assert!(p.undo(), "one gesture, one undo"); assert_eq!(p.clips().len(), 1); } /// The offset model's point: one lane loses a clip, the other does not move. #[test] fn a_lift_leaves_a_gap_and_moves_nothing() { let mut p = three(); let video_before = shape(&p)[0].clone(); assert!(p.lift(Lane::A1, 1)); assert_eq!(shape(&p)[0], video_before, "the picture never moved"); assert_eq!(p.timeline_frames(), 9, "nor did the timeline get shorter"); assert_eq!(p.lane_spans(Lane::A1), vec![(0, 3), (5, 4)]); // The hole is a gap: mapped as nothing, spanned as a gap of its length. assert_eq!(p.map(Lane::A1, 4), None); assert_eq!( p.span_at(Lane::A1, 3), Some(Span { reverse: false, ramp: None, start: 3, len: 2, from: None, speed: Speed::NORMAL, pitch: 0, }) ); assert_eq!(p.map(Lane::V1, 4), Some((1, 4)), "video plays on"); assert!(p.undo()); assert_eq!(p.lane_spans(Lane::A1), vec![(0, 3), (3, 2), (5, 4)]); } #[test] fn a_trailing_gap_is_a_gap_to_the_end_of_the_timeline() { let mut p = three(); assert!(p.lift(Lane::A1, 2)); assert_eq!(p.timeline_frames(), 9, "video still runs to 9"); assert_eq!( p.span_at(Lane::A1, 5), Some(Span { reverse: false, ramp: None, start: 5, len: 4, from: None, speed: Speed::NORMAL, pitch: 0, }), "the audio lane holds silence to the end of the picture" ); assert_eq!(p.span_at(Lane::A1, 9), None); // Every lane covers the timeline exactly, gaps included. for l in p.lanes() { let spans = p.spans_from(l, 0); assert_eq!(spans.iter().map(|s| s.len).sum::<u32>(), 9, "{}", l.label()); assert_eq!(spans[0].start, 0); } } /// The timeline is emptiable: the last placement comes off like any other, /// the project holds "nothing on any lane" as a state, and one undo per /// gesture brings it back. #[test] fn the_last_clip_of_the_last_lane_lifts_and_undoes() { let mut p = Project::single(FILE, 9); assert!(p.lift(Lane::A1, 0), "a silent timeline is fine"); assert!(p.lift(Lane::V1, 0), "and an empty one is a timeline too"); assert_eq!(p.timeline_frames(), 0); assert_eq!(p.composite_span_at(0), None, "nothing to show"); assert!(p.audio_segments_from(0, FPS).len() == 1, "one all-gap list"); assert!(!p.lift(Lane::A1, 0), "index past the end"); assert!(p.undo(), "and the last lift comes back"); assert_eq!(p.lane_spans(Lane::V1), vec![(0, 9)]); // A saved-and-loaded empty timeline is a project like any other, and it // still names the file its frame rate came from. assert!(p.lift(Lane::V1, 0)); let (sources, lanes, eq, color, _transform) = p.without_orphan_sources(); assert_eq!(sources.len(), 1, "source 0 survives an emptied timeline"); let back = Project::from_parts(sources, lanes, eq, color, Vec::new()) .expect("an empty project loads"); assert_eq!(back.timeline_frames(), 0); assert_eq!(back.lanes().len(), 2, "and it kept its lanes"); } /// What a keyboard selection walks: one answer per lane, gaps included -- /// the composite's own answer is one lane's and cannot say what the audio /// lane holds under the same playhead. #[test] fn lane_clip_at_answers_for_each_lane_separately() { let mut p = Project::single(FILE, 9); assert!(p.split(3), "both lanes split"); assert_eq!(p.lane_clip_at(Lane::V1, 0), Some(0)); assert_eq!(p.lane_clip_at(Lane::V1, 3), Some(1)); assert_eq!(p.lane_clip_at(Lane::A1, 3), Some(1)); assert_eq!(p.lane_clip_at(Lane::V1, 9), None, "past the end"); // A gap on one lane leaves the other's clip selectable there. assert!(p.lift(Lane::A1, 0)); assert_eq!(p.lane_clip_at(Lane::A1, 0), None, "the gap holds nothing"); assert_eq!( p.lane_clip_at(Lane::A1, 3), Some(0), "indices moved with it" ); assert_eq!(p.lane_clip_at(Lane::V1, 0), Some(0)); // A lane that is not there is not a panic. assert_eq!(p.lane_clip_at(Lane::new(LaneKind::Video, 1), 0), None); } #[test] fn place_overwrites_only_its_own_lane() { let mut p = three(); let audio_before = shape(&p)[1].clone(); // Straddles [3,5) and eats into [5,9): the first is trimmed, the second // is trimmed, nothing shifts. assert!(p.place(Lane::V1, 4, clip(0, 100, 102, 0))); assert_eq!( shape(&p)[0], vec![ clip(0, 0, 3, 0), clip(3, 3, 4, 0), clip(4, 100, 102, 0), clip(6, 6, 9, 0), ] ); assert_eq!(shape(&p)[1], audio_before, "the audio lane is untouched"); assert_eq!(p.timeline_frames(), 9, "an overwrite is not an insert"); // Placed inside one clip, it splits it in two. let mut p = Project::single(FILE, 9); assert!(p.place(Lane::V1, 4, clip(0, 100, 101, 0))); assert_eq!( shape(&p)[0], vec![clip(0, 0, 4, 0), clip(4, 100, 101, 0), clip(5, 5, 9, 0)] ); // ...and placed past the end it makes a gap, which is the whole point. assert!(p.place(Lane::V1, 20, clip(0, 100, 102, 0))); assert_eq!(p.timeline_frames(), 22); assert_eq!( p.span_at(Lane::V1, 9), Some(Span { reverse: false, ramp: None, start: 9, len: 11, from: None, speed: Speed::NORMAL, pitch: 0, }) ); assert!(!p.place(Lane::V1, 0, clip(0, 7, 7, 0)), "empty clip"); } /// A file with sound let go over a further video track: both halves go down, /// on the same frames, and the audio row is added when the `+ V` button left /// the project without one. One undo takes the lane and both clips back. #[test] fn a_take_placed_on_a_layer_lands_with_its_sound() { let mut p = three(); let v2 = p.add_lane(LaneKind::Video); assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2], "no A2 yet"); assert!(p.place_take(v2, 4, clip(0, 100, 102, 0))); let a2 = Lane::new(LaneKind::Audio, 1); assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2, a2], "A2 was added"); assert_eq!(p.lane(v2), p.lane(a2), "same frames, same source"); assert_eq!(p.lane(v2)[0].start, 4); assert_eq!(p.lane(v2)[0].end(), 6); assert_eq!( p.lane(v2)[0].link, p.lane(a2)[0].link, "one take, not two placements" ); assert!(p.lane(v2)[0].link.is_some()); assert_eq!(shape(&p)[0], shape(&three())[0], "V1 untouched"); assert_eq!(shape(&p)[1], shape(&three())[1], "and A1 with it"); // The pair overwrites its own two lanes and nothing else, exactly as a // one-lane place does. assert!(p.place_take(v2, 5, clip(0, 200, 203, 0))); assert_eq!(p.lane(v2), p.lane(a2)); assert_eq!(p.lane(v2).len(), 2, "the tail of the first is still there"); assert_eq!(shape(&p)[0], shape(&three())[0], "V1 still untouched"); assert!(p.undo(), "one step for the second pair"); assert_eq!(p.lane(v2).len(), 1); assert!(p.undo(), "one step for the first pair *and* its lane"); assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2]); assert!(p.lane(v2).is_empty()); // Refusals cost nothing: sound placed alone is `place`'s door. assert!( !p.place_take(Lane::A1, 0, clip(0, 100, 102, 0)), "audio lane" ); assert!(!p.place_take(v2, 0, clip(0, 7, 7, 0)), "empty clip"); assert!( !p.place_take(Lane::new(LaneKind::Video, 9), 0, clip(0, 1, 2, 0)), "a lane that is not there" ); assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2]); } /// The drag between tracks: let go at the frames it already covers the clip /// keeps them, one undo takes it back, and every way it can be refused /// leaves the project untouched. #[test] fn move_clip_keeps_the_frames_and_refuses_the_rest() { let v2 = Lane::new(LaneKind::Video, 1); let mut p = three(); assert_eq!(p.add_lane(LaneKind::Video), v2); let before = shape(&p); assert!(p.move_clip(Lane::V1, 1, v2, 3), "V1's middle clip moves up"); assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(5, 5, 9, 0)]); assert_eq!(shape(&p)[2], vec![clip(3, 3, 5, 0)], "same frames on V2"); assert_eq!(shape(&p)[1], before[1], "the audio lane is untouched"); assert_eq!(p.timeline_frames(), 9, "a move is not an insert"); // One snapshot: a single undo, and the lane list survives it. assert!(p.undo()); assert_eq!(shape(&p), before); // A lane that is not there, the lane and frame it is already at, an // index that is not there, and a move across kinds: all refused, // nothing changed. let history = p.history.len(); for (from, idx, to, start) in [ (Lane::V1, 1, Lane::new(LaneKind::Video, 7), 3), (Lane::V1, 1, Lane::V1, 3), (Lane::V1, 9, v2, 3), (Lane::V1, 1, Lane::A1, 3), (Lane::A1, 1, v2, 3), ] { assert!( !p.move_clip(from, idx, to, start), "{from:?} {idx} -> {to:?} at {start}" ); } assert_eq!(shape(&p), before); assert_eq!(p.history.len(), history, "a refusal snapshots nothing"); // Landing on another clip is refused rather than overwriting it: the // pointer named the lane, never the take already sitting there. assert!(p.place(v2, 3, clip(0, 100, 101, 0)), "V2 holds [3,4)"); assert!(!p.move_clip(Lane::V1, 1, v2, 3), "let go inside it"); assert!(p.move_clip(Lane::V1, 0, v2, 0), "[0,3) merely abuts it"); assert_eq!(shape(&p)[0], vec![clip(3, 3, 5, 0), clip(5, 5, 9, 0)]); assert_eq!(shape(&p)[2], vec![clip(0, 0, 3, 0), clip(3, 100, 101, 0)]); } /// A lane's name is a label: the address is unchanged, it is one undo step, an /// empty name clears it, a lane's removal takes it along, and a clip's deinterlace /// choice survives a split, a copy and an undo. #[test] fn lane_names_and_deinterlace_follow_the_usual_edit_rules() { let sources = vec![Source::new(FILE, 0)]; let lanes = vec![(LaneKind::Video, vec![clip(0, 0, 10, 0)]), (LaneKind::Audio, vec![clip(0, 0, 10, 0)])]; let mut p = Project::from_parts(sources, lanes, vec![], vec![], Vec::new()).expect("valid parts"); assert_eq!(p.set_lane_name(Lane::V1, " Interview "), Ok(true)); assert_eq!(p.lane_name(Lane::V1), Some("Interview")); assert_eq!(p.lane_title(Lane::V1), "Interview"); assert_eq!(p.lane_title(Lane::A1), "A1"); assert_eq!(p.set_lane_name(Lane::V1, "Interview"), Ok(false), "no change, no history"); assert!(p.set_lane_name(Lane::V1, &"x".repeat(MAX_LANE_NAME_CHARS + 1)).is_err()); assert!(p.set_lane_name(Lane::V1, "a\nb").is_err()); assert_eq!(p.set_lane_name(Lane::S1, "x"), Ok(false), "no such lane"); assert!(p.undo()); assert_eq!(p.lane_name(Lane::V1), None); assert!(p.redo()); assert_eq!(p.lane_names(), vec!["Interview".to_string(), String::new()]); assert_eq!(p.set_lane_name(Lane::V1, " "), Ok(true), "blank clears"); assert_eq!(p.lane_name(Lane::V1), None); assert!(p.set_deinterlace(Lane::V1, 0, Deinterlace::Field)); assert!(!p.set_deinterlace(Lane::V1, 0, Deinterlace::Field)); assert!(!p.set_deinterlace(Lane::A1, 0, Deinterlace::Off), "video clips only"); assert!(p.split(5)); assert_eq!(p.deinterlace_of(Lane::V1, 0), Deinterlace::Field); assert_eq!(p.deinterlace_of(Lane::V1, 1), Deinterlace::Field, "both halves keep it"); assert!(p.undo() && p.undo()); assert_eq!(p.deinterlace_of(Lane::V1, 0), Deinterlace::Frame); assert!(p.redo()); let copy = p.lane(Lane::V1)[0].clone(); assert_eq!(copy.deinterlace, Deinterlace::Field, "a copy carries it"); } /// The drag *along* a track, which is the one every other editor has: the /// clip lands on the frame the pointer let it go at, and stops dead against /// the neighbour rather than overwriting it. #[test] fn a_clip_slides_along_its_own_lane_and_butts_against_its_neighbour() { let sources = vec![Source::new(FILE, 0)]; let lanes = vec![ (LaneKind::Video, vec![clip(0, 0, 3, 0), clip(10, 3, 6, 0)]), (LaneKind::Audio, vec![clip(0, 0, 3, 0)]), ]; let mut p = Project::from_parts(sources, lanes, vec![], vec![], Vec::new()).expect("valid parts"); let before = shape(&p); // Into the gap, exactly where it was let go -- nothing else moves. assert!(p.move_clip(Lane::V1, 1, Lane::V1, 5)); assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(5, 3, 6, 0)]); assert_eq!(shape(&p)[1], before[1], "the audio lane is untouched"); // Dragged past the clip in front of it: clamped to its wall, laid end // to end with it, never over it. assert!(p.move_clip(Lane::V1, 1, Lane::V1, 0)); assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 6, 0)]); // One undo per drag, and both of them together are the state it started // in -- the frames each clip plays never changed. assert!(p.undo() && p.undo()); assert_eq!(shape(&p), before); // A drop that changes nothing is a click, not an edit. let history = p.history.len(); assert!(!p.move_clip(Lane::V1, 1, Lane::V1, 10)); assert_eq!(p.history.len(), history, "a refusal snapshots nothing"); } /// A move lands on any empty frames of its lane, jumping over the clips /// between -- where a drag only slides up to the first one it meets. #[test] fn a_move_jumps_over_clips_to_empty_frames() { let sources = vec![Source::new(FILE, 0)]; let lanes = vec![ (LaneKind::Video, vec![clip(0, 0, 4, 0), clip(6, 4, 8, 0), clip(12, 8, 12, 0)]), (LaneKind::Audio, vec![clip(0, 0, 4, 0)]), ]; let mut p = Project::from_parts(sources, lanes, vec![], vec![], Vec::new()).expect("valid parts"); // [0,4) to [16,20): past the clip at 6 and the clip at 12, both empty // frames of the lane -- a move, not an insert, so nothing else shifts. assert!(p.move_clip(Lane::V1, 0, Lane::V1, 16)); assert_eq!( shape(&p)[0], vec![clip(6, 4, 8, 0), clip(12, 8, 12, 0), clip(16, 0, 4, 0)] ); // A gap of two frames (10..12) is too narrow for a four-frame clip: refused. let before = shape(&p); assert!(!p.move_clip(Lane::V1, 2, Lane::V1, 10), "no gap that wide"); assert_eq!(shape(&p), before); } /// A linked take jumps as one: its partner on the audio lane must be free /// at the same frames, and where it is not the drop is clamped to the /// partner's wall rather than overlapping it. #[test] fn a_linked_take_jumps_only_where_its_partner_is_free() { let link = |mut c: Clip| { c.link = Some(7); c }; let sources = vec![Source::new(FILE, 0)]; let lanes = vec![ (LaneKind::Video, vec![link(clip(0, 0, 4, 0)), clip(12, 4, 8, 0)]), (LaneKind::Audio, vec![link(clip(0, 0, 4, 0))]), ]; let mut p = Project::from_parts(sources, lanes, vec![], vec![], Vec::new()).expect("valid parts"); let v2 = p.add_lane(LaneKind::Video); // Both halves free at [20,24): the take lands there, partner included. assert!(p.move_clip(Lane::V1, 0, v2, 20)); assert_eq!(p.lane(v2).iter().map(|c| c.start).collect::<Vec<_>>(), [20]); assert_eq!(p.lane(Lane::A1)[0].start, 20, "partner moves the same distance"); assert_eq!(p.lane(Lane::A1)[0].link, Some(7)); // A partner blocked at [22,26) on the audio lane: the drop stops at the // frame where the partner meets nothing, never over the blocker. let sources = vec![Source::new(FILE, 0)]; let lanes = vec![ (LaneKind::Video, vec![link(clip(0, 0, 4, 0))]), (LaneKind::Audio, vec![link(clip(0, 0, 4, 0)), clip(22, 0, 4, 0)]), ]; let mut p = Project::from_parts(sources, lanes, vec![], vec![], Vec::new()).expect("valid parts"); let v2 = p.add_lane(LaneKind::Video); assert!(p.move_clip(Lane::V1, 0, v2, 20)); assert_eq!(p.lane(v2)[0].start, 18, "clamped to the partner's wall"); assert_eq!(p.lane(Lane::A1)[0].start, 18); assert_eq!(p.lane(Lane::A1)[1].start, 22, "the blocker is not moved"); } /// A drag carries the whole take: the sound half travels exactly as far as /// the picture does, and the tighter of their two walls stops both. #[test] fn a_dragged_take_carries_its_sound_and_stops_at_the_tighter_wall() { let mut p = three(); assert!( p.lift(Lane::V1, 2) && p.lift(Lane::A1, 2), "room to the right" ); let link = p.lane(Lane::V1)[1].link.expect("a split hands out ids"); assert_eq!(p.lane(Lane::A1)[1].link, Some(link), "both halves grouped"); // Something in the sound's way, and nothing at all in the picture's. assert!(p.place(Lane::A1, 10, clip(0, 0, 2, 0)), "A1 holds [10,12)"); let before = shape(&p); // Let go over empty frames on both lanes: the take lands there whole, // jumping the blocker that lies between. assert!(p.move_clip(Lane::V1, 1, Lane::V1, 30), "dragged far right"); assert!(p.lane(Lane::V1).iter().any(|c| c.start == 30)); assert!(p.lane(Lane::A1).iter().any(|c| c.start == 30), "sound came with it"); links_are_consistent(&p.lanes).expect("one id per lane, one span"); assert!(p.undo()); assert_eq!(shape(&p), before, "one undo for the whole take"); // Let go on the sound's blocker: the take stops against it, picture // and sound alike, short of the frame the pointer named. assert!(p.move_clip(Lane::V1, 1, Lane::V1, 9), "dropped on the blocker"); assert!(p.lane(Lane::V1).iter().any(|c| c.start == 8), "stopped where the sound did"); assert!(p.lane(Lane::A1).iter().any(|c| c.start == 8), "and the sound came with it"); links_are_consistent(&p.lanes).expect("one id per lane, one span"); assert!(p.undo()); assert_eq!(shape(&p), before, "one undo for the whole take"); } /// The drag that does both at once -- another lane *and* another frame -- /// which is what a pointer let go over a lane always names. #[test] fn a_clip_dragged_to_another_lane_lands_on_the_pointers_frame() { let v2 = Lane::new(LaneKind::Video, 1); let mut p = three(); assert_eq!(p.add_lane(LaneKind::Video), v2); assert!(p.move_clip(Lane::V1, 2, v2, 20), "[5,9) -> V2 at 20"); assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 5, 0)]); assert_eq!(shape(&p)[2], vec![clip(20, 5, 9, 0)], "at the pointer"); assert_eq!( p.lane(Lane::A1)[2].start, 20, "its sound travelled the same distance" ); links_are_consistent(&p.lanes).expect("one id per lane, one span"); assert_eq!(p.timeline_frames(), 24, "a move is not an insert"); } /// How long `FILE` is, which is what a trim's tail is allowed to reach: /// [`three`] cuts up all nine of its frames. const SRC: &[u32] = &[9]; /// The drag on a clip's tail: it plays less (or more) of its source, nothing /// else on the lane moves, and every wall stops the edge rather than /// refusing the gesture. #[test] fn trimming_the_tail_stops_at_every_wall() { let mut p = three(); // Pulled in: a gap opens behind it and the clip after it stays put. assert!(p.trim(Lane::V1, 1, Edge::End, 4, SRC)); assert_eq!( shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 4, 0), clip(5, 5, 9, 0)] ); assert_eq!(p.timeline_frames(), 9, "a trim ripples nothing"); assert_eq!(p.map(Lane::V1, 4), None, "the frame it gave up is a gap"); // ...and back out, no further than the clip behind it. assert!(p.trim(Lane::V1, 1, Edge::End, 8, SRC)); assert_eq!( shape(&p)[0][1], clip(3, 3, 5, 0), "stopped at the neighbour" ); assert!( !p.trim(Lane::V1, 1, Edge::End, 8, SRC), "already at the wall" ); // One frame of clip always survives, however far back the pointer went. assert!(p.trim(Lane::V1, 1, Edge::End, 0, SRC)); assert_eq!(shape(&p)[0][1], clip(3, 3, 4, 0)); // The last clip has no neighbour, so what stops it is the file: nine // frames is nine frames. assert!(p.trim(Lane::V1, 2, Edge::End, 7, SRC)); assert!(p.trim(Lane::V1, 2, Edge::End, 9999, SRC)); assert_eq!(shape(&p)[0][2], clip(5, 5, 9, 0), "back to the whole take"); assert!(!p.trim(Lane::V1, 2, Edge::End, 9999, SRC), "and no further"); // A source nobody told us the length of may not grow at all -- an // out-point past the end of a file is a save that will not open again. assert!(p.trim(Lane::V1, 2, Edge::End, 7, &[])); assert!(!p.trim(Lane::V1, 2, Edge::End, 9, &[]), "length unknown"); } /// The head pulled in takes the in-point with it -- what stays plays what it /// always played -- and stops at the source's own first frame. #[test] fn trimming_the_head_moves_the_in_point() { let mut p = three(); assert!(p.trim(Lane::V1, 0, Edge::Start, 2, SRC)); assert_eq!( shape(&p)[0][0], clip(2, 2, 3, 0), "two frames off the front" ); assert_eq!(p.map(Lane::V1, 2), Some((0, 2)), "frame 2 still plays 2"); assert_eq!(p.map(Lane::V1, 1), None, "and the front is a gap"); assert!(p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "back out again"); assert_eq!(shape(&p)[0][0], clip(0, 0, 3, 0)); // One frame survives here too. assert!(p.trim(Lane::V1, 0, Edge::Start, 9, SRC)); assert_eq!(shape(&p)[0][0], clip(2, 2, 3, 0)); // A clip that does not begin at its source's first frame: its head goes // back to source frame 0 and not one frame further. let mut p = Project::single(FILE, 9); assert!(p.place(Lane::V1, 5, clip(0, 1, 4, 0))); assert!(p.lift(Lane::V1, 0), "nothing in front of it to stop it"); assert!(p.trim(Lane::V1, 0, Edge::Start, 0, SRC)); assert_eq!(shape(&p)[0][0], clip(4, 0, 4, 0), "one frame of head left"); } /// A clip whose in-point is *past* its own start -- what a ripple delete /// leaves, the piece in front of it gone and this one slid back to frame 0 /// carrying the in-point the cut gave it. Its head has more source behind it /// than the timeline has room for, and the timeline's own first frame is the /// wall: asking is not an overflow, and the answer is 0. #[test] fn a_ripple_closed_clip_can_still_be_head_trimmed() { let mut p = Project::single(FILE, 9); assert!(p.split(5)); assert!(p.delete_in(Lane::V1, 0), "the first piece goes"); assert_eq!(shape(&p)[0], vec![clip(0, 5, 9, 0)], "start 0, in-point 5"); assert_eq!( p.trim_room(Lane::V1, 0, Edge::Start, SRC), Some((0, 3)), "back to frame 0 at most, and one frame of clip always survives" ); assert!(!p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "already there"); assert!(p.trim(Lane::V1, 0, Edge::Start, 2, SRC)); assert_eq!( shape(&p)[0], vec![clip(2, 7, 9, 0)], "the in-point followed" ); assert!(p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "and back out"); assert_eq!(shape(&p)[0], vec![clip(0, 5, 9, 0)]); } /// A still is trimmed from its head exactly as it is from its tail: it has /// no earlier frame to walk an in-point back to, so the wall is the length /// the caller's table allows it, measured back from the tail. Measured from /// the in-point -- which a placed picture has at 0 -- its left edge could /// never be dragged outwards at all. #[test] fn a_stills_head_stretches_out_like_its_tail() { const STILL: &str = "/nonexistent/card.png"; /// What a still is held to, as `PlaybackSession` fills the table in. const CAP: &[u32] = &[60]; let mut p = Project::single(STILL, 20); assert!(p.lift(Lane::A1, 0), "a picture is silent"); assert!(p.lift(Lane::V1, 0), "and this one is not at frame 0"); assert!(p.place(Lane::V1, 100, clip(0, 0, 20, 0))); // A source with no entry in the table may not grow, here as at the tail. assert_eq!( p.trim_room(Lane::V1, 0, Edge::Start, &[]), Some((100, 119)), "a length nobody told us buys no head room" ); assert_eq!( p.trim_room(Lane::V1, 0, Edge::Start, CAP), Some((60, 119)), "back to a whole cap's worth, one frame of clip surviving" ); assert!(p.trim(Lane::V1, 0, Edge::Start, 80, CAP)); assert_eq!( shape(&p)[0][0], clip(80, 0, 40, 0), "twenty frames longer, and still read from the source's first frame" ); // Past the cap is clamped, exactly as the tail is -- not refused. assert!(p.trim(Lane::V1, 0, Edge::Start, 0, CAP)); assert_eq!( shape(&p)[0][0], clip(60, 0, 60, 0), "a cap's worth, no more" ); // ...and the clip in front is the nearer wall of the two: no head trim // may open an overlap. assert!(p.trim(Lane::V1, 0, Edge::Start, 90, CAP)); assert!(p.place(Lane::V1, 70, clip(0, 0, 15, 0))); assert_eq!( p.trim_room(Lane::V1, 1, Edge::Start, CAP), Some((85, 119)), "up to the neighbour's last frame and not over it" ); assert!(p.trim(Lane::V1, 1, Edge::Start, 0, CAP)); assert_eq!(shape(&p)[0][1], clip(85, 0, 35, 0), "butted against it"); assert!(sorted_disjoint(p.lane(Lane::V1)), "no overlap"); } /// An end edge that runs out of source names the source's tail as its /// wall, and the same edge stopped by the clip after it does not -- and the /// walls are the ones `trim_room` gives, not a second computation. #[test] fn trim_walls_name_the_source_tail_or_the_neighbour() { let mut p = Project::single(FILE, 9); assert_eq!( p.trim_walls(Lane::V1, 0, Edge::End, SRC), Some(TrimWalls { lo: 1, hi: 9, lo_media: false, hi_media: true, }), "the whole source, and it runs out at its tail" ); assert_eq!( p.trim_walls(Lane::V1, 0, Edge::End, SRC) .map(|w| (w.lo, w.hi)), p.trim_room(Lane::V1, 0, Edge::End, SRC), ); assert!(p.split(5)); assert_eq!( p.trim_walls(Lane::V1, 0, Edge::End, SRC), Some(TrimWalls { lo: 1, hi: 5, lo_media: false, hi_media: false, }), "the source has more, but the next piece is nearer" ); assert_eq!( p.trim_walls(Lane::V1, 1, Edge::End, SRC), Some(TrimWalls { lo: 6, hi: 9, lo_media: false, hi_media: true, }), "the last piece runs to the source's tail" ); assert_eq!( p.trim_walls(Lane::V1, 2, Edge::End, SRC), None, "no such clip" ); } /// A start edge that drags back to the source's first frame names that /// head as its wall; one stopped by the piece in front of it does not. #[test] fn trim_walls_name_the_source_head_or_the_neighbour() { let mut p = Project::single(FILE, 9); assert!(p.split(5)); assert!(p.delete_in(Lane::V1, 0), "the first piece goes"); assert_eq!(shape(&p)[0], vec![clip(0, 5, 9, 0)], "start 0, in-point 5"); assert_eq!( p.trim_walls(Lane::V1, 0, Edge::Start, SRC), Some(TrimWalls { lo: 0, hi: 3, lo_media: true, hi_media: false, }), "frame 0 is the head's first frame, and the clip's own minimum is the other wall" ); let mut p = Project::single(FILE, 9); assert!(p.split(5)); assert_eq!( p.trim_walls(Lane::V1, 1, Edge::Start, SRC), Some(TrimWalls { lo: 5, hi: 8, lo_media: false, hi_media: false, }), "the head has room back to 0, but the piece in front stops it at 5" ); } /// A still has no media limit: its head and tail reach as far as the cap /// says, and that reach is never reported as the source's end. #[test] fn a_still_never_reports_media() { const STILL: &str = "/nonexistent/card.png"; const CAP: &[u32] = &[60]; let mut p = Project::single(STILL, 20); assert!(p.lift(Lane::A1, 0), "a picture is silent"); assert!(p.lift(Lane::V1, 0), "and this one is not at frame 0"); assert!(p.place(Lane::V1, 100, clip(0, 0, 20, 0))); assert_eq!( p.trim_walls(Lane::V1, 0, Edge::Start, CAP), Some(TrimWalls { lo: 60, hi: 119, lo_media: false, hi_media: false, }), ); assert_eq!( p.trim_walls(Lane::V1, 0, Edge::End, CAP), Some(TrimWalls { lo: 101, hi: 160, lo_media: false, hi_media: false, }), ); } /// Linked halves trim as one: a link is one span on however many lanes, so /// the sound follows the picture's edge -- and the tightest wall of the two /// is what stops both. #[test] fn linked_halves_trim_together() { let mut p = three(); let link = p.lane(Lane::V1)[2].link.expect("a split hands out ids"); assert_eq!(p.lane(Lane::A1)[2].link, Some(link), "both halves grouped"); assert!(p.trim(Lane::V1, 2, Edge::Start, 7, SRC)); assert_eq!(shape(&p)[0][2], clip(7, 7, 9, 0)); assert_eq!(shape(&p)[1][2], clip(7, 7, 9, 0), "the sound followed"); assert!( p.trim(Lane::A1, 2, Edge::End, 8, SRC), "either half drags it" ); assert_eq!(shape(&p)[0][2], clip(7, 7, 8, 0), "and the picture follows"); links_are_consistent(&p.lanes).expect("one id per lane, one span"); // Something in the way on the *audio* lane stops the picture's tail as // well: the group can only go as far as its tightest member. assert!(p.place(Lane::A1, 9, clip(0, 0, 1, 0))); assert!(p.trim(Lane::V1, 2, Edge::End, 9999, SRC)); assert_eq!(shape(&p)[0][2], clip(7, 7, 9, 0), "stopped by A1's clip"); assert_eq!(shape(&p)[1][2], clip(7, 7, 9, 0)); links_are_consistent(&p.lanes).expect("still one span"); } /// An overwrite that cuts through a take on both of its lanes leaves the take whole: the head /// and the tail of the cut each keep one id shared across the lanes, so a trim of either lane /// still drags the other. A cut that would leave one lane's half without its partner detaches. #[test] fn an_overwrite_through_a_take_keeps_the_take_whole() { let mut p = three(); assert!(p.edit_from_source(SourceEdit::Overwrite, 6, &[(Lane::V1, clip(0, 0, 2, 0)), (Lane::A1, clip(0, 0, 2, 0))]).is_ok()); let head = p.lane(Lane::V1)[2].link; assert!(head.is_some(), "the head of the cut is still one take"); assert_eq!(p.lane(Lane::A1)[2].link, head, "and so is the sound's head"); assert_eq!(p.lane(Lane::V1)[4].link, p.lane(Lane::A1)[4].link, "the tail is one take too"); assert!(p.lane(Lane::V1)[4].link.is_some()); assert_ne!(p.lane(Lane::V1)[4].link, head, "the tail is a take of its own"); links_are_consistent(&p.lanes).expect("one id per lane, one span"); // The head's sound follows the picture's trim, as it does everywhere else. assert!(p.trim(Lane::V1, 0, Edge::End, 2, SRC)); assert_eq!(p.lane(Lane::A1)[0].end(), 2, "the sound followed the picture"); links_are_consistent(&p.lanes).expect("still one span"); } /// A take that loses its last partner is detached: the survivor keeps an id /// that no other placement shares (a lone id, which the front-ends draw as /// detached), whichever edit took the partner away -- a lift, a split on some /// lanes -- and one undo puts the take back exactly. #[test] fn a_take_down_to_one_member_is_detached() { let mut p = three(); let link = p.lane(Lane::V1)[2].link.expect("a split hands out ids"); assert_eq!(p.lane(Lane::A1)[2].link, Some(link)); assert!(p.lift(Lane::V1, 2)); assert_eq!(p.lane(Lane::A1)[2].link, Some(link), "the survivor keeps its id"); assert!(p.undo()); assert_eq!(p.lane(Lane::V1)[2].link, Some(link), "undo restores the picture's link"); assert_eq!(p.lane(Lane::A1)[2].link, Some(link), "and the sound's"); // A split on the picture lane only: every half of the take is detached // (a fresh id of its own), in one snapshot. let history = p.history.len(); assert!(p.split_on(7, &[Lane::V1])); assert_eq!(p.history.len(), history + 1, "one snapshot for the whole split"); let ids: Vec<Option<u32>> = vec![ p.lane(Lane::V1)[2].link, p.lane(Lane::V1)[3].link, p.lane(Lane::A1)[2].link, ]; assert!(ids.iter().all(Option::is_some), "each half is detached: {ids:?}"); assert!(ids[0] != ids[1] && ids[0] != ids[2] && ids[1] != ids[2], "{ids:?}"); links_are_consistent(&p.lanes).expect("no two members share an id"); assert!(p.undo()); assert_eq!(p.lane(Lane::A1)[2].link, Some(link), "undo restores the take"); assert_eq!(p.lane(Lane::V1)[2].link, Some(link)); } /// `swap` trades two placements of one lane: the first takes the second's /// start, the one between them ripples by the length difference, and the /// second lands where the first was (moved by that difference). The span /// keeps its length; a grouped placement refuses; one undo puts it back. #[test] fn swap_trades_two_clips_and_ripples_the_difference() { let mut p = Project::single(FILE, 1); let v = p.add_lane(LaneKind::Video); assert!(p.place(v, 0, clip(0, 0, 3, 0))); assert!(p.place(v, 3, clip(3, 3, 5, 0))); assert!(p.place(v, 5, clip(5, 5, 9, 0))); // [0,3) len 3 and [5,9) len 4 trade places; the middle [3,5) moves by +1. assert!(p.swap(v, 0, 2)); let got: Vec<(u32, u32, u32)> = p .lane(v) .iter() .map(|c| (c.start, c.frames(), c.in_frame)) .collect(); assert_eq!(got, vec![(0, 4, 5), (4, 2, 3), (6, 3, 0)]); assert!(p.undo()); assert_eq!(p.lane(v)[0].start, 0); assert_eq!(p.lane(v)[0].frames(), 3, "one undo puts the order back"); // A grouped placement is not swapped apart. let mut t = three(); assert!(!t.swap(Lane::V1, 0, 2), "the take is moved as a take"); assert!(!t.swap(Lane::V1, 1, 1), "same clip"); } /// A clip whose link no other clip shares (what `ungroup` leaves) swaps like any other. #[test] fn swap_moves_a_detached_clip_but_not_a_take() { let mut p = three(); assert!(p.ungroup(Lane::V1, 0) && p.ungroup(Lane::V1, 2), "both ends are detached first"); assert!(p.lane(Lane::V1)[0].link.is_some(), "the lone link stays"); assert!(p.swap(Lane::V1, 0, 2), "a lone link is not a take"); assert_eq!(p.lane(Lane::V1)[0].in_frame, 5, "the later clip took the head"); assert!(p.undo()); // Clip 1 on V1 is still in its take with A1, so a swap that includes it is refused. let mut t = three(); assert!(t.ungroup(Lane::V1, 0)); assert!(!t.swap(Lane::V1, 0, 1), "a take still moves as a take"); assert!(t.swap_in_place(Lane::V1, 0, 1).is_err(), "a take still moves as a take"); } /// The non-rippling swap: the two clips trade heads, the shorter leaves a gap /// where it was, and nothing between them moves. A clip moving into the slot /// the other vacates is allowed -- the check is on the finished positions. #[test] fn swap_in_place_exchanges_heads_and_moves_nothing_else() { let mut p = Project::single(FILE, 1); let v = p.add_lane(LaneKind::Video); assert!(p.place(v, 0, clip(0, 0, 2, 0))); assert!(p.place(v, 4, clip(4, 4, 5, 0))); assert!(p.place(v, 9, clip(9, 9, 12, 0))); p.swap_in_place(v, 0, 2).expect("the longer clip fits the 0..4 space"); let got: Vec<(u32, u32, u32)> = p .lane(v) .iter() .map(|c| (c.start, c.frames(), c.in_frame)) .collect(); assert_eq!(got, vec![(0, 3, 9), (4, 1, 4), (9, 2, 0)]); assert!(p.undo()); assert_eq!(p.lane(v)[0].in_frame, 0, "one undo restores the lane"); assert_eq!(p.lane(v)[2].start, 9); } /// A longer clip that cannot fit its new head is refused by name, and the lane /// is untouched -- nothing is half-swapped. #[test] fn swap_in_place_refuses_a_clip_that_would_overlap_by_name() { let mut p = Project::single(FILE, 1); let v = p.add_lane(LaneKind::Video); assert!(p.place(v, 0, clip(0, 0, 2, 0))); assert!(p.place(v, 2, clip(2, 2, 3, 0))); assert!(p.place(v, 4, clip(4, 4, 8, 0))); let before: Vec<(u32, u32)> = p.lane(v).iter().map(|c| (c.start, c.frames())).collect(); let err = p.swap_in_place(v, 0, 2).expect_err("the 4-frame clip does not fit 0..2"); assert!(err.to_string().contains("clip 1"), "names the clip in the way: {err}"); let after: Vec<(u32, u32)> = p.lane(v).iter().map(|c| (c.start, c.frames())).collect(); assert_eq!(before, after, "a refusal changes nothing"); assert!(p.swap_in_place(v, 1, 1).is_err(), "the same clip twice"); } /// `ungroup` takes a whole take apart: each member gets an id of its own, /// so the take is no longer a group, and one undo puts it back. #[test] fn ungroup_gives_each_member_its_own_id() { let mut p = three(); assert!(p.ungroup(Lane::A1, 2)); assert_ne!(p.lane(Lane::A1)[2].link, p.lane(Lane::V1)[2].link); assert!(p.undo()); assert!(p.lane(Lane::V1)[2].link.is_some(), "undo puts the take back"); } /// The bug this guards: grouping a take's picture with a clip from another /// take merges all four into one id, and ungrouping the picture must hand /// each take back whole -- not detach both halves of the first take too. #[test] fn ungroup_of_a_member_of_a_merged_group_keeps_its_take() { let v2 = Lane::new(LaneKind::Video, 1); let a2 = Lane::new(LaneKind::Audio, 1); let mut p = Project::single(FILE, 9); p.add_lane(LaneKind::Video); let other = p.lane(Lane::V1)[0].clone(); assert!(p.place_take(v2, 20, other)); let all = |p: &Project| -> Vec<Vec<Clip>> { p.lanes().iter().map(|&l| p.lane(l).to_vec()).collect() }; let before = all(&p); p.group_all(&[(Lane::V1, 0), (v2, 0)]).unwrap(); assert_eq!(p.lane(Lane::A1)[0].link, p.lane(v2)[0].link, "one group of four"); assert!(p.ungroup(Lane::V1, 0)); assert_eq!(p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link, "the first take is still linked"); assert_eq!(p.lane(v2)[0].link, p.lane(a2)[0].link, "the second take is still linked"); assert_ne!(p.lane(Lane::V1)[0].link, p.lane(v2)[0].link, "and the two takes are apart"); // One undo is the merged group, and a second one the state before it. assert!(p.undo()); assert_eq!(p.lane(Lane::V1)[0].link, p.lane(v2)[0].link, "the merge is back"); assert!(p.undo()); assert_eq!(all(&p), before, "undo is exact"); } /// A caption grouped onto a whole take: ungrouping the caption takes the /// caption off and leaves the take linked, the same as a loose clip would. #[test] fn ungroup_of_a_caption_on_a_take_keeps_the_take() { let mut p = Project::single(FILE, 9).with_subtitles(vec![subtitle_track()]); let subs = p.add_lane(LaneKind::Subtitle); let caption = SubClip { start: 0, frames: 9, track: 0, in_us: 0, out_us: 300_000, link: None }; p.place_sub(subs, 0, caption).expect("the caption goes down"); p.group_all(&[(Lane::V1, 0), (subs, 0)]).expect("a clip and a caption are a group"); assert!(p.ungroup(subs, 0)); assert_eq!(p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link, "the take is still linked"); let cap = p.sub_lane(subs)[0].link; assert!(cap.is_some() && cap != p.lane(Lane::V1)[0].link, "the caption is a half of its own"); } /// A take grouped with one loose clip: ungrouping the take's picture /// detaches the loose clip and leaves the take linked, and ungrouping the /// loose clip does the same from the other side. #[test] fn ungroup_of_a_take_with_a_loose_clip_detaches_only_the_loose_clip() { let v2 = Lane::new(LaneKind::Video, 1); let loose = |p: &Project| Clip { link: None, ..p.lane(Lane::V1)[0].clone() }; let mut p = Project::single(FILE, 9); p.add_lane(LaneKind::Video); let mut clip = loose(&p); clip.start = 20; assert!(p.place(v2, 20, clip.clone())); p.group_all(&[(Lane::V1, 0), (v2, 0)]).unwrap(); assert!(p.ungroup(v2, 0)); assert_eq!(p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link, "the take is still linked"); assert!(p.lane(v2)[0].link.is_some() && p.lane(v2)[0].link != p.lane(Lane::V1)[0].link); assert!(p.undo()); p.group_all(&[(Lane::V1, 0), (v2, 0)]).unwrap(); assert!(p.ungroup(Lane::V1, 0)); assert_eq!(p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link, "from the picture too"); assert_ne!(p.lane(v2)[0].link, p.lane(Lane::V1)[0].link); assert!(links_are_consistent(&p.lanes).is_ok()); } /// A whole drag is one undo step -- the front-end commits once, at the /// release -- and an edge asked to stay where it is costs none at all. #[test] fn a_trim_is_one_undo_step() { let mut p = three(); let before = shape(&p); let history = p.history.len(); assert!(p.trim(Lane::V1, 1, Edge::End, 4, SRC)); assert_eq!(p.history.len(), history + 1, "one snapshot per gesture"); assert!(!p.trim(Lane::V1, 1, Edge::End, 4, SRC), "already there"); assert!(!p.trim(Lane::V1, 9, Edge::End, 4, SRC), "no such clip"); assert!(!p.trim(Lane::new(LaneKind::Video, 7), 0, Edge::End, 4, SRC)); assert_eq!(p.history.len(), history + 1, "a refusal snapshots nothing"); assert!(p.undo()); assert_eq!(shape(&p), before, "both halves back, in one step"); } /// A moved half stays in its group: a link names a span, not a lane, so the /// picture on `V2` is still the same take as the sound under it on `A1`. #[test] fn move_clip_keeps_the_group() { let v2 = Lane::new(LaneKind::Video, 1); let mut p = three(); p.add_lane(LaneKind::Video); let link = p.lane(Lane::V1)[1].link.expect("a split hands out ids"); assert_eq!(p.lane(Lane::A1)[1].link, Some(link), "both halves grouped"); assert!(p.move_clip(Lane::V1, 1, v2, 3)); assert_eq!(p.lane(v2)[0].link, Some(link), "the id travelled with it"); links_are_consistent(&p.lanes).expect("one id per lane, one span"); assert_eq!( p.lane(v2)[0].start, p.lane(Lane::A1)[1].start, "and still covers the same span as its sound" ); } /// The gap-close menu's hit test: what `gap_at` says about a frame at the /// head of a lane, between two clips, inside a clip, past the last one, /// and on a lane holding nothing at all. #[test] fn gap_at_finds_the_hole_a_frame_sits_in() { let mut p = Project::single(FILE, 1); let v = p.add_lane(LaneKind::Video); // Empty lane: nothing before and nothing after -- not a gap, since // there is nothing to ripple toward. assert_eq!(p.gap_at(v, 0), None, "an empty lane has nothing to close"); assert!(p.place(v, 5, clip(5, 0, 3, 0)), "single clip at [5,8)"); // Before the only clip: the gap runs from the timeline's own head. assert_eq!(p.gap_at(v, 0), Some((0, 5))); assert_eq!(p.gap_at(v, 4), Some((0, 5))); // Inside the clip: not a gap. assert_eq!(p.gap_at(v, 5), None); assert_eq!(p.gap_at(v, 7), None); // Past the only clip: the open end of the lane, not a gap -- there is // nothing after it to slide back. assert_eq!(p.gap_at(v, 8), None); assert_eq!(p.gap_at(v, 100), None); assert!( p.place(v, 12, clip(12, 0, 2, 0)), "a second clip at [12,14)" ); // Between the two: the gap [8,12). assert_eq!(p.gap_at(v, 8), Some((8, 4))); assert_eq!(p.gap_at(v, 11), Some((8, 4))); assert_eq!(p.gap_at(v, 14), None, "still the open end"); assert!( p.place(v, 8, clip(8, 0, 4, 0)), "fills [8,12) exactly, meeting both neighbours" ); assert_eq!( p.gap_at(v, 8), None, "adjacent clips leave no frame to name a gap" ); assert_eq!(p.gap_at(v, 11), None); } /// Closing a gap is [`Project::cut_regions`] scoped to the one lane the /// menu named: the clip after the gap slides back on that lane, a clip on /// another lane at the very same frame range does not move, and the whole /// close is one press of undo. #[test] fn closing_a_gap_ripples_only_its_own_lane_and_undoes_in_one_step() { let mut p = Project::single(FILE, 1); let v = p.add_lane(LaneKind::Video); let a = p.add_lane(LaneKind::Audio); assert!(p.place(v, 5, clip(5, 0, 3, 0)), "V clip at [5,8)"); assert!( p.place(a, 0, clip(0, 0, 3, 0)), "A clip at [0,3) -- outside the video lane's gap and this scope" ); let before = shape(&p); let (start, len) = p.gap_at(v, 0).expect("a gap before the only clip"); assert_eq!((start, len), (0, 5)); assert!(p.cut_regions(&[(start, len)], &[v]).is_ok()); assert_eq!(p.lane(v)[0].start, 0, "the clip slid back to close the gap"); assert_eq!( p.lane(a)[0].start, 0, "the untouched lane's clip did not move" ); assert!(p.undo(), "one press undoes the whole close"); assert_eq!(shape(&p), before, "back to exactly where it was"); } /// A clip carrying an explicit link id -- what a /// [`from_parts`](Project::from_parts) fixture below needs to build a take /// by hand, the same shape `a_link_id_is_never_two_clips_of_one_lane` /// already builds inline. fn linked(start: u32, in_frame: u32, out_frame: u32, link: u32) -> Clip { Clip { speed_keys: Vec::new(), reversed: false, stabilise: None, deinterlace: Default::default(), mark: Default::default(), angle: Default::default(), fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), visualizer: 0, viz_paint: 0, gain_db: 0.0, gain_keys: Vec::new(), pan: 0, pitch: 0, transform_keys: Vec::new(), opacity_keys: Vec::new(), color_keys: Vec::new(), effects: Vec::new(), start, in_frame, out_frame, source: 0, link: Some(link), eq: None, color: None, transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, } } /// The ordinary case the defect made a permanent no-op: a gap on `V1` /// bordered by a linked take whose `A1` half is empty across the very /// same stretch widens the scope to both lanes, so the ripple carries the /// take rather than tearing it -- and one undo puts both halves back. #[test] fn closing_a_gap_carries_a_linked_take_when_the_gap_matches_on_both_lanes() { let sources = vec![Source::new(FILE, 0)]; let video = vec![linked(0, 0, 3, 1), linked(5, 5, 9, 3)]; let audio = vec![linked(0, 0, 3, 1), linked(5, 5, 9, 3)]; let mut p = Project::from_parts( sources, two(video, audio), Vec::new(), Vec::new(), Vec::new(), ) .expect("valid parts"); let before = shape(&p); assert_eq!(p.gap_at(Lane::V1, 3), Some((3, 2))); assert_eq!( p.gap_at(Lane::A1, 3), Some((3, 2)), "the same stretch is empty on A1 too" ); let scope = p .gap_take_scope(Lane::V1, 3, 2) .expect("a matching gap on both halves widens the scope"); assert_eq!(scope.len(), 2, "the take's other lane joins"); assert!(scope.contains(&Lane::V1) && scope.contains(&Lane::A1)); assert!(p.cut_regions(&[(3, 2)], &scope).is_ok()); assert_eq!(p.lane(Lane::V1)[1].start, 3, "V1's far half slid back"); assert_eq!( p.lane(Lane::A1)[1].start, 3, "A1's far half rode along, in sync" ); assert!(p.undo(), "one press undoes the whole close"); assert_eq!(shape(&p), before, "back to exactly where it was"); } /// The real design question: a take whose gap is not the *same size* on /// both lanes cannot ripple by one number without pulling a lane's clip /// out from under the other's sync -- refused by name, with the frame /// naming which lane is out of step, rather than silently taking real /// media off the shorter side. #[test] fn closing_a_gap_refuses_a_take_whose_gap_lengths_differ() { let sources = vec![Source::new(FILE, 0)]; let video = vec![linked(0, 0, 3, 1), linked(5, 5, 9, 3)]; // A1's second half starts at 4, not 5: its gap is [3,4), one frame // shorter than V1's [3,5) -- a legal offset-preserving group, and // still one take. let audio = vec![linked(0, 0, 3, 1), linked(4, 5, 9, 3)]; let p = Project::from_parts( sources, two(video, audio), Vec::new(), Vec::new(), Vec::new(), ) .expect("valid parts"); assert_eq!(p.gap_at(Lane::V1, 3), Some((3, 2))); assert_eq!(p.gap_at(Lane::A1, 3), Some((3, 1)), "a shorter gap on A1"); let err = p .gap_take_scope(Lane::V1, 3, 2) .expect_err("a mismatched gap cannot ripple by one number") .to_string(); assert!(err.contains("A1"), "{err}"); assert!(err.contains("out of sync"), "{err}"); } /// A gap on `V1` whose `A1` half is not a gap at all -- real audio plays /// there -- cannot close without cutting that audio out from under /// nothing asked to touch it: refused, not widened silently past. #[test] fn closing_a_gap_refuses_a_take_whose_other_half_is_solid() { let sources = vec![Source::new(FILE, 0)]; let video = vec![linked(0, 0, 3, 1), linked(5, 5, 9, 3)]; // A1 plays through the whole span as one continuous clip, sharing // V1's first-half link: nothing empty at frame 3 there at all. let audio = vec![linked(0, 0, 9, 1)]; let p = Project::from_parts( sources, two(video, audio), Vec::new(), Vec::new(), Vec::new(), ) .expect("valid parts"); assert_eq!(p.gap_at(Lane::V1, 3), Some((3, 2))); assert_eq!( p.gap_at(Lane::A1, 3), None, "A1 plays through, no gap there" ); let err = p .gap_take_scope(Lane::V1, 3, 2) .expect_err("a solid partner cannot ripple with the gap") .to_string(); assert!(err.contains("A1"), "{err}"); assert!(err.contains("out of sync"), "{err}"); } /// The sweep is the single-gap close repeated on one lane, but measured once /// and applied from the right. The differing gap lengths here expose a /// front-to-back bug: the last clip would be cut against a stale frame after /// the first gap shifted it. #[test] fn close_all_gaps_on_one_lane_closes_every_bounded_gap_and_undoes_once() { let mut p = Project::single(FILE, 1); let v = p.add_lane(LaneKind::Video); assert!(p.place(v, 2, clip(2, 0, 2, 0)), "head gap [0,2)"); assert!(p.place(v, 7, clip(7, 2, 4, 0)), "middle gap [4,7)"); assert!(p.place(v, 14, clip(14, 4, 5, 0)), "middle gap [9,14)"); let before = shape(&p); assert_eq!(p.gap_count(v), 3); let report = p.close_all_gaps_on_lane(v).expect("the lane exists"); assert_eq!(report.closed, 3); assert!(report.skipped.is_empty()); assert_eq!( p.lane(v), &[clip(0, 0, 2, 0), clip(2, 2, 4, 0), clip(4, 4, 5, 0)], "all gaps closed without eating source frames" ); assert!(p.undo(), "one undo for the whole sweep"); assert_eq!(shape(&p), before); } /// Empty, already-contiguous and tail-only lanes are not controls that do /// work. A single clip with a head gap does close, because there is a real /// placement to ripple back. #[test] fn close_all_gaps_on_one_lane_handles_empty_single_contiguous_and_tail_only_lanes() { let mut p = Project::single(FILE, 9); let v = p.add_lane(LaneKind::Video); let history = p.history.len(); let report = p.close_all_gaps_on_lane(v).expect("empty lane exists"); assert_eq!( report, GapSweep::default(), "an empty lane has no bounded gap" ); assert_eq!(p.history.len(), history, "no undo step for a no-op"); let mut p = Project::single(FILE, 9); let v = p.add_lane(LaneKind::Video); assert!(p.place(v, 5, clip(5, 0, 3, 0))); let before = shape(&p); let report = p .close_all_gaps_on_lane(v) .expect("single clip lane exists"); assert_eq!(report.closed, 1, "the head gap closes"); assert_eq!(p.lane(v), &[clip(0, 0, 3, 0)]); assert!(p.undo()); assert_eq!(shape(&p), before); let mut p = Project::single(FILE, 9); let history = p.history.len(); let report = p.close_all_gaps_on_lane(Lane::V1).expect("V1 exists"); assert_eq!(report, GapSweep::default(), "contiguous clips have no gaps"); assert_eq!(p.history.len(), history); let mut p = Project::single(FILE, 9); let v = p.add_lane(LaneKind::Video); assert!(p.place(v, 0, clip(0, 0, 3, 0))); let history = p.history.len(); let report = p.close_all_gaps_on_lane(v).expect("tail-only lane exists"); assert_eq!( report, GapSweep::default(), "the open-ended tail is not closed" ); assert_eq!(p.lane(v), &[clip(0, 0, 3, 0)]); assert_eq!(p.history.len(), history); } /// A sweep closes the gaps that satisfy the linked-take rule and leaves the /// rest in place with a count and reason. Here [2,4) matches on both lanes, /// while V1's [6,9) has only [6,8) empty on A1. #[test] fn close_all_gaps_on_one_lane_partially_closes_linked_takes_and_reports_skips() { let sources = vec![Source::new(FILE, 0)]; let video = vec![linked(0, 0, 2, 1), linked(4, 4, 6, 2), clip(9, 9, 11, 0)]; let audio = vec![linked(0, 0, 2, 1), linked(4, 4, 6, 2), clip(8, 8, 11, 0)]; let mut p = Project::from_parts( sources, two(video, audio), Vec::new(), Vec::new(), Vec::new(), ) .expect("valid parts"); let before = shape(&p); let report = p.close_all_gaps_on_lane(Lane::V1).expect("V1 exists"); assert_eq!(report.closed, 1); assert_eq!(report.skipped.len(), 1); assert_eq!(report.skipped[0].start, 6); assert!(report.skipped[0].reason.contains("A1"), "{report:?}"); assert!( report.skipped[0].reason.contains("out of sync"), "{report:?}" ); assert_eq!(p.lane(Lane::V1)[1].start, 2, "the matched take moved"); assert_eq!( p.lane(Lane::A1)[1].start, 2, "and its other half stayed in sync" ); assert_eq!( p.lane(Lane::V1)[2].start, 7, "the skipped later gap remains" ); assert_eq!( p.lane(Lane::A1)[2].start, 6, "A1's shorter gap remains shorter" ); links_are_consistent(&p.lanes).expect("every linked take that moved stayed whole"); assert!(p.undo(), "one undo restores every closed gap"); assert_eq!(shape(&p), before); } /// Subtitle lanes use the same bounded-hole hit test as clip lanes, against /// their own caption storage: head gaps and gaps between cues count, an /// empty lane and the tail after the last cue do not. #[test] fn gap_at_finds_the_hole_a_caption_frame_sits_in() { let mut p = Project::single(FILE, 20).with_subtitles(vec![subtitle_track()]); let s = p.add_lane(LaneKind::Subtitle); assert_eq!( p.gap_at(s, 0), None, "an empty subtitle lane has nothing to close" ); p.place_sub(s, 5, caption(0, 3)) .expect("single cue at [5,8)"); assert_eq!(p.gap_at(s, 0), Some((0, 5)), "head gap before first cue"); assert_eq!(p.gap_at(s, 4), Some((0, 5))); assert_eq!(p.gap_at(s, 5), None, "inside the cue"); assert_eq!(p.gap_at(s, 8), None, "tail after a single cue is not a gap"); p.place_sub(s, 12, caption(0, 2)) .expect("second cue at [12,14)"); assert_eq!(p.gap_at(s, 8), Some((8, 4)), "gap between cues"); assert_eq!(p.gap_at(s, 11), Some((8, 4))); assert_eq!( p.gap_at(s, 14), None, "tail after the last cue is still open-ended" ); } /// Closing a caption gap ripples the subtitle lane only. It is not a media /// take close, so the video and audio lanes keep their frame positions. #[test] fn closing_a_caption_gap_ripples_no_video_or_audio_clips() { let mut p = Project::single(FILE, 20).with_subtitles(vec![subtitle_track()]); let s = p.add_lane(LaneKind::Subtitle); p.place_sub(s, 5, caption(0, 3)) .expect("single cue at [5,8)"); let before_clips = shape(&p); let before_subs = p.sub_lane(s).to_vec(); let (start, len) = p.gap_at(s, 0).expect("gap before the caption"); assert_eq!((start, len), (0, 5)); p.cut_regions(&[(start, len)], &[s]) .expect("caption lane gap closes"); assert_eq!(shape(&p), before_clips, "media lanes did not move"); assert_eq!(p.sub_lane(s), &[caption(0, 3)], "caption slid left"); assert!(p.undo(), "one undo restores the caption close"); assert_eq!(shape(&p), before_clips); assert_eq!(p.sub_lane(s), before_subs); } /// The subtitle sweep is the clip sweep's bounded-gap arithmetic on /// [`SubClip`] storage: right-to-left, one undo, no tail close, and no /// history entry for empty or already-contiguous lanes. #[test] fn close_all_gaps_on_subtitle_lane_closes_bounded_gaps_and_undoes_once() { let mut p = Project::single(FILE, 30).with_subtitles(vec![subtitle_track()]); let s = p.add_lane(LaneKind::Subtitle); let history = p.history.len(); let report = p .close_all_gaps_on_lane(s) .expect("empty subtitle lane exists"); assert_eq!( report, GapSweep::default(), "empty subtitle lane has no bounded gap" ); assert_eq!(p.history.len(), history, "empty sweep is not an undo step"); p.place_sub(s, 2, caption(0, 2)).expect("head gap [0,2)"); p.place_sub(s, 7, caption(0, 2)).expect("middle gap [4,7)"); p.place_sub(s, 14, caption(0, 1)) .expect("middle gap [9,14), tail after 15"); let before = p.sub_lane(s).to_vec(); assert_eq!(p.gap_count(s), 3); let report = p.close_all_gaps_on_lane(s).expect("subtitle lane exists"); assert_eq!(report.closed, 3); assert!(report.skipped.is_empty()); assert_eq!( p.sub_lane(s), &[caption(0, 2), caption(2, 2), caption(4, 1)], "all bounded caption gaps closed without touching windows" ); assert!(p.undo(), "one undo restores every caption gap"); assert_eq!(p.sub_lane(s), before); let mut p = Project::single(FILE, 30).with_subtitles(vec![subtitle_track()]); let s = p.add_lane(LaneKind::Subtitle); p.place_sub(s, 0, caption(0, 3)) .expect("contiguous at the head"); let history = p.history.len(); let report = p.close_all_gaps_on_lane(s).expect("subtitle lane exists"); assert_eq!(report, GapSweep::default(), "the open tail is not closed"); assert_eq!(p.sub_lane(s), &[caption(0, 3)]); assert_eq!(p.history.len(), history); } #[test] fn ripple_delete_spans_partial_clips() { let mut p = three(); // [2, 6) covers the tail of clip 0, all of clip 1 and the head of clip 2. assert!(p.ripple_delete(2, 4)); assert_eq!(shape(&p)[0], vec![clip(0, 0, 2, 0), clip(2, 6, 9, 0)]); assert_eq!(shape(&p)[1], shape(&p)[0]); assert_eq!(p.timeline_frames(), 5); assert!(!p.ripple_delete(0, 0), "an empty range is not a delete"); assert!(p.undo()); assert_eq!(shape(&p), shape(&three())); // One that takes everything is a delete like any other: the timeline // empties, and the undo it cost brings it back whole. assert!(p.ripple_delete(0, 100), "the timeline is emptiable"); assert_eq!(p.timeline_frames(), 0); assert!( !p.ripple_delete(0, 100), "and a delete with nothing left to take is not an undo step" ); assert!(p.undo()); assert_eq!(shape(&p), shape(&three())); } /// A ripple trim closes the hole its trim leaves: the head cut from the middle clip goes, the /// clip keeps its start, and the last clip moves up by as much; an end edge moves what follows. #[test] fn ripple_trim_closes_the_hole_it_leaves() { let mut p = three(); assert!(p.ripple_trim(Lane::V1, 1, Edge::Start, 4, &[9])); assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 4, 5, 0), clip(4, 5, 9, 0)]); assert_eq!(shape(&p)[1], shape(&p)[0], "the linked audio moves with it"); assert!(p.undo()); assert_eq!(shape(&p), shape(&three())); assert!(p.ripple_trim(Lane::V1, 1, Edge::End, 4, &[9])); assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 4, 0), clip(4, 5, 9, 0)]); } /// Undo says the newest step's name and Redo the one it took back, and the names /// walk back and forth with the steps they belong to. #[test] fn labels_follow_undo_and_redo() { let mut p = Project::single(FILE, 270); let mark = p.step_mark(); assert!(p.split(100)); p.name_step(mark, "Split"); let mark = p.step_mark(); assert!(p.set_lane_gain_db(Lane::A1, -3.0)); p.name_step(mark, "Volume"); assert_eq!(p.undo_label(), Some("Volume")); assert_eq!(p.redo_label(), None); assert!(p.undo()); assert_eq!(p.undo_label(), Some("Split")); assert_eq!(p.redo_label(), Some("Volume")); assert!(p.undo()); assert_eq!(p.undo_label(), None, "nothing left to take back"); assert_eq!(p.redo_label(), Some("Split")); assert!(p.redo()); assert_eq!(p.redo_label(), Some("Volume")); assert!(p.redo()); assert_eq!(p.redo_label(), None); assert_eq!(p.undo_label(), Some("Volume")); } /// A name goes only on a step the named action pushed: an action that changed /// nothing, and one that pushed nothing at all, leave the step under them alone. #[test] fn a_no_op_takes_no_name_and_drops_none() { let mut p = Project::single(FILE, 270); assert!(p.split(100)); let mark = p.step_mark(); assert!(!p.set_lane_gain_db(Lane::A1, 0.0), "already at 0 dB"); assert_eq!(p.undo_label(), None, "the unnamed split is still the newest step"); p.name_step(mark, "Volume"); assert_eq!(p.undo_label(), None, "the no-op pushed nothing to name"); let mark = p.step_mark(); assert!(p.set_lane_gain_db(Lane::A1, -3.0)); assert!(!p.set_lane_gain_db(Lane::A1, -3.0), "a repeat is a no-op"); assert_eq!(p.undo_label(), None, "the real edit is unnamed so far"); p.name_step(mark, "Volume"); assert_eq!(p.undo_label(), Some("Volume")); assert_eq!(p.history_len(), 2); } /// A step that names itself and then gets settled as a no-op carries its name away /// with it: the step under it keeps its own. #[test] fn a_settled_no_op_drops_its_name_with_its_step() { let mut p = Project::single(FILE, 270); let mark = p.step_mark(); assert!(p.split(100)); p.name_step(mark, "Split"); let mark = p.step_mark(); assert!(!p.set_lane_gain_db(Lane::A1, 0.0)); assert_eq!(p.undo_label(), Some("Split"), "the no-op on top is not the step"); p.name_step(mark, "Volume"); assert_eq!(p.undo_label(), Some("Split")); assert_eq!(p.history_len(), 1); assert_eq!(p.redo_len(), 0); } /// Names ride with their steps through the cap: the oldest step goes, every step kept /// still says what it did, and the one taken back stays named after a redo. #[test] fn labels_survive_the_cap_eviction() { let mut p = Project::single(FILE, 400); let frames = HISTORY_CAP as u32 + 5; for frame in 1..=frames { let mark = p.step_mark(); assert!(p.split(frame)); let name = if frame <= 3 { "Old" } else { "Split" }; p.name_step(mark, name); } assert_eq!(p.history_len(), HISTORY_CAP); let mut taken = 0; while let Some(label) = p.undo_label().map(str::to_string) { assert_eq!(label, "Split", "a kept step keeps its own name"); assert!(p.undo()); taken += 1; } assert_eq!(taken, HISTORY_CAP); assert!(p.redo()); assert_eq!(p.undo_label(), Some("Split")); assert_eq!(p.redo_label(), Some("Split")); } /// A batch is one step and so one name: whatever the batch did, Undo names it once. #[test] fn a_batch_carries_one_label() { let mut p = Project::single(FILE, 270); let mark = p.step_mark(); p.begin_batch(); assert!(p.split(100)); assert!(p.set_lane_gain_db(Lane::A1, -3.0)); p.end_batch(); p.name_step(mark, "Split, volume"); assert_eq!(p.history_len(), 1, "the batch is one step"); assert_eq!(p.undo_label(), Some("Split, volume")); assert!(p.undo()); assert_eq!(p.redo_label(), Some("Split, volume")); } #[test] fn undo_restores_exactly() { let mut p = three(); let after_splits = shape(&p); assert!(p.delete(1)); assert!(p.undo()); assert_eq!(shape(&p), after_splits); // walk all the way back through both splits, then run dry assert!(p.undo()); assert_eq!(p.clips().len(), 2); assert!(p.undo()); assert_eq!(shape(&p)[0], vec![clip(0, 0, 9, 0)]); assert!(!p.undo(), "empty history"); assert_eq!(shape(&p)[0], vec![clip(0, 0, 9, 0)]); } #[test] fn redo_restores_exactly() { let mut p = three(); assert!(p.delete(1)); let after_delete = p.parts(); assert!(p.undo()); assert!(p.redo()); assert_eq!(p.parts(), after_delete, "redo lands back byte-identical"); assert!(!p.redo(), "empty redo stack"); } #[test] fn edit_after_undo_empties_redo() { let mut p = three(); assert!(p.delete(1)); assert!(p.undo()); assert_eq!(p.redo_len(), 1, "undo left a branch to redo"); assert!(p.split(1), "a fresh edit"); assert_eq!(p.redo_len(), 0, "the fresh edit clears it"); assert!(!p.redo(), "so redo has nothing left"); } #[test] fn redo_on_empty_stack_is_false() { let mut p = three(); assert!(!p.redo(), "nothing has been undone yet"); } #[test] fn take_and_restore_redo_round_trips() { let mut p = three(); assert!(p.delete(1)); let after_delete = p.parts(); assert!(p.undo()); let cp = p.take_redo(); assert_eq!(p.redo_len(), 0, "taken out with no step"); assert!(p.split(1), "a trial edit, which clears what is left"); p.restore_redo(cp); assert_eq!(p.redo_len(), 1, "the stack the trial found is back"); assert!(p.redo()); assert_eq!(p.parts(), after_delete, "and it still redoes to the same state"); } #[test] fn held_history_evicts_nothing_until_released() { let mut p = Project::single(FILE, 400); for frame in 1..=HISTORY_CAP as u32 { assert!(p.split(frame)); } p.hold_history(true); for frame in 101..=103 { assert!(p.split(frame)); } assert_eq!(p.history.len(), HISTORY_CAP + 3, "a held trial evicts nothing"); for _ in 0..3 { assert!(p.undo()); } assert_eq!(p.history.len(), HISTORY_CAP, "the trial rolled back, the cap is still the same"); p.hold_history(false); assert_eq!(p.history.len(), HISTORY_CAP); for frame in 101..=103 { assert!(p.split(frame)); } p.hold_history(true); for frame in 110..=112 { assert!(p.split(frame)); } assert_eq!(p.history.len(), HISTORY_CAP + 3); p.hold_history(false); assert_eq!(p.history.len(), HISTORY_CAP, "releasing trims back to the cap"); } /// Past the cap the *oldest* step goes, so a long session undoes exactly /// `HISTORY_CAP` gestures and then runs dry -- it does not grow for ever. #[test] fn history_stops_at_the_cap() { let mut p = Project::single(FILE, 400); for frame in 1..=150 { assert!(p.split(frame), "split {frame} is an edit"); } assert_eq!(p.history.len(), HISTORY_CAP, "and only the last 100 kept"); for step in 0..HISTORY_CAP { assert!(p.undo(), "undo {step} is inside the cap"); } assert!(!p.undo(), "and the 101st has nothing left to take"); // 100 undos off 150 splits is the timeline after the 50th: the older // steps were dropped, not the newer ones. assert_eq!(p.clips().len(), 51); } /// The push count keeps rising past the cap, where `history_len` stops, so a /// caller can tell how many snapshots a batch pushed even at the cap. #[test] fn undo_pushes_counter_keeps_counting_past_the_cap() { let mut p = Project::single(FILE, 400); let start = p.history_pushes(); for frame in 1..=HISTORY_CAP as u32 + 5 { assert!(p.split(frame)); } assert_eq!(p.history.len(), HISTORY_CAP, "the length has saturated"); assert_eq!(p.history_pushes() - start, HISTORY_CAP as u64 + 5); assert!(p.undo()); assert_eq!( p.history_pushes() - start, HISTORY_CAP as u64 + 5, "an undo pushes nothing" ); } /// Undo/redo by hand across a split must never merge two takes into one /// group by reusing an id. #[test] fn group_ids_are_never_reused() { let mut p = Project::single(FILE, 9); assert!(p.split(3)); let first = p.clips()[1].link; assert!(p.undo()); assert!(p.split(5)); assert_ne!(p.clips()[1].link, first, "a retired id must not come back"); } #[test] fn segments_from_reads_the_audio_lane() { let p = three(); // mid-clip: partial first segment, whole clips after it assert_eq!( p.segments_from(4, FPS), vec![ (Some(0), 4.0 / 30.0, 5.0 / 30.0), (Some(0), 5.0 / 30.0, 9.0 / 30.0) ] ); // on a boundary: nothing partial assert_eq!( p.segments_from(3, FPS), vec![ (Some(0), 3.0 / 30.0, 5.0 / 30.0), (Some(0), 5.0 / 30.0, 9.0 / 30.0) ] ); // from the top: one entry per clip assert_eq!(p.segments_from(0, FPS).len(), 3); // last clip only assert_eq!(p.segments_from(8, FPS), vec![(Some(0), 8.0 / 30.0, 0.3)]); // past the end / unusable fps assert!(p.segments_from(9, FPS).is_empty()); assert!(p.segments_from(0, 0.0).is_empty()); assert!(p.segments_from(0, f64::NAN).is_empty()); } #[test] fn a_gap_in_the_audio_lane_is_a_silent_segment_of_its_own_length() { let mut p = three(); assert!(p.lift(Lane::A1, 1)); // silence over timeline [3, 5) assert_eq!( p.segments_from(0, FPS), vec![ (Some(0), 0.0, 3.0 / 30.0), (None, 0.0, 2.0 / 30.0), (Some(0), 5.0 / 30.0, 9.0 / 30.0), ] ); // The list still covers exactly the timeline: that is what stops the // master clock stalling at the hole. let played: f64 = p.segments_from(0, FPS).iter().map(|(_, a, b)| b - a).sum(); assert!((played - 9.0 / 30.0).abs() < 1e-9, "{played}"); // Starting inside the gap trims the silence, not the clip after it. assert_eq!( p.segments_from(4, FPS), vec![(None, 0.0, 1.0 / 30.0), (Some(0), 5.0 / 30.0, 9.0 / 30.0)] ); // ...and a lane that is nothing but gap is a run of silence. assert!(p.lift(Lane::A1, 0)); assert!(p.lift(Lane::A1, 0)); assert_eq!(p.segments_from(0, FPS), vec![(None, 0.0, 0.3)]); } /// `three()` plus a whole second source appended: clips [0,3) [3,5) [5,9) /// of source 0 then [0,4) of source 1. fn two_sources() -> Project { let mut p = three(); let s = p.import(FILE2, 0); assert_eq!(s, 1); assert!(p.append_clip(s, 4)); p } #[test] fn import_dedups_and_appends() { let mut p = Project::single(FILE, 9); assert_eq!(p.sources().len(), 1, "the opened file is source 0"); assert_eq!(p.import(FILE, 0), 0, "reimporting the open file reuses 0"); assert_eq!(p.import(FILE2, 0), 1); assert_eq!(p.import(FILE2, 0), 1, "second import of the same path"); // ...but the same file on another audio stream is another source: it is // what a clip names, and a clip plays exactly one stream. assert_eq!(p.import(FILE2, 1), 2, "a second stream is a second source"); assert_eq!(p.import(FILE2, 1), 2, "and dedups on the pair"); assert_eq!(p.sources()[2].audio_stream, 1); assert!(p.append_clip(2, 4), "a stream entry is placeable"); assert!(p.undo()); assert_eq!(p.sources().len(), 3); assert!(!p.append_clip(3, 5), "unknown source index"); assert_eq!(p.clips().len(), 1, "a refusal changes nothing"); // Two spellings of one real file are one source: this is the case the // raw-path comparison above would get wrong. let here = concat!(env!("CARGO_MANIFEST_DIR"), "/src/project.rs"); let detour = concat!(env!("CARGO_MANIFEST_DIR"), "/src/../src/project.rs"); let mut p = Project::single(here, 9); assert_eq!(p.import(detour, 0), 0); assert_eq!(p.sources().len(), 1); } #[test] fn append_clip_is_one_undo_step() { let mut p = two_sources(); assert_eq!(p.timeline_frames(), 13); assert_eq!(shape(&p)[0][3], clip(9, 0, 4, 1)); assert_eq!(shape(&p)[1][3], clip(9, 0, 4, 1), "and on the audio lane"); assert_eq!(p.map_timeline(9), Some((3, 0)), "source 1 starts at 9"); assert!(p.undo()); assert_eq!(shape(&p), shape(&three()), "one step back to one source"); assert_eq!( p.sources().len(), 2, "the orphan source entry stays -- indexes are forever" ); // ...and it is still usable, so a redo-by-hand costs no reimport. assert!(p.append_clip(1, 0)); assert_eq!(p.clips()[3].len(), 1, "clamped like `single`"); } #[test] fn edits_carry_the_source_along() { let mut p = two_sources(); // A split inside the second source splits into two source-1 clips. assert!(p.split(11)); assert_eq!(shape(&p)[0][3], clip(9, 0, 2, 1)); assert_eq!(shape(&p)[0][4], clip(11, 2, 4, 1)); // A source-1 clip pasted into source-0 territory keeps its file, and so // does the source-0 half that got split off around it. let copied = p.clips()[4].clone(); assert!(p.paste(1, copied)); assert_eq!(p.clips()[1].source, 1); assert_eq!(p.clips()[1].in_frame, 2); assert_eq!(p.clips()[2].source, 0); assert!(p.undo()); assert_eq!(p.clips()[1].source, 0, "undo restores sources too"); } #[test] fn segments_from_names_the_source() { let p = two_sources(); // Mid-clip in source 0, then the rest of source 0, then source 1 whole. assert_eq!( p.segments_from(4, FPS), vec![ (Some(0), 4.0 / 30.0, 5.0 / 30.0), (Some(0), 5.0 / 30.0, 9.0 / 30.0), (Some(1), 0.0, 4.0 / 30.0), ] ); // Mid-clip in source 1: only that source is left. assert_eq!( p.segments_from(10, FPS), vec![(Some(1), 1.0 / 30.0, 4.0 / 30.0)] ); assert!(p.segments_from(13, FPS).is_empty()); // Segments still cover exactly the timeline across the join. let played: f64 = p.segments_from(0, FPS).iter().map(|(_, a, b)| b - a).sum(); assert!((played - f64::from(p.timeline_frames()) / FPS).abs() < 1e-9); } #[test] fn segments_follow_deletes_and_fps() { let mut p = three(); assert!(p.delete(0)); // source seconds, not timeline: deleting the head does not shift them let segs = p.segments_from(0, 25.0); assert_eq!( segs, vec![(Some(0), 3.0 / 25.0, 0.2), (Some(0), 0.2, 9.0 / 25.0)] ); let played: f64 = segs.iter().map(|(_, a, b)| b - a).sum(); assert!( (played - p.timeline_frames() as f64 / 25.0).abs() < 1e-9, "segments must cover exactly the timeline: {played}" ); } #[test] fn every_source_is_saved_in_session_order_and_the_clips_keep_theirs() { // Import a second file, undo it: source 1 is now an orphan. It is kept // anyway -- an import is not an undo step, so the library entry stays // at the index a reload will look for it at. let mut p = two_sources(); assert!(p.undo()); let (sources, lanes, eq, color, _transform) = p.without_orphan_sources(); assert_eq!( sources, vec![Source::new(FILE, 0), Source::new(FILE2, 0)], "the orphan is kept, in session order" ); assert!(eq.is_empty(), "and a project with no equalizer writes none"); assert!(color.is_empty(), "nor a colour it never graded with"); assert_eq!( lanes, two(three().clips().to_vec(), three().lane(Lane::A1).to_vec()), "the clips are untouched" ); // Three sources where only the middle one is unused: all three are // saved, in order, and the clip keeps the index it was given -- 2 here, // not renumbered down to 1. (The clip from `single` is still source 0.) let mut p = Project::single(FILE, 9); assert_eq!(p.import(FILE2, 0), 1); assert_eq!(p.import("/nonexistent/c.mp4", 0), 2); assert!(p.append_clip(2, 4)); let (sources, lanes, eq, color, _transform) = p.without_orphan_sources(); assert_eq!( sources, vec![ Source::new(FILE, 0), Source::new(FILE2, 0), Source::new("/nonexistent/c.mp4", 0) ] ); assert_eq!( lanes[0].1.iter().map(|c| c.source).collect::<Vec<_>>(), [0, 2] ); // ...and what comes out is loadable, with the same timeline. let reloaded = Project::from_parts(sources, lanes, eq, color, Vec::new()).expect("from_parts"); assert_eq!(reloaded.timeline_frames(), p.timeline_frames()); assert_eq!(reloaded.sources().len(), 3); } /// Taking a file out of the library: refused by name -- lanes and counts -- /// while a clip still plays from it, and once nothing does, the entry goes /// and every clip that indexed past it is renumbered onto the short list. /// A project rebuilt from parts keeps each source's bin and star (the loader's own door). #[test] fn from_parts_keeps_a_sources_bin_and_star() { let mut one = Source::new(FILE, 0); one.fav = true; one.bin = 7; let lanes = vec![(LaneKind::Video, vec![clip(0, 0, 10, 0)]), (LaneKind::Audio, vec![clip(0, 0, 10, 0)])]; let p = Project::from_parts(vec![one], lanes, vec![], vec![], Vec::new()).expect("valid parts"); assert!(p.sources()[0].fav); assert_eq!(p.sources()[0].bin, 7); } #[test] fn bins_and_favourites_follow_the_usual_edit_rules() { let mut p = two_sources(); let a = p.create_bin(" Interviews ", 0).expect("makes a bin"); assert_eq!(p.bins()[0].name, "Interviews"); assert!(p.create_bin("interviews", 0).is_err(), "no twin at one place"); let b = p.create_bin("Day 1", a).expect("one level of nesting"); assert!(p.create_bin("deeper", b).is_err(), "no third level"); assert!(p.create_bin(" ", 0).is_err() && p.create_bin("a\nb", 0).is_err() && p.create_bin("x", 99).is_err()); assert_eq!(p.move_to_bin(&[0, 1, 1], b), Ok(2)); assert_eq!(p.move_to_bin(&[0], b), Ok(0), "no change, no history"); assert!(p.move_to_bin(&[7], b).is_err() && p.move_to_bin(&[0], 42).is_err()); assert_eq!(p.set_favourite(&[1], true), Ok(1)); assert_eq!(p.set_favourite(&[1], true), Ok(0)); assert!(p.sources()[1].fav && p.sources()[1].bin == b); assert_eq!(p.rename_bin(b, "Day one"), Ok(true)); assert_eq!(p.rename_bin(b, "Day one"), Ok(false)); // Undo walks back one step at a time: rename, star, move, then the bins themselves. assert!(p.undo() && p.bins()[1].name == "Day 1"); assert!(p.undo() && !p.sources()[1].fav); assert!(p.undo() && p.sources()[0].bin == 0); assert!(p.redo() && p.sources()[0].bin == b); // Removing the outer bin keeps what it held: sources go up, inner bins to the top. assert_eq!(p.remove_bin(a), Ok(0)); assert_eq!(p.bins().len(), 1); assert_eq!(p.bins()[0].parent, 0); assert_eq!(p.remove_bin(b), Ok(2)); assert!(p.sources().iter().all(|s| s.bin == 0)); assert!(p.undo() && p.sources()[0].bin == b && p.bins().len() == 1); assert!(p.undo() && p.bins().len() == 2 && p.bins()[1].parent == a); // A source's identity is its file and stream, not its star. assert_eq!(p.sources()[0], Source::new(&p.sources()[0].path, 0)); } #[test] fn remove_source_refuses_what_plays_and_renumbers_what_is_left() { // Sources 0 (FILE, three clips) and 1 (FILE2, one take on both lanes), // plus a second stream of FILE as source 2 with a take of its own. let mut p = two_sources(); assert_eq!(p.import(FILE, 1), 2, "another stream is another source"); assert!(p.append_clip(2, 4)); let refusal = p .remove_source(1) .expect_err("a source with clips on it cannot go") .to_string(); assert!(refusal.contains(FILE2), "names the file: {refusal}"); assert!( refusal.contains("V1 (1 clip)") && refusal.contains("A1 (1 clip)"), "names the lanes holding it: {refusal}" ); assert!(refusal.contains("still plays"), "{refusal}"); assert_eq!(p.sources().len(), 3, "a refusal changes nothing"); assert_eq!(shape(&p)[0][3].source, 1); // Delete FILE2's take -- the whole span, on every lane -- and the entry // is free to go. Source 2's clips become source 1's. assert!(p.delete_in(Lane::V1, 3)); assert!(!p.clips().iter().any(|c| c.source == 1), "nothing plays it"); p.remove_source(1).expect("nothing plays FILE2 any more"); assert_eq!( p.sources(), [Source::new(FILE, 0), Source::new(FILE, 1)], "the middle entry is gone" ); assert_eq!( shape(&p)[0].iter().map(|c| c.source).collect::<Vec<_>>(), [0, 0, 0, 1], "the clips past it renumbered" ); assert_eq!( shape(&p)[1].iter().map(|c| c.source).collect::<Vec<_>>(), [0, 0, 0, 1], "on every lane" ); // What is left is a project that still loads, which is the whole point // of renumbering rather than leaving a hole. let (sources, lanes, eq, color, _transform) = p.without_orphan_sources(); Project::from_parts(sources, lanes, eq, color, Vec::new()).expect("from_parts"); } /// The two edges of a removal: it retires the undo stack (the corner-cut on /// [`Project::remove_source`]), and the last entry goes like any other /// once nothing plays it -- a project may name no file at all. #[test] fn relink_is_one_undo_step_and_undo_puts_the_old_path_back() { let mut p = two_sources(); let old = p.sources()[1].path.clone(); let new = Source::new(FILE, 0).path; let depth = p.history_len(); p.relink_source(1, Path::new(FILE)).expect("relinks"); assert_eq!(p.sources()[1].path, new); assert_eq!(p.history_len(), depth + 1, "one step"); assert!(p.undo()); assert_eq!(p.sources()[1].path, old, "undo points back at the old file"); assert!(p.redo()); assert_eq!(p.sources()[1].path, new, "redo re-points it"); p.relink_source(1, Path::new(FILE)).expect("the path it has"); assert_eq!(p.history_len(), depth + 1, "a relink to the same path is no step"); assert!(p.relink_source(9, Path::new(FILE)).is_err(), "no source 9"); } #[test] fn undo_keeps_a_source_imported_after_the_step() { let mut p = three(); assert_eq!(p.import(FILE2, 0), 1); assert!(p.undo(), "the last split"); assert_eq!(p.sources().len(), 2, "the import outlives the undo, as an import does"); assert_eq!(p.sources()[1].path, Source::new(FILE2, 0).path); assert!(p.redo()); assert_eq!(p.sources().len(), 2); } #[test] fn adopt_as_step_takes_the_other_timeline_and_undo_gives_this_one_back() { let geometry = |p: &Project| -> Vec<Vec<(u32, u32, u32, usize)>> { p.lanes() .into_iter() .map(|l| p.lane(l).iter().map(|c| (c.start, c.in_frame, c.out_frame, c.source)).collect()) .collect() }; let mut p = three(); assert!(p.set_eq(Lane::A1, 0, Some(band_at(7))), "this project's own table entry"); let mut other = two_sources(); assert!(other.set_eq(Lane::A1, 0, Some(band_at(2)))); let (want, want_eq) = (geometry(&other), other.eq_of(Lane::A1, 0).cloned()); let saved = geometry(&p); let depth = p.history_len(); assert!(p.adopt_as_step(&other)); assert_eq!(p.history_len(), depth + 1, "one step"); assert_eq!(geometry(&p), want, "the other timeline"); assert_eq!(p.eq_of(Lane::A1, 0).cloned(), want_eq, "its effects read from the appended table"); assert!(p.undo()); assert_eq!(geometry(&p), saved); assert_eq!(p.eq_of(Lane::A1, 0).cloned(), Some(band_at(7)), "and this project's own table is still there"); assert!(p.redo()); assert_eq!(geometry(&p), want); assert_eq!(p.eq_of(Lane::A1, 0).cloned(), want_eq); assert_eq!(p.sources()[1].path, other.sources()[1].path, "the other sources come with it"); } /// A recovery takes the autosave's markers with its timeline, and undo gives back the /// markers this project had: the marker list is part of what a step restores. #[test] fn adopt_as_step_carries_the_markers_of_the_other_timeline() { let marker = |frame: u32, name: &str| Marker { frame, length: 0, name: name.into(), note: String::new(), color: MarkerColor::Red, }; let mut p = three(); assert!(p.add_marker(marker(4, "mine")).is_ok()); let mut other = two_sources(); assert!(other.add_marker(marker(9, "theirs")).is_ok()); assert!(p.adopt_as_step(&other)); let names: Vec<&str> = p.markers().iter().map(|m| m.name.as_str()).collect(); assert_eq!(names, ["theirs"], "the other timeline's markers"); assert!(p.undo()); let names: Vec<&str> = p.markers().iter().map(|m| m.name.as_str()).collect(); assert_eq!(names, ["mine"], "undo gives this project's markers back"); } #[test] fn revision_walks_back_with_undo_and_never_repeats() { let mut p = three(); let saved = p.revision(); assert!(p.delete_in(Lane::V1, 2)); let edited = p.revision(); assert_ne!(edited, saved, "an edit moves the timeline off the saved state"); assert!(p.undo()); assert_eq!(p.revision(), saved, "undo back to the saved timeline gives its id back"); assert!(p.redo()); assert_eq!(p.revision(), edited, "redo walks forward to the same id"); // A new edit after an undo is a state of its own: it must not read as the saved one. assert!(p.undo()); assert!(p.delete_in(Lane::V1, 0)); assert_ne!(p.revision(), saved, "a branch off the saved timeline is not the saved one"); assert_ne!(p.revision(), edited); assert!(!p.redo(), "the branch cleared the redo stack"); assert_eq!(p.revision(), p.revision(), "reading is not an edit"); } #[test] fn revision_moves_once_per_batch_step_and_undo_takes_the_whole_batch_back() { let mut p = three(); let saved = p.revision(); p.begin_batch(); assert!(p.delete_in(Lane::V1, 2)); let first = p.revision(); assert!(p.delete_in(Lane::V1, 1)); assert_ne!(p.revision(), first, "each edit in a batch is a new state"); p.end_batch(); assert!(p.undo()); assert_eq!(p.revision(), saved, "one undo takes the whole batch back to the saved id"); } #[test] fn revision_of_a_recovery_step_is_undone_back_to_the_timeline_it_replaced() { let mut current = three(); assert!(current.delete_in(Lane::V1, 2)); let edited = current.revision(); let other = two_sources(); assert!(current.adopt_as_step(&other)); assert_ne!(current.revision(), edited, "the other timeline is a new state"); assert!(current.undo()); assert_eq!(current.revision(), edited, "undo gives back the timeline it replaced, by its id"); } #[test] fn remove_source_retires_undo_and_empties_the_library() { let mut p = two_sources(); assert!(p.delete_in(Lane::V1, 3), "FILE2's take goes first"); assert!(!p.history.is_empty(), "there is something to undo"); p.remove_source(1).expect("nothing plays FILE2"); assert!( p.history.is_empty(), "a snapshot naming the removed source must not survive it" ); assert!(!p.undo(), "and so there is nothing left to undo"); // The last file standing is held to the one rule every row is -- what // plays cannot go -- and to no other. assert_eq!(p.sources().len(), 1); let refusal = p .remove_source(0) .expect_err("its clips are still on the lanes") .to_string(); assert!(refusal.contains("still plays"), "{refusal}"); assert_eq!(p.sources().len(), 1, "a refusal changes nothing"); while p.delete_in(Lane::V1, 0) {} p.remove_source(0) .expect("the last row goes like any other"); assert!( p.sources().is_empty(), "a project may name no file at all: an empty library over an empty timeline" ); assert_eq!(p.timeline_frames(), 0); assert!( p.remove_source(0).is_err(), "and an index that is not there is refused, not panicked on" ); } /// A clip's equalizer is the clip's: it survives every edit that copies a /// placement, and it comes back with an undo. #[test] fn an_equalizer_follows_the_clip_through_split_copy_and_undo() { let mut p = three(); assert!(p.eq_of(Lane::V1, 0).is_none(), "a fresh clip plays flat"); assert!(p.set_eq(Lane::A1, 0, Some(band_at(2)))); assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(2))); assert!( p.eq_of(Lane::V1, 0).is_none(), "one clip, not the lane above" ); // Cutting an EQ'd clip must not silence half of it: both halves inherit. assert!(p.split(1)); assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(2))); assert_eq!(p.eq_of(Lane::A1, 1), Some(&band_at(2))); assert!(p.eq_of(Lane::A1, 2).is_none(), "the clip after it is flat"); // A clipboard copy carries it -- onto another lane, at that. let copied = p.lane(Lane::A1)[1].clone(); assert!(p.place(Lane::V1, 20, copied)); assert_eq!(p.eq_of(Lane::V1, 4), Some(&band_at(2))); assert!(p.undo()); assert_eq!(p.lane(Lane::V1).len(), 4, "the placement came back off"); // ...and a change to one is one undo step, in both directions. assert!(p.set_eq(Lane::A1, 0, Some(band_at(5)))); assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(5))); assert!(p.undo()); assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(2))); assert!(p.set_eq(Lane::A1, 0, None), "and taking it off is an edit"); assert!(p.eq_of(Lane::A1, 0).is_none()); assert!(p.undo()); assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(2))); // Two refusals, neither of which costs an undo step. let mut nan = band_at(1); nan.bands[0].q = f32::INFINITY; assert!(!p.set_eq(Lane::A1, 99, Some(band_at(1))), "no clip there"); assert!(!p.set_eq(Lane::A1, 0, Some(nan)), "not a finite band"); assert_eq!(p.eq_of(Lane::A1, 0), Some(&band_at(2))); assert!(p.undo()); assert_eq!( p.lane(Lane::A1).len(), 3, "one step back is before the split" ); assert!(p.undo()); assert!( p.eq_of(Lane::A1, 0).is_none(), "and the next is before the setting: the refusals pushed none" ); } /// The table is append-only within a session, exactly as the sources are, so /// an undone setting is still in it -- and a save is where it goes. #[test] fn an_undone_equalizer_is_pruned_when_the_project_is_saved() { let mut p = three(); assert!(p.set_eq(Lane::V1, 0, Some(band_at(1)))); assert!(p.set_eq(Lane::V1, 0, Some(band_at(2)))); assert!(p.undo(), "back to the first setting"); assert_eq!(p.eq_of(Lane::V1, 0), Some(&band_at(1))); let (_, lanes, eq, _, _) = p.without_orphan_sources(); assert_eq!(eq, vec![band_at(1)], "what nothing plays is not written"); assert_eq!(lanes[0].1[0].eq, Some(0), "and the survivor renumbers"); assert_eq!(lanes[0].1[1].eq, None); // One curve on three clips is one entry: settings that are equal share. assert!(p.set_eq(Lane::V1, 1, Some(band_at(1)))); assert!(p.set_eq(Lane::A1, 2, Some(band_at(1)))); let (_, lanes, eq, _, _) = p.without_orphan_sources(); assert_eq!(eq.len(), 1, "equal settings share their entry"); assert_eq!(lanes[1].1[2].eq, Some(0)); reloads(&p, "an equalizer that outlived an undo"); } /// [`an_equalizer_follows_the_clip_through_split_copy_and_undo`]'s twin: a /// grade is the clip's, through every edit that copies a placement. #[test] fn a_colour_follows_the_clip_through_split_copy_and_undo() { let mut p = three(); assert!( p.color_of(Lane::V1, 0).is_none(), "a fresh clip is ungraded" ); assert!(p.set_color(Lane::V1, 0, Some(grade_at(2)))); assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(2))); assert!( p.color_of(Lane::A1, 0).is_none(), "one clip, not the lane below" ); assert!( p.eq_of(Lane::V1, 0).is_none(), "and a grade is not an equalizer" ); // Cutting a graded clip must not grade half of it: both halves inherit. assert!(p.split(1)); assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(2))); assert_eq!(p.color_of(Lane::V1, 1), Some(&grade_at(2))); assert!( p.color_of(Lane::V1, 2).is_none(), "the clip after it is ungraded" ); // A clipboard copy carries it -- onto another lane, at that. let copied = p.lane(Lane::V1)[1].clone(); assert!(p.place(Lane::A1, 20, copied)); assert_eq!(p.color_of(Lane::A1, 4), Some(&grade_at(2))); assert!(p.undo()); assert_eq!(p.lane(Lane::A1).len(), 4, "the placement came back off"); // ...and a change to one is one undo step, in both directions. assert!(p.set_color(Lane::V1, 0, Some(grade_at(5)))); assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(5))); assert!(p.undo()); assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(2))); assert!( p.set_color(Lane::V1, 0, None), "and taking it off is an edit" ); assert!(p.color_of(Lane::V1, 0).is_none()); assert!(p.undo()); assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(2))); // Two refusals, neither of which costs an undo step. let nan = ColorParams { contrast: f32::NAN, ..grade_at(1) }; assert!( !p.set_color(Lane::V1, 99, Some(grade_at(1))), "no clip there" ); assert!(!p.set_color(Lane::V1, 0, Some(nan)), "not a finite value"); assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(2))); assert!(p.undo()); assert_eq!( p.lane(Lane::V1).len(), 3, "one step back is before the split" ); assert!(p.undo()); assert!( p.color_of(Lane::V1, 0).is_none(), "and the next is before the grade: the refusals pushed none" ); } /// A slider drag is one gesture: the press snapshots, every sample after it /// only regrades, and the single undo lands on what the clip was *before* /// the hand touched it -- not one step back down the drag. #[test] fn a_whole_colour_drag_undoes_in_one_step() { let mut p = three(); assert!(p.set_color(Lane::V1, 0, Some(grade_at(1))), "the press"); for n in 2..=8 { assert!(p.set_color_live(Lane::V1, 0, Some(grade_at(n))), "a sample"); } assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(8))); assert!(p.undo()); assert!( p.color_of(Lane::V1, 0).is_none(), "one undo is the whole gesture, back to ungraded" ); // The live write refuses what the snapshotting one refuses, and a // refusal still costs no history either way. assert!(!p.set_color_live(Lane::V1, 99, Some(grade_at(1)))); assert!(!p.set_color_live( Lane::V1, 0, Some(ColorParams { contrast: f32::NAN, ..grade_at(1) }) )); assert!(p.undo()); assert_eq!( p.lane(Lane::V1).len(), 2, "the next step back is the split before the drag: no sample pushed one" ); } /// A drag across the colour wheel is one gesture -- the look's twin of /// [`a_whole_colour_drag_undoes_in_one_step`]: the press snapshots, every /// sample after it only repaints, and the single undo lands on the look /// the clip had *before* the hand touched it. The committing setter is /// still exactly one step, refusals included. /// A LUT is relinked like media: every clip naming the old path moves to the /// new one in one undo step, a missing file is a named problem rather than an /// error, and keys leave the look alone. #[test] fn a_lut_relinks_in_one_step_and_a_missing_one_is_named() { let dir = std::env::temp_dir().join(format!("edith-relink-lut-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let good = dir.join("good.cube"); std::fs::write(&good, "LUT_1D_SIZE 2\n0 0 0\n1 1 1\n").unwrap(); let gone = dir.join("gone.cube"); let mut p = three(); let with = |path: &Path| ColorParams { look: crate::color::Look { lut: Some(crate::cube::LutRef::open(path)), ..Default::default() }, ..Default::default() }; assert!(p.set_color(Lane::V1, 0, Some(with(&gone)))); assert!(p.set_color(Lane::V1, 1, Some(with(&gone)))); let problems = p.lut_problems(); assert_eq!(problems.len(), 1, "one file, named once"); assert!(problems[0].0 == gone && problems[0].1.contains("cannot read")); assert_eq!(p.relink_lut(&dir.join("other.cube"), &good), 0, "nothing names it"); assert_eq!(p.relink_lut(&gone, &good), 2); assert!(p.lut_problems().is_empty()); assert_eq!(p.color_of(Lane::V1, 1).and_then(|c| c.look.lut).map(|r| r.path()), Some(good.clone())); assert!(p.undo()); assert_eq!(p.lut_problems().len(), 1, "one undo puts both back"); std::fs::remove_dir_all(&dir).ok(); } #[test] fn a_whole_look_drag_undoes_in_one_step() { let look = |hue: u8| crate::decode::with_viz_paint_hue(crate::decode::VIZ_PAINT_AZURE, hue); let mut p = three(); assert!(p.set_viz_paint(Lane::V1, 0, look(1)), "the press"); for hue in 2..=8u8 { assert!(p.set_viz_paint_live(Lane::V1, 0, look(hue)), "a sample"); } assert_eq!(p.lane(Lane::V1)[0].viz_paint, look(8)); assert!(p.undo()); assert_eq!( p.lane(Lane::V1)[0].viz_paint, 0, "one undo is the whole gesture, back to the default look" ); // The live write refuses an index that is not there -- and a refusal // costs no history -- while one committing write is one step back. assert!(!p.set_viz_paint_live(Lane::V1, 99, look(1))); assert!(p.set_viz_paint(Lane::V1, 0, look(1)), "a click, not a drag"); assert_eq!(p.lane(Lane::V1)[0].viz_paint, look(1)); assert!(p.undo()); assert_eq!( p.lane(Lane::V1)[0].viz_paint, 0, "the committing setter is one undo step, no more" ); } /// The colour table is append-only within a session, exactly as the eq one /// is -- and a save is where an undone grade goes. #[test] fn an_undone_colour_is_pruned_when_the_project_is_saved() { let mut p = three(); assert!(p.set_color(Lane::V1, 0, Some(grade_at(1)))); assert!(p.set_color(Lane::V1, 0, Some(grade_at(2)))); assert!(p.undo(), "back to the first grade"); assert_eq!(p.color_of(Lane::V1, 0), Some(&grade_at(1))); let (_, lanes, _, color, _) = p.without_orphan_sources(); assert_eq!( color, vec![grade_at(1)], "what nothing plays is not written" ); assert_eq!(lanes[0].1[0].color, Some(0), "and the survivor renumbers"); assert_eq!(lanes[0].1[1].color, None); // One grade on three clips is one entry: settings that are equal share. assert!(p.set_color(Lane::V1, 1, Some(grade_at(1)))); assert!(p.set_color(Lane::A1, 2, Some(grade_at(1)))); let (_, lanes, _, color, _) = p.without_orphan_sources(); assert_eq!(color.len(), 1, "equal grades share their entry"); assert_eq!(lanes[1].1[2].color, Some(0)); reloads(&p, "a colour that outlived an undo"); // An equalizer and a grade on one clip are two independent tables. assert!(p.set_eq(Lane::V1, 0, Some(band_at(3)))); let (_, lanes, eq, color, _) = p.without_orphan_sources(); assert_eq!((eq.len(), color.len()), (1, 1)); assert_eq!((lanes[0].1[0].eq, lanes[0].1[0].color), (Some(0), Some(0))); } /// [`a_colour_follows_the_clip_through_split_copy_and_undo`]'s twin: a /// placement is the clip's, through every edit that copies one. #[test] fn a_transform_follows_the_clip_through_split_copy_and_undo() { let mut p = three(); assert!( p.transform_of(Lane::V1, 0).is_none(), "a fresh clip is untransformed" ); assert!(p.set_transform(Lane::V1, 0, Some(transform_at(2)))); assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(2))); assert!( p.transform_of(Lane::A1, 0).is_none(), "one clip, not the lane below" ); assert!( p.color_of(Lane::V1, 0).is_none(), "and a placement is not a grade" ); // Cutting a transformed clip must not transform half of it: both // halves inherit. assert!(p.split(1)); assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(2))); assert_eq!(p.transform_of(Lane::V1, 1), Some(&transform_at(2))); assert!( p.transform_of(Lane::V1, 2).is_none(), "the clip after it is untransformed" ); // A clipboard copy carries it -- onto another lane, at that. let copied = p.lane(Lane::V1)[1].clone(); assert!(p.place(Lane::A1, 20, copied)); assert_eq!(p.transform_of(Lane::A1, 4), Some(&transform_at(2))); assert!(p.undo()); assert_eq!(p.lane(Lane::A1).len(), 4, "the placement came back off"); // ...and a change to one is one undo step, in both directions. assert!(p.set_transform(Lane::V1, 0, Some(transform_at(5)))); assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(5))); assert!(p.undo()); assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(2))); assert!( p.set_transform(Lane::V1, 0, None), "and taking it off is an edit" ); assert!(p.transform_of(Lane::V1, 0).is_none()); assert!(p.undo()); assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(2))); // Two refusals, neither of which costs an undo step. let nan = TransformParams { scale: f32::NAN, ..transform_at(1) }; assert!( !p.set_transform(Lane::V1, 99, Some(transform_at(1))), "no clip there" ); assert!( !p.set_transform(Lane::V1, 0, Some(nan)), "not a finite value" ); assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(2))); assert!(p.undo()); assert_eq!( p.lane(Lane::V1).len(), 3, "one step back is before the split" ); assert!(p.undo()); assert!( p.transform_of(Lane::V1, 0).is_none(), "and the next is before the placement: the refusals pushed none" ); } /// The transform table is append-only within a session, exactly as the eq /// and colour ones are -- and a save is where an undone placement goes. #[test] fn an_undone_transform_is_pruned_when_the_project_is_saved() { let mut p = three(); assert!(p.set_transform(Lane::V1, 0, Some(transform_at(1)))); assert!(p.set_transform(Lane::V1, 0, Some(transform_at(2)))); assert!(p.undo(), "back to the first placement"); assert_eq!(p.transform_of(Lane::V1, 0), Some(&transform_at(1))); let (_, lanes, _, _, transform) = p.without_orphan_sources(); assert_eq!( transform, vec![transform_at(1)], "what nothing plays is not written" ); assert_eq!( lanes[0].1[0].transform, Some(0), "and the survivor renumbers" ); assert_eq!(lanes[0].1[1].transform, None); // One placement on three clips is one entry: settings that are equal // share. assert!(p.set_transform(Lane::V1, 1, Some(transform_at(1)))); assert!(p.set_transform(Lane::A1, 2, Some(transform_at(1)))); let (_, lanes, _, _, transform) = p.without_orphan_sources(); assert_eq!(transform.len(), 1, "equal placements share their entry"); assert_eq!(lanes[1].1[2].transform, Some(0)); reloads(&p, "a transform that outlived an undo"); // A grade and a placement on one clip are two independent tables. assert!(p.set_color(Lane::V1, 0, Some(grade_at(3)))); let (_, lanes, _, color, transform) = p.without_orphan_sources(); assert_eq!((color.len(), transform.len()), (1, 1)); assert_eq!( (lanes[0].1[0].color, lanes[0].1[0].transform), (Some(0), Some(0)) ); } /// Not a claim about correctness but about cost: a clip is cloned on every /// paste, every undo snapshot and every lane clone, so its size is worth /// bounding. A clip is no longer `Copy` by design -- the keyframed gain /// envelope (`gain_keys`) owns a `Vec` -- so a clone is a heap allocation /// too, and the bound that matters is the inline size: 224 bytes today (the /// deinterlace choice, one byte, took it from 216 as there was no padding left) /// (the visualizer `u64` and six `Vec`s, the volume, placement, speed /// ramp, opacity and colour keys and the effects stack, are the wide fields; the placement keys raised it from 96 by one /// `Vec`, the speed ramp raised it from 120 by another, the opacity and /// colour keys took it from 144 to 192, and the effects stack to 216, the multicam angle to 224). The /// bound is `<=` rather than `==` so a field that shrinks the clip does not /// trip it; growing past it is the thing to notice. #[test] fn a_clip_stays_within_224_bytes() { assert!( std::mem::size_of::<Clip>() <= 224, "Clip grew to {} bytes -- every undo snapshot and clone pays for it", std::mem::size_of::<Clip>() ); } #[test] fn from_parts_has_no_history_and_checks_the_invariants() { let (sources, lanes, eq, color, _transform) = three().without_orphan_sources(); let (video, audio) = (lanes[0].1.clone(), lanes[1].1.clone()); let mut p = Project::from_parts( sources.clone(), lanes.clone(), eq.clone(), color.clone(), Vec::new(), ) .expect("valid parts"); assert_eq!(p.clips(), three().clips()); assert!(!p.undo(), "a loaded project has nothing to undo"); assert!(p.split(4), "...and is editable from there"); assert!(p.undo()); // A lane may be empty, and so may every lane -- an emptied timeline is a // project. No lane at all is not one: there would be nothing to place on. assert!( Project::from_parts( sources.clone(), two(video.clone(), Vec::new()), Vec::new(), Vec::new(), Vec::new(), ) .is_ok() ); assert!( Project::from_parts( sources.clone(), two(Vec::new(), Vec::new()), Vec::new(), Vec::new(), Vec::new(), ) .is_ok() ); assert!( Project::from_parts( sources.clone(), Vec::new(), Vec::new(), Vec::new(), Vec::new() ) .is_err() ); let bad: [Vec<Clip>; 5] = [ vec![clip(0, 0, 3, 1)], // source that is not there vec![clip(0, 3, 3, 0)], // empty clip vec![clip(3, 0, 3, 0), clip(0, 3, 5, 0)], // out of order vec![clip(0, 0, 5, 0), clip(3, 3, 5, 0)], // overlapping vec![clip(u32::MAX - 1, 0, 3, 0)], // end past the last frame ]; for clips in bad { assert!( Project::from_parts( sources.clone(), two(clips.clone(), Vec::new()), Vec::new(), Vec::new(), Vec::new(), ) .is_err(), "{clips:?}" ); assert!( Project::from_parts( sources.clone(), two(video.clone(), clips.clone()), Vec::new(), Vec::new(), Vec::new(), ) .is_err() ); // ...and on a third lane, which is checked by the same walk. assert!( Project::from_parts( sources.clone(), vec![ (LaneKind::Video, video.clone()), (LaneKind::Audio, audio.clone()), (LaneKind::Video, clips), ], Vec::new(), Vec::new(), Vec::new(), ) .is_err() ); } // An equalizer index no table entry answers, and a table entry the file // format could not have written, are refused at this door too. let eqd = |i: u16| { vec![Clip { fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), visualizer: 0, viz_paint: 0, gain_db: 0.0, eq: Some(i), ..video[0].clone() }] }; assert!( Project::from_parts( sources.clone(), two(eqd(0), Vec::new()), Vec::new(), Vec::new(), Vec::new(), ) .is_err() ); let mut nan = band_at(1); nan.bands[0].freq_hz = f32::NAN; assert!( Project::from_parts( sources.clone(), two(eqd(0), Vec::new()), vec![nan], Vec::new(), Vec::new(), ) .is_err() ); let loaded = Project::from_parts( sources.clone(), two(eqd(0), Vec::new()), vec![band_at(1)], Vec::new(), Vec::new(), ) .expect("an eq the table holds"); assert_eq!(loaded.eq_of(Lane::V1, 0), Some(&band_at(1))); // The same two refusals for a colour, at the same door. let graded = |i: u16| { vec![Clip { fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), visualizer: 0, viz_paint: 0, gain_db: 0.0, color: Some(i), fit: FitPolicy::default(), speed: Speed::NORMAL, ..video[0].clone() }] }; assert!( Project::from_parts( sources.clone(), two(graded(0), Vec::new()), Vec::new(), Vec::new(), Vec::new(), ) .is_err(), "a colour index no entry answers" ); assert!( Project::from_parts( sources.clone(), two(graded(0), Vec::new()), Vec::new(), vec![ColorParams { tint: f32::NAN, ..grade_at(1) }], Vec::new(), ) .is_err(), "a value the file format could not have written" ); let loaded = Project::from_parts( sources.clone(), two(graded(0), Vec::new()), Vec::new(), vec![grade_at(1)], Vec::new(), ) .expect("a colour the table holds"); assert_eq!(loaded.color_of(Lane::V1, 0), Some(&grade_at(1))); // Group ids survive a load, and the next split gets a fresh one. let mut p = Project::from_parts(sources, lanes, eq, color, Vec::new()).expect("valid parts"); assert!(p.split(4)); assert!(p.clips().iter().all(|c| c.link.is_some())); } /// The grouping rules, at the untrusted door and after every edit that can /// touch a link. A link id is at most one placement per lane -- a clip or a /// caption, whichever carries it -- and no sequence of edits may produce /// otherwise, because what an edit produces is what a save writes and a load /// reads. /// /// Amended for the offset-preserving group: the members keep their own /// spans, so two placements of one id covering different frames is *legal* /// -- it is what a hand-built group is -- and the only refusal left is the /// doubled id inside one lane. #[test] fn a_link_id_is_never_two_clips_of_one_lane() { let sources = vec![Source::new(FILE, 0)]; let linked = |start, in_frame, out_frame, link| Clip { speed_keys: Vec::new(), reversed: false, stabilise: None, deinterlace: Default::default(), mark: Default::default(), angle: Default::default(), fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), visualizer: 0, viz_paint: 0, gain_db: 0.0, gain_keys: Vec::new(), pan: 0, pitch: 0, transform_keys: Vec::new(), opacity_keys: Vec::new(), color_keys: Vec::new(), effects: Vec::new(), start, in_frame, out_frame, source: 0, link: Some(link), eq: None, color: None, transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; // The door: a doubled id inside one lane, refused by name. let err = |video: Vec<Clip>, audio: Vec<Clip>| { Project::from_parts( sources.clone(), two(video, audio), Vec::new(), Vec::new(), Vec::new(), ) .expect_err("refused") .to_string() }; assert!( err(vec![linked(0, 0, 3, 7), linked(3, 3, 5, 7)], Vec::new()) .contains("link 7 names two clips in the V1 lane"), "a duplicate id inside one lane is refused by name" ); // ...and the same doubling on a subtitle lane, in its own words: a // caption is the other placement an id may name. let caption = |start: u32, frames: u32, link| SubClip { start, frames, track: 0, in_us: 0, out_us: i64::from(frames) * 1_000_000, link: Some(link), }; let subs = LaneData { clips: Vec::new(), subs: vec![caption(0, 3, 7), caption(4, 3, 7)], ..LaneData::new(LaneKind::Subtitle, Vec::new()) }; assert_eq!( links_are_consistent(&[subs]).unwrap_err().to_string(), "link 7 names two captions in the S1 lane" ); // A pair covering *different* frames is legal now: the members of a // hand-built group keep their own spans, and what binds them is the id. let apart = Project::from_parts( sources.clone(), two(vec![linked(0, 0, 5, 2)], vec![linked(0, 0, 3, 2)]), Vec::new(), Vec::new(), Vec::new(), ); assert!(apart.is_ok(), "offsets within one group load: {apart:?}"); // A one-sided link is *not* an error: it is what a lift leaves behind. let mut p = Project::single(FILE, 9); assert!(p.lift(Lane::A1, 0)); let (sources, lanes, eq, color, _transform) = p.clone().without_orphan_sources(); assert!( Project::from_parts(sources, lanes, eq, color, Vec::new()).is_ok(), "a lifted lane's project has to load again" ); // The edits: nothing below may break either rule. let consistent = |p: &Project, what: &str| { assert!( links_are_consistent(&p.lanes).is_ok(), "{what}: {:?}", p.lanes ); }; let mut p = three(); let copied = p.clips()[0].clone(); assert!( copied.link.is_some(), "the clipboard clip carries its group" ); assert!(p.lift(Lane::V1, 0)); assert!(p.place(Lane::V1, 0, copied.clone())); assert!( p.place(Lane::V1, 20, copied.clone()), "and again, further out" ); assert_eq!( p.clips().iter().filter(|c| c.link == copied.link).count(), 0, "a placement belongs to no group, so it cannot duplicate one" ); consistent(&p, "lift + place twice"); assert!( p.place(Lane::A1, 4, copied.clone()), "over a linked clip's middle" ); consistent(&p, "place through a grouped clip"); assert!( p.paste(6, copied.clone()), "a rippling paste through another" ); consistent(&p, "paste"); assert!(p.split(2)); consistent(&p, "split"); assert!(p.ripple_delete(1, 3)); consistent(&p, "ripple_delete"); assert!(p.undo()); consistent(&p, "undo"); } /// What an edit produces is what a save writes, so *every* state the public /// API can reach has to load again. The two the verifier panel found where /// it did not: a split and a regroup handed one group id to two clips that /// were not the same span, because the lanes had been edited apart. #[test] fn every_reachable_state_reloads() { let mut p = Project::single(FILE, 150); assert!(p.place(Lane::A1, 3, clip(0, 0, 50, 0))); assert!(p.split(10), "the lanes are cut apart at 10"); reloads(&p, "place on one lane, then split"); let mut p = Project::single(FILE, 150); assert!(p.split(30)); assert!(p.lift(Lane::V1, 0), "the picture goes, the sound stays"); assert!(p.regroup(30), "only the audio lane can rejoin"); assert!(p.split(60)); reloads(&p, "lift, regroup one lane, split"); // Two audio streams of one file, both placed: two source entries with // one path between them, which is the state a stream picked out of the // library reaches -- and the state a save has to write and read back. let mut p = Project::single(FILE, 150); let second = p.import(FILE, 1); assert_eq!(second, 1, "the same file on another stream is a new source"); assert!(p.append_clip(second, 150)); assert!(p.split(200)); reloads( &p, "a second audio stream of the same file, placed and split", ); let (sources, ..) = p.without_orphan_sources(); assert_eq!( sources.iter().map(|s| s.audio_stream).collect::<Vec<_>>(), [0, 1], "both entries survive the orphan prune: both are played" ); } /// The pairing the whole model rests on: [`Speed::source_at`] (floor) is /// what an export encodes at a timeline frame, [`Speed::timeline_at`] (ceil) /// is the stamp playback puts on a decoded source frame -- and the *last* /// source frame the pump is still holding at a timeline frame has to be /// exactly the one the export encoded there, at every rate. Anything else is /// A/V drift that grows with the clip, and no test of either side alone /// would see it. /// /// (Grafted from the rival build of this feature, whose floor/ceil pairing /// is a better answer than the floor/floor this had: floor/floor put the /// preview a whole held frame off the export at slow rates.) #[test] fn speed_maps_both_ways() { for permille in (250..=4000).step_by(1) { let speed = Speed::from_permille(permille); assert_eq!(speed.permille(), permille, "no clamping in range"); for d in 0..200u32 { let want = speed.source_at(d); // What the pump shows at `d` is the last frame whose stamp has // come due -- and it is `want`, the frame the export encodes // there. Slowed, that is the frame it is still holding; sped up, // it is the newest of the several that arrived. let held = (0..=want + 8).rev().find(|&s| speed.timeline_at(s) <= d); assert_eq!(held, Some(want), "{speed} at timeline frame {d}"); } for n in 1..300u32 { // A footprint is never zero: a clip occupying no timeline frame // is a placement the lane invariants cannot hold. let frames = speed.frames(n); assert!(frames >= 1, "{speed} of {n} frames"); // ...and its last timeline frame never reads past the clip. assert!( speed.source_at(frames - 1) < n, "{speed}: the last timeline frame of {n} reads past the clip" ); // The trim inverse never overshoots the room it was given, and // answers for every room a footprint can be. let fit = speed.fit(frames).expect("a clip fits its own footprint"); assert!(speed.frames(fit) <= frames, "{speed}: fit({frames}) grew"); // A room narrower than one source frame's own footprint holds // nothing, and says so rather than rounding something into it. assert_eq!(speed.fit(speed.room(1) - 1), None, "{speed}: too narrow"); assert_eq!(speed.room(0), 0, "{speed}: no head is no room"); } } // Real time is the identity map, byte for byte. for d in 0..1000 { assert_eq!(Speed::NORMAL.source_at(d), d); assert_eq!(Speed::NORMAL.timeline_at(d), d); assert_eq!(Speed::NORMAL.frames(d.max(1)), d.max(1)); assert_eq!(Speed::NORMAL.fit(d.max(1)), Some(d.max(1))); assert_eq!(Speed::NORMAL.repeats(d, d + 1), 1); } // The one door: no zero, no reverse, nothing past the ends. assert_eq!(Speed::from_permille(0), Speed::MIN); assert_eq!(Speed::from_permille(249), Speed::MIN); assert_eq!(Speed::from_permille(4001), Speed::MAX); assert_eq!(Speed::from_permille(1000), Speed::NORMAL); assert_eq!(Speed::NORMAL.to_string(), "1.00x"); assert_eq!(Speed::from_permille(2500).to_string(), "2.50x"); } /// [`Rate`] is [`Speed`]'s pair again, over the *file's* rate: the frame an /// export encodes at a timeline frame is the one playback is holding there, /// a file is as long in timeline frames as the seconds it lasts, and the two /// compose so a speeded clip at another rate is exactly both. #[test] fn rate_composes_with_speed() { // 23.976 into 30 (the ratio that does not terminate), 30 into 23.976, // 60 into 30, 25 into 30 -- and the timeline's own rate, which must be // the identity map or a single-rate project would move. for (source_fps, timeline_fps) in [ (24000. / 1001., 30.), (30., 24000. / 1001.), (60., 30.), (25., 30.), (30., 30.), ] { let rate = Rate::from_fps(source_fps, timeline_fps).expect("a nameable rate"); for n in 0..2000u32 { let source = rate.source_at(n); // The stamp is the *first* timeline-rate frame that shows it, so // the picture on screen at `n` is the picture encoded at `n`. let held = (0..=source + 4).rev().find(|&s| rate.timeline_at(s) <= n); assert_eq!(held, Some(source), "{source_fps} on {timeline_fps} at {n}"); } // A file of `count` frames is `timeline_at(count)` frames long here, // and its last one still reads a frame the file has. for count in 1..500u32 { let frames = rate.timeline_at(count); assert!(frames >= 1, "a file is never zero frames long"); assert!( rate.source_at(frames - 1) < count, "{source_fps} on {timeline_fps}: {count} frames reads past the file" ); } // No drift: the mapping is one exact division of the *rate itself*, // not an accumulation and not a rounded rate, so an hour in is as // true as the first second. (Held to the real ratio of the two rates // the caller asked for -- `as_f64` would be true of a wrong rate.) for n in [1u32, 100, 10_000, 500_000, 3_000_000] { let want = f64::from(n) * source_fps / timeline_fps; assert!( (f64::from(rate.source_at(n)) - want).abs() < 1., "{source_fps} on {timeline_fps}: frame {n} drifted" ); } } // The timeline's own rate is the identity, whichever way it is written. assert!(Rate::from_fps(30., 30.).expect("30 over 30").is_real_time()); assert!( Rate::from_fps(30.0004, 30.) .expect("30.0004 over 30") .is_real_time(), "a rate is named by the muxer's timescales, and 30.0004 is 30030/1001" ); // ...and a rate no timescale can name is an `Err`, not a silent 1:1: the // one thing `matches_timeline` refuses a file for now that the rate gate // is gone. assert!(Rate::from_fps(0., 30.).is_err(), "not a rate at all"); assert!(Rate::from_fps(30., f64::NAN).is_err()); for n in 0..1000 { assert_eq!(Rate::REAL_TIME.source_at(n), n); assert_eq!(Rate::REAL_TIME.timeline_at(n), n); } // ...and the composition, in the order the decoder's door applies it: // the speed picks the clip's (timeline-rate) frame, the rate picks the // file's. A 2x clip of a 23.976 fps file on a 30 fps timeline reads two // timeline frames per frame shown, each of them exactly 800/1001 of a // file frame. let rate = Rate::from_fps(24000. / 1001., 30.).expect("23.976 over 30"); let speed = Speed::from_permille(2000); for d in 0..100u32 { assert_eq!( rate.source_at(speed.source_at(d)), (u64::from(d) * 2 * 800 / 1001) as u32, "2x of a 23.976 fps file at timeline frame {d}" ); } } /// The mapping a speed changes, and the one it does not: a 2x clip reads two /// source frames per timeline frame and still starts at its own in-point, /// and a half-speed one shows every source frame twice. #[test] fn a_rate_maps_timeline_frames_onto_source_frames() { let mut p = Project::single(FILE, 40); p.set_speed(Lane::V1, 0, Speed::from_permille(2000)) .expect("room enough"); assert_eq!(p.timeline_frames(), 20, "40 source frames at 2x"); assert_eq!( p.map(Lane::V1, 0), Some((0, 0)), "and it starts where it did" ); assert_eq!(p.map(Lane::V1, 1), Some((0, 2))); assert_eq!(p.map(Lane::V1, 19), Some((0, 38))); assert_eq!(p.map(Lane::V1, 20), None, "and ends where the clip does"); // The span a decoder is handed: as many source frames as it will read, // which is the last shown frame's own and not one more -- timeline frame // 19 shows source frame 38, so 39 frames are read and the 40th is a // picture nothing would display. let span = p.span_at(Lane::V1, 0).expect("a span"); assert_eq!((span.len, span.source_len()), (20, 39)); assert_eq!(span.timeline_at(38), 19, "and it is stamped where it shows"); // ...and the same clip the other way round. let mut p = Project::single(FILE, 40); p.set_speed(Lane::V1, 0, Speed::from_permille(500)) .expect("room enough"); assert_eq!(p.timeline_frames(), 80); assert_eq!(p.map(Lane::V1, 0), Some((0, 0))); assert_eq!(p.map(Lane::V1, 1), Some((0, 0)), "each frame shows twice"); assert_eq!(p.map(Lane::V1, 2), Some((0, 1))); assert_eq!( p.map(Lane::V1, 79), Some((0, 39)), "and the last timeline frame is still inside the source range" ); } /// A trim of a speeded clip is still a trim of its *source*, and the edge /// still stops at the wall: the room a rate leaves is measured in timeline /// frames and the range it commits in source ones. #[test] fn trimming_a_speeded_clip_stays_inside_its_room() { let mut p = Project::single(FILE, 40); p.set_speed(Lane::V1, 0, Speed::from_permille(2000)) .expect("room enough"); // A second clip butted against the first, so the wall is a real one. assert!(p.place(Lane::V1, 20, clip(0, 0, 10, 0))); let room = p.trim_room(Lane::V1, 0, Edge::End, &[40]).expect("a clip"); assert_eq!(room, (1, 20), "out to the neighbour and no further"); assert!( !p.trim(Lane::V1, 0, Edge::End, 30, &[40]), "clamped to the wall, which is where the edge already is" ); let c = p.clips()[0].clone(); assert_eq!(c.end(), 20, "so nothing moved"); assert_eq!(c.out_frame, 40, "and it is still the whole source range"); // ...and in: half the timeline frames is half the source range. assert!(p.trim(Lane::V1, 0, Edge::End, 10, &[40])); let c = p.clips()[0].clone(); assert_eq!((c.in_frame, c.out_frame), (0, 20), "in source frames"); assert_eq!(c.end(), 10, "and ten timeline frames of them"); assert!(sorted_disjoint(p.clips())); } /// The frames a rate cannot address: at half speed each source frame is on /// screen twice, and the cut between the two showings is not a cut in the /// file. Refused, so the two halves always add up to the clip that was cut. #[test] fn a_slow_clip_cuts_only_where_its_source_has_a_frame() { let mut p = Project::single(FILE, 10); p.set_speed(Lane::V1, 0, Speed::from_permille(500)) .expect("room enough"); assert_eq!(p.timeline_frames(), 20); assert!( !p.split(3), "an odd frame is between two showings of one frame" ); assert_eq!(p.clips().len(), 1, "and nothing was cut"); assert!(p.split(4), "an even one is a frame boundary"); let halves = p.clips().to_vec(); assert_eq!( (halves[0].end(), halves[1].start, halves[1].end()), (4, 4, 20), "the halves meet, and still end where the clip did" ); assert_eq!(halves[0].out_frame, halves[1].in_frame, "in source frames"); assert!(p.regroup(4), "and the inverse puts it back"); assert_eq!(p.clips().len(), 1); } /// The same claim, swept: random op sequences off the public surface, the /// project reloaded after every one of them. A failure prints its seed, and /// the seed replays the whole sequence. #[test] fn random_edit_sequences_reload() { // Two thousand, not two hundred: the overlap a hole punched into a slow // clip at a frame its rate cannot address leaves behind sat in ~0.7% of // sequences, and a 200-seed sweep walked straight past it (it took a // judge's 2000-seed run to surface `place` at all). A sweep this cheap // has no business being the narrow one. for seed in 0..2000u64 { let mut rng = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); let mut next = move |n: u32| { // xorshift64*: no dependency, and the seed replays it exactly. rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; (rng >> 33) as u32 % n }; let mut p = Project::single(FILE, 40); for step in 0..24 { let frame = next(44); // A clipboard copy carries the group id of the take it came from // -- the input that made a placement duplicate an id -- and, one // time in three, a rate of its own: a *speeded clip placed over // a speeded clip* is the shape whose remainders have to be run // through `Speed::fit`, and it is not a shape the lanes hand out // on their own often enough to be sure of. let mut copied = p .lane(if next(2) == 0 { Lane::V1 } else { Lane::A1 }) .get(next(4) as usize) .cloned() .unwrap_or_else(|| clip(0, 0, 5, 0)); if next(3) == 0 { copied.speed = Speed::from_permille(250 + next(16) as u16 * 250); } let lane = if next(2) == 0 { Lane::V1 } else { Lane::A1 }; let idx = next(4) as usize; let _ = match next(16) { // A clip dragged along its own lane to wherever the pointer // let it go: the op that moves a placement without changing // what it plays, so what it may not do is land on top of the // neighbour it was dragged at. 15 => p.move_clip(lane, idx, lane, frame), // ...and a second `place`, so a punch lands inside a clip // far more often than one op in fourteen would put it there. 14 => p.place(lane, frame, copied.clone()), 0 => p.split(frame), 1 => p.regroup(frame), 2 => p.lift(lane, idx), 3 => p.place(lane, frame, copied.clone()), 4 => p.paste(frame, copied.clone()), 5 => p.ripple_delete(frame, next(9)), 6 => p.delete(idx), 7 => p.undo(), // An equalizer put on and taken off in the mix: what a clip // carries has to reload with it, and the entries an undo // orphans must not come back out of the prune. 8 => p.set_eq(lane, idx, Some(band_at(next(4)))), 9 => p.set_eq(lane, idx, None), // ...and a colour grade in the same mix, for the same // reason: it rides on the clip and its orphans must go. 10 => p.set_color(lane, idx, Some(grade_at(next(4)))), 11 => p.set_color(lane, idx, None), // ...and a rate in with them, which is the one of the three // that moves *placements*: a clip that grew has to be // refused rather than overlap, and one that shrank leaves a // lane that still loads. Both are what `reloads` asks. 12 => p .set_speed(lane, idx, Speed::from_permille(250 + next(16) as u16 * 250)) .is_ok(), _ => p.append_clip(0, 1 + next(9)), }; reloads(&p, &format!("seed {seed}, step {step}")); } } } /// The save/load round trip a project must survive: the parts a save writes, /// handed back to the constructor a load goes through -- every lane of them, /// and the reloaded timeline has to be the same lanes in the same order. fn reloads(p: &Project, what: &str) { let (sources, lanes, eq, color, transform) = p.clone().without_orphan_sources(); match Project::from_parts(sources, lanes, eq, color, transform) { Err(e) => panic!("{what}: saved but would not load: {e}\n{:?}", p.lanes), Ok(back) => { assert_eq!(back.lanes(), p.lanes(), "{what}: the lane list changed"); // Placements, not whole clips: an orphan prune renumbers the // sources a clip names, which is the point of it. let spans = |p: &Project| -> Vec<Vec<(u32, u32)>> { p.lanes().into_iter().map(|l| p.lane_spans(l)).collect() }; assert_eq!(spans(&back), spans(p), "{what}: the placements changed"); // The prune renumbers the eq table too, so what has to survive // is the settings themselves, clip by clip. let eqs = |p: &Project| -> Vec<Vec<Option<EqParams>>> { p.lanes() .into_iter() .map(|l| { (0..p.lane(l).len()) .map(|i| p.eq_of(l, i).cloned()) .collect() }) .collect() }; assert_eq!(eqs(&back), eqs(p), "{what}: the equalizers changed"); // The colour table renumbers in the same prune, so the same // claim: the params a clip names, not the index it names them by. let colors = |p: &Project| -> Vec<Vec<Option<ColorParams>>> { p.lanes() .into_iter() .map(|l| { (0..p.lane(l).len()) .map(|i| p.color_of(l, i).copied()) .collect() }) .collect() }; assert_eq!(colors(&back), colors(p), "{what}: the colours changed"); // ...and the transform table, by the same claim again. let transforms = |p: &Project| -> Vec<Vec<Option<TransformParams>>> { p.lanes() .into_iter() .map(|l| { (0..p.lane(l).len()) .map(|i| p.transform_of(l, i).copied()) .collect() }) .collect() }; assert_eq!( transforms(&back), transforms(p), "{what}: the transforms changed" ); } } } /// The two invariants [`Project::from_parts`] checks that are about *lanes* /// -- each one sorted and disjoint, and one span per group id -- asserted /// directly, where the point is the invariant rather than the round trip. fn invariants_hold(p: &Project, what: &str) { for lane in p.lanes() { assert!( sorted_disjoint(p.lane(lane)), "{what}: {} is out of order or overlaps itself: {:?}", lane.label(), p.lane(lane) ); } if let Err(e) = links_are_consistent(&p.lanes) { panic!("{what}: {e}\n{:?}", p.lanes); } } /// A lane is a lane: `V2` is added, counted, reached by `(kind, ord)`, and /// edited without any other lane hearing about it. A handle naming a lane /// that is not there reads as empty and refuses to mutate. #[test] fn a_third_lane_is_a_peer() { let mut p = three(); assert_eq!(p.lane_count(LaneKind::Video), 1); assert_eq!(p.lane_count(LaneKind::Audio), 1); let v2 = p.add_lane(LaneKind::Video); assert_eq!(v2, Lane::new(LaneKind::Video, 1)); assert_eq!(v2.label(), "V2"); assert_eq!(p.lane_count(LaneKind::Video), 2); assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2]); assert!(p.lane(v2).is_empty(), "a new lane holds nothing"); // A handle for a lane that is not there: empty, and no mutation takes. let v3 = Lane::new(LaneKind::Video, 2); assert!(p.lane(v3).is_empty()); assert!(!p.place(v3, 0, clip(0, 0, 3, 0)), "no lane, no placement"); assert!(!p.lift(v3, 0)); assert_eq!(p.lane_count(LaneKind::Video), 2, "and none was made"); // Placing on V2 moves nothing else, and the lane can outrun the rest. let before = shape(&p); assert!(p.place(v2, 4, clip(0, 20, 32, 0))); assert_eq!(shape(&p)[0], before[0], "V1 never moved"); assert_eq!(shape(&p)[1], before[1], "A1 never moved"); assert_eq!(p.lane_spans(v2), vec![(4, 12)]); assert_eq!( p.timeline_frames(), 16, "the last lane to end sets the length" ); assert_eq!(p.map(v2, 5), Some((0, 21))); assert_eq!(p.map(v2, 3), None, "V2's own leading gap"); assert_eq!( p.span_at(v2, 0), Some(Span { reverse: false, ramp: None, start: 0, len: 4, from: None, speed: Speed::NORMAL, pitch: 0, }) ); // Every lane still covers the whole timeline, gaps included. for l in p.lanes() { let spans = p.spans_from(l, 0); assert_eq!( spans.iter().map(|s| s.len).sum::<u32>(), 16, "{}", l.label() ); } // ...and playback still reads V1 and A1, whatever else is there. assert_eq!(p.clips(), p.lane(Lane::V1)); assert_eq!(p.map_timeline(4), p.map(Lane::V1, 4)); let segments = p.segments_from(0, FPS); assert_eq!(segments[..3], three().segments_from(0, FPS)[..]); assert_eq!( segments[3], (None, 0.0, 7.0 / FPS), "A1 keeps the clock counting over the picture V2 added" ); invariants_hold(&p, "place on V2"); // A lift is that lane's business too, and the lane list undoes. assert!(p.lift(v2, 0)); assert!(p.lane(v2).is_empty()); assert_eq!(p.timeline_frames(), 9, "V2 no longer runs the longest"); assert!(p.undo()); assert_eq!(p.lane_spans(v2), vec![(4, 12)]); assert!(p.undo()); assert!(p.lane(v2).is_empty(), "the placement came off V2"); assert!(p.undo(), "and one more step takes the lane itself back"); assert_eq!(p.lane_count(LaneKind::Video), 1); assert_eq!(shape(&p), shape(&three())); } /// The add taken back: an empty lane comes off, a lane holding clips never /// does (and says which clips), the last lane of a kind never does, the /// lanes below the one that went move up an `ord`, and one undo puts it /// back where it stood. #[test] fn an_empty_lane_comes_off_again() { let mut p = three(); let v2 = p.add_lane(LaneKind::Video); let v3 = p.add_lane(LaneKind::Video); assert!(p.place(v3, 0, clip(0, 0, 3, 0))); let why = |r: crate::Result<()>| r.expect_err("refused").to_string(); let history = p.history.len(); // The last lane of its kind stays: `V1`/`A1` are where an import and a // paste land, and a project missing one would swallow half of a file // dropped on it. assert!(why(p.remove_lane(Lane::A1)).contains("only audio track")); assert_eq!( why(p.remove_lane(Lane::new(LaneKind::Video, 9))), "there is no V10 to remove" ); // A lane with clips on it refuses and names them: a track removal never // deletes a take. let refusal = why(p.remove_lane(v3)); assert!(refusal.contains("V3 still holds"), "{refusal}"); assert!(refusal.contains(FILE), "{refusal}"); assert!(refusal.contains("at frame 0"), "{refusal}"); assert_eq!(p.lane_spans(v3), vec![(0, 3)], "the clip is still there"); assert_eq!(p.history.len(), history, "a refusal snapshots nothing"); // The empty one in the middle goes, and `V3` becomes `V2` -- clip and // all. p.remove_lane(v2).expect("V2 is empty"); assert_eq!(p.lane_count(LaneKind::Video), 2); assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1, v2]); assert_eq!(p.lane_spans(v2), vec![(0, 3)], "the lane below moved up"); assert_eq!(p.history.len(), history + 1, "one snapshot per removal"); invariants_hold(&p, "remove V2"); // ...and one stroke puts the empty lane back above it. assert!(p.undo()); assert_eq!(p.lane_count(LaneKind::Video), 3); assert!(p.lane(v2).is_empty(), "back where it stood, still empty"); assert_eq!(p.lane_spans(v3), vec![(0, 3)]); } /// A duck is the audio lane's own setting: it names another audio lane as /// its key, refuses itself and video lanes as a key or a target, is one undo /// step, and reaches the mix only for a key that is in the mix. #[test] fn a_duck_keys_off_another_audio_lane_and_is_one_undo_step() { use crate::duck::Duck; let mut p = Project::single(FILE, 9); let a2 = p.add_lane(LaneKind::Audio); let duck = Duck::new(0, -12.0); let history = p.history.len(); assert!( !p.set_lane_duck(Lane::A1, Some(duck)), "a lane is not its own key" ); assert!( !p.set_lane_duck(Lane::V1, Some(duck)), "a video lane does not duck" ); assert!( !p.set_lane_duck(a2, Some(Duck::new(5, -12.0))), "no such key" ); assert!(!p.set_lane_duck( a2, Some(Duck { depth_db: f32::NAN, ..duck }) )); assert_eq!(p.history.len(), history, "a refusal writes no history"); assert!(p.set_lane_duck(a2, Some(duck))); assert_eq!(p.lane_duck(a2), Some(duck)); assert_eq!(p.history.len(), history + 1, "one undo step"); // A2 is empty, so it is not in the mix and cannot be a key in it. assert_eq!(p.audio_ducks(), vec![Vec::<Duck>::new()], "one mixed lane, unducked"); assert_eq!(p.lane_ducks()[2], vec![duck], "saved by its own lane"); assert!(p.undo()); assert_eq!(p.lane_duck(a2), None, "undone"); } /// A lane is ducked under several keys at once, each set on its own: a duck /// under a key the lane already has replaces that one, a clear takes one key /// away, and the mix reads them all in the order they were set. #[test] fn a_lane_ducks_under_several_keys_and_each_can_be_cleared() { use crate::duck::Duck; let mut p = Project::single(FILE, 9); let a2 = p.add_lane(LaneKind::Audio); let a3 = p.add_lane(LaneKind::Audio); // A1 and A2 are the keys that A3 is ducked under. assert!(p.set_lane_duck(a3, Some(Duck::new(0, -12.0))), "A3 under A1"); assert!(p.set_lane_duck(a3, Some(Duck::new(1, -6.0))), "A3 under A2 too"); assert_eq!(p.lane_duck_list(a3).len(), 2, "both kept"); assert_eq!(p.lane_duck(a3), Some(Duck::new(0, -12.0)), "the first set"); // The same key again replaces its duck and keeps the other. assert!(p.set_lane_duck(a3, Some(Duck::new(1, -20.0)))); let list = p.lane_duck_list(a3); assert_eq!(list.len(), 2); assert_eq!(list[1].depth_db, -20.0, "the A2 key's depth changed in place"); assert_eq!(list[0].depth_db, -12.0, "the A1 key untouched"); // A key that is the lane itself is still refused, whatever else it holds. assert!(!p.set_lane_duck(a3, Some(Duck::new(2, -6.0)))); // Clearing one key leaves the other; clearing a key it lacks is a refusal. assert!(p.clear_lane_duck(a3, 0)); assert_eq!(p.lane_duck_list(a3), vec![Duck { depth_db: -20.0, ..Duck::new(1, -20.0) }]); assert!(!p.clear_lane_duck(a3, 0), "no duck under A1 now"); // None clears them all. assert!(p.set_lane_duck(a3, None)); assert!(p.lane_duck_list(a3).is_empty()); assert!(!p.set_lane_duck(a2, None), "nothing to clear is no change"); } /// Mute is the audio lane's switch and nothing else: it zeroes the lane's /// amplitude in the mix, leaves the lane's volume and clips where they were, /// is one undo step each way, and refuses the lanes it cannot mean anything /// for. #[test] fn a_muted_audio_lane_is_silent_in_the_mix_and_unmuting_restores_it() { let mut p = Project::single(FILE, 9); assert_eq!(p.audio_gains(), vec![1.0], "one lane at unity"); assert!(p.set_lane_gain_db(Lane::A1, -6.0)); let loud = p.audio_gains()[0]; let history = p.history.len(); assert!(p.set_lane_muted(Lane::A1, true)); assert_eq!(p.audio_gains(), vec![0.0], "muted is silence in the sum"); assert_eq!(p.lane_gain_db(Lane::A1), -6.0, "the volume is kept"); assert_eq!(p.lane(Lane::A1).len(), 1, "and so are the clips"); assert_eq!(p.history.len(), history + 1, "one undo step"); assert!( !p.set_lane_muted(Lane::A1, true), "already muted is a no-op" ); assert_eq!(p.history.len(), history + 1, "and no undo step for it"); assert!(p.set_lane_muted(Lane::A1, false)); assert_eq!(p.audio_gains(), vec![loud], "unmuted is what it was"); // One undo per switch: the flag comes back through the lane list. assert!(p.undo()); assert!(p.lane_muted(Lane::A1), "undo restores the muted flag"); assert!(p.undo()); assert!(!p.lane_muted(Lane::A1), "and the one before it"); assert!( !p.set_lane_muted(Lane::V1, true), "a video lane is never mixed" ); assert!( !p.set_lane_hidden(Lane::A1, true), "an audio lane has no picture to hide" ); assert!( !p.set_lane_hidden(Lane::new(LaneKind::Video, 7), true), "a lane that is not there" ); } /// Hiding a video lane takes it out of the composite: the picture is what /// the visible lanes cover, the lane under a hidden one shows through, and /// hiding the last covering lane leaves black. One undo step, and the flag /// is in the lane list, so a save and an undo both carry it. #[test] fn a_hidden_video_lane_leaves_the_composite() { let mut p = Project::single(FILE, 9); let v2 = p.add_lane(LaneKind::Video); assert!(p.place(v2, 0, clip(0, 0, 9, 0))); assert_eq!(p.composite_clip_at(3), Some((v2, 0)), "V2 is on top"); let history = p.history.len(); assert!(p.set_lane_hidden(v2, true)); assert_eq!(p.history.len(), history + 1, "one undo step"); assert!(p.lane_hidden(v2)); assert_eq!( p.composite_clip_at(3), Some((Lane::V1, 0)), "V1 shows through" ); assert_eq!( p.composite_spans_from(0).len(), 1, "one span, from V1 alone" ); assert!(p.set_lane_hidden(Lane::V1, true)); assert_eq!(p.composite_clip_at(3), None, "nothing visible, so black"); assert!(p.undo()); assert!(!p.lane_hidden(Lane::V1), "undo restores the flag"); assert!(p.lane_hidden(v2)); assert!(p.undo()); assert!(!p.lane_hidden(v2)); assert_eq!(p.composite_clip_at(3), Some((v2, 0))); // Lock is state only: it refuses nothing in the engine. assert!(p.set_lane_locked(v2, true)); assert!(p.lane_locked(v2)); assert!( p.place(v2, 0, clip(0, 0, 3, 0)), "a locked lane still takes edits" ); assert!(p.undo()); assert!(p.lane_locked(v2), "undoing the edit leaves the lock"); assert!(p.undo()); assert!(!p.lane_locked(v2), "and the lock is one step of its own"); } /// A hidden subtitle lane has no cues, so the screen draws none and the /// export writes no track for it; showing it again brings them back. #[test] fn a_hidden_subtitle_lane_yields_no_cues_until_shown_again() { 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: "a line".into(), image: None, words: Vec::new(), karaoke: Vec::new(), }], bitmap: false, refused: None, }; let mut p = Project::single(FILE, 300).with_subtitles(vec![track]); let s1 = p.add_lane(LaneKind::Subtitle); p.place_sub( s1, 0, SubClip { start: 0, frames: 300, track: 0, in_us: 0, out_us: 10_000_000, link: None, }, ) .expect("placed"); assert_eq!(p.sub_lane_cues(s1, FPS).len(), 1, "shown, the cue is there"); assert!(p.set_lane_hidden(s1, true)); assert!( p.sub_lane_cues(s1, FPS).is_empty(), "hidden, the lane gives no cues" ); assert!(p.set_lane_hidden(s1, false)); assert_eq!( p.sub_lane_cues(s1, FPS).len(), 1, "shown again, the cue comes back" ); } /// The switches live in the lane list, so a snapshot carries them with the /// rest; and [`Project::with_lane_flags`] puts them back on load with no undo /// step, as [`Project::with_mix`] does for the volumes. #[test] fn lane_switches_come_back_on_load_without_an_undo_step() { let mut p = Project::single(FILE, 9); assert!(p.set_lane_locked(Lane::V1, true)); assert!(p.set_lane_hidden(Lane::V1, true)); let flags = p.lane_flags(); assert_eq!( flags[0], LaneFlags { hidden: true, muted: false, locked: true, pan: 0.0, } ); let back = Project::from_parts( p.sources().to_vec(), p.lanes() .into_iter() .map(|l| (l.kind, p.lane(l).to_vec())) .collect(), Vec::new(), Vec::new(), Vec::new(), ) .expect("the same lanes load") .with_lane_flags(&flags); assert!(back.lane_hidden(Lane::V1) && back.lane_locked(Lane::V1)); assert!(!back.lane_muted(Lane::A1)); let mut back = back; assert!(!back.undo(), "a loaded switch is not an undo step"); } /// A track dragged to another place in the stack: the whole lane travels -- /// clips, gain and kind -- nothing is remapped, an audio track may sit above /// a video one, a lane that crosses one of its own kind swaps names with it /// (and swaps the composite with it), one undo puts the order back, and a /// save carries it. #[test] fn a_track_is_dragged_to_another_place_in_the_stack() { let mut p = three(); assert!(p.set_lane_gain_db(Lane::A1, -6.0)); let before = shape(&p); let history = p.history.len(); // A1 over V1: the screen is rearranged and nothing else is. Both keep // their names -- neither crossed a lane of its own kind -- so every // handle a front-end holds still names the track it named before. assert_eq!(p.move_lane(Lane::A1, 0), Some(Lane::A1)); assert_eq!(p.lanes(), vec![Lane::A1, Lane::V1], "A1 is on top now"); assert_eq!( shape(&p), vec![before[1].clone(), before[0].clone()], "each lane's clips travelled with it, and none was remapped" ); assert_eq!(p.lane_gain_db(Lane::A1), -6.0, "so did its gain"); assert_eq!(p.history.len(), history + 1, "one snapshot per move"); invariants_hold(&p, "A1 over V1"); // Nothing moves for a lane that is not there, a slot that is not, or a // lane already standing in the one it was pointed at. assert_eq!(p.move_lane(Lane::new(LaneKind::Video, 9), 0), None); assert_eq!(p.move_lane(Lane::V1, 2), None, "there is no third slot"); assert_eq!(p.move_lane(Lane::A1, 0), None, "already there"); assert_eq!(p.history.len(), history + 1, "and none of those snapshot"); // ...and one stroke puts the order back. assert!(p.undo()); assert_eq!(p.lanes(), vec![Lane::V1, Lane::A1]); assert_eq!(shape(&p), before); // A second video lane over the first: display order *is* the stack, so // the last video lane covering a frame is the picture that shows. let v2 = p.add_lane(LaneKind::Video); assert!(p.place(v2, 0, clip(0, 0, 5, 0))); let over = shape(&p)[2].clone(); assert_eq!( p.composite_span_at(0).map(|s| s.len), Some(5), "V2's five frames are what frame 0 shows" ); // V2 dragged to the top takes V1's slot -- and V1's name, a label being // a position among the lanes of its kind. The picture that wins changes // with it: the take that was covering is the covered one now. assert_eq!(p.move_lane(v2, 0), Some(Lane::V1), "a label is a position"); assert_eq!(p.lanes(), vec![Lane::V1, v2, Lane::A1]); assert_eq!( shape(&p), vec![over.clone(), before[0].clone(), before[1].clone()], "V2's clips answer to V1 now, the old V1's to V2, and A1 kept its own" ); assert_eq!( p.composite_span_at(0).map(|s| s.len), Some(3), "the lower video lane wins, and it is the old V1 now" ); invariants_hold(&p, "V2 over V1"); // A save writes that order and a load takes it back. let (sources, lanes, eq, color, _transform) = p.without_orphan_sources(); assert_eq!( lanes.iter().map(|(k, _)| *k).collect::<Vec<_>>(), [LaneKind::Video, LaneKind::Video, LaneKind::Audio] ); let back = Project::from_parts(sources, lanes, eq, color, Vec::new()).expect("three lanes load"); assert_eq!(back.lanes(), p.lanes()); assert_eq!(shape(&back), shape(&p), "the same lane is still on top"); assert!(p.undo(), "and the stack goes back to how it stood"); assert_eq!(shape(&p), vec![before[0].clone(), before[1].clone(), over]); } /// The grouping rule across more than two lanes, under the offset model: a /// split *keeps* the group on the left halves and hands the right halves of /// that group one fresh id of their own -- a clip in no group comes out of /// the cut in none. So a take cut apart is two takes, one per side, and a /// lane that was never part of it stays out of both. #[test] fn a_split_keeps_the_left_group_and_shares_a_fresh_right_one() { let mut p = Project::single(FILE, 9); let take = p.clips()[0].link.expect("a fresh project is one take"); let v2 = p.add_lane(LaneKind::Video); assert!( p.place(v2, 0, clip(0, 0, 9, 0)), "a placement joins no group" ); assert!(p.split(4)); let side = |p: &Project, i: usize| -> Vec<Option<u32>> { p.lanes().into_iter().map(|l| p.lane(l)[i].link).collect() }; let (left, right) = (side(&p, 0), side(&p, 1)); // The lanes in order are V1, A1, V2: the take's two keep its id on the // left, and the placed clip never had one for the cut to keep. assert_eq!( left, vec![Some(take), Some(take), None], "the left halves keep the take" ); // ...and the right halves of that take are one fresh group. assert_eq!(right[0], right[1]); assert_ne!(right[0], Some(take)); assert_eq!(right[2], None, "an unlinked lane stays unlinked"); invariants_hold(&p, "split across three lanes"); // V2 ends early: the left halves still keep what each clip carried -- // the take on V1 and A1, nothing on V2 -- and the fresh right id is // shared by exactly the take's lanes. let mut p = Project::single(FILE, 9); let v2 = p.add_lane(LaneKind::Video); assert!(p.place(v2, 0, clip(0, 0, 6, 0))); assert!(p.split(3)); assert_eq!(p.lane(Lane::V1)[0].link, Some(take)); assert_eq!(p.lane(v2)[0].link, None, "[0, 3) of a placement"); assert_eq!(p.lane(Lane::V1)[1].link, p.lane(Lane::A1)[1].link); assert_eq!(p.lane(v2)[1].link, None, "[3, 6) of a placement"); invariants_hold(&p, "split where one lane ends early"); // The inverse rejoins each lane into the group its left half kept. assert!(p.regroup(3)); assert_eq!(p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link); assert_eq!( p.lane(Lane::V1)[0].link, Some(take), "the take's id is back, not a fresh one" ); assert_eq!(p.lane(v2)[0].link, None, "V2 rejoined into no group"); invariants_hold(&p, "regroup across three lanes"); } /// A group is one placement per lane, on however many lanes carry the id -- /// not a video/audio pair and not a pairing by `ord` -- and the members may /// cover whatever frames they cover: the offset between them is what a /// hand-built group keeps. Lanes built by hand, because the rule is checked /// at the untrusted door and no edit can produce the one thing it refuses. #[test] fn a_group_may_span_any_lane() { let lane = |kind, clips: Vec<Clip>| LaneData::new(kind, clips); let one = |start: u32, end: u32, link| Clip { speed_keys: Vec::new(), reversed: false, stabilise: None, deinterlace: Default::default(), mark: Default::default(), angle: Default::default(), fade_in: 0, fade_out: 0, transition_out: 0, transition_kind: Default::default(), visualizer: 0, viz_paint: 0, gain_db: 0.0, gain_keys: Vec::new(), pan: 0, pitch: 0, transform_keys: Vec::new(), opacity_keys: Vec::new(), color_keys: Vec::new(), effects: Vec::new(), start, in_frame: start, out_frame: end, source: 0, link: Some(link), eq: None, color: None, transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, }; // V1, A1, V2, A2 -- one take over [0, 4) on all four. let kinds = [ LaneKind::Video, LaneKind::Audio, LaneKind::Video, LaneKind::Audio, ]; let build = |clips: [Vec<Clip>; 4]| -> Vec<LaneData> { kinds.iter().zip(clips).map(|(&k, c)| lane(k, c)).collect() }; let take = || vec![one(0, 4, 7)]; assert!(links_are_consistent(&build([take(), take(), take(), take()])).is_ok()); // The same take on V1 + A2 only, the lanes between it empty: legal, the // way a lifted half is legal. let across = build([take(), Vec::new(), Vec::new(), take()]); assert!(links_are_consistent(&across).is_ok(), "V1 grouped with A2"); // ...and so is a group whose members disagree about their spans: the // offset between them is the hand's business, not the loader's. let apart = build([take(), Vec::new(), Vec::new(), vec![one(2, 6, 7)]]); assert!( links_are_consistent(&apart).is_ok(), "[0, 4) on V1 and [2, 6) on A2 are one group" ); // One id twice in one lane is still that lane's error, by name. let twice = build([ Vec::new(), Vec::new(), vec![one(0, 4, 7), one(4, 8, 7)], Vec::new(), ]); assert_eq!( links_are_consistent(&twice).unwrap_err().to_string(), "link 7 names two clips in the V2 lane" ); } /// Every lane a save writes comes out, in display order and with its kind -- /// an empty one included, since it is state a front-end shows -- and a /// two-lane project still writes exactly the parts it always did. #[test] fn a_save_writes_every_lane_it_has() { let mut p = three(); let before = p.without_orphan_sources(); assert_eq!(before.1.len(), 2, "V1 and A1"); let v2 = p.add_lane(LaneKind::Video); assert!(p.place(v2, 4, clip(0, 20, 32, 0))); let a2 = p.add_lane(LaneKind::Audio); let (sources, lanes, eq, color, _transform) = p.without_orphan_sources(); assert_eq!( lanes.iter().map(|(k, _)| *k).collect::<Vec<_>>(), [ LaneKind::Video, LaneKind::Audio, LaneKind::Video, LaneKind::Audio ], "display order, not video-then-audio" ); assert_eq!(lanes[2].1, p.lane(v2), "V2's clips are written"); assert!(lanes[3].1.is_empty(), "and the empty A2 is still a lane"); // ...and all four load again as the same four. let back = Project::from_parts(sources, lanes, eq, color, Vec::new()).expect("four lanes load"); assert_eq!(back.lanes(), p.lanes()); assert_eq!(back.lane(a2), p.lane(a2)); // Taking the lanes back leaves exactly what a two-lane save wrote. for _ in 0..3 { assert!(p.undo()); } assert_eq!( p.without_orphan_sources(), before, "byte for byte the same parts a two-lane save always wrote" ); } /// [`random_edit_sequences_reload`]'s sweep on a timeline that starts with /// three lanes and grows more: every lane's own order and the one /// span-per-id rule after every op, *and* the reload the v4 format restored /// as the oracle -- with `add_lane` in the op mix, so what is reloaded is a /// project of any number of lanes. #[test] fn random_edit_sequences_keep_many_lanes_consistent() { for seed in 0..2000u64 { let mut rng = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); let mut next = move |n: u32| { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; (rng >> 33) as u32 % n }; let mut p = Project::single(FILE, 40); let v2 = p.add_lane(LaneKind::Video); assert!(p.place(v2, 5, clip(0, 0, 20, 0))); let lanes = [Lane::V1, Lane::A1, v2]; for step in 0..24 { let frame = next(44); let copied = p .lane(lanes[next(3) as usize]) .get(next(4) as usize) .cloned() .unwrap_or_else(|| clip(0, 0, 5, 0)); let lane = lanes[next(3) as usize]; let idx = next(4) as usize; let _ = match next(14) { // The drag, in the mix: a clip dragged along its own lane // and onto another one, at a frame the pointer picked -- // which is where the group rule and the sorted+disjoint one // meet, since every half travels the same distance. 13 => p.move_clip(lane, idx, lanes[next(3) as usize], frame), 10 => p.set_eq(lane, idx, Some(band_at(next(4)))), 11 => p.set_eq(lane, idx, None), // A rate, which on many lanes is the group rule as well: // every clip carrying the link moves together or none of // them does, and `invariants_hold` is what says so. 12 => p .set_speed(lane, idx, Speed::from_permille(250 + next(16) as u16 * 250)) .is_ok(), 0 => p.split(frame), 1 => p.regroup(frame), 2 => p.lift(lane, idx), 3 => p.place(lane, frame, copied.clone()), 4 => p.paste(frame, copied.clone()), 5 => p.ripple_delete(frame, next(9)), 6 => p.delete(idx), 7 => p.undo(), 8 => { p.add_lane(if next(2) == 0 { LaneKind::Video } else { LaneKind::Audio }); true } _ => p.append_clip(0, 1 + next(9)), }; invariants_hold(&p, &format!("seed {seed}, step {step}")); reloads(&p, &format!("seed {seed}, step {step}")); } } } /// The compositing rule, in one walk: `V2` covers `V1` while it has a clip /// there, `V1` shows again on either side of it, and a frame no video lane /// covers is a gap however many lanes there are. #[test] fn the_composite_is_the_topmost_lane_with_a_clip() { let sources = vec![Source::new(FILE, 0), Source::new(FILE2, 0)]; let lanes = vec![ // V1: the whole 30 frames of source 0. (LaneKind::Video, vec![clip(0, 0, 30, 0)]), // V2: source 1 over the middle third. (LaneKind::Video, vec![clip(10, 5, 15, 1)]), (LaneKind::Audio, vec![clip(0, 0, 30, 0)]), ]; let p = Project::from_parts(sources, lanes, vec![], vec![], Vec::new()).expect("valid parts"); assert_eq!( p.composite_spans_from(0), vec![ Span { reverse: false, ramp: None, start: 0, len: 10, from: Some((0, 0)), speed: Speed::NORMAL, pitch: 0, }, Span { reverse: false, ramp: None, start: 10, len: 10, from: Some((1, 5)), speed: Speed::NORMAL, pitch: 0, }, Span { reverse: false, ramp: None, start: 20, len: 10, from: Some((0, 20)), speed: Speed::NORMAL, pitch: 0, }, ], "V2 takes over for its own span and hands V1 back" ); // A mid-span ask resolves the same way, which is what a seek does. assert_eq!( p.composite_span_at(15), Some(Span { reverse: false, ramp: None, start: 15, len: 5, from: Some((1, 10)), speed: Speed::NORMAL, pitch: 0, }) ); assert_eq!(p.composite_span_at(30), None, "past the end"); // A hole in V1 under V2's clip: black between the two, and black after // both run out (the audio lane is still holding the timeline open). let sources = vec![Source::new(FILE, 0), Source::new(FILE2, 0)]; let holed = vec![ (LaneKind::Video, vec![clip(0, 0, 5, 0)]), (LaneKind::Video, vec![clip(10, 0, 10, 1)]), (LaneKind::Audio, vec![clip(0, 0, 30, 0)]), ]; let p = Project::from_parts(sources, holed, vec![], vec![], Vec::new()).expect("valid parts"); assert_eq!( p.composite_spans_from(0), vec![ Span { reverse: false, ramp: None, start: 0, len: 5, from: Some((0, 0)), speed: Speed::NORMAL, pitch: 0, }, Span { reverse: false, ramp: None, start: 5, len: 5, from: None, speed: Speed::NORMAL, pitch: 0, }, Span { reverse: false, ramp: None, start: 10, len: 10, from: Some((1, 0)), speed: Speed::NORMAL, pitch: 0, }, Span { reverse: false, ramp: None, start: 20, len: 10, from: None, speed: Speed::NORMAL, pitch: 0, }, ] ); // One video lane: the composite *is* that lane, span for span -- the // promise every existing two-lane project rests on. let p = Project::single(FILE, 30); assert_eq!(p.composite_spans_from(0), p.spans_from(Lane::V1, 0)); assert_eq!(p.composite_span_at(7), p.span_at(Lane::V1, 7)); } /// Every audio lane holding something gets a play list, and only those. #[test] fn audio_segments_cover_every_lane_that_holds_something() { let sources = vec![Source::new(FILE, 0), Source::new(FILE2, 0)]; let lanes = vec![ (LaneKind::Video, vec![clip(0, 0, 30, 0)]), (LaneKind::Audio, vec![clip(0, 0, 30, 0)]), (LaneKind::Audio, vec![clip(15, 0, 15, 1)]), (LaneKind::Audio, Vec::new()), ]; let p = Project::from_parts(sources, lanes, vec![], vec![], Vec::new()).expect("valid parts"); let lists = p.audio_segments_from(0, FPS); assert_eq!(lists.len(), 2, "the empty lane is not mixed in"); assert_eq!(lists[0], p.segments_from(0, FPS), "A1 is unchanged"); // A2 is a gap up to frame 15, then the whole of source 1. assert_eq!(lists[1], vec![(None, 0.0, 0.5), (Some(1), 0.0, 0.5)]); // No audio lane holds anything: one all-gap list, so such a timeline // still plays silence against a master clock. let p = Project::from_parts( vec![Source::new(FILE, 0)], vec![ (LaneKind::Video, vec![clip(0, 0, 30, 0)]), (LaneKind::Audio, Vec::new()), ], vec![], vec![], Vec::new(), ) .expect("valid parts"); assert_eq!(p.audio_segments_from(0, FPS), vec![vec![(None, 0.0, 1.0)]]); } /// D4: removing a subtitle column used to clear the whole undo history, /// because a placement names its track by index into the palette and the /// reindex left every later entry naming the wrong one. The entry holds the /// palette beside the lanes now, so the reindex undoes with it. #[test] fn removing_a_subtitle_column_undoes_with_its_reindex() { let track = |label: &str| SubtitleTrack { path: FILE.into(), track: None, language: "eng".into(), name: String::new(), label: label.into(), cues: Vec::new(), bitmap: false, refused: None, }; let mut p = Project::single(FILE, 300).with_subtitles(vec![ track("one"), track("two"), track("three"), ]); assert_eq!(p.subtitles().len(), 3); let before = p.parts(); let palette = p.subtitles().to_vec(); // Column 0 goes, which renumbers the two behind it. p.remove_subtitles(0).expect("a column with nothing on it"); assert_eq!(p.subtitles().len(), 2); assert_eq!(p.subtitles()[0].label, "two"); assert!(p.undo(), "the removal is a step like any other cut"); // The palette itself, and not only what `parts` happens to compare: this // is the assertion that cannot go vacuous if the restore below it ever // leaves a field of it behind. assert_eq!( p.subtitles(), palette.as_slice(), "the palette itself, back" ); assert_eq!(p.parts(), before, "the palette and the lanes, both back"); assert!(p.redo(), "and forward again"); assert_eq!(p.subtitles().len(), 2, "the column goes again"); assert_eq!(p.subtitles()[0].label, "two"); assert_ne!(p.subtitles(), palette.as_slice(), "and it is not back"); } /// Finds every line that **writes** a caption's clock, in three forms: an /// assignment through any operator (`.out_us = `, `.out_us += `, /// `.out_us <<= `), a struct-literal field (`out_us: ...`) and the /// shorthand form (`out_us,`). A `let` binding is skipped for the literal /// forms only, so `let x: &mut SubClip = ..; (*x).in_us = 5;` is still /// caught while `let out_us = s.out_us;` is not. /// /// The operator list is `=` plus one assignment operator each, and /// deliberately excludes `<=`: the first version of this matcher counted /// `s.out_us <= s.in_us` as a write, which would have flagged every /// never-empty check in the file. fn window_writes(text: &str) -> Vec<(usize, String)> { const FIELDS: [&str; 2] = ["in_us", "out_us"]; const OPS: [&str; 11] = [ "=", "+=", "-=", "*=", "/=", "%=", "^=", "&=", "|=", "<<=", ">>=", ]; let mut found = Vec::new(); for (n, raw) in text.lines().enumerate() { let line = raw.trim(); if line.starts_with("//") { continue; } let is_binding = line.starts_with("let "); // `let (a, b) = ..` is a tuple pattern, not a struct literal: the // commas there bind names. `let half = SubClip { frames: n, in_us, // out_us, ..s };` is the opposite -- the same commas are shorthand // fields -- and what separates them is the paren, not the `let`. let tuple_pattern = line.starts_with("let ("); for field in FIELDS { let access = format!(".{field}"); if let Some(at) = line.find(&access) { let after = &line[at + access.len()..]; let after = after.trim_start(); if OPS .iter() .any(|op| after.starts_with(op) && !after[op.len()..].starts_with('=')) { found.push((n + 1, line.to_owned())); continue; } } // A bare name: a literal field, a shorthand, or a local // binding. Skipped when a `.` or an identifier character // precedes it -- that is a read, not a field being set. // // The `let` exclusion is per-occurrence and keyed on what // FOLLOWS, not on the line. `let out_us = s.out_us;` names a // local, so the `=` form is skipped; but `let tail = SubClip { // in_us: cut, out_us: s.out_us, ..s };` is a struct literal // behind a `let`, and an earlier version of this matcher // skipped the whole line for being a `let` -- so that shape, // which is exactly what a plausible composer looks like, went // through green. Keyed on the follower, a `let` hides nothing. for (at, _) in line.match_indices(field) { let preceded_by_name = line[..at] .chars() .next_back() .is_some_and(|c| c == '.' || c.is_alphanumeric() || c == '_'); if preceded_by_name { continue; } let after = line[at + field.len()..].trim_start(); let sets_a_field = after.starts_with(':') || (after.starts_with(',') && !tuple_pattern); let names_a_local = after.starts_with('=') && !after[1..].starts_with('='); if sets_a_field || (names_a_local && !is_binding) { found.push((n + 1, line.to_owned())); break; } } } } found } /// A **heuristic guard**, not a proof. The honest reading of a green here /// is the residual risk in this comment, not the green itself. /// /// Three rounds of review each found one more composer a per-arm check had /// missed -- a tail trim, a head trim, a rejoin that SUMS two legal /// widths -- and each produced the same failure: the editor wrote a /// `.edith` its own loader then refused, with no in-app way back. The /// invariant is now structural in the code (every composer calls /// [`set_window`]); this is what keeps that structure from quietly losing /// a caller. /// /// **Why this is a scan and not the compiler**, which is the better answer /// and was priced rather than skipped: making `in_us`/`out_us` private to /// this module turns every outside write into a compile error, which no /// one-token edit can defeat. It costs 74 `SubClip` construction sites /// (31 of them in this crate's own integration tests) plus a public /// constructor, and it reaches `crates/app`'s `timeline_edit.rs` and /// `timeline_math.rs`, which other lanes own. That is a refactor of a /// door's public shape rather than a bound, and the wrong size for this /// lane -- so the guard stands here with its limits written down instead. /// /// **Residual risk, by class, most likely first.** These are not all /// equally hypothetical; the first is the one a real composer would reach /// for, and it is the one this matcher cannot see at all: /// /// 1. **Field-less writes.** `*a = *b;`, `*a = other;`, /// `std::mem::swap(a, b)`, `a.clone_from(b)`, and any macro that /// expands to a field write (`bump!(s.out_us, by)`). None of these ever /// spell `in_us` or `out_us` on the line, so `window_writes` returns /// nothing for all of them. A *helper* that writes the field directly IS /// caught; it is specifically the whole-struct and macro forms that go /// through. This is the class most likely to be used, and it is /// unmatched by construction rather than by oversight. /// 2. **A rename.** The matcher keys on the literal strings `in_us` and /// `out_us`. Renaming the fields silences it entirely while it stays /// green -- the sharpest failure here, because the guard is supposed to /// be the thing that notices a rule drifting. /// 3. **A writer inside a test.** `/tests/` paths and `mod tests` blocks /// are cut. A test cannot produce a file a user loses, so the line is /// drawn there deliberately, not by omission. /// 4. **A file the walk does not reach**: a path outside `crates/`, or one /// under a directory named `target`, `.git`, `vendor`, or any /// dot-directory (`.delta/` holds scratch clones of other checkouts). /// /// What the matcher DOES now cover, after four rounds of it not doing so: /// every assignment operator; struct-literal fields and the shorthand form, /// including behind a `let`; writes through an indirect `&mut`; and every /// `.rs` under `crates/` with no filter on what the file contains, so a /// glob import or a type alias can no longer hide a module. #[test] fn every_window_write_outside_set_window_is_named() { // `CARGO_MANIFEST_DIR`, not `file!()`: a test runs with the WORKSPACE // root as its working directory, so `file!()`'s crate-relative path // does not resolve from where the binary actually starts. let workspace = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) .parent() .and_then(std::path::Path::parent) .expect("crates/engine sits two levels under the workspace root") .to_path_buf(); // Every line in the workspace that names a clock and is not a // composer, each with its reason. A new entry is a claim someone has // to defend in review, which is the point of writing the list out. let allowed = [ ("pub in_us: i64,", "the field declaration"), ("pub out_us: i64,", "the field declaration"), ( "fn set_window(s: &mut SubClip, in_us: i64, out_us: i64) {", "set_window's own signature", ), ("s.in_us = in_us;", "set_window"), ("s.out_us = out_us;", "set_window"), ( "set_window(&mut data.subs[idx], in_us, out_us);", "regroup naming the two values it is about to set", ), ("in_us: micros(f[3], n)?,", "edith.rs: the parse door"), ("out_us: micros(f[4], n)?,", "edith.rs: the parse door"), // Non-test helpers that build a value to hand `place_sub`, which is // a door. Not composers: nobody was mid-gesture, which is exactly // why a door refuses where a composer clamps. ( "in_us: 0,", "sweep.rs caption helper, and timeline_edit.rs's palette row, -> place_sub", ), ( "out_us: i64::from(frames) * 1_000_000,", "sweep.rs caption helper -> place_sub", ), // `SubClip::whole_track`: the one constructor a palette drop uses; its // window is the frames' own length, and the value goes to place_sub. ( "SubClip { start: 0, frames, track, in_us: 0, out_us, link: None }", "SubClip::whole_track: whole-track value -> place_sub", ), ]; let mut found: Vec<String> = Vec::new(); let (mut files, mut lines) = (0usize, 0usize); let mut seen_app = false; for (file, text) in read_workspace_rust(&workspace) { // Test code, by path or by module: a test builds `SubClip`s by hand // and its value is not one the loader ever reads, so the 31 sites in // `crates/engine/tests/` and three in `crates/app/src/tests/` are // not composers. A real limit rather than a technicality -- the // guard does not cover a writer hidden inside a test -- but a test // cannot produce a file a user loses, so it is where the line goes. let path = file.to_string_lossy().replace('\\', "/"); if path.contains("/tests/") { continue; } let code = match text.find("\n#[cfg(test)]\nmod tests {") { Some(at) => &text[..at], None => text.as_str(), }; lines += code.lines().count(); files += 1; seen_app |= path.contains("crates/app/"); for (n, line) in window_writes(code) { if !allowed.iter().any(|(text, _)| *text == line) { let at = file.strip_prefix(&workspace).unwrap_or(&file); found.push(format!("{}:{n}: {line}", at.display())); } } } // A floor, not a census: the walk now opens EVERY `.rs` under // `crates/` that is not test code -- 86 files and ~77,600 lines today, // which is 141 files and ~116,000 before `/tests/` and `mod tests` are // cut. The point is only to catch a walk rooted in the wrong place, // which reads a file or two. // The `seen_app` half is the one that matters: the two-file version of // this guard read `project.rs` and `edith.rs` and was silently // certifying a claim about the app it had never opened, and the // `contains("SubClip")` filter that came next // was the same mistake wearing a derived hat -- a file that reached // the type by glob import or alias was never opened either. assert!( lines > 70_000 && files >= 80 && seen_app, "the walk read {lines} lines in {files} files (app reached: \ {seen_app}) -- it is not covering the workspace, and a guard that \ reads nothing is worse than no guard because it reads as one" ); // The assertion this whole test exists for. It was DELETED by a clumsy // edit while the coverage floor above was being retuned, and for a // moment the test reported green while `found` held a real offender -- // the fourth version of this guard failing in the one way that looks // exactly like success. If you retune anything here, check this line // still exists. assert!( found.is_empty(), "a caption window is written outside set_window:\n {}", found.join("\n ") ); } /// The blind spot, pinned as cases. Every one of these is a write, and the /// guard exists because each of its earlier versions caught none of them -- /// `s.out_us += by(...)` being a one-token edit that made it green while a /// grouped caption really did grow past the ceiling. A matcher that has /// quietly stopped matching is the failure mode that looks most like a /// pass, so it gets its own test rather than a comment. /// /// The last group is the opposite: the field-less forms that are a write /// and that this matcher CANNOT see. They are asserted to be missed, on /// purpose, so that the day someone can match them the test says so -- /// and so nobody reads this file as claiming more than it does. #[test] fn the_window_write_matcher_catches_the_ways_a_composer_hides() { for (what, code) in [ ( "the one-token compound assignment", " s.out_us += by(s.end(), to);", ), ("a plain compound assignment", " s.in_us -= 1;"), ("a shift-assign", " s.out_us <<= 1;"), ( "a write through a &mut the matcher cannot see past", " let x: &mut SubClip = q; (*x).in_us = 5;", ), ( "a struct literal", " out.push(SubClip { in_us: 0, out_us: 1, ..s });", ), ( "the shorthand form", " SubClip { in_us, out_us, ..s }", ), ( "a struct literal behind a `let`, which the old line-wide `let` \ exclusion skipped whole", " let tail = SubClip { in_us: cut, out_us: s.out_us, ..s };", ), ( "a struct literal behind a `let` using the shorthand too", " let half = SubClip { frames: n, in_us, out_us, ..s };", ), ] { assert!( !window_writes(code).is_empty(), "the matcher misses {what}: {code:?}" ); } // ...and the other side, so the allowlist is not padded with reads. for quiet in [ " if sub.frames == 0 || sub.out_us <= sub.in_us || sub.in_us < 0 {", " debug_assert!(s.frames >= 1 && s.out_us > s.in_us && s.in_us >= 0);", " let out_us = s.out_us;", " (next.start == frame && next.in_us == subs[idx].out_us)", " let total = s.out_us - s.in_us;", ] { assert!( window_writes(quiet).is_empty(), "the matcher flags a read as a write: {quiet:?}" ); } // The class this matcher CANNOT see, asserted as missed. Each of these // really does write a caption's window and none of them spell the field // name, so a field matcher returns nothing -- that is a property of the // approach, not a bug to be fixed here, and the test exists so the // doc comment's residual-risk list cannot quietly grow a claim this // matcher has not earned. If one of these ever starts matching, this // test fails and asks which side of the line moved. for unmatchable in [ " *a = *b;", " *a = other;", " std::mem::swap(a, b);", " a.clone_from(b);", " bump!(s.out_us, by);", ] { assert!( window_writes(unmatchable).is_empty(), "the matcher now matches a field-less form it documented as \ unmatched -- update the residual-risk list too: {unmatchable:?}" ); } } /// Every `.rs` under the workspace, paired with its text. Recursive /// because the set of files that can name `SubClip` is a fact about the /// tree, and a hardcoded pair of names goes stale the moment a lane adds a /// module -- which is how the two-file version of this guard came to /// certify a claim about `crates/app` it had never read. fn read_workspace_rust(root: &std::path::Path) -> Vec<(std::path::PathBuf, String)> { let mut out = Vec::new(); let mut stack = vec![root.to_path_buf()]; while let Some(dir) = stack.pop() { let Ok(entries) = std::fs::read_dir(&dir) else { continue; }; for entry in entries.flatten() { let path = entry.path(); if path.is_dir() { // `target` is not source and `vendor` is not ours to rule on. // // Every DOT-directory, for the same reason and one more: a // dot-dir is by convention not part of the tree we are // making claims about. This workspace root holds `.delta/`, // a scratch area of clones and worktrees left by delta/A-B // tooling, and the walk used to descend into it and report // the ten window writes inside a checkout that is not this // one -- so the guard was RED here for a file nobody edits, // and, worse, VACUOUS about its own tree: the same stale // clone that produced the noise could have hidden a real // offender behind it, and the floor below was being met in // part by lines this guard has no business reading. Skipping // the whole class rather than naming `.delta` is the point: // the next scratch dir a tool drops here is skipped too. let name = path.file_name().unwrap_or_default().to_string_lossy(); let skip = name == "target" || name == ".git" || name == "vendor" || name.starts_with('.'); if !skip { stack.push(path); } // EVERY `.rs`, with no filter on what it contains. The filter // that was here (`text.contains("SubClip")`) was a name-based // guess about which files can reach the type, and a glob import // or a type alias defeats it exactly as it defeats a // hardcoded file list: a new module holding the struct behind // `use engine::project::*;` was never opened, and stayed green. // The scan is text-only and the tree is small, so the honest // thing costs nothing here. } else if path.extension().is_some_and(|e| e == "rs") && let Ok(text) = std::fs::read_to_string(&path) { out.push((path, text)); } } } out } } #[cfg(test)] mod title_tests { use super::*; use crate::text::{Outline, Place}; const FILE: &str = "/nonexistent/film.mkv"; fn title(start: u32, frames: u32, text: &str) -> Title { Title { start, frames, text: text.to_string(), style: TextStyle::default(), fade_in: 0, fade_out: 0, in_anim: Default::default(), out_anim: Default::default(), anim_frames: 0, keys: Vec::new(), } } /// Going from 30 to 25 fps keeps the seconds: a title that shows for two /// seconds still shows for two (50 frames, not the 60 it was stored as), /// and every frame-counted envelope -- title fades, clip fades, the dissolve /// into the next clip, a volume point -- lands on the same moment of the /// new grid as the picture it belongs to. #[test] fn a_rate_change_rescales_titles_and_frame_envelopes() { let k = Rate::from_fps(30.0, 25.0).expect("a rate both grids name"); let mut p = Project::single(FILE, 300); assert!(p.split(150)); assert!(p.set_fade_in(Lane::A1, 0, 30)); assert!(p.set_fade_out(Lane::A1, 0, 30)); assert!(p.set_transition_out(Lane::V1, 0, 30)); assert!(p.set_gain_keys( Lane::A1, 0, vec![ GainKey::new(60, -6.0), GainKey::new(120, -3.0), ], )); assert!(p.add_title(Title { fade_in: 0, fade_out: 12, ..title(0, 60, "Intro") }).is_ok()); assert!(p.add_title(Title { fade_in: 12, fade_out: 0, ..title(90, 60, "Credit") }).is_ok()); p.retime(k, &[250]); let t = p.titles(); assert_eq!((t[0].start, t[0].frames, t[0].fade_out), (0, 50, 10)); assert_eq!((t[1].start, t[1].frames, t[1].fade_in), (75, 50, 10)); assert_eq!(t[0].frames * 6, 60 * 5, "the two seconds stay two seconds"); let a = p.lane(Lane::A1); assert_eq!((a[0].fade_in, a[0].fade_out), (25, 25)); assert_eq!(p.lane(Lane::V1)[0].transition_out, 25); let keys: Vec<u32> = a[0].gain_keys.iter().map(|g| g.at).collect(); assert_eq!(keys, [50, 100], "volume points follow their footage"); } #[test] fn title_add_set_remove_and_undo_redo() { let mut p = Project::single(FILE, 150); assert_eq!(p.add_title(title(160, 40, "Episode 17")).unwrap(), 0); assert_eq!( p.add_title(title(10, 20, "Intro")).unwrap(), 0, "kept sorted by start" ); assert_eq!(p.titles()[0].text, "Intro"); assert_eq!( p.timeline_frames(), 200, "a title past the clip extends the timeline" ); assert_eq!(p.media_frames(), 200, "and the picture runs on over black"); assert!(p.undo()); assert_eq!(p.titles().len(), 1); assert_eq!(p.timeline_frames(), 200); assert!(p.undo()); assert!(p.titles().is_empty()); assert_eq!(p.timeline_frames(), 150, "the timeline shrinks back"); assert!(p.redo()); assert!(p.redo()); assert_eq!(p.titles().len(), 2); assert!(p.remove_title(0)); assert_eq!(p.titles().len(), 1); assert!(p.undo()); assert_eq!(p.titles().len(), 2); assert!(!p.remove_title(9), "an index that is not there is refused"); let mut edited = p.titles()[1].clone(); edited.text = "Episode 18".into(); assert!(p.set_title(1, edited.clone())); assert!(!p.set_title(1, edited), "the same title is no edit"); assert_eq!(p.titles()[1].text, "Episode 18"); assert!(p.undo()); assert_eq!(p.titles()[1].text, "Episode 17"); } #[test] fn chapter_add_set_move_remove_and_undo() { let chapter = |frame, title: &str| Chapter { frame, title: title.into() }; let mut p = Project::single(FILE, 300); assert_eq!(p.add_chapter(chapter(150, "Setup")).unwrap(), 0); assert_eq!(p.add_chapter(chapter(0, "Intro")).unwrap(), 0, "kept sorted by frame"); assert!(p.add_chapter(chapter(150, "Again")).is_err(), "one chapter per frame"); assert!(p.add_chapter(chapter(20, " ")).is_err(), "a chapter needs a name"); assert_eq!(p.chapters().len(), 2, "a refusal changes nothing"); assert!(p.undo()); assert_eq!(p.chapters(), &[chapter(150, "Setup")], "undo takes back the last add"); assert!(p.undo()); assert!(p.chapters().is_empty()); assert!(p.redo()); assert!(p.redo()); assert_eq!(p.move_chapter(0, 200).unwrap(), 1, "its index follows its frame"); assert_eq!(p.chapters()[1], chapter(200, "Intro")); assert!(p.move_chapter(0, 200).is_err(), "a frame another chapter holds is refused"); assert!(p.move_chapter(9, 10).is_err(), "an index that is not there is refused"); assert!(p.set_chapter_title(0, "Begin")); assert!(!p.set_chapter_title(0, "Begin"), "the same name is no edit"); assert!(!p.set_chapter_title(0, ""), "an empty name is refused"); assert!(p.remove_chapter(1)); assert_eq!(p.chapters(), &[chapter(150, "Begin")]); assert!(!p.remove_chapter(4), "an index that is not there is refused"); assert!(p.undo()); assert_eq!(p.chapters().len(), 2); assert_eq!(p.chapters()[0], chapter(150, "Begin"), "the rename is still there"); assert_eq!(p.chapters()[1], chapter(200, "Intro")); } #[test] fn the_caption_style_is_an_undoable_project_setting() { use crate::captionanim::{CaptionAnim, CaptionStyle}; let mut p = Project::single(FILE, 9); assert_eq!(p.caption_style(), CaptionStyle::default()); assert!(!p.set_caption_style(CaptionStyle::default()), "the style in force is no step"); let karaoke = CaptionStyle { anim: CaptionAnim::Karaoke, words_per_line: 40, ..CaptionStyle::default() }; assert!(p.set_caption_style(karaoke)); assert_eq!(p.caption_style().words_per_line, crate::captionanim::MAX_WORDS_PER_LINE, "clamped"); assert!(p.undo()); assert_eq!(p.caption_style(), CaptionStyle::default()); assert!(p.redo()); assert_eq!(p.caption_style().anim, CaptionAnim::Karaoke); } #[test] fn markers_add_set_remove_remap_and_undo() { let m = |frame, length, name: &str| Marker { frame, length, name: name.into(), note: String::new(), color: MarkerColor::Red }; let mut p = Project::single(FILE, 9); assert_eq!(p.add_marker(m(100, 0, "B")).unwrap(), 0); assert_eq!(p.add_marker(m(10, 40, "A")).unwrap(), 0, "kept sorted by frame"); assert_eq!(p.add_marker(m(100, 0, "B2")).unwrap(), 2, "a second on a frame goes after the first"); assert!(p.add_marker(m(5, 0, " ")).is_err(), "a marker needs a name"); assert_eq!(p.markers().len(), 3); assert!(p.undo()); assert_eq!(p.markers().len(), 2, "undo takes back the last add"); // A set that moves the frame re-sorts; an identical set is refused. assert_eq!(p.set_marker(0, m(200, 0, "A")).unwrap(), 1); assert!(p.set_marker(1, m(200, 0, "A")).is_err()); assert!(p.set_marker(9, m(1, 0, "x")).is_err()); // A remap moves a range's start and end; a ripple inside a range grows it. assert!(p.remap_markers(|f| if f >= 150 { f + 30 } else { f })); assert_eq!(p.markers()[0].frame, 100); assert_eq!(p.markers()[1].frame, 230); assert!(!p.remap_markers(|f| f), "no frame moves: no step"); assert!(p.undo()); assert_eq!(p.markers()[1].frame, 200, "one undo takes the whole remap back"); let mut q = Project::single(FILE, 9); q.add_marker(m(10, 40, "R")).unwrap(); assert!(q.remap_markers(|f| if f >= 30 { f + 100 } else { f })); assert_eq!((q.markers()[0].frame, q.markers()[0].length), (10, 140), "an insert inside a range grows it"); assert!(q.remap_markers(|f| if f >= 20 { 20 } else { f })); assert_eq!(q.markers()[0].length, 10, "a cut through a range shortens it"); assert!(q.remove_marker(0)); assert!(!q.remove_marker(0)); } #[test] fn remap_chapters_is_one_undo_step_and_keeps_the_first_of_a_pair() { let mut p = Project::single(FILE, 300); p.add_chapter(Chapter { frame: 0, title: "A".into() }).unwrap(); p.add_chapter(Chapter { frame: 50, title: "B".into() }).unwrap(); p.add_chapter(Chapter { frame: 60, title: "C".into() }).unwrap(); // C moves onto B's frame, so it is the one dropped. let dropped = p.remap_chapters(|f| if f == 60 { 50 } else { f }); assert_eq!(dropped, 1); let titles: Vec<&str> = p.chapters().iter().map(|c| c.title.as_str()).collect(); assert_eq!(titles, ["A", "B"]); assert!(p.undo()); assert_eq!(p.chapters().len(), 3, "one undo brings back every chapter"); assert_eq!(p.remap_chapters(|f| f), 0, "no frame moves: no step"); assert!(p.undo()); assert_eq!(p.chapters().len(), 2, "the step before was the add of C"); } #[test] fn title_refusals_change_nothing() { let mut p = Project::single(FILE, 150); let before = p.clone(); assert!(p.add_title(title(0, 0, "none")).is_err(), "no frames"); assert!(p.add_title(title(0, 10, " ")).is_err(), "no text"); assert!( p.add_title(title(0, 10, &"x".repeat(MAX_TITLE_CHARS + 1))) .is_err() ); assert!( p.add_title(title(MAX_TITLE_END, 10, "past")).is_err(), "past the maximum" ); let mut big = title(0, 10, "size"); big.style.size = 9; assert!(p.add_title(big).is_err(), "a size under the range"); let mut fade = title(0, 10, "fade"); fade.fade_in = 11; assert!(p.add_title(fade).is_err(), "a fade longer than the title"); assert!(p.titles().is_empty()); assert_eq!(p.parts(), before.parts()); assert!( !p.undo() || p.titles().is_empty(), "a refusal takes no undo step" ); } #[test] fn title_alpha_ramps_in_and_out() { let mut t = title(10, 10, "fades"); t.fade_in = 4; t.fade_out = 4; assert_eq!(t.alpha_at(9), 0, "before the title"); assert_eq!(t.alpha_at(10), 51, "the first frame is one part in five"); assert_eq!(t.alpha_at(13), 204); assert_eq!(t.alpha_at(14), 255, "full between the fades"); assert_eq!(t.alpha_at(16), 204, "the fade-out counts off the end"); assert_eq!(t.alpha_at(19), 51); assert_eq!(t.alpha_at(20), 0, "after the title"); let plain = title(0, 3, "plain"); assert_eq!( (0..3).map(|f| plain.alpha_at(f)).collect::<Vec<_>>(), [255, 255, 255] ); } #[test] fn title_style_outline_is_kept_by_the_title() { let mut t = title(0, 5, "edged"); t.style.outline = Outline::Thick; t.style.place = Place::Top; let mut p = Project::single(FILE, 150); p.add_title(t.clone()).unwrap(); assert_eq!(p.titles()[0], t); } } #[cfg(test)] mod speed_ramp_tests { use super::*; const FILE: &str = "/nonexistent/a.mp4"; fn key(at: u32, permille: u16) -> SpeedKey { SpeedKey { at, speed: Speed::from_permille(permille), } } /// The 1x-to-2x ramp over 60 frames of the brief. fn ramp() -> Vec<SpeedKey> { vec![key(0, 1000), key(60, 2000)] } #[test] fn the_integral_of_a_one_to_two_ramp_over_sixty_frames_covers_ninety() { let keys = ramp(); assert!((ramp_source_at(&keys, 60.) - 90.).abs() < 1e-9); // The midpoint: 30 frames at ~1.25x average to 37.5 of source. assert!((ramp_source_at(&keys, 30.) - 37.5).abs() < 1e-9); // Held at 2x past the last key: 90 + 2 * 10. assert!((ramp_source_at(&keys, 70.) - 110.).abs() < 1e-9); // Held at 1x before the first key. assert!((ramp_source_at(&[key(10, 1000), key(20, 2000)], 5.) - 5.).abs() < 1e-9); } #[test] fn a_ramp_of_ninety_source_frames_is_sixty_timeline_frames() { assert_eq!(ramp_frames(&ramp(), 90), 60); // Constant keys are the constant speed, to the frame. for permille in [250u16, 500, 1000, 1500, 4000] { let flat = [key(0, permille)]; for len in [1u32, 7, 90, 301] { assert_eq!(ramp_frames(&flat, len), Speed::from_permille(permille).frames(len), "{permille} over {len}"); } } // Never zero, even for a source frame at the fastest speed. assert_eq!(ramp_frames(&[key(0, 4000), key(1, 4000)], 1), 1); } #[test] fn source_pos_is_the_integral_and_constant_speed_is_bit_identical() { let mut p = Project::single(FILE, 90); let lane = Lane::V1; let clip = p.lane(lane)[0].clone(); // An unramped clip maps exactly as the constant speed always did. for t in 0..=clip.frames() { let want = clip.in_frame + clip.speed.source_at(t); assert_eq!(source_pos(&clip, t).floor() as u32, want, "frame {t}"); assert_eq!(source_pos(&clip, t).fract(), 0.0); } p.set_speed_ramp(lane, 0, &ramp()).expect("the ramp sets"); let clip = p.lane(lane)[0].clone(); assert_eq!(clip.frames(), 60, "the ramp's footprint"); assert!((source_pos(&clip, 30) - 37.5).abs() < 1e-9); assert_eq!(source_frame(&clip, 30), 37, "the midpoint shows source frame 37"); assert_eq!(source_frame(&clip, 59), 88, "the last frame shows the ramp's end"); } #[test] fn a_ramp_span_maps_both_ways_and_reads_real_time_for_its_field() { let mut p = Project::single(FILE, 90); p.set_speed_ramp(Lane::V1, 0, &ramp()).expect("the ramp sets"); let span = p.span_at(Lane::V1, 0).expect("a span"); assert_eq!(span.len, 60); assert_eq!(span.speed, Speed::NORMAL, "a ramped span has no constant rate"); assert_eq!(span.source_offset(30), 37, "offset 30 is source frame 37"); // The last timeline frame shows source 88, as a constant 2x would stop short // of the file's last frame: 89 frames are read, the same rule as a constant speed. assert_eq!(span.source_len(), 89, "the span reads up to its last frame's source"); for s in 0..90 { let t = span.timeline_at(s); assert!(span.source_offset(t) >= s, "source {s} is shown by timeline {t}"); assert!(t == 0 || span.source_offset(t - 1) < s, "and not before it: {s}"); } // A span from the middle of the clip is based there. let mid = p.span_at(Lane::V1, 30).expect("a span"); assert_eq!(mid.from, Some((0, 37))); // S(40) - S(30) in floors: 53 - 37. assert_eq!(mid.source_offset(10), 16, "ten frames on from the midpoint"); } #[test] fn a_ramp_is_saved_undone_and_cleared_as_one_step_each() { let mut p = Project::single(FILE, 90); let before = p.lane(Lane::V1)[0].clone(); p.set_speed_ramp(Lane::V1, 0, &ramp()).expect("the ramp sets"); assert_eq!(p.lane(Lane::V1)[0].speed_keys, ramp()); assert!(p.undo(), "one undo step"); assert_eq!(p.lane(Lane::V1)[0], before, "undo restores the constant clip"); p.set_speed_ramp(Lane::V1, 0, &ramp()).expect("the ramp sets again"); p.set_speed_ramp(Lane::V1, 0, &[]).expect("clears"); assert_eq!(p.lane(Lane::V1)[0].frames(), 90, "a cleared ramp is real time"); } #[test] fn a_ramp_refuses_a_bad_key_list_and_a_constant_speed_on_top() { let mut p = Project::single(FILE, 90); assert!(p.set_speed_ramp(Lane::V1, 0, &[key(5, 1000), key(5, 2000)]).is_err()); assert!(p.set_speed_ramp(Lane::V1, 0, &[key(9, 1000), key(3, 2000)]).is_err()); p.set_speed_ramp(Lane::V1, 0, &ramp()).expect("the ramp sets"); assert!(p.set_speed(Lane::V1, 0, Speed::from_permille(2000)).is_err(), "clear the ramp first"); assert!(p.speeded_playhead(Lane::V1, 0, Speed::from_permille(2000), 5).is_none()); // A ramp cuts where it lands on a whole source frame: at 30 the halves are 38 and 52. assert!(p.split(30), "a ramp cuts at a whole frame"); let clips = p.lane(Lane::V1); assert_eq!((clips.len(), clips[0].frames(), clips[1].start, clips[1].end()), (2, 30, 30, 60)); assert!(clips.iter().all(|c| !c.speed_keys.is_empty()), "both halves keep the ramp"); } #[test] fn a_tail_trim_of_a_ramp_refits_its_keys_and_a_head_trim_is_refused() { let mut p = Project::single(FILE, 90); p.set_speed_ramp(Lane::V1, 0, &ramp()).expect("the ramp sets"); let clip = p.lane(Lane::V1)[0].clone(); // The largest source length whose ramp footprint is at most 30 frames. let fit = clip.fit_source(30).expect("some of it fits"); assert!(ramp_frames(&ramp(), fit) <= 30 && ramp_frames(&ramp(), fit + 1) > 30, "fit {fit}"); assert_eq!(clip.fit_source(0), None); assert!(!p.trim(Lane::V1, 0, Edge::Start, 10, &[90]), "a head trim of a ramp is refused"); assert_eq!(p.lane(Lane::V1)[0], clip, "and changes nothing"); } #[test] fn ramp_keys_from_rebases_a_tail_at_the_cut() { let keys = ramp(); // Cut at 30: the speed there is 1.5x, then the key at 60 is 30 frames on. let tail = ramp_keys_from(&keys, 30); assert_eq!(tail, vec![key(0, 1500), key(30, 2000)]); assert!(valid_speed_keys(&tail)); } /// A ramp cuts where its own integral lands on a whole source frame, and both halves /// come back to their own lengths: 1x-to-2x over 60 frames is 90 source frames, and /// the cut at frame 30 is source 37.5, which rounds to 38. #[test] fn a_ramp_splits_into_two_halves_that_add_up() { let keys = ramp(); let (src, head, tail) = ramp_split(&keys, 90, 30).expect("a whole frame at 30"); assert_eq!(src, 38); assert!(valid_speed_keys(&head) && valid_speed_keys(&tail)); assert_eq!(ramp_frames(&head, src), 30); assert_eq!(ramp_frames(&tail, 90 - src), 30); // Nothing at the ends, and nothing past the clip. assert!(ramp_split(&keys, 90, 0).is_none()); assert!(ramp_split(&keys, 90, 60).is_none()); } /// A freeze on a ramp takes the nearest frame every lane can cut at: the frame itself /// when it is one, else one a few frames off, and the cut there really lands. #[test] fn a_freeze_on_a_ramp_finds_a_frame_the_ramp_can_cut_at() { let mut p = Project::single(FILE, 90); p.set_speed_ramp(Lane::V1, 0, &ramp()).expect("the ramp sets"); assert_eq!(p.cut_frame_near(30, 12), Some(30)); let near = p.cut_frame_near(31, 12).expect("a cut within reach"); assert!(near.abs_diff(31) <= 12, "{near}"); assert!(p.split(near), "the frame it names is one the ramp cuts at"); } /// The clip-level mappings every source/timeline conversion outside the engine /// goes through: a ramp's integral and its inverses, and a constant speed's own. #[test] fn clip_mappings_follow_the_ramp_and_keep_the_constant_ones() { let mut c = Clip::source_range(0, 10, 400); c.speed_keys = vec![key(0, 1000), key(60, 2000)]; // 1x to 2x over 60: source frame t + t^2/120. assert_eq!(c.source_offset(60), 90); assert_eq!(c.source_offset(30), 37); assert!((c.source_offset_f(60.0) - 90.0).abs() < 1e-9); // The ceil inverse: first timeline frame at or past a source offset. assert_eq!(c.timeline_offset(30), 25); assert_eq!(c.timeline_offset_floor(60), 43); assert!((c.timeline_offset_f(90.0) - 60.0).abs() < 1e-6); for src in [1, 17, 90, 130, 300] { let t = c.timeline_offset(src); assert!(c.source_offset(t) >= src && (t == 0 || c.source_offset(t - 1) < src), "{src}"); } // A constant speed keeps its own arithmetic, bit for bit. c.speed_keys.clear(); c.speed = Speed::from_permille(2000); assert_eq!(c.source_offset(30), 60); assert_eq!(c.timeline_offset(60), 30); assert_eq!(c.timeline_offset_floor(61), 30); assert!((c.timeline_offset_f(60.0) - 30.0).abs() < 1e-12); } } #[cfg(test)] mod three_point_tests { use super::*; const V: &str = "/nonexistent/a.mp4"; /// A clip of source `source`, `in_frame..out_frame`, with the fields the tests do not look at left plain. fn take(source: usize, in_frame: u32, out_frame: u32) -> Clip { Project::single(V, 1).lane(Lane::V1)[0].clone().tap(|c| { c.source = source; c.in_frame = in_frame; c.out_frame = out_frame; c.start = 0; c.link = None; }) } trait Tap: Sized { fn tap(self, f: impl FnOnce(&mut Self)) -> Self; } impl Tap for Clip { fn tap(mut self, f: impl FnOnce(&mut Self)) -> Self { f(&mut self); self } } /// `(start, end, in_frame, source)` of every clip on `lane`. fn spans(p: &Project, lane: Lane) -> Vec<(u32, u32, u32, usize)> { p.lane(lane).iter().map(|c| (c.start, c.end(), c.in_frame, c.source)).collect() } /// A 90-frame take of source 0 on V1+A1, plus a second source (`b.mp4`) to edit in. fn bed() -> Project { let mut p = Project::single(V, 90); p.sources.push(Source::new("/nonexistent/b.mp4", 0)); p } fn both(source: usize, a: u32, b: u32) -> Vec<(Lane, Clip)> { vec![(Lane::V1, take(source, a, b)), (Lane::A1, take(source, a, b))] } #[test] fn insert_into_the_middle_of_a_clip_splits_and_ripples() { let mut p = bed(); p.edit_from_source(SourceEdit::Insert, 30, &both(1, 10, 25)).unwrap(); let want = vec![(0, 30, 0, 0), (30, 45, 10, 1), (45, 105, 30, 0)]; assert_eq!(spans(&p, Lane::V1), want); assert_eq!(spans(&p, Lane::A1), want); assert_eq!(p.timeline_frames(), 105); // The new take is one link; the halves of the split clip are one link each, and all differ. let (v, a) = (p.lane(Lane::V1), p.lane(Lane::A1)); for i in 0..3 { assert!(v[i].link.is_some() && v[i].link == a[i].link, "clip {i} stays a take"); } assert!(v[0].link != v[1].link && v[1].link != v[2].link && v[0].link != v[2].link); assert!(links_are_consistent(&p.lanes).is_ok()); } #[test] fn insert_at_a_cut_and_at_zero_does_not_split() { let mut p = bed(); p.edit_from_source(SourceEdit::Insert, 0, &both(1, 0, 10)).unwrap(); assert_eq!(spans(&p, Lane::V1), vec![(0, 10, 0, 1), (10, 100, 0, 0)]); } #[test] fn overwrite_across_two_clips_trims_and_splits_nothing_moves() { let mut p = bed(); assert!(p.split(30)); assert!(p.split(60)); p.edit_from_source(SourceEdit::Overwrite, 20, &both(1, 0, 50)).unwrap(); // 0..20 kept, 20..70 the new take, 70..90 the tail of the last clip; the clip at 30..60 is gone. let want = vec![(0, 20, 0, 0), (20, 70, 0, 1), (70, 90, 70, 0)]; assert_eq!(spans(&p, Lane::V1), want); assert_eq!(spans(&p, Lane::A1), want); assert_eq!(p.timeline_frames(), 90, "nothing moved"); } #[test] fn overwrite_inside_one_clip_splits_it_in_three() { let mut p = bed(); p.edit_from_source(SourceEdit::Overwrite, 40, &both(1, 5, 15)).unwrap(); assert_eq!(spans(&p, Lane::V1), vec![(0, 40, 0, 0), (40, 50, 5, 1), (50, 90, 50, 0)]); } #[test] fn overwrite_past_the_end_keeps_the_gap_and_extends_the_timeline() { let mut p = bed(); p.edit_from_source(SourceEdit::Overwrite, 100, &both(1, 0, 20)).unwrap(); assert_eq!(spans(&p, Lane::V1), vec![(0, 90, 0, 0), (100, 120, 0, 1)]); assert_eq!(p.timeline_frames(), 120); // An overwrite that starts inside the last clip and runs past its end. let mut p = bed(); p.edit_from_source(SourceEdit::Overwrite, 80, &both(1, 0, 30)).unwrap(); assert_eq!(spans(&p, Lane::V1), vec![(0, 80, 0, 0), (80, 110, 0, 1)]); } #[test] fn an_insert_past_the_end_just_places() { let mut p = bed(); p.edit_from_source(SourceEdit::Insert, 95, &both(1, 0, 5)).unwrap(); assert_eq!(spans(&p, Lane::V1), vec![(0, 90, 0, 0), (95, 100, 0, 1)]); } #[test] fn linked_takes_move_whole_and_an_untargeted_half_refuses() { let mut p = bed(); // Insert on V1 alone: the take's sound on A1 would be pulled apart. let before = p.lane(Lane::V1).to_vec(); let history = p.history_len(); let err = p.edit_from_source(SourceEdit::Insert, 30, &[(Lane::V1, take(1, 0, 10))]).unwrap_err(); assert!(err.to_string().contains("one take with the A1 clip"), "{err}"); assert_eq!(p.lane(Lane::V1), &before[..], "nothing changed"); assert_eq!(p.history_len(), history, "and no undo step"); // Detached, the same insert is fine and leaves A1 alone. assert!(p.ungroup(Lane::V1, 0)); p.edit_from_source(SourceEdit::Insert, 30, &[(Lane::V1, take(1, 0, 10))]).unwrap(); assert_eq!(spans(&p, Lane::V1), vec![(0, 30, 0, 0), (30, 40, 0, 1), (40, 100, 30, 0)]); assert_eq!(spans(&p, Lane::A1), vec![(0, 90, 0, 0)]); } #[test] fn a_lane_that_is_not_targeted_is_neither_cut_nor_moved() { let mut p = bed(); let v2 = Lane::new(LaneKind::Video, 1); p.lanes.push(LaneData::new(LaneKind::Video, Vec::new())); let a2 = Lane::new(LaneKind::Audio, 1); p.lanes.push(LaneData::new(LaneKind::Audio, Vec::new())); p.edit_from_source(SourceEdit::Overwrite, 20, &[(v2, take(1, 0, 60)), (a2, take(1, 0, 60))]).unwrap(); p.edit_from_source(SourceEdit::Insert, 30, &both(1, 0, 10)).unwrap(); assert_eq!(spans(&p, v2), vec![(20, 80, 0, 1)], "the V2 clip across the insert is untouched"); assert_eq!(spans(&p, a2), vec![(20, 80, 0, 1)]); assert_eq!(spans(&p, Lane::V1)[1], (30, 40, 0, 1)); } #[test] fn a_locked_lane_refuses() { let mut p = bed(); assert!(p.set_lane_locked(Lane::A1, true)); let before = spans(&p, Lane::V1); for kind in [SourceEdit::Insert, SourceEdit::Overwrite] { let err = p.edit_from_source(kind, 10, &both(1, 0, 10)).unwrap_err(); assert!(err.to_string().contains("A1 is locked"), "{err}"); } assert_eq!(spans(&p, Lane::V1), before); } #[test] fn an_audio_only_source_goes_on_an_audio_lane_only() { let mut p = bed(); p.sources.push(Source::new("/nonexistent/song.mp3", 0)); let err = p.edit_from_source(SourceEdit::Overwrite, 10, &[(Lane::V1, take(2, 0, 10))]).unwrap_err(); assert!(err.to_string().contains("no picture"), "{err}"); assert!(p.ungroup(Lane::V1, 0)); p.edit_from_source(SourceEdit::Insert, 30, &[(Lane::A1, take(2, 0, 10))]).unwrap(); assert_eq!(spans(&p, Lane::A1), vec![(0, 30, 0, 0), (30, 40, 0, 2), (40, 100, 30, 0)]); assert_eq!(spans(&p, Lane::V1), vec![(0, 90, 0, 0)]); assert_eq!(p.lane(Lane::A1)[1].link, None, "a lone half belongs to no group"); } #[test] fn a_still_goes_on_a_video_lane_only() { let mut p = bed(); p.sources.push(Source::new("/nonexistent/pic.png", 0)); let err = p.edit_from_source(SourceEdit::Overwrite, 10, &[(Lane::A1, take(2, 0, 10))]).unwrap_err(); assert!(err.to_string().contains("still"), "{err}"); assert!(p.ungroup(Lane::V1, 0)); p.edit_from_source(SourceEdit::Overwrite, 10, &[(Lane::V1, take(2, 0, 125))]).unwrap(); assert_eq!(spans(&p, Lane::V1), vec![(0, 10, 0, 0), (10, 135, 0, 2)]); assert_eq!(spans(&p, Lane::A1), vec![(0, 90, 0, 0)]); } #[test] fn each_edit_is_one_undo_step() { for kind in [SourceEdit::Insert, SourceEdit::Overwrite] { let mut p = bed(); let before = (p.lane(Lane::V1).to_vec(), p.lane(Lane::A1).to_vec()); let history = p.history_len(); p.edit_from_source(kind, 30, &both(1, 0, 10)).unwrap(); assert_eq!(p.history_len(), history + 1); assert!(p.undo()); assert_eq!((p.lane(Lane::V1).to_vec(), p.lane(Lane::A1).to_vec()), before, "{kind:?}"); assert!(p.redo()); assert_eq!(spans(&p, Lane::V1)[1], (30, 40, 0, 1)); } } #[test] fn bad_edits_refuse_and_cost_no_undo_step() { let mut p = bed(); let history = p.history_len(); assert!(p.edit_from_source(SourceEdit::Insert, 0, &[]).is_err()); assert!(p.edit_from_source(SourceEdit::Insert, 0, &[(Lane::V1, take(1, 5, 5))]).is_err()); assert!(p.edit_from_source(SourceEdit::Insert, 0, &[(Lane::V1, take(9, 0, 5))]).is_err()); assert!(p.edit_from_source(SourceEdit::Insert, 0, &[(Lane::new(LaneKind::Video, 4), take(1, 0, 5))]).is_err()); assert!(p.edit_from_source(SourceEdit::Insert, 0, &[(Lane::V1, take(1, 0, 5)), (Lane::V1, take(1, 0, 5))]).is_err()); assert!(p.edit_from_source(SourceEdit::Overwrite, u32::MAX, &both(1, 0, 5)).is_err()); assert_eq!(p.history_len(), history); } } /// Roll, slip and slide: the trims that move two things at once. #[cfg(test)] mod pro_trim_tests { use super::*; const FILE: &str = "/nonexistent/a.mp4"; fn clip(start: u32, in_frame: u32, out_frame: u32) -> Clip { Clip { speed_keys: Vec::new(), opacity_keys: Vec::new(), color_keys: Vec::new(), effects: Vec::new(), transition_kind: Default::default(), reversed: false, stabilise: None, deinterlace: Default::default(), mark: Default::default(), angle: Default::default(), fade_in: 0, fade_out: 0, transition_out: 0, visualizer: 0, viz_paint: 0, gain_db: 0.0, gain_keys: Vec::new(), pan: 0, pitch: 0, transform_keys: Vec::new(), start, in_frame, out_frame, source: 0, link: None, eq: None, color: None, transform: None, fit: FitPolicy::default(), speed: Speed::NORMAL, } } fn fast(start: u32, in_frame: u32, out_frame: u32, permille: u16) -> Clip { Clip { speed: Speed::from_permille(permille), ..clip(start, in_frame, out_frame) } } /// 30 frames of V1+A1 (a linked take) cut into 0..10, 10..20, 20..30. fn three() -> Project { let mut p = Project::single(FILE, 30); assert!(p.split(10)); assert!(p.split(20)); p } fn w(t: &[u32]) -> SourceWalls<'_> { SourceWalls::same(t) } /// `(start, end, in, out)` of every clip on the lane. fn spans(p: &Project, lane: Lane) -> Vec<(u32, u32, u32, u32)> { p.lane(lane).iter().map(|c| (c.start, c.end(), c.in_frame, c.out_frame)).collect() } /// A lane with `v2` holding the given clips; no linked partners. fn lone(clips: &[Clip]) -> (Project, Lane) { let mut p = Project::single(FILE, 1); let v = p.add_lane(LaneKind::Video); for c in clips { assert!(p.place(v, c.start, c.clone())); } (p, v) } #[test] fn roll_moves_the_cut_of_a_take_and_keeps_the_length() { let mut p = three(); let steps = p.history.len(); assert_eq!(p.roll(Lane::V1, 0, 14, w(&[30])), Some(14)); for lane in [Lane::V1, Lane::A1] { assert_eq!( spans(&p, lane), [(0, 14, 0, 14), (14, 20, 14, 20), (20, 30, 20, 30)], "{lane:?}: both halves of the take rolled, the far cut stayed" ); } assert_eq!(p.history.len(), steps + 1, "one undo step"); assert!(p.undo()); assert_eq!(spans(&p, Lane::V1), [(0, 10, 0, 10), (10, 20, 10, 20), (20, 30, 20, 30)]); assert_eq!(spans(&p, Lane::A1), [(0, 10, 0, 10), (10, 20, 10, 20), (20, 30, 20, 30)]); // Rolled back toward the start, from the other side of the take. assert_eq!(p.roll(Lane::A1, 1, 25, w(&[30])), Some(25)); assert_eq!(spans(&p, Lane::V1)[1..], [(10, 25, 10, 25), (25, 30, 25, 30)]); } #[test] fn roll_is_clamped_to_a_frame_each_side_and_changes_nothing_in_place() { let mut p = three(); assert_eq!(p.roll_room(Lane::V1, 0, w(&[30])), Some((1, 19))); assert_eq!(p.roll(Lane::V1, 0, 500, w(&[30])), Some(19)); assert_eq!(spans(&p, Lane::V1)[..2], [(0, 19, 0, 19), (19, 20, 19, 20)]); assert_eq!(p.roll(Lane::V1, 0, 0, w(&[30])), Some(1)); let steps = p.history.len(); assert_eq!(p.roll(Lane::V1, 0, 1, w(&[30])), Some(1), "already there"); assert_eq!(p.history.len(), steps, "and a roll that moves nothing costs no step"); } #[test] fn roll_stops_at_the_source_media() { // A plays 5..15 of a source that is 18 frames long; B plays 25..30. let (mut p, v) = lone(&[clip(0, 5, 15), clip(10, 25, 30)]); assert_eq!(p.roll_room(v, 0, w(&[18])), Some((1, 13))); assert_eq!(p.roll(v, 0, 100, w(&[18])), Some(13)); assert_eq!(p.lane(v)[0].out_frame, 18, "the out-point is the source's last frame"); assert_eq!(spans(&p, v)[1], (13, 15, 28, 30), "the next in-point moved by the same frames"); // The other way: B can back up only to its own first frame. let (mut p, v) = lone(&[clip(0, 5, 15), clip(10, 3, 8)]); assert_eq!(p.roll(v, 0, 0, w(&[30])), Some(7), "3 frames of head: the cut stops 3 frames back"); assert_eq!(p.lane(v)[1].in_frame, 0); } #[test] fn roll_needs_a_cut() { let mut p = three(); assert_eq!(p.roll(Lane::V1, 2, 25, w(&[30])), None, "the last clip has no neighbour"); assert_eq!(p.roll(Lane::V1, 9, 25, w(&[30])), None, "no such clip"); let (mut p, v) = lone(&[clip(0, 0, 10), clip(12, 0, 10)]); let steps = p.history.len(); assert_eq!(p.roll(v, 0, 5, w(&[30])), None, "a gap is not a cut"); assert_eq!(p.history.len(), steps); } #[test] fn roll_at_speed_moves_source_by_the_rate() { // Two clips at 2x: 10 timeline frames each, twenty source frames each. let (mut p, v) = lone(&[fast(0, 0, 20, 2000), fast(10, 30, 50, 2000)]); assert_eq!(p.roll(v, 0, 14, w(&[100])), Some(14)); assert_eq!(spans(&p, v), [(0, 14, 0, 28), (14, 20, 38, 50)], "4 timeline frames are 8 source frames"); assert!(p.undo()); assert_eq!(spans(&p, v), [(0, 10, 0, 20), (10, 20, 30, 50)]); } #[test] fn roll_at_a_slow_rate_never_opens_a_gap_or_changes_the_length() { // 0.4x: a source range of n frames is round(2.5 n) timeline frames, so most // timeline lengths are unreachable. for to in 1..20 { let (mut p, v) = lone(&[fast(0, 0, 4, 400), fast(10, 10, 14, 400)]); let end = p.lane(v)[1].end(); let landed = p.roll(v, 0, to, w(&[100])).expect("a cut"); let [a, b] = [&p.lane(v)[0], &p.lane(v)[1]]; assert_eq!((a.end(), b.start, b.end()), (landed, landed, end), "roll to {to}"); assert!(sorted_disjoint(p.lane(v))); } } #[test] fn roll_pins_at_a_ramped_clip_behind_the_cut() { let (mut p, v) = lone(&[clip(0, 0, 10), clip(10, 0, 10)]); p.set_speed_ramp(v, 1, &[SpeedKey { at: 0, speed: Speed::NORMAL }, SpeedKey { at: 8, speed: Speed::from_permille(2000) }]) .expect("ramp"); assert_eq!(p.roll(v, 0, 5, w(&[100])), Some(10), "a ramped in-point cannot move"); // The ramp may be the one before the cut: its tail is the only edge that moves. let (mut p, v) = lone(&[clip(0, 0, 20)]); p.set_speed_ramp(v, 0, &[SpeedKey { at: 0, speed: Speed::NORMAL }, SpeedKey { at: 15, speed: Speed::from_permille(2000) }]) .expect("ramp"); let at = p.lane(v)[0].end(); assert!(p.place(v, at, clip(at, 5, 15))); let end = p.lane(v)[1].end(); let cut = p.lane(v)[0].end(); let landed = p.roll(v, 0, cut - 3, w(&[100])).expect("a cut"); assert!(landed < cut); assert_eq!((p.lane(v)[0].end(), p.lane(v)[1].start, p.lane(v)[1].end()), (landed, landed, end)); } #[test] fn slip_shifts_the_window_and_nothing_else() { let mut p = three(); let steps = p.history.len(); assert_eq!(p.slip(Lane::V1, 1, 5, w(&[30])), Some(5)); for lane in [Lane::V1, Lane::A1] { assert_eq!( spans(&p, lane)[1], (10, 20, 15, 25), "{lane:?}: same slot, later footage" ); assert_eq!(spans(&p, lane)[0], (0, 10, 0, 10), "the neighbours are untouched"); } assert_eq!(p.history.len(), steps + 1); assert!(p.undo()); assert_eq!(spans(&p, Lane::V1)[1], (10, 20, 10, 20)); assert_eq!(spans(&p, Lane::A1)[1], (10, 20, 10, 20)); } #[test] fn slip_is_clamped_to_the_source_and_each_member_to_its_own() { let mut p = three(); assert_eq!(p.slip_room(Lane::V1, 1, w(&[30])), Some((-10, 10))); assert_eq!(p.slip(Lane::V1, 1, -99, w(&[30])), Some(-10)); assert_eq!(spans(&p, Lane::V1)[1], (10, 20, 0, 10)); assert_eq!(p.slip(Lane::V1, 1, 99, w(&[30])), Some(20)); assert_eq!(spans(&p, Lane::V1)[1], (10, 20, 20, 30)); // The sound is 5 frames shorter than the picture: it stops where it stops. let mut p = three(); let walls = SourceWalls { picture: &[30], sound: &[25] }; assert_eq!(p.slip(Lane::V1, 1, 8, walls), Some(8)); assert_eq!(spans(&p, Lane::V1)[1], (10, 20, 18, 28)); assert_eq!(spans(&p, Lane::A1)[1], (10, 20, 15, 25), "the sound ran out of file first"); // Pinned: no change, no step. let steps = p.history.len(); assert_eq!(p.slip(Lane::V1, 1, 1, w(&[28])), Some(0)); assert_eq!(p.history.len(), steps); } #[test] fn slip_at_speed_keeps_the_slot_and_converts_timeline_frames() { let (mut p, v) = lone(&[fast(0, 10, 20, 2000)]); assert_eq!(p.lane(v)[0].end(), 5); assert_eq!(p.slip_source_delta(v, 0, 3), Some(6), "3 timeline frames at 2x"); assert_eq!(p.slip_source_delta(v, 0, -3), Some(-6)); assert_eq!(p.slip(v, 0, 6, w(&[60])), Some(6)); assert_eq!(spans(&p, v), [(0, 5, 16, 26)]); assert_eq!(p.slip(v, 0, 1000, w(&[60])), Some(34), "clamped to the 60 frames the source has"); assert_eq!(spans(&p, v), [(0, 5, 50, 60)]); // Half speed: a timeline frame is half a source frame. let (p, v) = lone(&[fast(0, 10, 20, 500)]); assert_eq!(p.slip_source_delta(v, 0, 8), Some(4)); } #[test] fn slip_keeps_a_ramped_clip_in_its_slot() { let (mut p, v) = lone(&[clip(0, 10, 30)]); let keys = [SpeedKey { at: 0, speed: Speed::NORMAL }, SpeedKey { at: 10, speed: Speed::from_permille(2000) }]; p.set_speed_ramp(v, 0, &keys).expect("ramp"); let before = (p.lane(v)[0].start, p.lane(v)[0].end()); assert_eq!(p.slip(v, 0, 7, w(&[100])), Some(7)); let c = &p.lane(v)[0]; assert_eq!((c.start, c.end(), c.in_frame, c.out_frame), (before.0, before.1, 17, 37)); assert_eq!(c.speed_keys.len(), 2, "the ramp stays on the clip"); } #[test] fn slip_has_nothing_to_do_to_a_still_or_a_missing_clip() { let mut p = Project::single("/nonexistent/pic.png", 100); let steps = p.history.len(); assert_eq!(p.slip(Lane::V1, 0, 5, w(&[100])), None); assert_eq!(p.slip(Lane::V1, 3, 5, w(&[100])), None); assert_eq!(p.history.len(), steps); } #[test] fn slide_moves_the_clip_and_its_neighbours_absorb_it() { let mut p = three(); let steps = p.history.len(); assert_eq!(p.slide(Lane::V1, 1, 13, w(&[30])), Some(13)); for lane in [Lane::V1, Lane::A1] { assert_eq!( spans(&p, lane), [(0, 13, 0, 13), (13, 23, 10, 20), (23, 30, 23, 30)], "{lane:?}: same content, same length, neighbours absorb" ); } assert_eq!(p.history.len(), steps + 1, "one undo step"); assert!(p.undo()); assert_eq!(spans(&p, Lane::V1), [(0, 10, 0, 10), (10, 20, 10, 20), (20, 30, 20, 30)]); assert_eq!(spans(&p, Lane::A1), [(0, 10, 0, 10), (10, 20, 10, 20), (20, 30, 20, 30)]); assert_eq!(p.slide(Lane::A1, 1, 6, w(&[30])), Some(6), "either half drags the take"); assert_eq!(spans(&p, Lane::V1), [(0, 6, 0, 6), (6, 16, 10, 20), (16, 30, 16, 30)]); } #[test] fn slide_is_clamped_to_a_frame_of_each_neighbour_and_their_media() { let mut p = three(); assert_eq!(p.slide_room(Lane::V1, 1, w(&[30])), Some((1, 19))); assert_eq!(p.slide(Lane::V1, 1, 100, w(&[30])), Some(19)); assert_eq!(spans(&p, Lane::V1), [(0, 19, 0, 19), (19, 29, 10, 20), (29, 30, 29, 30)]); assert_eq!(p.slide(Lane::V1, 1, 0, w(&[30])), Some(1)); // The clip before it only has source up to frame 12; it cannot give more. let (mut p, v) = lone(&[clip(0, 0, 10), clip(10, 40, 50), clip(20, 60, 70)]); assert_eq!(p.slide(v, 1, 99, w(&[12])), Some(12)); assert_eq!(p.lane(v)[0].out_frame, 12, "it gave every frame it had"); } #[test] fn slide_needs_a_neighbour_on_each_side() { let mut p = three(); let steps = p.history.len(); assert_eq!(p.slide(Lane::V1, 0, 3, w(&[30])), None, "first clip"); assert_eq!(p.slide(Lane::V1, 2, 3, w(&[30])), None, "last clip"); assert_eq!(p.slide(Lane::V1, 8, 3, w(&[30])), None, "no clip"); assert_eq!(p.history.len(), steps); let (mut p, v) = lone(&[clip(0, 0, 10)]); assert_eq!(p.slide(v, 0, 3, w(&[30])), None, "alone on its lane"); } #[test] fn slide_beside_a_gap_edits_only_the_cut_side() { // 0..10 | gap 10..15 | 15..25 | 25..35 let (mut p, v) = lone(&[clip(0, 0, 10), clip(15, 20, 30), clip(25, 40, 50)]); assert_eq!(p.slide_room(v, 1, w(&[100])), Some((10, 24))); assert_eq!(p.slide(v, 1, 0, w(&[100])), Some(10), "into the gap, but not over the clip before it"); assert_eq!(spans(&p, v), [(0, 10, 0, 10), (10, 20, 20, 30), (20, 35, 35, 50)]); } #[test] fn slide_at_speed_moves_neighbour_source_by_their_rate() { // The clip before it plays at 2x: a frame of timeline is two of source. let (mut p, v) = lone(&[fast(0, 0, 20, 2000), clip(10, 40, 50), clip(20, 60, 70)]); assert_eq!(p.slide(v, 1, 13, w(&[100])), Some(13)); assert_eq!(spans(&p, v), [(0, 13, 0, 26), (13, 23, 40, 50), (23, 30, 63, 70)]); assert!(p.undo()); assert_eq!(spans(&p, v), [(0, 10, 0, 20), (10, 20, 40, 50), (20, 30, 60, 70)]); } #[test] fn slide_keeps_the_length_whatever_the_rate() { for to in 1..40 { let (mut p, v) = lone(&[fast(0, 0, 4, 400), clip(10, 40, 50), fast(20, 10, 14, 400)]); let end = p.lane(v)[2].end(); let landed = p.slide(v, 1, to, w(&[100])).expect("neighbours"); let l = p.lane(v); assert_eq!((l[0].end(), l[1].start, l[1].end(), l[2].start, l[2].end()), (landed, landed, landed + 10, landed + 10, end), "slide to {to}"); } } }