From 88fbed0f96af61ae29980a214cef7c9f78625e21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 10 Aug 2026 23:44:28 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(timeline):=20a=20jumpcut=20rea?= =?UTF-8?q?ches=20as=20far=20as=20you=20tell=20it=20to?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A ripple used to be the whole timeline's business. It is a choice now: the card's first row says whether an apply reaches this take (its picture and its sound), this track alone, or every track -- and a track nobody named does not move, which is how a podcast track loses its silences while the music under it stays where it was. The engine takes an arbitrary set of lanes, not a kind or a range, so what "selected" means stays the front-end's business. It refuses, by name, a scope that would take one half of a take with it rather than quietly widening to the other half: widening would edit a track the user did not name, which is the surprise scoping exists to end. Naming the take is a row, and detaching is the other answer -- the refusal says both. The notice names what it reached: "5 SILENCES CUT on A1 — other tracks untouched". --- crates/app/src/main.rs | 186 +++++++++++++++++++++++++------- crates/engine/src/playback.rs | 31 ++++-- crates/engine/src/project.rs | 187 +++++++++++++++++++++++++-------- crates/engine/tests/silence.rs | 133 +++++++++++++++++++++-- 4 files changed, 434 insertions(+), 103 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 2b64199..30482e8 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -357,10 +357,49 @@ fn speed_at(permille: i32) -> Speed { Speed::from_permille(permille.clamp(0, i32::from(u16::MAX)) as u16) } -/// The silence card's rows, in the order it lists them: the four settings a -/// scan is told and the rate the speed-up plays at. What `silence_field` -/// indexes and what [`Player::nudge_silence`] moves. -const SILENCE_ROWS: usize = 5; +/// The silence card's rows, in the order it lists them: how wide the apply +/// reaches, the four settings a scan is told, and the rate the speed-up plays +/// at. What `silence_field` indexes and what [`Player::nudge_silence`] moves. +const SILENCE_ROWS: usize = 6; + +/// How wide a jumpcut reaches. A ripple used to be the whole timeline's +/// business and nothing else; it is a *choice* now, because a podcast track's +/// silences are not the music track's business -- and the choice has to be on +/// screen, because "everything after this moved" is not a thing to discover +/// afterwards. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Scope { + /// The lanes of the take the scanned clip belongs to: its picture and its + /// sound stay one take, and nothing else moves. The default, because a clip + /// picked on screen is what a person means by "this". + Take, + /// That clip's lane, alone. Refused by the engine, by name, while the take + /// has a half elsewhere -- detaching is how a person says they mean it. + Track, + /// Every lane, which is what a ripple always was and what the timeline-wide + /// jumpcut still is. + Everything, +} + +/// The order the card cycles them in. +const SCOPES: [Scope; 3] = [Scope::Take, Scope::Track, Scope::Everything]; + +impl Scope { + /// What the row says, given the lanes it works out to: the *names* of the + /// tracks, because "this take" means nothing until it says which two. + fn label(self, lanes: &[Lane]) -> String { + let named = lanes + .iter() + .map(|l| l.label()) + .collect::>() + .join("+"); + match self { + Scope::Take => format!("this take ({named})"), + Scope::Track => format!("this track ({named})"), + Scope::Everything => "every track".to_string(), + } + } +} /// One press of a nudge key on each kind of row: a dB on the threshold, a /// twentieth of a second on the three durations, and the speed card's own step @@ -606,6 +645,10 @@ struct Player { /// second run offers what the first one settled on. silence: engine::silence::Settings, silence_factor: Speed, + /// How wide the apply reaches ([`Scope`]). Kept across closes for the same + /// reason, and never *widened* on anyone's behalf: the whole point of it is + /// that a track nobody named does not move. + silence_scope: Scope, /// Which of the card's [`SILENCE_ROWS`] the arrow keys move. The card's own /// focus, since nothing in it takes gpui's (ledger:182). silence_field: usize, @@ -1259,27 +1302,65 @@ impl Player { .clamp(SILENCE_SECS_RANGE.0, SILENCE_SECS_RANGE.1) }; match self.silence_field { + // Round either way, like the fit policy's cycle: three choices are + // a ring, not a range. 0 => { + let at = SCOPES.iter().position(|&s| s == self.silence_scope); + let step = steps.rem_euclid(SCOPES.len() as i32) as usize; + self.silence_scope = SCOPES[(at.unwrap_or(0) + step) % SCOPES.len()]; + } + 1 => { self.silence.threshold_db = (self.silence.threshold_db + steps as f32 * SILENCE_DB_STEP) .clamp(SILENCE_DB_RANGE.0, SILENCE_DB_RANGE.1) } - 1 => self.silence.min_silence = secs(self.silence.min_silence), - 2 => self.silence.padding = secs(self.silence.padding), - 3 => self.silence.min_keep = secs(self.silence.min_keep), + 2 => self.silence.min_silence = secs(self.silence.min_silence), + 3 => self.silence.padding = secs(self.silence.padding), + 4 => self.silence.min_keep = secs(self.silence.min_keep), _ => { self.silence_factor = silence_rate(i32::from(self.silence_factor.permille()) + steps * SPEED_STEP) } } - // The rate is not part of the scan, but re-running is cheap (the levels - // are cached) and one path is one place for the marks to come from. + // Neither the scope nor the rate is part of the scan, but re-running is + // cheap (the levels are cached) and one path is one place for the marks + // to come from. self.scan_silences(); } - /// What an apply acts on: the previewed set, or nothing at all with a - /// notice saying so in the numbers that found nothing. - fn previewed(&mut self) -> Option> { + /// Which lanes an apply reaches, as the card's scope row says it: the + /// lanes of the take the scanned clip belongs to, that clip's lane alone, + /// or every lane there is. + /// + /// The take's lanes are the ones carrying its group id -- a link is one + /// span on however many lanes, so "the take" is exactly the set of lanes + /// that would otherwise be pulled apart. Nothing widens behind the user's + /// back: [`Project::cut_regions`] refuses a scope that would split a take, + /// and this row is how the user says the take instead. + fn silence_lanes(&self) -> Vec { + let (Some((lane, idx)), Some(session)) = (self.silence_open, self.session.as_ref()) else { + return Vec::new(); + }; + match self.silence_scope { + Scope::Track => vec![lane], + Scope::Everything => session.lanes(), + Scope::Take => match session.lane_clips(lane).get(idx).and_then(|c| c.link) { + None => vec![lane], + Some(id) => session + .lanes() + .into_iter() + .filter(|&l| { + l == lane || session.lane_clips(l).iter().any(|c| c.link == Some(id)) + }) + .collect(), + }, + } + } + + /// What an apply acts on: the previewed set and the lanes it reaches, or + /// nothing at all with a notice saying so in the numbers that found + /// nothing. + fn previewed(&mut self) -> Option<(Vec<(u32, u32)>, Vec)> { if self.silence_marks.is_empty() { self.notice = Some( format!( @@ -1290,60 +1371,81 @@ impl Player { ); return None; } - Some(self.silence_marks.clone()) + Some((self.silence_marks.clone(), self.silence_lanes())) } - /// Cuts every previewed silence out of every lane, rippling each hole - /// closed -- one edit and **one** undo press however many there were - /// ([`engine::PlaybackSession::cut_regions`]). The linked picture slides - /// with its sound, because a ripple is uniform across the lanes. + /// What an apply says afterwards: which tracks it reached, and -- when that + /// was not all of them -- that the rest were left where they were. The + /// scope is a choice, so the confirmation has to name the choice. + fn silence_reach(&self, lanes: &[Lane]) -> String { + let named = lanes + .iter() + .map(|l| l.label()) + .collect::>() + .join("+"); + match self.silence_scope { + Scope::Everything => "on every track".to_string(), + _ => format!("on {named} — other tracks untouched"), + } + } + + /// Cuts every previewed silence out of the lanes the scope names, rippling + /// each hole closed -- one edit and **one** undo press however many there + /// were ([`engine::PlaybackSession::cut_regions`]). Tracks outside the + /// scope do not move; a scope that would take half a take with it comes + /// back refused in the engine's own words, naming both halves. fn cut_silences(&mut self, cx: &mut Context) { - let Some(regions) = self.previewed() else { + let Some((regions, lanes)) = self.previewed() else { cx.notify(); return; }; let saved = f64::from(regions.iter().map(|&(_, len)| len).sum::()) / self.fps; - let count = regions.len(); - let cut = self - .session - .as_mut() - .is_some_and(|session| session.cut_regions(®ions)); - if cut { - self.close_silence(); - self.reset_after_reseek(); - self.notice = Some( - format!( - "{count} SILENCES CUT — {saved:.1}s shorter, {} takes it back", - self.keymap.display(ActionId::Undo) - ) - .into(), - ); + let (count, reach) = (regions.len(), self.silence_reach(&lanes)); + let Some(session) = self.session.as_mut() else { + cx.notify(); + return; + }; + match session.cut_regions(®ions, &lanes) { + Ok(()) => { + self.close_silence(); + self.reset_after_reseek(); + self.notice = Some( + format!( + "{count} SILENCES CUT {reach} — {saved:.1}s shorter, {} takes it back", + self.keymap.display(ActionId::Undo) + ) + .into(), + ); + } + Err(e) => self.notice = Some(e.to_string().into()), } cx.notify(); } /// Plays them fast instead of cutting them, closing the room each one no - /// longer needs. One undo press like the cut; the refusal (a clip on - /// another lane lapping over a silence) comes back in the engine's own - /// words and names the lane and frame, and the card stays up so the numbers - /// that produced it are still on screen. + /// longer needs. One undo press like the cut, and the same scope; the + /// refusals (a clip lapping over a silence, a scope that would split a + /// take) come back in the engine's own words and name the lane and frame, + /// and the card stays up so the numbers that produced it are still on + /// screen. fn speed_silences(&mut self, cx: &mut Context) { - let Some(regions) = self.previewed() else { + let Some((regions, lanes)) = self.previewed() else { cx.notify(); return; }; let (count, rate) = (regions.len(), self.silence_factor); + let reach = self.silence_reach(&lanes); let Some(session) = self.session.as_mut() else { cx.notify(); return; }; - match session.speed_regions(®ions, rate) { + match session.speed_regions(®ions, rate, &lanes) { Ok(()) => { self.close_silence(); self.reset_after_reseek(); self.notice = Some( format!( - "{count} SILENCES AT {rate} — {} takes it back", + "{count} SILENCES AT {rate} {reach} — {} takes it back", self.keymap.display(ActionId::Undo) ) .into(), @@ -4447,6 +4549,7 @@ impl Player { let (lane, idx) = self.silence_open?; let cfg = self.silence; let rows = [ + ("Apply to", self.silence_scope.label(&self.silence_lanes())), ("Silence is under", format!("{:.0} dBFS", cfg.threshold_db)), ( "Forgive quiet shorter than", @@ -8621,6 +8724,9 @@ fn main() { // scan that leaves a little too much is one nobody undoes. silence: engine::silence::Settings::default(), silence_factor: Speed::MAX, + // The take, not the timeline: the narrower answer is the + // one a person can widen on purpose. + silence_scope: Scope::Take, silence_field: 0, silence_marks: Vec::new(), silence_levels: None, diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 61eeea5..19aa574 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -1004,23 +1004,32 @@ impl PlaybackSession { } /// Cuts every one of `regions` -- `(start, len)` in timeline frames -- out - /// of every lane and closes each hole, as **one** edit + /// of the lanes in `scope` and closes each hole, as **one** edit /// ([`Project::cut_regions`]): the jumpcut a silence scan /// ([`crate::silence`]) asks for, one undo press however many silences it - /// found. Reseeks like every other edit, so what plays is the cut timeline - /// from the playhead on. `false`, and no undo step, for an empty list. - pub fn cut_regions(&mut self, regions: &[(u32, u32)]) -> bool { - self.edit(|p| p.cut_regions(regions)) + /// found. A lane outside `scope` does not move. Reseeks like every other + /// edit, so what plays is the cut timeline from the playhead on. The `Err` + /// names what is in the way and nothing changes. + pub fn cut_regions(&mut self, regions: &[(u32, u32)], scope: &[Lane]) -> crate::Result<()> { + // Spelled out rather than through `edit`, whose closure hands back a + // bool and would drop the refusal's own words. + self.project.cut_regions(regions, scope)?; + let now = self.now(); + self.seek(now); + Ok(()) } /// Plays every one of those regions at `speed` instead of cutting them, /// closing the room each one no longer needs ([`Project::speed_regions`]) -- - /// one edit, one undo press. The `Err` names the lane and frame in the way - /// and nothing changes. - pub fn speed_regions(&mut self, regions: &[(u32, u32)], speed: Speed) -> crate::Result<()> { - // Spelled out rather than through `edit`, whose closure hands back a - // bool and would drop the refusal's own words. - self.project.speed_regions(regions, speed)?; + /// one edit, one undo press, and the same scope rule. The `Err` names the + /// lane and frame in the way and nothing changes. + pub fn speed_regions( + &mut self, + regions: &[(u32, u32)], + speed: Speed, + scope: &[Lane], + ) -> crate::Result<()> { + self.project.speed_regions(regions, speed, scope)?; let now = self.now(); self.seek(now); Ok(()) diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index be98983..9d1b789 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -1306,17 +1306,24 @@ impl Project { /// /// Metadata only: no mapping changes, in any lane. pub fn split(&mut self, timeline_frame: u32) -> bool { - self.write_split(timeline_frame, true) + self.write_split(timeline_frame, true, &all_lanes(&self.lanes)) } - /// [`split`](Project::split), with the snapshot optional: a batch that has - /// already taken one ([`speed_regions`](Project::speed_regions)) splits at - /// two frames per region and must still be one undo press. - fn write_split(&mut self, timeline_frame: u32, snapshot: bool) -> bool { + /// [`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> = self .lanes .iter() - .map(|l| splittable(&l.clips, timeline_frame).map(|idx| l.clips[idx])) + .enumerate() + .map(|(i, l)| { + on.contains(&i) + .then(|| splittable(&l.clips, timeline_frame).map(|idx| l.clips[idx])) + .flatten() + }) .collect(); if cut.iter().all(Option::is_none) { return false; @@ -1330,8 +1337,16 @@ impl Project { let ends: Vec> = cut.iter().map(|c| c.map(|c| c.end())).collect(); let left = self.group_ids(&starts); let right = self.group_ids(&ends); - for ((data, left), right) in self.lanes.iter_mut().zip(left).zip(right) { - let Some(idx) = splittable(&data.clips, timeline_frame) else { + for (((data, left), right), cut) in self + .lanes + .iter_mut() + .zip(left) + .zip(right) + .zip(cut.iter().map(Option::is_some)) + { + // 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) else { continue; }; let mut tail = data.clips[idx]; @@ -1870,13 +1885,21 @@ impl Project { return false; } self.snapshot(); - ripple(&mut self.lanes, at, len); + let on = all_lanes(&self.lanes); + ripple(&mut self.lanes, &on, at, len); true } /// Cut **every** one of `regions` -- `(start, len)` in timeline frames -- - /// out of every lane and close each hole, as one edit: the jumpcut a - /// silence scan asks for ([`crate::silence`]). + /// 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 @@ -1886,19 +1909,74 @@ impl Project { /// /// 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 -- and `false`, with no undo step, for a list with - /// nothing in it. - pub fn cut_regions(&mut self, regions: &[(u32, u32)]) -> bool { + /// 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); - if regions.is_empty() { - return false; - } + 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() { - ripple(&mut self.lanes, at, len); + ripple(&mut self.lanes, &on, at, len); } debug_assert!(links_are_consistent(&self.lanes).is_ok()); - true + 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> { + let on: Vec = 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 link is one span on however + /// many lanes**, so a batch that moves one half of a take and not the other + /// would leave a group whose halves disagree -- a project no save could + /// load ([`links_are_consistent`]). + /// + /// 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); + for &i in on { + for c in self.lanes[i].clips.iter().filter(|c| c.end() > from) { + let Some(id) = c.link else { continue }; + for (j, data) in self.lanes.iter().enumerate() { + if on.contains(&j) { + continue; + } + if let Some(half) = data.clips.iter().find(|o| o.link == Some(id)) { + return Err(format!( + "the {} clip at frame {} is one take with the {} clip at frame {}: \ + moving one track of a take would pull it apart -- take the whole take, \ + or detach them first", + labels[i].label(), + c.start, + labels[j].label(), + half.start + ) + .into()); + } + } + } + } + Ok(()) } /// Play every one of `regions` at `speed` instead of cutting it: each is @@ -1912,11 +1990,17 @@ impl Project { /// 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: /// - /// * a clip on any 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; + /// * 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 @@ -1924,19 +2008,26 @@ impl Project { /// [`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) -> crate::Result<()> { + pub fn speed_regions( + &mut self, + regions: &[(u32, u32)], + speed: Speed, + scope: &[Lane], + ) -> crate::Result<()> { let regions = tidy(regions); - if regions.is_empty() { + 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)?; 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, data) in self.lanes.iter().enumerate() { - for c in &data.clips { + 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!( @@ -1958,12 +2049,13 @@ impl Project { // refusal still costs no step (`undo` pops the one just pushed). self.snapshot(); for &(at, len) in regions.iter().rev() { - self.write_split(at, false); - self.write_split(at + len, false); + 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, data) in self.lanes.iter().enumerate() { - for c in data.clips.iter().filter(|c| c.end() > at && c.start < at + len) { + 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 @@ -2015,15 +2107,15 @@ impl Project { ) .into()); }; - for data in &mut self.lanes { - for c in &mut data.clips { + 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(&data.clips)); + debug_assert!(sorted_disjoint(&self.lanes[l].clips)); } } debug_assert!(links_are_consistent(&self.lanes).is_ok()); @@ -2466,20 +2558,29 @@ fn clear(clips: &mut Vec, start: u32, end: u32) { *clips = out; } -/// Cuts `[at, at + len)` out of every lane and slides what is behind it back -- -/// the body of [`Project::ripple_delete`], and of every cut a batch -/// ([`Project::cut_regions`]) makes. No snapshot of its own: whose undo step -/// this is belongs to the caller. -fn ripple(lanes: &mut [LaneData], at: u32, len: u32) { - for data in lanes { - clear(&mut data.clips, at, at + len); - for c in data.clips.iter_mut().filter(|c| c.start >= at) { +/// 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(&data.clips)); + debug_assert!(sorted_disjoint(clips)); } } +/// 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 { + (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. /// diff --git a/crates/engine/tests/silence.rs b/crates/engine/tests/silence.rs index dc40fdf..fc5a181 100644 --- a/crates/engine/tests/silence.rs +++ b/crates/engine/tests/silence.rs @@ -58,7 +58,8 @@ fn a_batch_of_cuts_is_one_edit_and_moves_every_lane_alike() { assert!(p.split(60)); let before = p.lane(Lane::V1).to_vec(); let regions = [(10, 5), (30, 5), (50, 5), (70, 5), (90, 5)]; - assert!(p.cut_regions(®ions)); + p.cut_regions(®ions, &p.lanes().clone()) + .expect("every lane is in scope"); let cut: u32 = regions.iter().map(|&(_, len)| len).sum(); assert_eq!(p.timeline_frames(), was - cut); @@ -85,7 +86,8 @@ fn speeding_silences_up_closes_the_room_they_no_longer_need() { let mut p = fixture(); let was = p.timeline_frames(); let regions = [(30, 20), (80, 20)]; - p.speed_regions(®ions, Speed::MAX).expect("no lane laps"); + p.speed_regions(®ions, Speed::MAX, &p.lanes().clone()) + .expect("no lane laps"); // 20 source frames at 4x occupy 5, so each region gives back 15. let shrunk = Speed::MAX.frames(20); @@ -114,7 +116,8 @@ fn speeding_silences_up_closes_the_room_they_no_longer_need() { // now leaves the timeline byte for byte as it was -- 4x set twice is 4x. let settled = p.lane(Lane::V1).to_vec(); let again: Vec<(u32, u32)> = fast.iter().map(|&at| (at, shrunk)).collect(); - p.speed_regions(&again, Speed::MAX).expect("still no lap"); + p.speed_regions(&again, Speed::MAX, &p.lanes().clone()) + .expect("still no lap"); assert_eq!(p.lane(Lane::V1), settled, "a second pass compounded"); assert!(p.undo()); @@ -148,7 +151,7 @@ fn speed_mode_refuses_a_clip_that_laps_over_a_silence() { let before = spans(&p); let err = p - .speed_regions(&[(30, 20)], Speed::MAX) + .speed_regions(&[(30, 20)], Speed::MAX, &p.lanes().clone()) .expect_err("a half-covered region must not be sped up") .to_string(); assert!(err.contains("V2"), "{err}"); @@ -161,10 +164,117 @@ fn speed_mode_refuses_a_clip_that_laps_over_a_silence() { // The same regions cut instead: a ripple is uniform, so nothing laps. assert!(p.place(lane, 40, broll)); - assert!(p.cut_regions(&[(30, 20)])); + p.cut_regions(&[(30, 20)], &p.lanes().clone()) + .expect("a ripple is uniform"); loadable(&p); } +/// A clip covering the whole fixture, for a second track to carry. +fn whole(source: usize, frames: u32) -> Clip { + Clip { + start: 0, + in_frame: 0, + out_frame: frames, + source, + link: None, + eq: None, + color: None, + fit: Default::default(), + speed: Speed::NORMAL, + } +} + +/// The scope is the point: cutting a take's silences must not drag the music +/// track under it back. The lanes that were named lose exactly what was cut; +/// the lane that was not is byte for byte what it was, and one press still +/// takes the whole batch back. +#[test] +fn a_scoped_cut_leaves_the_track_it_was_not_given_alone() { + let mut p = fixture(); + let frames = p.timeline_frames(); + let music = p.add_lane(LaneKind::Audio); + assert_eq!(music.label(), "A2"); + assert!(p.place(music, 0, whole(0, frames))); + let untouched = p.lane(music).to_vec(); + + // The take's own two lanes, and not the third. + let take = [Lane::V1, Lane::A1]; + let regions = [(10, 5), (30, 5)]; + p.cut_regions(®ions, &take) + .expect("the take is whole inside the scope"); + + assert_eq!(p.lane(music), untouched, "the music track moved"); + let cut: u32 = regions.iter().map(|&(_, len)| len).sum(); + for lane in take { + assert_eq!( + p.lane(lane).last().expect("clips").end(), + frames - cut, + "{} did not lose what was cut", + lane.label() + ); + } + // The timeline is as long as its longest lane, which is now the music. + assert_eq!(p.timeline_frames(), frames); + loadable(&p); + + assert!(p.undo()); + assert_eq!(p.lane(Lane::V1).last().expect("clips").end(), frames); + assert_eq!(p.lane(Lane::V1).len(), 1, "one press took the whole batch"); +} + +/// One track of a take is refused **by name**, in both modes: moving the sound +/// of a shot and not its picture leaves a group whose halves disagree about +/// their span -- a project no save could load. The refusal says which two clips +/// and what to do, and detaching them is exactly what makes the same call work. +#[test] +fn one_track_of_a_take_is_refused_by_name_until_it_is_detached() { + let mut p = fixture(); + let before = spans(&p); + let err = p + .cut_regions(&[(10, 5)], &[Lane::A1]) + .expect_err("half a take must not be cut alone") + .to_string(); + assert!(err.contains("A1") && err.contains("V1"), "{err}"); + assert!(err.contains("detach"), "{err}"); + // Speed mode lives under the same law and says the same thing. + let same = p + .speed_regions(&[(30, 20)], Speed::MAX, &[Lane::A1]) + .expect_err("half a take must not be re-rated alone") + .to_string(); + assert!(same.contains("A1") && same.contains("V1"), "{same}"); + assert_eq!(spans(&p), before, "a refusal changed the timeline"); + assert!(!p.undo(), "a refusal cost an undo step"); + + // Detached, the sound is its own to cut -- and the picture stays put. + assert!(p.ungroup(Lane::A1, 0)); + let picture = p.lane(Lane::V1).to_vec(); + p.cut_regions(&[(10, 5)], &[Lane::A1]) + .expect("a detached half is one track"); + assert_eq!(p.lane(Lane::V1), picture, "the picture followed anyway"); + assert_eq!(p.lane(Lane::A1).last().expect("clips").end(), 145); + loadable(&p); +} + +/// Scoping every lane is the ripple this always was: the same cuts through the +/// batch and through `ripple_delete` one at a time land on the same timeline. +#[test] +fn every_lane_in_scope_is_the_ripple_it_always_was() { + let regions = [(10, 5), (30, 5), (50, 5)]; + let mut batch = fixture(); + let all = batch.lanes(); + batch.cut_regions(®ions, &all).expect("every lane"); + + let mut one_at_a_time = fixture(); + for &(at, len) in regions.iter().rev() { + assert!(one_at_a_time.ripple_delete(at, len)); + } + assert_eq!(batch.lane(Lane::V1), one_at_a_time.lane(Lane::V1)); + assert_eq!(batch.lane(Lane::A1), one_at_a_time.lane(Lane::A1)); + // ...at one undo step against three. + assert!(batch.undo()); + assert!(!batch.undo()); +} + /// A rate that cannot address an edge of a silence is refused naming **that** /// edge -- not the region's start, which at a slow rate is often the one frame /// that *was* addressable. A cut at the wrong frame is a user sent to look in @@ -175,16 +285,20 @@ fn speed_mode_refuses_a_clip_that_laps_over_a_silence() { #[test] fn an_unaddressable_edge_is_named_where_it_actually_is() { let mut p = fixture(); - p.set_speed(Lane::V1, 0, Speed::MIN).expect("nothing after it"); + p.set_speed(Lane::V1, 0, Speed::MIN) + .expect("nothing after it"); let before = spans(&p); // Starts where the clip starts -- no split needed there -- and ends at a // frame a quarter-speed clip cannot be cut at. let err = p - .speed_regions(&[(0, 5)], Speed::MAX) + .speed_regions(&[(0, 5)], Speed::MAX, &p.lanes().clone()) .expect_err("frame 5 is not a cut this rate can make") .to_string(); assert!(err.contains("frame 5"), "{err}"); - assert!(!err.contains("frame 0:"), "it named the region's start: {err}"); + assert!( + !err.contains("frame 0:"), + "it named the region's start: {err}" + ); // Rolled back whole, and the refusal cost no undo step: the press below is // the `set_speed` above. assert_eq!(spans(&p), before); @@ -233,7 +347,8 @@ fn scanning_a_clip_and_cutting_what_it_finds() { assert!(!p.undo(), "the scan took an undo step"); let cut: u32 = regions.iter().map(|&(_, len)| len).sum(); - assert!(p.cut_regions(®ions)); + p.cut_regions(®ions, &p.lanes().clone()) + .expect("every lane is in scope"); assert_eq!(p.timeline_frames(), was - cut); let (video, audio_spans) = spans(&p); assert_eq!(video, audio_spans);