Keep-only invert — cut speech, leave the quiet
Browse files at this commit5cd971b6b898c868d6270be31773b7f40e3039beParent: 423536a- feat
- keep-only invert — cut speech, leave the quiet
7 changed files +210 -34
- crates/app/src/keymap.rs +7 -1
- crates/app/src/main.rs +7 -0
- crates/app/src/player/cards.rs +42 -16
- crates/app/src/tests/cards.rs +5 -5
- crates/app/src/ui/cards.rs +39 -11
- crates/engine/src/silence.rs +46 -1
- crates/engine/tests/silence.rs +64 -0
crates/app/src/keymap.rsmodified+7 -1
| @@ -613,7 +613,7 @@ pub enum Reach { | |||
| 613 | Gesture, | 613 | Gesture, |
| 614 | } | 614 | } |
| 615 | 615 | ||
| 616 | -pub static FIXED: std::sync::LazyLock<[Fixed; 27]> = std::sync::LazyLock::new(|| { | 616 | +pub static FIXED: std::sync::LazyLock<[Fixed; 28]> = std::sync::LazyLock::new(|| { |
| 617 | [ | 617 | [ |
| 618 | // Not a chord at all but a way of pressing one, and the only place the | 618 | // Not a chord at all but a way of pressing one, and the only place the |
| 619 | // editor can say so: holding a key that moves a *value* runs it, and | 619 | // editor can say so: holding a key that moves a *value* runs it, and |
| @@ -795,6 +795,12 @@ pub static FIXED: std::sync::LazyLock<[Fixed; 27]> = std::sync::LazyLock::new(|| | |||
| 795 | category: Category::Clips, | 795 | category: Category::Clips, |
| 796 | reach: Reach::Click("silence-apply"), | 796 | reach: Reach::Click("silence-apply"), |
| 797 | }, | 797 | }, |
| 798 | + Fixed { | ||
| 799 | + chord: "k".into(), | ||
| 800 | + label: "Keep only the silences the card found", | ||
| 801 | + category: Category::Clips, | ||
| 802 | + reach: Reach::Click("silence-keep-only"), | ||
| 803 | + }, | ||
| 798 | ] | 804 | ] |
| 799 | }); | 805 | }); |
| 800 | 806 | ||
crates/app/src/main.rsmodified+7 -0
| @@ -718,6 +718,12 @@ struct Player { | |||
| 718 | /// inventing, and a made-up SPL would be a lie about what was measured); | 718 | /// inventing, and a made-up SPL would be a lie about what was measured); |
| 719 | /// what it changes is which of the two spellings a person reads. | 719 | /// what it changes is which of the two spellings a person reads. |
| 720 | silence_dbfs: bool, | 720 | silence_dbfs: bool, |
| 721 | + /// Keep only the silences: the card's applies act on the *complement* of | ||
| 722 | + /// what the scan found -- the speech between the quiet stretches -- so a | ||
| 723 | + /// cut removes the words and leaves the room tone. Off, today's jumpcut, | ||
| 724 | + /// until asked; kept across closes like [`Self::silence_dbfs`], which is | ||
| 725 | + /// the same kind of switch: how the card reads, not what it scans. | ||
| 726 | + silence_keep_only: bool, | ||
| 721 | /// What the last scan found, in timeline frames: what the lane draws marks | 727 | /// What the last scan found, in timeline frames: what the lane draws marks |
| 722 | /// over and what an apply acts on -- *exactly* the previewed set, never a | 728 | /// over and what an apply acts on -- *exactly* the previewed set, never a |
| 723 | /// second scan at the moment of the press. | 729 | /// second scan at the moment of the press. |
| @@ -1044,6 +1050,7 @@ fn main() { | |||
| 1044 | // The reference named, which is what the card said before | 1050 | // The reference named, which is what the card said before |
| 1045 | // there was a choice about it. | 1051 | // there was a choice about it. |
| 1046 | silence_dbfs: true, | 1052 | silence_dbfs: true, |
| 1053 | + silence_keep_only: false, | ||
| 1047 | silence_marks: Vec::new(), | 1054 | silence_marks: Vec::new(), |
| 1048 | silence_levels: HashMap::new(), | 1055 | silence_levels: HashMap::new(), |
| 1049 | silence_scan: None, | 1056 | silence_scan: None, |
crates/app/src/player/cards.rsmodified+42 -16
| @@ -1111,11 +1111,27 @@ impl Player { | |||
| 1111 | let Some(levels) = levels else { | 1111 | let Some(levels) = levels else { |
| 1112 | return; | 1112 | return; |
| 1113 | }; | 1113 | }; |
| 1114 | - self.silence_marks = engine::silence::timeline_regions( | 1114 | + // Keep-only flips what the applies act on: the *speech* between the |
| 1115 | - &clip, | 1115 | + // quiet stretches, so a cut removes the words and leaves the room |
| 1116 | - self.fps, | 1116 | + // tone. Skipped when nothing was quiet -- inverting an empty scan |
| 1117 | - &engine::silence::regions(&levels, self.silence), | 1117 | + // would mark the whole clip, the one press this card must never make |
| 1118 | - ); | 1118 | + // by accident. |
| 1119 | + let quiet = engine::silence::regions(&levels, self.silence); | ||
| 1120 | + let source = if self.silence_keep_only && !quiet.is_empty() { | ||
| 1121 | + let duration = levels.len() as f64 / f64::from(engine::silence::WINDOWS_PER_SEC); | ||
| 1122 | + engine::silence::invert(duration, &quiet) | ||
| 1123 | + } else { | ||
| 1124 | + quiet | ||
| 1125 | + }; | ||
| 1126 | + self.silence_marks = engine::silence::timeline_regions(&clip, self.fps, &source); | ||
| 1127 | + } | ||
| 1128 | + | ||
| 1129 | + /// Keep-only-silence off and on, the marks going with it: the same scan, | ||
| 1130 | + /// the complement of the quiet when it is on. A preview is not an edit, | ||
| 1131 | + /// so a flip costs nothing and changes nothing but what is marked. | ||
| 1132 | + pub(crate) fn toggle_silence_keep_only(&mut self) { | ||
| 1133 | + self.silence_keep_only = !self.silence_keep_only; | ||
| 1134 | + self.scan_silences(); | ||
| 1119 | } | 1135 | } |
| 1120 | 1136 | ||
| 1121 | /// Moves the picked row by `steps` and re-runs the scan against it, so the | 1137 | /// Moves the picked row by `steps` and re-runs the scan against it, so the |
| @@ -1195,11 +1211,15 @@ impl Player { | |||
| 1195 | pub(crate) fn previewed(&mut self) -> Option<(Vec<(u32, u32)>, Vec<Lane>)> { | 1211 | pub(crate) fn previewed(&mut self) -> Option<(Vec<(u32, u32)>, Vec<Lane>)> { |
| 1196 | if self.silence_marks.is_empty() { | 1212 | if self.silence_marks.is_empty() { |
| 1197 | self.notify_user( | 1213 | self.notify_user( |
| 1198 | - format!( | 1214 | + if self.silence_keep_only { |
| 1199 | - "NO SILENCE — under {:.0} dBFS lasting {:.2} s", | 1215 | + "NO SPEECH TO CUT — keep-only-silence needs silence and speech".into() |
| 1200 | - self.silence.threshold_db, self.silence.min_silence | 1216 | + } else { |
| 1201 | - ) | 1217 | + format!( |
| 1202 | - .into(), | 1218 | + "NO SILENCE — under {:.0} dBFS lasting {:.2} s", |
| 1219 | + self.silence.threshold_db, self.silence.min_silence | ||
| 1220 | + ) | ||
| 1221 | + .into() | ||
| 1222 | + }, | ||
| 1203 | ); | 1223 | ); |
| 1204 | return None; | 1224 | return None; |
| 1205 | } | 1225 | } |
| @@ -1246,9 +1266,10 @@ impl Player { | |||
| 1246 | // moves indexes (a delete, a paste, an undo). | 1266 | // moves indexes (a delete, a paste, an undo). |
| 1247 | self.selected.clear(); | 1267 | self.selected.clear(); |
| 1248 | self.reset_after_reseek(); | 1268 | self.reset_after_reseek(); |
| 1269 | + let what = if self.silence_keep_only { "SPEECH" } else { "SILENCES" }; | ||
| 1249 | self.notify_user( | 1270 | self.notify_user( |
| 1250 | format!( | 1271 | format!( |
| 1251 | - "{count} SILENCES CUT {reach} — {} shorter · undo {}", | 1272 | + "{count} {what} CUT {reach} — {} shorter · undo {}", |
| 1252 | secs_label(saved), | 1273 | secs_label(saved), |
| 1253 | self.keymap.display(ActionId::Undo) | 1274 | self.keymap.display(ActionId::Undo) |
| 1254 | ) | 1275 | ) |
| @@ -1285,9 +1306,10 @@ impl Player { | |||
| 1285 | // goes with them. | 1306 | // goes with them. |
| 1286 | self.selected.clear(); | 1307 | self.selected.clear(); |
| 1287 | self.reset_after_reseek(); | 1308 | self.reset_after_reseek(); |
| 1309 | + let what = if self.silence_keep_only { "SPEECH" } else { "SILENCES" }; | ||
| 1288 | self.notify_user( | 1310 | self.notify_user( |
| 1289 | format!( | 1311 | format!( |
| 1290 | - "{count} SILENCES AT {rate} {reach} · undo {}", | 1312 | + "{count} {what} AT {rate} {reach} · undo {}", |
| 1291 | self.keymap.display(ActionId::Undo) | 1313 | self.keymap.display(ActionId::Undo) |
| 1292 | ) | 1314 | ) |
| 1293 | .into(), | 1315 | .into(), |
| @@ -1733,10 +1755,11 @@ impl Player { | |||
| 1733 | return true; | 1755 | return true; |
| 1734 | } | 1756 | } |
| 1735 | // The silence card, the same way again: the arrows pick one of its | 1757 | // The silence card, the same way again: the arrows pick one of its |
| 1736 | - // rows and move it, and its two apply keys are the two things it can | 1758 | + // rows and move it, its two apply keys are the two things it can do |
| 1737 | - // do to the timeline. Card-local, every one of them -- and listed in | 1759 | + // to the timeline, and `k` flips which half of the sound they act |
| 1738 | - // the keys menu (keymap.rs `FIXED`), because a key that cuts forty | 1760 | + // on. Card-local, every one of them -- and listed in the keys menu |
| 1739 | - // places at once is not a secret. | 1761 | + // (keymap.rs `FIXED`), because a key that cuts forty places at once |
| 1762 | + // is not a secret. | ||
| 1740 | if self.silence_open.is_some() { | 1763 | if self.silence_open.is_some() { |
| 1741 | if key == ESCAPE && self.card_maximized { | 1764 | if key == ESCAPE && self.card_maximized { |
| 1742 | self.toggle_maximize(); | 1765 | self.toggle_maximize(); |
| @@ -1755,6 +1778,9 @@ impl Player { | |||
| 1755 | self.cut_silences(cx); | 1778 | self.cut_silences(cx); |
| 1756 | } else if key == "f" { | 1779 | } else if key == "f" { |
| 1757 | self.speed_silences(cx); | 1780 | self.speed_silences(cx); |
| 1781 | + } else if key == "k" { | ||
| 1782 | + self.toggle_silence_keep_only(); | ||
| 1783 | + cx.notify(); | ||
| 1758 | } | 1784 | } |
| 1759 | return true; | 1785 | return true; |
| 1760 | } | 1786 | } |
crates/app/src/tests/cards.rsmodified+5 -5
| @@ -1482,19 +1482,19 @@ fn a_held_key_moves_a_value_and_nothing_else() { | |||
| 1482 | #[test] | 1482 | #[test] |
| 1483 | fn the_silence_card_fits_the_smallest_window_and_never_slows_a_silence_down() { | 1483 | fn the_silence_card_fits_the_smallest_window_and_never_slows_a_silence_down() { |
| 1484 | // The same 640x360 floor, and this card starts below the header: a | 1484 | // The same 640x360 floor, and this card starts below the header: a |
| 1485 | - // title and a hint over its [`SILENCE_ROWS`] rows, the count line and | 1485 | + // title over its [`SILENCE_ROWS`] rows, the count line, the keep-only |
| 1486 | - // the two buttons. | 1486 | + // switch, and the two apply buttons. Help is the head's `?`, not a line. |
| 1487 | - let (title, hint, count) = (17., 17., 17.); | 1487 | + let (title, count) = (17., 17.); |
| 1488 | + let button_rows = 2.; | ||
| 1488 | let gaps = 6. * 5.; | 1489 | let gaps = 6. * 5.; |
| 1489 | let padding = 24.; | 1490 | let padding = 24.; |
| 1490 | assert!( | 1491 | assert!( |
| 1491 | HEADER_H | 1492 | HEADER_H |
| 1492 | + 8. | 1493 | + 8. |
| 1493 | + title | 1494 | + title |
| 1494 | - + hint | ||
| 1495 | + SILENCE_ROWS as f32 * KEYS_ROW_H | 1495 | + SILENCE_ROWS as f32 * KEYS_ROW_H |
| 1496 | + count | 1496 | + count |
| 1497 | - + KEYS_ROW_H | 1497 | + + button_rows * KEYS_ROW_H |
| 1498 | + gaps | 1498 | + gaps |
| 1499 | + padding | 1499 | + padding |
| 1500 | <= 360., | 1500 | <= 360., |
crates/app/src/ui/cards.rsmodified+39 -11
| @@ -121,7 +121,6 @@ fn dark_card_head( | |||
| 121 | .into() | 121 | .into() |
| 122 | }) | 122 | }) |
| 123 | .child(if max { "▣ m" } else { "⤢ m" }) | 123 | .child(if max { "▣ m" } else { "⤢ m" }) |
| 124 | - .tooltip(crate::ui::widgets::overlay_tip_hover("Toggle card size", "", None)) | ||
| 125 | .children(hitmap::control("card.maximize", "Toggle card size", true)) | 124 | .children(hitmap::control("card.maximize", "Toggle card size", true)) |
| 126 | })) | 125 | })) |
| 127 | .child( | 126 | .child( |
| @@ -2306,10 +2305,15 @@ impl Player { | |||
| 2306 | scan.started.elapsed().as_secs_f32(), | 2305 | scan.started.elapsed().as_secs_f32(), |
| 2307 | scan.since.elapsed().as_secs_f32(), | 2306 | scan.since.elapsed().as_secs_f32(), |
| 2308 | ), | 2307 | ), |
| 2309 | - None => match found { | 2308 | + None => match (self.silence_keep_only, found) { |
| 2310 | - 0 => "nothing quiet enough for long enough".to_string(), | 2309 | + (true, 0) => "nothing to cut — need both silence and speech".to_string(), |
| 2311 | - 1 => format!("1 silence, {}", secs_label(secs)), | 2310 | + (true, 1) => format!("1 speech stretch, {} — silences kept", secs_label(secs)), |
| 2312 | - n => format!("{n} silences, {}", secs_label(secs)), | 2311 | + (true, n) => { |
| 2312 | + format!("{n} speech stretches, {} — silences kept", secs_label(secs)) | ||
| 2313 | + } | ||
| 2314 | + (false, 0) => "nothing quiet enough for long enough".to_string(), | ||
| 2315 | + (false, 1) => format!("1 silence, {}", secs_label(secs)), | ||
| 2316 | + (false, n) => format!("{n} silences, {}", secs_label(secs)), | ||
| 2313 | }, | 2317 | }, |
| 2314 | }; | 2318 | }; |
| 2315 | let rows: Vec<_> = rows | 2319 | let rows: Vec<_> = rows |
| @@ -2374,6 +2378,22 @@ impl Player { | |||
| 2374 | ) | 2378 | ) |
| 2375 | .into_any_element() | 2379 | .into_any_element() |
| 2376 | }; | 2380 | }; |
| 2381 | + // The keep-only toggle rides in the same row, first: a switch, not a | ||
| 2382 | + // third apply, so it is always pressable -- even with nothing found, | ||
| 2383 | + // which is exactly when the two applies dim -- and its own ghost | ||
| 2384 | + // call rather than the `button` closure above, whose `active` means | ||
| 2385 | + // "there is something to apply". | ||
| 2386 | + let (cut_label, speed_label) = if self.silence_keep_only { | ||
| 2387 | + ( | ||
| 2388 | + "Cut speech out (enter)", | ||
| 2389 | + format!("Play speech at {} (f)", self.silence_factor), | ||
| 2390 | + ) | ||
| 2391 | + } else { | ||
| 2392 | + ( | ||
| 2393 | + "Cut them out (enter)", | ||
| 2394 | + format!("Play them at {} (f)", self.silence_factor), | ||
| 2395 | + ) | ||
| 2396 | + }; | ||
| 2377 | Some( | 2397 | Some( |
| 2378 | scrim() | 2398 | scrim() |
| 2379 | .flex() | 2399 | .flex() |
| @@ -2418,13 +2438,21 @@ impl Player { | |||
| 2418 | .map(move |d| d.child(head.unwrap())) | 2438 | .map(move |d| d.child(head.unwrap())) |
| 2419 | .children(rows) | 2439 | .children(rows) |
| 2420 | .map(|d| d.child(dark_help(status.clone()))) | 2440 | .map(|d| d.child(dark_help(status.clone()))) |
| 2421 | - .child(div().flex().gap(px(4.)).children([ | 2441 | + .child( |
| 2422 | - button(0, "Cut them out (enter)".into(), Self::cut_silences), | 2442 | + dark_ghost_button( |
| 2423 | - button( | 2443 | + "silence-keep-only", |
| 2424 | - 1, | 2444 | + "Keep only silence", |
| 2425 | - format!("Play them at {} (f)", self.silence_factor), | 2445 | + "k", |
| 2426 | - Self::speed_silences, | 2446 | + self.silence_keep_only, |
| 2447 | + cx.listener(|this, _: &ClickEvent, _, cx| { | ||
| 2448 | + this.toggle_silence_keep_only(); | ||
| 2449 | + cx.notify(); | ||
| 2450 | + }), | ||
| 2427 | ), | 2451 | ), |
| 2452 | + ) | ||
| 2453 | + .child(div().flex().gap(px(4.)).children([ | ||
| 2454 | + button(0, cut_label.into(), Self::cut_silences), | ||
| 2455 | + button(1, speed_label, Self::speed_silences), | ||
| 2428 | ])), | 2456 | ])), |
| 2429 | ), | 2457 | ), |
| 2430 | ) | 2458 | ) |
crates/engine/src/silence.rsmodified+46 -1
| @@ -229,6 +229,30 @@ pub fn regions(levels: &[f32], cfg: Settings) -> Vec<(f64, f64)> { | |||
| 229 | out | 229 | out |
| 230 | } | 230 | } |
| 231 | 231 | ||
| 232 | +/// The stretches of `[0, duration)` that `regions` did not cover, same | ||
| 233 | +/// half-open `(from, to)` seconds, in order, never overlapping. | ||
| 234 | +pub fn invert(duration: f64, regions: &[(f64, f64)]) -> Vec<(f64, f64)> { | ||
| 235 | + if !(duration.is_finite() && duration > 0.) { | ||
| 236 | + return Vec::new(); | ||
| 237 | + } | ||
| 238 | + let mut out: Vec<(f64, f64)> = Vec::new(); | ||
| 239 | + let mut cursor = 0.; | ||
| 240 | + for &(from, to) in regions { | ||
| 241 | + let (from, to) = (from.clamp(0., duration), to.clamp(0., duration)); | ||
| 242 | + if to <= from { | ||
| 243 | + continue; | ||
| 244 | + } | ||
| 245 | + if from > cursor { | ||
| 246 | + out.push((cursor, from)); | ||
| 247 | + } | ||
| 248 | + cursor = cursor.max(to); | ||
| 249 | + } | ||
| 250 | + if cursor < duration { | ||
| 251 | + out.push((cursor, duration)); | ||
| 252 | + } | ||
| 253 | + out | ||
| 254 | +} | ||
| 255 | + | ||
| 232 | /// Where those source stretches sit on the timeline, as `(start, len)` frame | 256 | /// Where those source stretches sit on the timeline, as `(start, len)` frame |
| 233 | /// pairs of the lane `clip` is on -- what a preview marks and what | 257 | /// pairs of the lane `clip` is on -- what a preview marks and what |
| 234 | /// [`crate::Project::cut_regions`] cuts. | 258 | /// [`crate::Project::cut_regions`] cuts. |
| @@ -283,7 +307,7 @@ fn floor_timeline(speed: Speed, offset: u32) -> u32 { | |||
| 283 | mod tests { | 307 | mod tests { |
| 284 | use std::path::PathBuf; | 308 | use std::path::PathBuf; |
| 285 | 309 | ||
| 286 | - use super::{Settings, levels, regions, timeline_regions}; | 310 | + use super::{Settings, invert, levels, regions, timeline_regions}; |
| 287 | use crate::project::{Clip, Speed}; | 311 | use crate::project::{Clip, Speed}; |
| 288 | use crate::scale::FitPolicy; | 312 | use crate::scale::FitPolicy; |
| 289 | 313 | ||
| @@ -546,4 +570,25 @@ mod tests { | |||
| 546 | // cut is never handed back. | 570 | // cut is never handed back. |
| 547 | assert!(timeline_regions(&whole, 30., &[(1.01, 1.02)]).is_empty()); | 571 | assert!(timeline_regions(&whole, 30., &[(1.01, 1.02)]).is_empty()); |
| 548 | } | 572 | } |
| 573 | + | ||
| 574 | + /// The complement, taken literally: what `regions` kept out is exactly | ||
| 575 | + /// what `invert` hands back, an empty scan inverts to the whole span, a | ||
| 576 | + /// scan of everything inverts to nothing, and a region hanging past the | ||
| 577 | + /// end is clamped to it rather than dragging the answer past `duration`. | ||
| 578 | + #[test] | ||
| 579 | + fn invert_is_the_complement_of_the_regions_it_is_given() { | ||
| 580 | + near( | ||
| 581 | + &invert(10., &[(2., 3.), (6., 8.)]), | ||
| 582 | + &[(0., 2.), (3., 6.), (8., 10.)], | ||
| 583 | + ); | ||
| 584 | + near(&invert(10., &[]), &[(0., 10.)]); | ||
| 585 | + assert!(invert(10., &[(0., 10.)]).is_empty()); | ||
| 586 | + near(&invert(10., &[(8., 20.)]), &[(0., 8.)]); | ||
| 587 | + // No span to invert *of*: zero, negative and nonsense durations | ||
| 588 | + // refuse rather than inventing one. | ||
| 589 | + assert!(invert(0., &[]).is_empty()); | ||
| 590 | + assert!(invert(-1., &[]).is_empty()); | ||
| 591 | + assert!(invert(f64::NAN, &[]).is_empty()); | ||
| 592 | + assert!(invert(f64::INFINITY, &[]).is_empty()); | ||
| 593 | + } | ||
| 549 | } | 594 | } |
crates/engine/tests/silence.rsmodified+64 -0
| @@ -407,3 +407,67 @@ fn scanning_a_clip_and_cutting_what_it_finds() { | |||
| 407 | assert!(p.undo()); | 407 | assert!(p.undo()); |
| 408 | assert_eq!(p.timeline_frames(), was); | 408 | assert_eq!(p.timeline_frames(), was); |
| 409 | } | 409 | } |
| 410 | + | ||
| 411 | +/// The reverse cut: the same scan, its quiet regions inverted into the | ||
| 412 | +/// speech between them, and cutting *those* -- what is left is the silences | ||
| 413 | +/// themselves, a timeline shorter by exactly the speech, far shorter than | ||
| 414 | +/// the jumpcut's remains (which keep everything but the dips), and one undo | ||
| 415 | +/// press takes it all back. | ||
| 416 | +#[test] | ||
| 417 | +fn keeping_only_silence_cuts_the_speech() { | ||
| 418 | + let mut p = fixture(); | ||
| 419 | + let was = p.timeline_frames(); | ||
| 420 | + let audio = Lane::new(LaneKind::Audio, 0); | ||
| 421 | + let clip = p.lane(audio)[0]; | ||
| 422 | + let range = ( | ||
| 423 | + f64::from(clip.in_frame) / FPS, | ||
| 424 | + f64::from(clip.out_frame) / FPS, | ||
| 425 | + ); | ||
| 426 | + let levels = silence::levels(asset("test_av.mp4"), 0, range) | ||
| 427 | + .expect("scan") | ||
| 428 | + .expect("the fixture has audio"); | ||
| 429 | + let cfg = Settings { | ||
| 430 | + threshold_db: -40., | ||
| 431 | + min_silence: 0.08, | ||
| 432 | + padding: 0.02, | ||
| 433 | + min_keep: 0., | ||
| 434 | + }; | ||
| 435 | + let quiet = silence::regions(&levels, cfg); | ||
| 436 | + assert!(!quiet.is_empty(), "the fixture has silences to keep"); | ||
| 437 | + let duration = levels.len() as f64 / f64::from(engine::silence::WINDOWS_PER_SEC); | ||
| 438 | + let speech = silence::invert(duration, &quiet); | ||
| 439 | + let regions = silence::timeline_regions(&clip, FPS, &speech); | ||
| 440 | + assert!(!regions.is_empty(), "the fixture has speech to cut"); | ||
| 441 | + | ||
| 442 | + let cut: u32 = regions.iter().map(|&(_, len)| len).sum(); | ||
| 443 | + let kept_quiet: u32 = silence::timeline_regions(&clip, FPS, &quiet) | ||
| 444 | + .iter() | ||
| 445 | + .map(|&(_, len)| len) | ||
| 446 | + .sum(); | ||
| 447 | + p.cut_regions(®ions, &p.lanes().clone()) | ||
| 448 | + .expect("every lane is in scope"); | ||
| 449 | + assert_eq!(p.timeline_frames(), was - cut); | ||
| 450 | + // What remains *is* the quiet: the scan's complement covers the span the | ||
| 451 | + // silences do not. The two sums differ only where a boundary falls | ||
| 452 | + // between frames -- both sides round inward, so one unclaimed frame can | ||
| 453 | + // open at every meeting of a silence and the speech beside it, and | ||
| 454 | + // nowhere else. | ||
| 455 | + let remaining = was - cut; | ||
| 456 | + assert!( | ||
| 457 | + remaining >= kept_quiet, | ||
| 458 | + "{remaining} left against {kept_quiet} of marked quiet" | ||
| 459 | + ); | ||
| 460 | + assert!( | ||
| 461 | + remaining - kept_quiet <= (quiet.len() + speech.len()) as u32, | ||
| 462 | + "{remaining} left against {kept_quiet} of marked quiet" | ||
| 463 | + ); | ||
| 464 | + // ...and the two cuts are opposites, not neighbours: five brief dips | ||
| 465 | + // against the speech around them. | ||
| 466 | + let jumpcut_left = was - kept_quiet; | ||
| 467 | + assert!( | ||
| 468 | + remaining * 4 < jumpcut_left, | ||
| 469 | + "{remaining} left against the jumpcut's {jumpcut_left}" | ||
| 470 | + ); | ||
| 471 | + assert!(p.undo()); | ||
| 472 | + assert_eq!(p.timeline_frames(), was); | ||
| 473 | +} | ||