forge
Explore
Log in

Don't you dare go Hollow

A jumpcut reaches as far as you tell it to

Browse files at this commit

88fbed0f96af61ae29980a214cef7c9f78625e21Ahmet Kaan GümüşParent: 564ba30
  • feat
    • a jumpcut reaches as far as you tell it to
Show full message
✨ feat(timeline): a jumpcut reaches as far as you tell it to

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.rsmodified+146 -40
@@ -357,10 +357,49 @@ fn speed_at(permille: i32) -> Speed {
357357 Speed::from_permille(permille.clamp(0, i32::from(u16::MAX)) as u16)
358358 }
359359
360-/// The silence card's rows, in the order it lists them: the four settings a
361-/// scan is told and the rate the speed-up plays at. What `silence_field`
362-/// indexes and what [`Player::nudge_silence`] moves.
363-const SILENCE_ROWS: usize = 5;
360+/// The silence card's rows, in the order it lists them: how wide the apply
361+/// reaches, the four settings a scan is told, and the rate the speed-up plays
362+/// at. What `silence_field` indexes and what [`Player::nudge_silence`] moves.
363+const SILENCE_ROWS: usize = 6;
364+
365+/// How wide a jumpcut reaches. A ripple used to be the whole timeline's
366+/// business and nothing else; it is a *choice* now, because a podcast track's
367+/// silences are not the music track's business -- and the choice has to be on
368+/// screen, because "everything after this moved" is not a thing to discover
369+/// afterwards.
370+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
371+enum Scope {
372+ /// The lanes of the take the scanned clip belongs to: its picture and its
373+ /// sound stay one take, and nothing else moves. The default, because a clip
374+ /// picked on screen is what a person means by "this".
375+ Take,
376+ /// That clip's lane, alone. Refused by the engine, by name, while the take
377+ /// has a half elsewhere -- detaching is how a person says they mean it.
378+ Track,
379+ /// Every lane, which is what a ripple always was and what the timeline-wide
380+ /// jumpcut still is.
381+ Everything,
382+}
383+
384+/// The order the card cycles them in.
385+const SCOPES: [Scope; 3] = [Scope::Take, Scope::Track, Scope::Everything];
386+
387+impl Scope {
388+ /// What the row says, given the lanes it works out to: the *names* of the
389+ /// tracks, because "this take" means nothing until it says which two.
390+ fn label(self, lanes: &[Lane]) -> String {
391+ let named = lanes
392+ .iter()
393+ .map(|l| l.label())
394+ .collect::<Vec<_>>()
395+ .join("+");
396+ match self {
397+ Scope::Take => format!("this take ({named})"),
398+ Scope::Track => format!("this track ({named})"),
399+ Scope::Everything => "every track".to_string(),
400+ }
401+ }
402+}
364403
365404 /// One press of a nudge key on each kind of row: a dB on the threshold, a
366405 /// twentieth of a second on the three durations, and the speed card's own step
@@ -606,6 +645,10 @@ struct Player {
606645 /// second run offers what the first one settled on.
607646 silence: engine::silence::Settings,
608647 silence_factor: Speed,
648+ /// How wide the apply reaches ([`Scope`]). Kept across closes for the same
649+ /// reason, and never *widened* on anyone's behalf: the whole point of it is
650+ /// that a track nobody named does not move.
651+ silence_scope: Scope,
609652 /// Which of the card's [`SILENCE_ROWS`] the arrow keys move. The card's own
610653 /// focus, since nothing in it takes gpui's (ledger:182).
611654 silence_field: usize,
@@ -1259,27 +1302,65 @@ impl Player {
12591302 .clamp(SILENCE_SECS_RANGE.0, SILENCE_SECS_RANGE.1)
12601303 };
12611304 match self.silence_field {
1305+ // Round either way, like the fit policy's cycle: three choices are
1306+ // a ring, not a range.
12621307 0 => {
1308+ let at = SCOPES.iter().position(|&s| s == self.silence_scope);
1309+ let step = steps.rem_euclid(SCOPES.len() as i32) as usize;
1310+ self.silence_scope = SCOPES[(at.unwrap_or(0) + step) % SCOPES.len()];
1311+ }
1312+ 1 => {
12631313 self.silence.threshold_db = (self.silence.threshold_db
12641314 + steps as f32 * SILENCE_DB_STEP)
12651315 .clamp(SILENCE_DB_RANGE.0, SILENCE_DB_RANGE.1)
12661316 }
1267- 1 => self.silence.min_silence = secs(self.silence.min_silence),
1268- 2 => self.silence.padding = secs(self.silence.padding),
1269- 3 => self.silence.min_keep = secs(self.silence.min_keep),
1317+ 2 => self.silence.min_silence = secs(self.silence.min_silence),
1318+ 3 => self.silence.padding = secs(self.silence.padding),
1319+ 4 => self.silence.min_keep = secs(self.silence.min_keep),
12701320 _ => {
12711321 self.silence_factor =
12721322 silence_rate(i32::from(self.silence_factor.permille()) + steps * SPEED_STEP)
12731323 }
12741324 }
1275- // The rate is not part of the scan, but re-running is cheap (the levels
1276- // are cached) and one path is one place for the marks to come from.
1325+ // Neither the scope nor the rate is part of the scan, but re-running is
1326+ // cheap (the levels are cached) and one path is one place for the marks
1327+ // to come from.
12771328 self.scan_silences();
12781329 }
12791330
1280- /// What an apply acts on: the previewed set, or nothing at all with a
1281- /// notice saying so in the numbers that found nothing.
1282- fn previewed(&mut self) -> Option<Vec<(u32, u32)>> {
1331+ /// Which lanes an apply reaches, as the card's scope row says it: the
1332+ /// lanes of the take the scanned clip belongs to, that clip's lane alone,
1333+ /// or every lane there is.
1334+ ///
1335+ /// The take's lanes are the ones carrying its group id -- a link is one
1336+ /// span on however many lanes, so "the take" is exactly the set of lanes
1337+ /// that would otherwise be pulled apart. Nothing widens behind the user's
1338+ /// back: [`Project::cut_regions`] refuses a scope that would split a take,
1339+ /// and this row is how the user says the take instead.
1340+ fn silence_lanes(&self) -> Vec<Lane> {
1341+ let (Some((lane, idx)), Some(session)) = (self.silence_open, self.session.as_ref()) else {
1342+ return Vec::new();
1343+ };
1344+ match self.silence_scope {
1345+ Scope::Track => vec![lane],
1346+ Scope::Everything => session.lanes(),
1347+ Scope::Take => match session.lane_clips(lane).get(idx).and_then(|c| c.link) {
1348+ None => vec![lane],
1349+ Some(id) => session
1350+ .lanes()
1351+ .into_iter()
1352+ .filter(|&l| {
1353+ l == lane || session.lane_clips(l).iter().any(|c| c.link == Some(id))
1354+ })
1355+ .collect(),
1356+ },
1357+ }
1358+ }
1359+
1360+ /// What an apply acts on: the previewed set and the lanes it reaches, or
1361+ /// nothing at all with a notice saying so in the numbers that found
1362+ /// nothing.
1363+ fn previewed(&mut self) -> Option<(Vec<(u32, u32)>, Vec<Lane>)> {
12831364 if self.silence_marks.is_empty() {
12841365 self.notice = Some(
12851366 format!(
@@ -1290,60 +1371,81 @@ impl Player {
12901371 );
12911372 return None;
12921373 }
1293- Some(self.silence_marks.clone())
1374+ Some((self.silence_marks.clone(), self.silence_lanes()))
12941375 }
12951376
1296- /// Cuts every previewed silence out of every lane, rippling each hole
1297- /// closed -- one edit and **one** undo press however many there were
1298- /// ([`engine::PlaybackSession::cut_regions`]). The linked picture slides
1299- /// with its sound, because a ripple is uniform across the lanes.
1377+ /// What an apply says afterwards: which tracks it reached, and -- when that
1378+ /// was not all of them -- that the rest were left where they were. The
1379+ /// scope is a choice, so the confirmation has to name the choice.
1380+ fn silence_reach(&self, lanes: &[Lane]) -> String {
1381+ let named = lanes
1382+ .iter()
1383+ .map(|l| l.label())
1384+ .collect::<Vec<_>>()
1385+ .join("+");
1386+ match self.silence_scope {
1387+ Scope::Everything => "on every track".to_string(),
1388+ _ => format!("on {named} — other tracks untouched"),
1389+ }
1390+ }
1391+
1392+ /// Cuts every previewed silence out of the lanes the scope names, rippling
1393+ /// each hole closed -- one edit and **one** undo press however many there
1394+ /// were ([`engine::PlaybackSession::cut_regions`]). Tracks outside the
1395+ /// scope do not move; a scope that would take half a take with it comes
1396+ /// back refused in the engine's own words, naming both halves.
13001397 fn cut_silences(&mut self, cx: &mut Context<Self>) {
1301- let Some(regions) = self.previewed() else {
1398+ let Some((regions, lanes)) = self.previewed() else {
13021399 cx.notify();
13031400 return;
13041401 };
13051402 let saved = f64::from(regions.iter().map(|&(_, len)| len).sum::<u32>()) / self.fps;
1306- let count = regions.len();
1307- let cut = self
1308- .session
1309- .as_mut()
1310- .is_some_and(|session| session.cut_regions(&regions));
1311- if cut {
1312- self.close_silence();
1313- self.reset_after_reseek();
1314- self.notice = Some(
1315- format!(
1316- "{count} SILENCES CUT — {saved:.1}s shorter, {} takes it back",
1317- self.keymap.display(ActionId::Undo)
1318- )
1319- .into(),
1320- );
1403+ let (count, reach) = (regions.len(), self.silence_reach(&lanes));
1404+ let Some(session) = self.session.as_mut() else {
1405+ cx.notify();
1406+ return;
1407+ };
1408+ match session.cut_regions(&regions, &lanes) {
1409+ Ok(()) => {
1410+ self.close_silence();
1411+ self.reset_after_reseek();
1412+ self.notice = Some(
1413+ format!(
1414+ "{count} SILENCES CUT {reach} — {saved:.1}s shorter, {} takes it back",
1415+ self.keymap.display(ActionId::Undo)
1416+ )
1417+ .into(),
1418+ );
1419+ }
1420+ Err(e) => self.notice = Some(e.to_string().into()),
13211421 }
13221422 cx.notify();
13231423 }
13241424
13251425 /// Plays them fast instead of cutting them, closing the room each one no
1326- /// longer needs. One undo press like the cut; the refusal (a clip on
1327- /// another lane lapping over a silence) comes back in the engine's own
1328- /// words and names the lane and frame, and the card stays up so the numbers
1329- /// that produced it are still on screen.
1426+ /// longer needs. One undo press like the cut, and the same scope; the
1427+ /// refusals (a clip lapping over a silence, a scope that would split a
1428+ /// take) come back in the engine's own words and name the lane and frame,
1429+ /// and the card stays up so the numbers that produced it are still on
1430+ /// screen.
13301431 fn speed_silences(&mut self, cx: &mut Context<Self>) {
1331- let Some(regions) = self.previewed() else {
1432+ let Some((regions, lanes)) = self.previewed() else {
13321433 cx.notify();
13331434 return;
13341435 };
13351436 let (count, rate) = (regions.len(), self.silence_factor);
1437+ let reach = self.silence_reach(&lanes);
13361438 let Some(session) = self.session.as_mut() else {
13371439 cx.notify();
13381440 return;
13391441 };
1340- match session.speed_regions(&regions, rate) {
1442+ match session.speed_regions(&regions, rate, &lanes) {
13411443 Ok(()) => {
13421444 self.close_silence();
13431445 self.reset_after_reseek();
13441446 self.notice = Some(
13451447 format!(
1346- "{count} SILENCES AT {rate} — {} takes it back",
1448+ "{count} SILENCES AT {rate} {reach} — {} takes it back",
13471449 self.keymap.display(ActionId::Undo)
13481450 )
13491451 .into(),
@@ -4447,6 +4549,7 @@ impl Player {
44474549 let (lane, idx) = self.silence_open?;
44484550 let cfg = self.silence;
44494551 let rows = [
4552+ ("Apply to", self.silence_scope.label(&self.silence_lanes())),
44504553 ("Silence is under", format!("{:.0} dBFS", cfg.threshold_db)),
44514554 (
44524555 "Forgive quiet shorter than",
@@ -8621,6 +8724,9 @@ fn main() {
86218724 // scan that leaves a little too much is one nobody undoes.
86228725 silence: engine::silence::Settings::default(),
86238726 silence_factor: Speed::MAX,
8727+ // The take, not the timeline: the narrower answer is the
8728+ // one a person can widen on purpose.
8729+ silence_scope: Scope::Take,
86248730 silence_field: 0,
86258731 silence_marks: Vec::new(),
86268732 silence_levels: None,
crates/engine/src/playback.rsmodified+20 -11
@@ -1004,23 +1004,32 @@ impl PlaybackSession {
10041004 }
10051005
10061006 /// Cuts every one of `regions` -- `(start, len)` in timeline frames -- out
1007- /// of every lane and closes each hole, as **one** edit
1007+ /// of the lanes in `scope` and closes each hole, as **one** edit
10081008 /// ([`Project::cut_regions`]): the jumpcut a silence scan
10091009 /// ([`crate::silence`]) asks for, one undo press however many silences it
1010- /// found. Reseeks like every other edit, so what plays is the cut timeline
1011- /// from the playhead on. `false`, and no undo step, for an empty list.
1012- pub fn cut_regions(&mut self, regions: &[(u32, u32)]) -> bool {
1013- self.edit(|p| p.cut_regions(regions))
1010+ /// found. A lane outside `scope` does not move. Reseeks like every other
1011+ /// edit, so what plays is the cut timeline from the playhead on. The `Err`
1012+ /// names what is in the way and nothing changes.
1013+ pub fn cut_regions(&mut self, regions: &[(u32, u32)], scope: &[Lane]) -> crate::Result<()> {
1014+ // Spelled out rather than through `edit`, whose closure hands back a
1015+ // bool and would drop the refusal's own words.
1016+ self.project.cut_regions(regions, scope)?;
1017+ let now = self.now();
1018+ self.seek(now);
1019+ Ok(())
10141020 }
10151021
10161022 /// Plays every one of those regions at `speed` instead of cutting them,
10171023 /// closing the room each one no longer needs ([`Project::speed_regions`]) --
1018- /// one edit, one undo press. The `Err` names the lane and frame in the way
1019- /// and nothing changes.
1020- pub fn speed_regions(&mut self, regions: &[(u32, u32)], speed: Speed) -> crate::Result<()> {
1021- // Spelled out rather than through `edit`, whose closure hands back a
1022- // bool and would drop the refusal's own words.
1023- self.project.speed_regions(regions, speed)?;
1024+ /// one edit, one undo press, and the same scope rule. The `Err` names the
1025+ /// lane and frame in the way and nothing changes.
1026+ pub fn speed_regions(
1027+ &mut self,
1028+ regions: &[(u32, u32)],
1029+ speed: Speed,
1030+ scope: &[Lane],
1031+ ) -> crate::Result<()> {
1032+ self.project.speed_regions(regions, speed, scope)?;
10241033 let now = self.now();
10251034 self.seek(now);
10261035 Ok(())
crates/engine/src/project.rsmodified+144 -43
@@ -1306,17 +1306,24 @@ impl Project {
13061306 ///
13071307 /// Metadata only: no mapping changes, in any lane.
13081308 pub fn split(&mut self, timeline_frame: u32) -> bool {
1309- self.write_split(timeline_frame, true)
1309+ self.write_split(timeline_frame, true, &all_lanes(&self.lanes))
13101310 }
13111311
1312- /// [`split`](Project::split), with the snapshot optional: a batch that has
1313- /// already taken one ([`speed_regions`](Project::speed_regions)) splits at
1314- /// two frames per region and must still be one undo press.
1315- fn write_split(&mut self, timeline_frame: u32, snapshot: bool) -> bool {
1312+ /// [`split`](Project::split), with the snapshot optional and the lanes
1313+ /// named: a batch that has already taken a snapshot
1314+ /// ([`speed_regions`](Project::speed_regions)) splits at two frames per
1315+ /// region and must still be one undo press, and a *scoped* batch must not
1316+ /// cut a lane it was told to leave alone.
1317+ fn write_split(&mut self, timeline_frame: u32, snapshot: bool, on: &[usize]) -> bool {
13161318 let cut: Vec<Option<Clip>> = self
13171319 .lanes
13181320 .iter()
1319- .map(|l| splittable(&l.clips, timeline_frame).map(|idx| l.clips[idx]))
1321+ .enumerate()
1322+ .map(|(i, l)| {
1323+ on.contains(&i)
1324+ .then(|| splittable(&l.clips, timeline_frame).map(|idx| l.clips[idx]))
1325+ .flatten()
1326+ })
13201327 .collect();
13211328 if cut.iter().all(Option::is_none) {
13221329 return false;
@@ -1330,8 +1337,16 @@ impl Project {
13301337 let ends: Vec<Option<u32>> = cut.iter().map(|c| c.map(|c| c.end())).collect();
13311338 let left = self.group_ids(&starts);
13321339 let right = self.group_ids(&ends);
1333- for ((data, left), right) in self.lanes.iter_mut().zip(left).zip(right) {
1334- let Some(idx) = splittable(&data.clips, timeline_frame) else {
1340+ for (((data, left), right), cut) in self
1341+ .lanes
1342+ .iter_mut()
1343+ .zip(left)
1344+ .zip(right)
1345+ .zip(cut.iter().map(Option::is_some))
1346+ {
1347+ // The lanes this call was not scoped to are not cut, whatever their
1348+ // own clips would allow.
1349+ let Some(idx) = splittable(&data.clips, timeline_frame).filter(|_| cut) else {
13351350 continue;
13361351 };
13371352 let mut tail = data.clips[idx];
@@ -1870,13 +1885,21 @@ impl Project {
18701885 return false;
18711886 }
18721887 self.snapshot();
1873- ripple(&mut self.lanes, at, len);
1888+ let on = all_lanes(&self.lanes);
1889+ ripple(&mut self.lanes, &on, at, len);
18741890 true
18751891 }
18761892
18771893 /// Cut **every** one of `regions` -- `(start, len)` in timeline frames --
1878- /// out of every lane and close each hole, as one edit: the jumpcut a
1879- /// silence scan asks for ([`crate::silence`]).
1894+ /// out of the lanes in `scope` and close each hole, as one edit: the
1895+ /// jumpcut a silence scan asks for ([`crate::silence`]).
1896+ ///
1897+ /// A lane that is not in `scope` **does not move**, which is the whole
1898+ /// point of the parameter: the silences of a podcast track come out
1899+ /// without the music track under it sliding. `scope` is an arbitrary set of
1900+ /// lanes and not a kind or a range, so a caller with its own idea of what
1901+ /// is selected (a take, a track, everything) says it in lanes and nothing
1902+ /// here has to learn about selection.
18801903 ///
18811904 /// One [`snapshot`](Project::snapshot) for the lot, which is the whole
18821905 /// reason this exists rather than a caller's loop over
@@ -1886,19 +1909,74 @@ impl Project {
18861909 ///
18871910 /// The list is sorted and overlapping entries are merged before anything is
18881911 /// cut ([`tidy`]) -- two regions that overlap would otherwise cut each
1889- /// other's frames -- and `false`, with no undo step, for a list with
1890- /// nothing in it.
1891- pub fn cut_regions(&mut self, regions: &[(u32, u32)]) -> bool {
1912+ /// other's frames. Refused, with no undo step, for an empty list, for a
1913+ /// scope naming no lane that is there, and -- by name -- when the scope
1914+ /// would take one half of a take with it
1915+ /// ([`scope_holds_takes_whole`](Project::scope_holds_takes_whole)).
1916+ pub fn cut_regions(&mut self, regions: &[(u32, u32)], scope: &[Lane]) -> crate::Result<()> {
18921917 let regions = tidy(regions);
1893- if regions.is_empty() {
1894- return false;
1895- }
1918+ let on = self.scoped(scope)?;
1919+ let Some(&(first, _)) = regions.first() else {
1920+ return Err("nothing to cut".into());
1921+ };
1922+ self.scope_holds_takes_whole(&on, first)?;
18961923 self.snapshot();
18971924 for &(at, len) in regions.iter().rev() {
1898- ripple(&mut self.lanes, at, len);
1925+ ripple(&mut self.lanes, &on, at, len);
18991926 }
19001927 debug_assert!(links_are_consistent(&self.lanes).is_ok());
1901- true
1928+ Ok(())
1929+ }
1930+
1931+ /// The storage indices of `scope`, which is what every batch below walks.
1932+ /// `Err` for a scope that names nothing there rather than a silent no-op:
1933+ /// an edit asked for on a lane that is not there did not happen, and the
1934+ /// caller has to hear so.
1935+ fn scoped(&self, scope: &[Lane]) -> crate::Result<Vec<usize>> {
1936+ let on: Vec<usize> = scope.iter().filter_map(|&l| self.index(l)).collect();
1937+ match on.is_empty() {
1938+ true => Err("no track to work on".into()),
1939+ false => Ok(on),
1940+ }
1941+ }
1942+
1943+ /// The law a scoped ripple lives under: **a link is one span on however
1944+ /// many lanes**, so a batch that moves one half of a take and not the other
1945+ /// would leave a group whose halves disagree -- a project no save could
1946+ /// load ([`links_are_consistent`]).
1947+ ///
1948+ /// Refused by name rather than widened silently. Widening would edit a
1949+ /// track the user did not scope, which is exactly the surprise scoping
1950+ /// exists to end; a front-end that wants the take is free to *say* the
1951+ /// take's lanes, and to name them in what it tells the user afterwards.
1952+ ///
1953+ /// Only from `from` on: a take sitting entirely before the first cut does
1954+ /// not move, so it is not this rule's business.
1955+ fn scope_holds_takes_whole(&self, on: &[usize], from: u32) -> crate::Result<()> {
1956+ let labels = handles(&self.lanes);
1957+ for &i in on {
1958+ for c in self.lanes[i].clips.iter().filter(|c| c.end() > from) {
1959+ let Some(id) = c.link else { continue };
1960+ for (j, data) in self.lanes.iter().enumerate() {
1961+ if on.contains(&j) {
1962+ continue;
1963+ }
1964+ if let Some(half) = data.clips.iter().find(|o| o.link == Some(id)) {
1965+ return Err(format!(
1966+ "the {} clip at frame {} is one take with the {} clip at frame {}: \
1967+ moving one track of a take would pull it apart -- take the whole take, \
1968+ or detach them first",
1969+ labels[i].label(),
1970+ c.start,
1971+ labels[j].label(),
1972+ half.start
1973+ )
1974+ .into());
1975+ }
1976+ }
1977+ }
1978+ }
1979+ Ok(())
19021980 }
19031981
19041982 /// Play every one of `regions` at `speed` instead of cutting it: each is
@@ -1912,11 +1990,17 @@ impl Project {
19121990 /// twice over the same stretch leaves it at `speed`, not at `speed`
19131991 /// squared, so a second pass over an already-cut timeline changes nothing.
19141992 ///
1993+ /// Scoped exactly as [`cut_regions`](Project::cut_regions) is, and under
1994+ /// the same take law: a lane outside `scope` is neither re-rated nor moved.
1995+ ///
19151996 /// Refused by name, with nothing changed and no undo step, when:
19161997 ///
1917- /// * a clip on any lane covers only *part* of a region -- the lanes shrink
1918- /// by different amounts and the ripple would pull them apart, which is
1919- /// what cutting does not suffer and why delete mode has no such refusal;
1998+ /// * the scope would take one half of a take with it
1999+ /// ([`scope_holds_takes_whole`](Project::scope_holds_takes_whole));
2000+ /// * a clip on a scoped lane covers only *part* of a region -- the lanes
2001+ /// shrink by different amounts and the ripple would pull them apart,
2002+ /// which is what cutting does not suffer and why delete mode has no such
2003+ /// refusal;
19202004 /// * a rate cannot address a region's edge ([`Speed::split_at`]), so the
19212005 /// piece could not be split out exactly;
19222006 /// * two lanes' pieces would end up different lengths, which is a group
@@ -1924,19 +2008,26 @@ impl Project {
19242008 /// [`write_speed`](Project::write_speed) already makes, in its words);
19252009 /// * the region would grow rather than shrink, i.e. it already plays faster
19262010 /// than `speed`.
1927- pub fn speed_regions(&mut self, regions: &[(u32, u32)], speed: Speed) -> crate::Result<()> {
2011+ pub fn speed_regions(
2012+ &mut self,
2013+ regions: &[(u32, u32)],
2014+ speed: Speed,
2015+ scope: &[Lane],
2016+ ) -> crate::Result<()> {
19282017 let regions = tidy(regions);
1929- if regions.is_empty() {
2018+ let on = self.scoped(scope)?;
2019+ let Some(&(first, _)) = regions.first() else {
19302020 return Err("nothing to speed up".into());
1931- }
2021+ };
2022+ self.scope_holds_takes_whole(&on, first)?;
19322023 let labels = handles(&self.lanes);
19332024 // Before anything is touched, because this is the refusal a person can
19342025 // act on: a clip that laps over the edge of a silence has to be trimmed
19352026 // (or the silences cut instead), and saying so after a rollback would
19362027 // be saying it about a timeline that already looks different.
19372028 for &(at, len) in &regions {
1938- for (l, data) in self.lanes.iter().enumerate() {
1939- for c in &data.clips {
2029+ for &l in &on {
2030+ for c in &self.lanes[l].clips {
19402031 if c.end() > at && c.start < at + len && !(c.start <= at && c.end() >= at + len)
19412032 {
19422033 return Err(format!(
@@ -1958,12 +2049,13 @@ impl Project {
19582049 // refusal still costs no step (`undo` pops the one just pushed).
19592050 self.snapshot();
19602051 for &(at, len) in regions.iter().rev() {
1961- self.write_split(at, false);
1962- self.write_split(at + len, false);
2052+ self.write_split(at, false, &on);
2053+ self.write_split(at + len, false, &on);
19632054 let mut room: Option<(Lane, u32)> = None;
19642055 let mut refused = None;
1965- for (l, data) in self.lanes.iter().enumerate() {
1966- for c in data.clips.iter().filter(|c| c.end() > at && c.start < at + len) {
2056+ for &l in &on {
2057+ let clips = &self.lanes[l].clips;
2058+ for c in clips.iter().filter(|c| c.end() > at && c.start < at + len) {
19672059 if (c.start, c.end()) != (at, at + len) {
19682060 // *Which* edge did not come off: a rate that cannot
19692061 // address the end of the silence is not a rate that
@@ -2015,15 +2107,15 @@ impl Project {
20152107 )
20162108 .into());
20172109 };
2018- for data in &mut self.lanes {
2019- for c in &mut data.clips {
2110+ for &l in &on {
2111+ for c in &mut self.lanes[l].clips {
20202112 if c.start == at && c.end() == at + len {
20212113 c.speed = speed;
20222114 } else if c.start >= at + len {
20232115 c.start -= delta;
20242116 }
20252117 }
2026- debug_assert!(sorted_disjoint(&data.clips));
2118+ debug_assert!(sorted_disjoint(&self.lanes[l].clips));
20272119 }
20282120 }
20292121 debug_assert!(links_are_consistent(&self.lanes).is_ok());
@@ -2466,20 +2558,29 @@ fn clear(clips: &mut Vec<Clip>, start: u32, end: u32) {
24662558 *clips = out;
24672559 }
24682560
2469-/// Cuts `[at, at + len)` out of every lane and slides what is behind it back --
2470-/// the body of [`Project::ripple_delete`], and of every cut a batch
2471-/// ([`Project::cut_regions`]) makes. No snapshot of its own: whose undo step
2472-/// this is belongs to the caller.
2473-fn ripple(lanes: &mut [LaneData], at: u32, len: u32) {
2474- for data in lanes {
2475- clear(&mut data.clips, at, at + len);
2476- for c in data.clips.iter_mut().filter(|c| c.start >= at) {
2561+/// Cuts `[at, at + len)` out of the lanes at indices `on` and slides what is
2562+/// behind it back -- the body of [`Project::ripple_delete`] (which passes every
2563+/// lane) and of every cut a batch ([`Project::cut_regions`]) makes on the lanes
2564+/// it was scoped to. No snapshot of its own: whose undo step this is belongs to
2565+/// the caller.
2566+fn ripple(lanes: &mut [LaneData], on: &[usize], at: u32, len: u32) {
2567+ for &i in on {
2568+ let clips = &mut lanes[i].clips;
2569+ clear(clips, at, at + len);
2570+ for c in clips.iter_mut().filter(|c| c.start >= at) {
24772571 c.start -= len;
24782572 }
2479- debug_assert!(sorted_disjoint(&data.clips));
2573+ debug_assert!(sorted_disjoint(clips));
24802574 }
24812575 }
24822576
2577+/// Every lane, as the index list the two above take: what "the whole timeline"
2578+/// is spelled as, and the scope [`Project::ripple_delete`] and
2579+/// [`Project::split`] have always had.
2580+fn all_lanes(lanes: &[LaneData]) -> Vec<usize> {
2581+ (0..lanes.len()).collect()
2582+}
2583+
24832584 /// A caller's region list as a batch may use it: sorted by start, empty entries
24842585 /// dropped, and touching or overlapping entries merged into one.
24852586 ///
crates/engine/tests/silence.rsmodified+124 -9
@@ -58,7 +58,8 @@ fn a_batch_of_cuts_is_one_edit_and_moves_every_lane_alike() {
5858 assert!(p.split(60));
5959 let before = p.lane(Lane::V1).to_vec();
6060 let regions = [(10, 5), (30, 5), (50, 5), (70, 5), (90, 5)];
61- assert!(p.cut_regions(&regions));
61+ p.cut_regions(&regions, &p.lanes().clone())
62+ .expect("every lane is in scope");
6263
6364 let cut: u32 = regions.iter().map(|&(_, len)| len).sum();
6465 assert_eq!(p.timeline_frames(), was - cut);
@@ -85,7 +86,8 @@ fn speeding_silences_up_closes_the_room_they_no_longer_need() {
8586 let mut p = fixture();
8687 let was = p.timeline_frames();
8788 let regions = [(30, 20), (80, 20)];
88- p.speed_regions(&regions, Speed::MAX).expect("no lane laps");
89+ p.speed_regions(&regions, Speed::MAX, &p.lanes().clone())
90+ .expect("no lane laps");
8991
9092 // 20 source frames at 4x occupy 5, so each region gives back 15.
9193 let shrunk = Speed::MAX.frames(20);
@@ -114,7 +116,8 @@ fn speeding_silences_up_closes_the_room_they_no_longer_need() {
114116 // now leaves the timeline byte for byte as it was -- 4x set twice is 4x.
115117 let settled = p.lane(Lane::V1).to_vec();
116118 let again: Vec<(u32, u32)> = fast.iter().map(|&at| (at, shrunk)).collect();
117- p.speed_regions(&again, Speed::MAX).expect("still no lap");
119+ p.speed_regions(&again, Speed::MAX, &p.lanes().clone())
120+ .expect("still no lap");
118121 assert_eq!(p.lane(Lane::V1), settled, "a second pass compounded");
119122
120123 assert!(p.undo());
@@ -148,7 +151,7 @@ fn speed_mode_refuses_a_clip_that_laps_over_a_silence() {
148151 let before = spans(&p);
149152
150153 let err = p
151- .speed_regions(&[(30, 20)], Speed::MAX)
154+ .speed_regions(&[(30, 20)], Speed::MAX, &p.lanes().clone())
152155 .expect_err("a half-covered region must not be sped up")
153156 .to_string();
154157 assert!(err.contains("V2"), "{err}");
@@ -161,10 +164,117 @@ fn speed_mode_refuses_a_clip_that_laps_over_a_silence() {
161164
162165 // The same regions cut instead: a ripple is uniform, so nothing laps.
163166 assert!(p.place(lane, 40, broll));
164- assert!(p.cut_regions(&[(30, 20)]));
167+ p.cut_regions(&[(30, 20)], &p.lanes().clone())
168+ .expect("a ripple is uniform");
165169 loadable(&p);
166170 }
167171
172+/// A clip covering the whole fixture, for a second track to carry.
173+fn whole(source: usize, frames: u32) -> Clip {
174+ Clip {
175+ start: 0,
176+ in_frame: 0,
177+ out_frame: frames,
178+ source,
179+ link: None,
180+ eq: None,
181+ color: None,
182+ fit: Default::default(),
183+ speed: Speed::NORMAL,
184+ }
185+}
186+
187+/// The scope is the point: cutting a take's silences must not drag the music
188+/// track under it back. The lanes that were named lose exactly what was cut;
189+/// the lane that was not is byte for byte what it was, and one press still
190+/// takes the whole batch back.
191+#[test]
192+fn a_scoped_cut_leaves_the_track_it_was_not_given_alone() {
193+ let mut p = fixture();
194+ let frames = p.timeline_frames();
195+ let music = p.add_lane(LaneKind::Audio);
196+ assert_eq!(music.label(), "A2");
197+ assert!(p.place(music, 0, whole(0, frames)));
198+ let untouched = p.lane(music).to_vec();
199+
200+ // The take's own two lanes, and not the third.
201+ let take = [Lane::V1, Lane::A1];
202+ let regions = [(10, 5), (30, 5)];
203+ p.cut_regions(&regions, &take)
204+ .expect("the take is whole inside the scope");
205+
206+ assert_eq!(p.lane(music), untouched, "the music track moved");
207+ let cut: u32 = regions.iter().map(|&(_, len)| len).sum();
208+ for lane in take {
209+ assert_eq!(
210+ p.lane(lane).last().expect("clips").end(),
211+ frames - cut,
212+ "{} did not lose what was cut",
213+ lane.label()
214+ );
215+ }
216+ // The timeline is as long as its longest lane, which is now the music.
217+ assert_eq!(p.timeline_frames(), frames);
218+ loadable(&p);
219+
220+ assert!(p.undo());
221+ assert_eq!(p.lane(Lane::V1).last().expect("clips").end(), frames);
222+ assert_eq!(p.lane(Lane::V1).len(), 1, "one press took the whole batch");
223+}
224+
225+/// One track of a take is refused **by name**, in both modes: moving the sound
226+/// of a shot and not its picture leaves a group whose halves disagree about
227+/// their span -- a project no save could load. The refusal says which two clips
228+/// and what to do, and detaching them is exactly what makes the same call work.
229+#[test]
230+fn one_track_of_a_take_is_refused_by_name_until_it_is_detached() {
231+ let mut p = fixture();
232+ let before = spans(&p);
233+ let err = p
234+ .cut_regions(&[(10, 5)], &[Lane::A1])
235+ .expect_err("half a take must not be cut alone")
236+ .to_string();
237+ assert!(err.contains("A1") && err.contains("V1"), "{err}");
238+ assert!(err.contains("detach"), "{err}");
239+ // Speed mode lives under the same law and says the same thing.
240+ let same = p
241+ .speed_regions(&[(30, 20)], Speed::MAX, &[Lane::A1])
242+ .expect_err("half a take must not be re-rated alone")
243+ .to_string();
244+ assert!(same.contains("A1") && same.contains("V1"), "{same}");
245+ assert_eq!(spans(&p), before, "a refusal changed the timeline");
246+ assert!(!p.undo(), "a refusal cost an undo step");
247+
248+ // Detached, the sound is its own to cut -- and the picture stays put.
249+ assert!(p.ungroup(Lane::A1, 0));
250+ let picture = p.lane(Lane::V1).to_vec();
251+ p.cut_regions(&[(10, 5)], &[Lane::A1])
252+ .expect("a detached half is one track");
253+ assert_eq!(p.lane(Lane::V1), picture, "the picture followed anyway");
254+ assert_eq!(p.lane(Lane::A1).last().expect("clips").end(), 145);
255+ loadable(&p);
256+}
257+
258+/// Scoping every lane is the ripple this always was: the same cuts through the
259+/// batch and through `ripple_delete` one at a time land on the same timeline.
260+#[test]
261+fn every_lane_in_scope_is_the_ripple_it_always_was() {
262+ let regions = [(10, 5), (30, 5), (50, 5)];
263+ let mut batch = fixture();
264+ let all = batch.lanes();
265+ batch.cut_regions(&regions, &all).expect("every lane");
266+
267+ let mut one_at_a_time = fixture();
268+ for &(at, len) in regions.iter().rev() {
269+ assert!(one_at_a_time.ripple_delete(at, len));
270+ }
271+ assert_eq!(batch.lane(Lane::V1), one_at_a_time.lane(Lane::V1));
272+ assert_eq!(batch.lane(Lane::A1), one_at_a_time.lane(Lane::A1));
273+ // ...at one undo step against three.
274+ assert!(batch.undo());
275+ assert!(!batch.undo());
276+}
277+
168278 /// A rate that cannot address an edge of a silence is refused naming **that**
169279 /// edge -- not the region's start, which at a slow rate is often the one frame
170280 /// 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() {
175285 #[test]
176286 fn an_unaddressable_edge_is_named_where_it_actually_is() {
177287 let mut p = fixture();
178- p.set_speed(Lane::V1, 0, Speed::MIN).expect("nothing after it");
288+ p.set_speed(Lane::V1, 0, Speed::MIN)
289+ .expect("nothing after it");
179290 let before = spans(&p);
180291 // Starts where the clip starts -- no split needed there -- and ends at a
181292 // frame a quarter-speed clip cannot be cut at.
182293 let err = p
183- .speed_regions(&[(0, 5)], Speed::MAX)
294+ .speed_regions(&[(0, 5)], Speed::MAX, &p.lanes().clone())
184295 .expect_err("frame 5 is not a cut this rate can make")
185296 .to_string();
186297 assert!(err.contains("frame 5"), "{err}");
187- assert!(!err.contains("frame 0:"), "it named the region's start: {err}");
298+ assert!(
299+ !err.contains("frame 0:"),
300+ "it named the region's start: {err}"
301+ );
188302 // Rolled back whole, and the refusal cost no undo step: the press below is
189303 // the `set_speed` above.
190304 assert_eq!(spans(&p), before);
@@ -233,7 +347,8 @@ fn scanning_a_clip_and_cutting_what_it_finds() {
233347 assert!(!p.undo(), "the scan took an undo step");
234348
235349 let cut: u32 = regions.iter().map(|&(_, len)| len).sum();
236- assert!(p.cut_regions(&regions));
350+ p.cut_regions(&regions, &p.lanes().clone())
351+ .expect("every lane is in scope");
237352 assert_eq!(p.timeline_frames(), was - cut);
238353 let (video, audio_spans) = spans(&p);
239354 assert_eq!(video, audio_spans);