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 the editor and the MCP server share: loudness normalizing, filler |
| 2 | //! cutting and the voiceover render. Each runs on a real session over `test_av.mp4` |
| 3 | //! (a 440/880 Hz stereo tone); the speech and piper ones skip where their binaries or models |
| 4 | //! are not installed. |
| 5 | |
| 6 | use std::path::PathBuf; |
| 7 | |
| 8 | use engine::PlaybackSession; |
| 9 | use engine::normalize::{self, Scope}; |
| 10 | use engine::project::Lane; |
| 11 | use engine::speech::{self, Spoken}; |
| 12 | |
| 13 | fn asset(name: &str) -> PathBuf { |
| 14 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets").join(name) |
| 15 | } |
| 16 | |
| 17 | fn word(text: &str, a: u32, b: u32) -> Spoken { |
| 18 | Spoken { text: text.into(), start_f: a, end_f: b, p: 0.9 } |
| 19 | } |
| 20 | |
| 21 | #[test] |
| 22 | fn a_filler_is_padded_but_never_into_its_neighbours() { |
| 23 | let words = [word("so", 0, 10), word("Um,", 12, 20), word("this", 21, 30), word("is", 30, 40), word("uh", 41, 50)]; |
| 24 | let found = speech::find_fillers(&words, 5, 60); |
| 25 | assert_eq!(found.len(), 2, "{found:?}"); |
| 26 | assert_eq!(found[0].text, "Um"); |
| 27 | // Pad 5 would reach 7..25, but the word before ends at 10 and the one after starts at 21. |
| 28 | assert_eq!(found[0].cut, (10, 21)); |
| 29 | // The last one pads out to 55 (the timeline ends at 60) but starts at 40, where the word before ends. |
| 30 | assert_eq!(found[1].cut, (40, 55)); |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn cutting_fillers_shortens_every_track_and_is_one_undo_step() { |
| 35 | let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open"); |
| 36 | let fps = s.meta().frame_rate; |
| 37 | let before = s.timeline_duration(); |
| 38 | let lanes = s.lanes(); |
| 39 | let pushes = s.history_pushes(); |
| 40 | let at = (fps * 0.5) as u32; |
| 41 | let words = [word("um", at, at + 6), word("hello", at + 8, at + 20)]; |
| 42 | let found = speech::find_fillers(&words, 0, (before * fps) as u32); |
| 43 | assert_eq!(found.len(), 1); |
| 44 | speech::cut_fillers(&mut s, &found, &lanes).expect("cut"); |
| 45 | let after = s.timeline_duration(); |
| 46 | assert!((before - after - 6.0 / fps).abs() < 1.5 / fps, "{before} -> {after}"); |
| 47 | // One step: a single undo restores the length. |
| 48 | assert!(s.undo()); |
| 49 | assert!((s.timeline_duration() - before).abs() < 1e-6); |
| 50 | assert!(s.history_pushes() > pushes); |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn nothing_to_cut_is_refused_and_changes_nothing() { |
| 55 | let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open"); |
| 56 | let lanes = s.lanes(); |
| 57 | let pushes = s.history_pushes(); |
| 58 | assert!(speech::cut_fillers(&mut s, &[], &lanes).is_err()); |
| 59 | assert_eq!(s.history_pushes(), pushes); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn normalize_moves_the_mix_to_the_target_in_one_undo_step() { |
| 64 | let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open"); |
| 65 | let gain_before = s.lane_gain_db(Lane::A1); |
| 66 | let report = normalize::normalize(&mut s, &Scope::Mix, -23.0, -1.0).expect("normalize"); |
| 67 | assert!(report.before.integrated_lufs.is_finite()); |
| 68 | assert!( |
| 69 | (report.after.integrated_lufs - -23.0).abs() < 0.5 || report.peak_held, |
| 70 | "landed on {} (before {})", |
| 71 | report.after.integrated_lufs, |
| 72 | report.before.integrated_lufs |
| 73 | ); |
| 74 | if (report.before.integrated_lufs - -23.0).abs() > 0.5 { |
| 75 | assert!((s.lane_gain_db(Lane::A1) - gain_before).abs() > 0.05, "the lane gain did not move"); |
| 76 | } |
| 77 | // One undo puts the gain back. |
| 78 | assert!(s.undo()); |
| 79 | assert!((s.lane_gain_db(Lane::A1) - gain_before).abs() < 1e-3); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn normalize_refuses_a_target_out_of_range_and_changes_nothing() { |
| 84 | let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open"); |
| 85 | let pushes = s.history_pushes(); |
| 86 | assert!(normalize::normalize(&mut s, &Scope::Mix, 5.0, -1.0).is_err()); |
| 87 | assert!(normalize::normalize(&mut s, &Scope::Mix, -14.0, 3.0).is_err()); |
| 88 | assert_eq!(s.history_pushes(), pushes); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn a_voiceover_renders_once_and_is_reused() { |
| 93 | let Ok(voices) = engine::tts::list_voices(&engine::tts::voices_dir()) else { |
| 94 | eprintln!("skipped: no piper voices installed"); |
| 95 | return; |
| 96 | }; |
| 97 | if voices.is_empty() || engine::tts::piper_bin().is_err() { |
| 98 | eprintln!("skipped: piper or its voices are not installed"); |
| 99 | return; |
| 100 | } |
| 101 | let dir = std::env::temp_dir().join(format!("edith-tts-test-{}", std::process::id())); |
| 102 | let first = engine::tts::render(&dir, "Hello there.", None, None, 1.0, 0.0).expect("render"); |
| 103 | assert!(!first.cached && first.file.exists()); |
| 104 | let again = engine::tts::render(&dir, "Hello there.", None, None, 1.0, 0.0).expect("render again"); |
| 105 | assert!(again.cached && again.file == first.file); |
| 106 | assert!(engine::probe_media(&first.file).duration_secs.unwrap_or(0.0) > 0.3); |
| 107 | let _ = std::fs::remove_dir_all(&dir); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn a_voiceover_refuses_empty_text_and_out_of_range_speed() { |
| 112 | let dir = std::env::temp_dir(); |
| 113 | assert!(engine::tts::render(&dir, " ", None, None, 1.0, 0.0).is_err()); |
| 114 | assert!(engine::tts::render(&dir, "hi", None, None, 9.0, 0.0).is_err()); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn listening_to_the_timeline_finds_the_words_when_speech_is_installed() { |
| 119 | if speech::available(speech::Device::Cpu).is_err() { |
| 120 | eprintln!("skipped: edith-stt or its model is not installed"); |
| 121 | return; |
| 122 | } |
| 123 | // The fixture is a tone, not speech: the pipeline must run end to end and say there are no |
| 124 | // words (or a silent mix), never crash or hang. |
| 125 | let s = PlaybackSession::open(asset("test_av.mp4")).expect("open"); |
| 126 | let job = speech::Job::default(); |
| 127 | let model = speech::installed_models(speech::Device::Cpu)[0]; |
| 128 | match speech::transcribe(&s, speech::Hear::Mix, model, speech::Device::Cpu, "en", false, &job) { |
| 129 | Ok(words) => assert!(words.len() < 20, "a tone is not a speech: {words:?}"), |
| 130 | Err(e) => assert!(!e.is_empty()), |
| 131 | } |
| 132 | } |