diff --git a/crates/app/src/keymap.rs b/crates/app/src/keymap.rs index c97134b..be18822 100644 --- a/crates/app/src/keymap.rs +++ b/crates/app/src/keymap.rs @@ -613,7 +613,7 @@ pub enum Reach { Gesture, } -pub static FIXED: std::sync::LazyLock<[Fixed; 27]> = std::sync::LazyLock::new(|| { +pub static FIXED: std::sync::LazyLock<[Fixed; 28]> = std::sync::LazyLock::new(|| { [ // Not a chord at all but a way of pressing one, and the only place the // 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(|| category: Category::Clips, reach: Reach::Click("silence-apply"), }, + Fixed { + chord: "k".into(), + label: "Keep only the silences the card found", + category: Category::Clips, + reach: Reach::Click("silence-keep-only"), + }, ] }); diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 3830e62..b831785 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -718,6 +718,12 @@ struct Player { /// inventing, and a made-up SPL would be a lie about what was measured); /// what it changes is which of the two spellings a person reads. silence_dbfs: bool, + /// Keep only the silences: the card's applies act on the *complement* of + /// what the scan found -- the speech between the quiet stretches -- so a + /// cut removes the words and leaves the room tone. Off, today's jumpcut, + /// until asked; kept across closes like [`Self::silence_dbfs`], which is + /// the same kind of switch: how the card reads, not what it scans. + silence_keep_only: bool, /// What the last scan found, in timeline frames: what the lane draws marks /// over and what an apply acts on -- *exactly* the previewed set, never a /// second scan at the moment of the press. @@ -1044,6 +1050,7 @@ fn main() { // The reference named, which is what the card said before // there was a choice about it. silence_dbfs: true, + silence_keep_only: false, silence_marks: Vec::new(), silence_levels: HashMap::new(), silence_scan: None, diff --git a/crates/app/src/player/cards.rs b/crates/app/src/player/cards.rs index 638603b..22ac27f 100644 --- a/crates/app/src/player/cards.rs +++ b/crates/app/src/player/cards.rs @@ -1111,11 +1111,27 @@ impl Player { let Some(levels) = levels else { return; }; - self.silence_marks = engine::silence::timeline_regions( - &clip, - self.fps, - &engine::silence::regions(&levels, self.silence), - ); + // Keep-only flips what the applies act on: the *speech* between the + // quiet stretches, so a cut removes the words and leaves the room + // tone. Skipped when nothing was quiet -- inverting an empty scan + // would mark the whole clip, the one press this card must never make + // by accident. + let quiet = engine::silence::regions(&levels, self.silence); + let source = if self.silence_keep_only && !quiet.is_empty() { + let duration = levels.len() as f64 / f64::from(engine::silence::WINDOWS_PER_SEC); + engine::silence::invert(duration, &quiet) + } else { + quiet + }; + self.silence_marks = engine::silence::timeline_regions(&clip, self.fps, &source); + } + + /// Keep-only-silence off and on, the marks going with it: the same scan, + /// the complement of the quiet when it is on. A preview is not an edit, + /// so a flip costs nothing and changes nothing but what is marked. + pub(crate) fn toggle_silence_keep_only(&mut self) { + self.silence_keep_only = !self.silence_keep_only; + self.scan_silences(); } /// Moves the picked row by `steps` and re-runs the scan against it, so the @@ -1195,11 +1211,15 @@ impl Player { pub(crate) fn previewed(&mut self) -> Option<(Vec<(u32, u32)>, Vec)> { if self.silence_marks.is_empty() { self.notify_user( - format!( - "NO SILENCE — under {:.0} dBFS lasting {:.2} s", - self.silence.threshold_db, self.silence.min_silence - ) - .into(), + if self.silence_keep_only { + "NO SPEECH TO CUT — keep-only-silence needs silence and speech".into() + } else { + format!( + "NO SILENCE — under {:.0} dBFS lasting {:.2} s", + self.silence.threshold_db, self.silence.min_silence + ) + .into() + }, ); return None; } @@ -1246,9 +1266,10 @@ impl Player { // moves indexes (a delete, a paste, an undo). self.selected.clear(); self.reset_after_reseek(); + let what = if self.silence_keep_only { "SPEECH" } else { "SILENCES" }; self.notify_user( format!( - "{count} SILENCES CUT {reach} — {} shorter · undo {}", + "{count} {what} CUT {reach} — {} shorter · undo {}", secs_label(saved), self.keymap.display(ActionId::Undo) ) @@ -1285,9 +1306,10 @@ impl Player { // goes with them. self.selected.clear(); self.reset_after_reseek(); + let what = if self.silence_keep_only { "SPEECH" } else { "SILENCES" }; self.notify_user( format!( - "{count} SILENCES AT {rate} {reach} · undo {}", + "{count} {what} AT {rate} {reach} · undo {}", self.keymap.display(ActionId::Undo) ) .into(), @@ -1733,10 +1755,11 @@ impl Player { return true; } // The silence card, the same way again: the arrows pick one of its - // rows and move it, and its two apply keys are the two things it can - // do to the timeline. Card-local, every one of them -- and listed in - // the keys menu (keymap.rs `FIXED`), because a key that cuts forty - // places at once is not a secret. + // rows and move it, its two apply keys are the two things it can do + // to the timeline, and `k` flips which half of the sound they act + // on. Card-local, every one of them -- and listed in the keys menu + // (keymap.rs `FIXED`), because a key that cuts forty places at once + // is not a secret. if self.silence_open.is_some() { if key == ESCAPE && self.card_maximized { self.toggle_maximize(); @@ -1755,6 +1778,9 @@ impl Player { self.cut_silences(cx); } else if key == "f" { self.speed_silences(cx); + } else if key == "k" { + self.toggle_silence_keep_only(); + cx.notify(); } return true; } diff --git a/crates/app/src/tests/cards.rs b/crates/app/src/tests/cards.rs index 3adb218..8aed8d1 100644 --- a/crates/app/src/tests/cards.rs +++ b/crates/app/src/tests/cards.rs @@ -1482,19 +1482,19 @@ fn a_held_key_moves_a_value_and_nothing_else() { #[test] fn the_silence_card_fits_the_smallest_window_and_never_slows_a_silence_down() { // The same 640x360 floor, and this card starts below the header: a - // title and a hint over its [`SILENCE_ROWS`] rows, the count line and - // the two buttons. - let (title, hint, count) = (17., 17., 17.); + // title over its [`SILENCE_ROWS`] rows, the count line, the keep-only + // switch, and the two apply buttons. Help is the head's `?`, not a line. + let (title, count) = (17., 17.); + let button_rows = 2.; let gaps = 6. * 5.; let padding = 24.; assert!( HEADER_H + 8. + title - + hint + SILENCE_ROWS as f32 * KEYS_ROW_H + count - + KEYS_ROW_H + + button_rows * KEYS_ROW_H + gaps + padding <= 360., diff --git a/crates/app/src/ui/cards.rs b/crates/app/src/ui/cards.rs index 6ab2ec8..809d5eb 100644 --- a/crates/app/src/ui/cards.rs +++ b/crates/app/src/ui/cards.rs @@ -121,7 +121,6 @@ fn dark_card_head( .into() }) .child(if max { "▣ m" } else { "⤢ m" }) - .tooltip(crate::ui::widgets::overlay_tip_hover("Toggle card size", "", None)) .children(hitmap::control("card.maximize", "Toggle card size", true)) })) .child( @@ -2306,10 +2305,15 @@ impl Player { scan.started.elapsed().as_secs_f32(), scan.since.elapsed().as_secs_f32(), ), - None => match found { - 0 => "nothing quiet enough for long enough".to_string(), - 1 => format!("1 silence, {}", secs_label(secs)), - n => format!("{n} silences, {}", secs_label(secs)), + None => match (self.silence_keep_only, found) { + (true, 0) => "nothing to cut — need both silence and speech".to_string(), + (true, 1) => format!("1 speech stretch, {} — silences kept", secs_label(secs)), + (true, n) => { + format!("{n} speech stretches, {} — silences kept", secs_label(secs)) + } + (false, 0) => "nothing quiet enough for long enough".to_string(), + (false, 1) => format!("1 silence, {}", secs_label(secs)), + (false, n) => format!("{n} silences, {}", secs_label(secs)), }, }; let rows: Vec<_> = rows @@ -2374,6 +2378,22 @@ impl Player { ) .into_any_element() }; + // The keep-only toggle rides in the same row, first: a switch, not a + // third apply, so it is always pressable -- even with nothing found, + // which is exactly when the two applies dim -- and its own ghost + // call rather than the `button` closure above, whose `active` means + // "there is something to apply". + let (cut_label, speed_label) = if self.silence_keep_only { + ( + "Cut speech out (enter)", + format!("Play speech at {} (f)", self.silence_factor), + ) + } else { + ( + "Cut them out (enter)", + format!("Play them at {} (f)", self.silence_factor), + ) + }; Some( scrim() .flex() @@ -2418,13 +2438,21 @@ impl Player { .map(move |d| d.child(head.unwrap())) .children(rows) .map(|d| d.child(dark_help(status.clone()))) - .child(div().flex().gap(px(4.)).children([ - button(0, "Cut them out (enter)".into(), Self::cut_silences), - button( - 1, - format!("Play them at {} (f)", self.silence_factor), - Self::speed_silences, + .child( + dark_ghost_button( + "silence-keep-only", + "Keep only silence", + "k", + self.silence_keep_only, + cx.listener(|this, _: &ClickEvent, _, cx| { + this.toggle_silence_keep_only(); + cx.notify(); + }), ), + ) + .child(div().flex().gap(px(4.)).children([ + button(0, cut_label.into(), Self::cut_silences), + button(1, speed_label, Self::speed_silences), ])), ), ) diff --git a/crates/engine/src/silence.rs b/crates/engine/src/silence.rs index 273bf50..0fa0925 100644 --- a/crates/engine/src/silence.rs +++ b/crates/engine/src/silence.rs @@ -229,6 +229,30 @@ pub fn regions(levels: &[f32], cfg: Settings) -> Vec<(f64, f64)> { out } +/// The stretches of `[0, duration)` that `regions` did not cover, same +/// half-open `(from, to)` seconds, in order, never overlapping. +pub fn invert(duration: f64, regions: &[(f64, f64)]) -> Vec<(f64, f64)> { + if !(duration.is_finite() && duration > 0.) { + return Vec::new(); + } + let mut out: Vec<(f64, f64)> = Vec::new(); + let mut cursor = 0.; + for &(from, to) in regions { + let (from, to) = (from.clamp(0., duration), to.clamp(0., duration)); + if to <= from { + continue; + } + if from > cursor { + out.push((cursor, from)); + } + cursor = cursor.max(to); + } + if cursor < duration { + out.push((cursor, duration)); + } + out +} + /// Where those source stretches sit on the timeline, as `(start, len)` frame /// pairs of the lane `clip` is on -- what a preview marks and what /// [`crate::Project::cut_regions`] cuts. @@ -283,7 +307,7 @@ fn floor_timeline(speed: Speed, offset: u32) -> u32 { mod tests { use std::path::PathBuf; - use super::{Settings, levels, regions, timeline_regions}; + use super::{Settings, invert, levels, regions, timeline_regions}; use crate::project::{Clip, Speed}; use crate::scale::FitPolicy; @@ -546,4 +570,25 @@ mod tests { // cut is never handed back. assert!(timeline_regions(&whole, 30., &[(1.01, 1.02)]).is_empty()); } + + /// The complement, taken literally: what `regions` kept out is exactly + /// what `invert` hands back, an empty scan inverts to the whole span, a + /// scan of everything inverts to nothing, and a region hanging past the + /// end is clamped to it rather than dragging the answer past `duration`. + #[test] + fn invert_is_the_complement_of_the_regions_it_is_given() { + near( + &invert(10., &[(2., 3.), (6., 8.)]), + &[(0., 2.), (3., 6.), (8., 10.)], + ); + near(&invert(10., &[]), &[(0., 10.)]); + assert!(invert(10., &[(0., 10.)]).is_empty()); + near(&invert(10., &[(8., 20.)]), &[(0., 8.)]); + // No span to invert *of*: zero, negative and nonsense durations + // refuse rather than inventing one. + assert!(invert(0., &[]).is_empty()); + assert!(invert(-1., &[]).is_empty()); + assert!(invert(f64::NAN, &[]).is_empty()); + assert!(invert(f64::INFINITY, &[]).is_empty()); + } } diff --git a/crates/engine/tests/silence.rs b/crates/engine/tests/silence.rs index 8d030bf..755bb3a 100644 --- a/crates/engine/tests/silence.rs +++ b/crates/engine/tests/silence.rs @@ -407,3 +407,67 @@ fn scanning_a_clip_and_cutting_what_it_finds() { assert!(p.undo()); assert_eq!(p.timeline_frames(), was); } + +/// The reverse cut: the same scan, its quiet regions inverted into the +/// speech between them, and cutting *those* -- what is left is the silences +/// themselves, a timeline shorter by exactly the speech, far shorter than +/// the jumpcut's remains (which keep everything but the dips), and one undo +/// press takes it all back. +#[test] +fn keeping_only_silence_cuts_the_speech() { + let mut p = fixture(); + let was = p.timeline_frames(); + let audio = Lane::new(LaneKind::Audio, 0); + let clip = p.lane(audio)[0]; + let range = ( + f64::from(clip.in_frame) / FPS, + f64::from(clip.out_frame) / FPS, + ); + let levels = silence::levels(asset("test_av.mp4"), 0, range) + .expect("scan") + .expect("the fixture has audio"); + let cfg = Settings { + threshold_db: -40., + min_silence: 0.08, + padding: 0.02, + min_keep: 0., + }; + let quiet = silence::regions(&levels, cfg); + assert!(!quiet.is_empty(), "the fixture has silences to keep"); + let duration = levels.len() as f64 / f64::from(engine::silence::WINDOWS_PER_SEC); + let speech = silence::invert(duration, &quiet); + let regions = silence::timeline_regions(&clip, FPS, &speech); + assert!(!regions.is_empty(), "the fixture has speech to cut"); + + let cut: u32 = regions.iter().map(|&(_, len)| len).sum(); + let kept_quiet: u32 = silence::timeline_regions(&clip, FPS, &quiet) + .iter() + .map(|&(_, len)| len) + .sum(); + p.cut_regions(®ions, &p.lanes().clone()) + .expect("every lane is in scope"); + assert_eq!(p.timeline_frames(), was - cut); + // What remains *is* the quiet: the scan's complement covers the span the + // silences do not. The two sums differ only where a boundary falls + // between frames -- both sides round inward, so one unclaimed frame can + // open at every meeting of a silence and the speech beside it, and + // nowhere else. + let remaining = was - cut; + assert!( + remaining >= kept_quiet, + "{remaining} left against {kept_quiet} of marked quiet" + ); + assert!( + remaining - kept_quiet <= (quiet.len() + speech.len()) as u32, + "{remaining} left against {kept_quiet} of marked quiet" + ); + // ...and the two cuts are opposites, not neighbours: five brief dips + // against the speech around them. + let jumpcut_left = was - kept_quiet; + assert!( + remaining * 4 < jumpcut_left, + "{remaining} left against the jumpcut's {jumpcut_left}" + ); + assert!(p.undo()); + assert_eq!(p.timeline_frames(), was); +}