edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! The creator tools in the window: normalize, ducking, captions, fillers and voiceover. |
| 2 | //! The window has no `TestAppContext`, so each is driven through the session by the very |
| 3 | //! functions the card's buttons call ([`crate::creator`]) and the timeline is read back; the |
| 4 | //! doors (menu rows, palette entries, card controls) are source scans. The speech and piper |
| 5 | //! tests skip where their binaries or models are not installed. |
| 6 | |
| 7 | use super::*; |
| 8 | use crate::creator::{self, DuckField}; |
| 9 | use engine::project::LaneKind; |
| 10 | use engine::speech::{self, Spoken}; |
| 11 | |
| 12 | fn session() -> PlaybackSession { |
| 13 | let s = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); |
| 14 | s.set_gain(0.0); |
| 15 | s |
| 16 | } |
| 17 | |
| 18 | fn word(text: &str, a: u32, b: u32) -> Spoken { |
| 19 | Spoken { text: text.into(), start_f: a, end_f: b, p: 0.9 } |
| 20 | } |
| 21 | |
| 22 | #[test] |
| 23 | fn normalize_sets_the_gain_and_one_undo_puts_it_back() { |
| 24 | let mut s = session(); |
| 25 | let before = s.lane_gain_db(Lane::A1); |
| 26 | let report = creator::normalize(&mut s, false, -23.0, -1.0).expect("normalize"); |
| 27 | let line = creator::normalize_line(&report, false); |
| 28 | assert!(line.contains("LUFS") && !line.contains('→') && !line.contains("undo"), "{line}"); |
| 29 | assert!((s.lane_gain_db(Lane::A1) - before).abs() > 0.05 || report.reached, "{line}"); |
| 30 | assert!(s.undo(), "one step"); |
| 31 | assert!((s.lane_gain_db(Lane::A1) - before).abs() < 1e-3, "and one step is all it took"); |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn normalize_each_track_names_the_tracks_it_moved() { |
| 36 | let mut s = session(); |
| 37 | let report = creator::normalize(&mut s, true, -16.0, -1.0).expect("normalize each"); |
| 38 | assert_eq!(report.lanes.len(), creator::normalizable_lanes(&s).len()); |
| 39 | assert!(creator::normalize_line(&report, true).contains("A1")); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn a_target_out_of_range_changes_nothing() { |
| 44 | let mut s = session(); |
| 45 | let pushes = s.history_pushes(); |
| 46 | assert!(creator::normalize(&mut s, false, 4.0, -1.0).is_err()); |
| 47 | assert_eq!(s.history_pushes(), pushes); |
| 48 | } |
| 49 | |
| 50 | #[test] |
| 51 | fn a_duck_is_set_stepped_and_taken_off_each_as_one_undo_step() { |
| 52 | let mut s = session(); |
| 53 | let music = Lane::A1; |
| 54 | let voice = s.add_lane(LaneKind::Audio); |
| 55 | creator::duck_under(&mut s, music, voice).expect("duck"); |
| 56 | let duck = s.lane_duck_list(music); |
| 57 | assert_eq!(duck.len(), 1); |
| 58 | assert_eq!(duck[0].under, voice.ord); |
| 59 | let depth = duck[0].depth_db; |
| 60 | // A stepper press changes one number and leaves the others. |
| 61 | creator::duck_step(&mut s, music, voice, DuckField::Depth, 2).expect("deeper"); |
| 62 | let stepped = s.lane_duck_list(music)[0]; |
| 63 | assert!((stepped.depth_db - (depth + 2.0)).abs() < 1e-3, "{depth} -> {}", stepped.depth_db); |
| 64 | assert_eq!(stepped.attack_ms, duck[0].attack_ms); |
| 65 | creator::duck_step(&mut s, music, voice, DuckField::Attack, 3).expect("slower attack"); |
| 66 | creator::duck_step(&mut s, music, voice, DuckField::Release, -2).expect("quicker release"); |
| 67 | let d = s.lane_duck_list(music)[0]; |
| 68 | assert!(d.attack_ms > duck[0].attack_ms && d.release_ms < duck[0].release_ms); |
| 69 | // The ends hold: a press past the range is a press that changes nothing, not an error. |
| 70 | creator::duck_step(&mut s, music, voice, DuckField::Depth, 100).expect("clamped"); |
| 71 | assert!(s.lane_duck_list(music)[0].depth_db <= engine::duck::MAX_DEPTH_DB); |
| 72 | creator::duck_off(&mut s, music, voice).expect("off"); |
| 73 | assert!(s.lane_duck_list(music).is_empty()); |
| 74 | assert!(s.undo(), "the off is a step of its own"); |
| 75 | assert_eq!(s.lane_duck_list(music).len(), 1); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn a_duck_refuses_itself_a_video_lane_and_a_locked_track() { |
| 80 | let mut s = session(); |
| 81 | let other = s.add_lane(LaneKind::Audio); |
| 82 | assert!(creator::duck_under(&mut s, Lane::A1, Lane::A1).is_err()); |
| 83 | assert!(creator::duck_under(&mut s, Lane::A1, Lane::V1).is_err()); |
| 84 | assert!(creator::duck_step(&mut s, Lane::A1, other, DuckField::Depth, 1).is_err(), "no duck to step"); |
| 85 | assert!(creator::duck_off(&mut s, Lane::A1, other).is_err(), "no duck to take off"); |
| 86 | s.set_lane_locked(Lane::A1, true); |
| 87 | assert!(creator::duck_under(&mut s, Lane::A1, other).is_err()); |
| 88 | assert!(s.lane_duck_list(Lane::A1).is_empty()); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn fillers_ticked_are_cut_from_the_timeline_in_one_undo_step() { |
| 93 | let mut s = session(); |
| 94 | let fps = s.meta().frame_rate; |
| 95 | let before = s.timeline_duration(); |
| 96 | let a = (fps * 0.5) as u32; |
| 97 | let b = (fps * 2.0) as u32; |
| 98 | let words = [ |
| 99 | word("so", 0, a - 2), |
| 100 | word("um", a, a + 6), |
| 101 | word("this", a + 8, a + 20), |
| 102 | word("uh", b, b + 6), |
| 103 | word("ends", b + 8, b + 18), |
| 104 | ]; |
| 105 | let found = creator::fillers_in(&s, &words); |
| 106 | assert_eq!(found.len(), 2, "{found:?}"); |
| 107 | // Only the first is ticked: the second stays. |
| 108 | let cut = creator::cut_fillers(&mut s, &found[..1]).expect("cut"); |
| 109 | assert_eq!(cut, 1); |
| 110 | let shorter = before - s.timeline_duration(); |
| 111 | assert!(shorter > 4.0 / fps && shorter < 12.0 / fps, "{shorter}"); |
| 112 | assert!(s.undo()); |
| 113 | assert!((s.timeline_duration() - before).abs() < 1e-6, "one undo restores the whole cut"); |
| 114 | // Both together are still one step. |
| 115 | assert_eq!(creator::cut_fillers(&mut s, &found).expect("cut both"), 2); |
| 116 | assert!(s.undo()); |
| 117 | assert!((s.timeline_duration() - before).abs() < 1e-6); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn captions_land_on_a_new_caption_lane_as_one_undo_step() { |
| 122 | let mut s = session(); |
| 123 | let dir = std::env::temp_dir().join(format!("edith-creator-{}", std::process::id())); |
| 124 | std::fs::create_dir_all(&dir).unwrap(); |
| 125 | let project = dir.join("film.edith"); |
| 126 | let words = [ |
| 127 | word("Hello", 0, 8), |
| 128 | word("there", 9, 18), |
| 129 | word("and", 19, 25), |
| 130 | word("welcome", 26, 40), |
| 131 | ]; |
| 132 | let lanes_before = s.subtitle_lanes().len(); |
| 133 | let (n, lane, replaced) = creator::place_captions(&mut s, &words, Some(&project)).expect("captions"); |
| 134 | assert!(n >= 1); |
| 135 | assert!(!replaced); |
| 136 | assert_eq!(lane.kind, LaneKind::Subtitle); |
| 137 | assert_eq!(s.subtitle_lanes().len(), lanes_before + 1); |
| 138 | assert_eq!(s.sub_lane(lane).len(), n); |
| 139 | assert!(dir.join("film.captions.srt").exists(), "the track is a file, beside the project"); |
| 140 | assert!(s.undo(), "one step takes the whole track back"); |
| 141 | assert!(s.sub_lane(lane).is_empty()); |
| 142 | let _ = std::fs::remove_dir_all(&dir); |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn captions_again_replace_the_first_run_in_one_step() { |
| 147 | let mut s = session(); |
| 148 | let dir = std::env::temp_dir().join(format!("edith-creator-again-{}", std::process::id())); |
| 149 | std::fs::create_dir_all(&dir).unwrap(); |
| 150 | let project = dir.join("again.edith"); |
| 151 | let words = [word("Hello", 0, 8), word("there", 9, 18), word("friend", 19, 30)]; |
| 152 | let (n, lane, _) = creator::place_captions(&mut s, &words, Some(&project)).expect("first"); |
| 153 | let tracks = s.subtitles().len(); |
| 154 | let lanes = s.subtitle_lanes().len(); |
| 155 | let (m, again, replaced) = creator::place_captions(&mut s, &words, Some(&project)).expect("second"); |
| 156 | assert!(replaced); |
| 157 | assert_eq!(again, lane, "the same lane, not a new one"); |
| 158 | assert_eq!(s.subtitle_lanes().len(), lanes); |
| 159 | assert_eq!(s.subtitles().len(), tracks, "the old track is dropped, not stacked"); |
| 160 | assert_eq!(s.sub_lane(lane).len(), m); |
| 161 | assert_eq!(m, n); |
| 162 | assert!(s.undo(), "one step takes the second run back"); |
| 163 | assert_eq!(s.sub_lane(lane).len(), n); |
| 164 | let _ = std::fs::remove_dir_all(&dir); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn no_words_is_no_captions_and_no_change() { |
| 169 | let mut s = session(); |
| 170 | let pushes = s.history_pushes(); |
| 171 | assert!(creator::place_captions(&mut s, &[], None).is_err()); |
| 172 | assert_eq!(s.history_pushes(), pushes); |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn a_voiceover_lands_at_the_playhead_on_a_free_audio_track() { |
| 177 | let mut s = session(); |
| 178 | let tone = asset("test_tone.wav"); |
| 179 | let at = 1.0; |
| 180 | let lane = creator::place_voiceover(&mut s, &tone, at).expect("place"); |
| 181 | assert_eq!(lane.kind, LaneKind::Audio); |
| 182 | let placed: Vec<_> = s.lane_clips(lane).iter().filter(|c| c.source != 0 || c.start > 0).collect(); |
| 183 | assert!(!placed.is_empty(), "a clip is on {}", lane.label()); |
| 184 | let fps = s.meta().frame_rate; |
| 185 | assert!(placed.iter().any(|c| (c.start as f64 / fps - at).abs() < 1.5 / fps), "at the playhead"); |
| 186 | assert!(s.undo(), "one undo step"); |
| 187 | assert!(s.lane_clips(lane).iter().all(|c| (c.start as f64 / fps - at).abs() > 1.5 / fps)); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn speaking_text_makes_a_voiceover_the_timeline_takes() { |
| 192 | let Ok(voices) = engine::tts::list_voices(&engine::tts::voices_dir()) else { |
| 193 | eprintln!("skipped: no piper voices installed"); |
| 194 | return; |
| 195 | }; |
| 196 | if voices.is_empty() || engine::tts::piper_bin().is_err() { |
| 197 | eprintln!("skipped: piper or its voices are not installed"); |
| 198 | return; |
| 199 | } |
| 200 | let mut s = session(); |
| 201 | let dir = creator::voiceover_dir(None).join("test"); |
| 202 | let rendered = engine::tts::render(&dir, "Testing the voiceover.", None, None, 1.0, 0.0).expect("render"); |
| 203 | let lane = creator::place_voiceover(&mut s, &rendered.file, 0.5).expect("place"); |
| 204 | assert!(!s.lane_clips(lane).is_empty()); |
| 205 | } |
| 206 | |
| 207 | #[test] |
| 208 | fn listening_for_captions_runs_end_to_end_when_speech_is_installed() { |
| 209 | if speech::available(speech::Device::Cpu).is_err() { |
| 210 | eprintln!("skipped: edith-stt or its model is not installed"); |
| 211 | return; |
| 212 | } |
| 213 | let s = session(); |
| 214 | let hears = creator::hear_choices(&s); |
| 215 | assert!(hears.contains(&speech::Hear::Mix) && hears.contains(&speech::Hear::Lane(Lane::A1))); |
| 216 | let heard = speech::prepare(&s, speech::Hear::Mix).expect("the fixture has sound"); |
| 217 | let job = speech::Job::default(); |
| 218 | let model = speech::installed_models(speech::Device::Cpu)[0]; |
| 219 | // A tone is not speech: whatever comes back, it comes back (and cancel stops it early). |
| 220 | job.cancel.store(true, std::sync::atomic::Ordering::Relaxed); |
| 221 | assert_eq!(speech::listen(heard, model, speech::Device::Cpu, "en", false, &job).unwrap_err(), speech::CANCELLED); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn the_language_ring_starts_on_auto_and_every_code_is_one_the_engine_takes() { |
| 226 | assert_eq!(creator::LANGUAGES[0].0, "auto"); |
| 227 | for (code, name) in creator::LANGUAGES { |
| 228 | assert!(speech::check_lang(code).is_ok(), "{code} ({name})"); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /// The doors: every tool is a menu row and a palette entry, and every control is on the card. |
| 233 | #[test] |
| 234 | fn every_creator_tool_has_a_menu_row_a_palette_entry_and_a_card_control() { |
| 235 | let menu = src_text("ui/menubar.rs"); |
| 236 | let source = ui_source(); |
| 237 | for action in [ActionId::Normalize, ActionId::Captions, ActionId::Fillers, ActionId::Voiceover] { |
| 238 | assert!(ActionId::ALL.contains(&action), "{action:?} is in the palette's list"); |
| 239 | assert!(menu.contains(&format!("Act(ActionId::{action:?})")), "{action:?} has no menu row"); |
| 240 | assert!(!action.label().is_empty()); |
| 241 | } |
| 242 | for id in [ |
| 243 | "tools-normalize", |
| 244 | "tools-captions", |
| 245 | "tools-find-fillers", |
| 246 | "tools-cut-fillers", |
| 247 | "tools-fillers-all", |
| 248 | "tools-fillers-none", |
| 249 | "tools-speak", |
| 250 | "tools-cancel", |
| 251 | "tools-target", |
| 252 | "tools-ceiling", |
| 253 | "tools-lang", |
| 254 | "tools-tab-a", |
| 255 | "tools-tab-b", |
| 256 | "mix-normalize", |
| 257 | "mix-duck-key", |
| 258 | "mix-duck-off", |
| 259 | ] { |
| 260 | assert!(source.contains(&format!("\"{id}\"")), "{id} is on no card"); |
| 261 | } |
| 262 | // Silences already had its door (the Clips menu); it must keep it. |
| 263 | assert!(menu.contains("Act(ActionId::Silence)")); |
| 264 | } |
| 265 | |
| 266 | #[test] |
| 267 | fn the_tools_card_closes_cancels_its_job_and_the_actions_open_it() { |
| 268 | let actions = src_text("player/actions.rs"); |
| 269 | // Captions starts its run in place (see `captions_starts_listening`). |
| 270 | for (action, tool) in [ |
| 271 | ("Normalize", "Normalize"), |
| 272 | ("Fillers", "Fillers"), |
| 273 | ("Voiceover", "Voiceover"), |
| 274 | ] { |
| 275 | assert!( |
| 276 | actions.contains(&format!("ActionId::{action} => self.open_tools(crate::player::tools::Tool::{tool}, cx)")), |
| 277 | "{action} does not open the card" |
| 278 | ); |
| 279 | } |
| 280 | assert!(fn_body("close_card").contains("close_tools()")); |
| 281 | assert!(src_text("player/tools.rs").contains("cancel.store(true")); |
| 282 | } |
| 283 | |
| 284 | #[test] |
| 285 | fn captions_starts_listening_in_place_and_opens_the_card_behind_it() { |
| 286 | let actions = src_text("player/actions.rs"); |
| 287 | assert!( |
| 288 | actions.contains("ActionId::Captions => {\n self.open_tools(crate::player::tools::Tool::Captions, cx);\n self.start_listening(false, cx);"), |
| 289 | "Generate captions must start the run, not only open the Tools card" |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | // ---- the Transcript tab --------------------------------------------------------------- |
| 294 | |
| 295 | #[test] |
| 296 | fn a_word_cut_ripples_every_lane_in_one_undo_step_and_the_words_follow() { |
| 297 | let mut s = session(); |
| 298 | let fps = s.meta().frame_rate; |
| 299 | let before = s.timeline_duration(); |
| 300 | let f = |secs: f64| (secs * fps) as u32; |
| 301 | let words = [ |
| 302 | word("one", f(0.2), f(0.5)), |
| 303 | word("two", f(0.6), f(1.0)), |
| 304 | word("three", f(1.1), f(1.5)), |
| 305 | word("four", f(1.6), f(2.0)), |
| 306 | ]; |
| 307 | let end = (before * fps).round() as u32; |
| 308 | let span = speech::word_span(&words, 1, 2, (0.06 * fps).round() as u32, end).expect("span"); |
| 309 | let lanes = s.lanes(); |
| 310 | let regions = speech::cut_spans(&mut s, &[span], &lanes).expect("cut"); |
| 311 | let shorter = before - s.timeline_duration(); |
| 312 | assert!(shorter > 0.8 && shorter < 1.2, "{shorter}"); |
| 313 | let kept = speech::remap_words(&words, ®ions); |
| 314 | assert_eq!(kept.iter().map(|w| w.text.as_str()).collect::<Vec<_>>(), ["one", "four"]); |
| 315 | assert!(kept[1].start_f < words[3].start_f, "the later word moved up"); |
| 316 | assert!(s.undo(), "one undo"); |
| 317 | assert!((s.timeline_duration() - before).abs() < 1e-6); |
| 318 | } |
| 319 | |
| 320 | #[test] |
| 321 | fn the_transcript_breaks_paragraphs_finds_the_playing_word_and_selects_runs() { |
| 322 | let mut t = crate::player::transcript::Transcript::new(); |
| 323 | let fps = 25.0; |
| 324 | t.words = Some(vec![word("Hello", 0, 10), word("there.", 12, 20), word("Later", 100, 110), word("um", 112, 118)]); |
| 325 | assert_eq!(t.paragraphs(fps), vec![(0, 1), (2, 3)], "a 3 s pause starts a paragraph"); |
| 326 | assert_eq!(t.playing_word(5, fps), Some(0)); |
| 327 | assert_eq!(t.playing_word(14, fps), Some(1)); |
| 328 | assert_eq!(t.playing_word(60, fps), None, "in the silence nothing is spoken"); |
| 329 | assert_eq!(t.selection(), None); |
| 330 | t.anchor = Some(3); |
| 331 | t.head = 1; |
| 332 | assert_eq!(t.selection(), Some((1, 3)), "dragging backwards still selects first to last"); |
| 333 | t.search = "LATER".into(); |
| 334 | assert!(t.matches(2) && !t.matches(0)); |
| 335 | } |
| 336 | |
| 337 | #[test] |
| 338 | fn a_tool_error_or_a_clock_or_a_model_tip_reads_plainly() { |
| 339 | let raw = "rendering the mix refused: 7 source(s) offline (moved or deleted): src:0 /tmp/a.mp4, src:1 /tmp/b.mp4; media_relink each one"; |
| 340 | let said = creator::plain_error(raw); |
| 341 | assert_eq!(said.line, "7 files are offline. Relink them in Media."); |
| 342 | assert!(said.offline && said.detail.as_deref() == Some(raw), "the whole refusal stays in the tooltip"); |
| 343 | let said = creator::plain_error("the sound is silent: there is nothing to transcribe"); |
| 344 | assert_eq!(said.line, "The sound is silent: there is nothing to transcribe"); |
| 345 | assert!(said.detail.is_none() && !said.offline); |
| 346 | assert_eq!(creator::clock(0.0), "0:00"); |
| 347 | assert_eq!(creator::clock(75.0), "1:15"); |
| 348 | assert_eq!(creator::clock(3725.0), "1:02:05"); |
| 349 | assert_eq!(creator::lufs_text(-20.0), "\u{2212}20.0"); |
| 350 | } |