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 Transcript dock tab's state and doings: listen to the timeline once, then edit by |
| 2 | //! words. Click a word to go there, select a run and delete it, remove the fillers. |
| 3 | //! |
| 4 | //! The listening is [`engine::speech`] (the Captions and Fillers cards' path) and every cut is |
| 5 | //! [`speech::cut_spans`] -> `PlaybackSession::cut_regions`, the call the MCP word cuts end in, |
| 6 | //! across every lane so linked picture and sound stay together, as one undo step. After a cut |
| 7 | //! the words are re-mapped through it ([`speech::remap_words`]); an undo or redo finds the |
| 8 | //! words that went with that state again, and any other edit to the timeline leaves the |
| 9 | //! transcript stale until it is run again. |
| 10 | |
| 11 | use std::sync::Arc; |
| 12 | |
| 13 | use engine::speech::{self, Hear, Job, Spoken}; |
| 14 | |
| 15 | use crate::creator; |
| 16 | use crate::keymap::ActionId; |
| 17 | use crate::player::tools::Running; |
| 18 | use crate::viewport::secs_label; |
| 19 | use crate::*; |
| 20 | |
| 21 | /// A silence this long (seconds) starts a new paragraph, as does a sentence end once a |
| 22 | /// paragraph has run [`PARA_WORDS`] words. |
| 23 | const PARA_GAP_SECS: f64 = 1.2; |
| 24 | const PARA_WORDS: usize = 18; |
| 25 | /// How many states of the transcript are kept for undo and redo to find again. |
| 26 | const KEEP: usize = 64; |
| 27 | |
| 28 | /// What identifies a state of the timeline well enough to tell whose words these are. |
| 29 | type Stamp = (usize, u32); |
| 30 | |
| 31 | /// The model and device a run goes to: the project's pick, else the first one installed for the |
| 32 | /// device it allows. `None` when no model is installed there. |
| 33 | pub(crate) fn speech_target(session: Option<&PlaybackSession>) -> Option<(String, speech::Device)> { |
| 34 | let choice = session.map_or(speech::DeviceChoice::Auto, PlaybackSession::speech_device); |
| 35 | let model = session.and_then(PlaybackSession::speech_model); |
| 36 | speech::plan(choice, model).filter(|(m, d)| speech::check_model(m, *d).is_ok()) |
| 37 | } |
| 38 | |
| 39 | /// The model the download button fetches and the device it goes to: the project's pick, else |
| 40 | /// large-v3-turbo on a GPU and base on the CPU. |
| 41 | pub(crate) fn speech_download(session: Option<&PlaybackSession>) -> (String, speech::Device) { |
| 42 | let choice = session.map_or(speech::DeviceChoice::Auto, PlaybackSession::speech_device); |
| 43 | let device = match choice { |
| 44 | speech::DeviceChoice::Cpu => speech::Device::Cpu, |
| 45 | speech::DeviceChoice::Gpu => speech::Device::Gpu, |
| 46 | speech::DeviceChoice::Auto if speech::gpu_available() => speech::Device::Gpu, |
| 47 | speech::DeviceChoice::Auto => speech::Device::Cpu, |
| 48 | }; |
| 49 | let model = session.and_then(PlaybackSession::speech_model).unwrap_or(match device { |
| 50 | speech::Device::Gpu => "large-v3-turbo", |
| 51 | speech::Device::Cpu => "base", |
| 52 | }); |
| 53 | (model.to_string(), device) |
| 54 | } |
| 55 | |
| 56 | pub(crate) struct Transcript { |
| 57 | /// The words as the timeline stands (timeline frames), `None` before the first listen. |
| 58 | pub(crate) words: Option<Vec<Spoken>>, |
| 59 | /// The timeline state `words` belong to, and the earlier and later ones undo can reach. |
| 60 | stamp: Stamp, |
| 61 | states: Vec<(Stamp, Vec<Spoken>)>, |
| 62 | /// True when the timeline was edited some other way since: the words no longer line up. |
| 63 | pub(crate) stale: bool, |
| 64 | /// The selected run: where it was started and where it reaches now (word indices). |
| 65 | pub(crate) anchor: Option<usize>, |
| 66 | pub(crate) head: usize, |
| 67 | pub(crate) dragging: bool, |
| 68 | pub(crate) search: String, |
| 69 | pub(crate) search_edit: bool, |
| 70 | /// The match the search is on now (a word index), once it was typed or stepped to. |
| 71 | pub(crate) search_at: Option<usize>, |
| 72 | pub(crate) running: Option<Running>, |
| 73 | /// The speech model being downloaded: its progress and cancel, and which download it is. |
| 74 | pub(crate) download: Option<(Arc<Job>, u64)>, |
| 75 | pub(crate) runs: u64, |
| 76 | pub(crate) status: String, |
| 77 | pub(crate) scroll: ScrollHandle, |
| 78 | /// The paragraph the playing word was last scrolled to, so the view moves once per paragraph. |
| 79 | pub(crate) followed: std::cell::Cell<Option<usize>>, |
| 80 | } |
| 81 | |
| 82 | impl Transcript { |
| 83 | pub(crate) fn new() -> Self { |
| 84 | Transcript { |
| 85 | words: None, |
| 86 | stamp: (0, 0), |
| 87 | states: Vec::new(), |
| 88 | stale: false, |
| 89 | anchor: None, |
| 90 | head: 0, |
| 91 | dragging: false, |
| 92 | search: String::new(), |
| 93 | search_edit: false, |
| 94 | search_at: None, |
| 95 | running: None, |
| 96 | download: None, |
| 97 | runs: 0, |
| 98 | status: String::new(), |
| 99 | scroll: ScrollHandle::new(), |
| 100 | followed: std::cell::Cell::new(None), |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// The selected run, first to last word, when there is one. |
| 105 | pub(crate) fn selection(&self) -> Option<(usize, usize)> { |
| 106 | // A stale transcript has no selection: its words no longer line up with the timeline. |
| 107 | if self.stale { |
| 108 | return None; |
| 109 | } |
| 110 | let n = self.words.as_ref()?.len(); |
| 111 | let a = self.anchor?; |
| 112 | let (lo, hi) = (a.min(self.head), a.max(self.head)); |
| 113 | (n > 0).then(|| (lo.min(n - 1), hi.min(n - 1))) |
| 114 | } |
| 115 | |
| 116 | /// Whether word `i` matches the search box (a word that contains what was typed). |
| 117 | pub(crate) fn matches(&self, i: usize) -> bool { |
| 118 | let q = speech::norm(&self.search); |
| 119 | !q.is_empty() && self.words.as_ref().and_then(|w| w.get(i)).is_some_and(|w| speech::norm(&w.text).contains(&q)) |
| 120 | } |
| 121 | |
| 122 | /// Every word index that matches the search, in order. |
| 123 | pub(crate) fn match_list(&self) -> Vec<usize> { |
| 124 | self.words.as_ref().map_or_else(Vec::new, |w| (0..w.len()).filter(|&i| self.matches(i)).collect()) |
| 125 | } |
| 126 | |
| 127 | /// Paragraphs as `(first, last)` word indices. |
| 128 | pub(crate) fn paragraphs(&self, fps: f64) -> Vec<(usize, usize)> { |
| 129 | let Some(words) = self.words.as_ref() else { |
| 130 | return Vec::new(); |
| 131 | }; |
| 132 | let gap = (PARA_GAP_SECS * fps).round() as u32; |
| 133 | let mut out: Vec<(usize, usize)> = Vec::new(); |
| 134 | for (i, w) in words.iter().enumerate() { |
| 135 | let breaks = match out.last() { |
| 136 | None => true, |
| 137 | Some(&(first, _)) => { |
| 138 | let prev = &words[i - 1]; |
| 139 | w.start_f.saturating_sub(prev.end_f) >= gap |
| 140 | || (i - first >= PARA_WORDS && prev.text.trim_end().ends_with(['.', '?', '!'])) |
| 141 | } |
| 142 | }; |
| 143 | match breaks { |
| 144 | true => out.push((i, i)), |
| 145 | false => out.last_mut().unwrap().1 = i, |
| 146 | } |
| 147 | } |
| 148 | out |
| 149 | } |
| 150 | |
| 151 | /// The word the playhead is in, or the one that just ended. |
| 152 | pub(crate) fn playing_word(&self, frame: u32, fps: f64) -> Option<usize> { |
| 153 | let words = self.words.as_ref()?; |
| 154 | let i = words.partition_point(|w| w.start_f <= frame).checked_sub(1)?; |
| 155 | let hold = words[i].end_f.max(words[i].start_f + 1) + (fps / 4.0).round() as u32; |
| 156 | (frame < hold).then_some(i) |
| 157 | } |
| 158 | |
| 159 | fn adopt(&mut self, stamp: Stamp, words: Vec<Spoken>) { |
| 160 | self.states.retain(|(s, _)| *s != stamp); |
| 161 | self.states.push((stamp, words.clone())); |
| 162 | if self.states.len() > KEEP { |
| 163 | self.states.remove(0); |
| 164 | } |
| 165 | self.words = Some(words); |
| 166 | self.stamp = stamp; |
| 167 | self.stale = false; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | impl Player { |
| 172 | fn timeline_stamp(&self) -> Option<Stamp> { |
| 173 | let s = self.session.as_ref()?; |
| 174 | Some((s.undo_depth(), (s.timeline_duration() * self.fps).round() as u32)) |
| 175 | } |
| 176 | |
| 177 | /// Once a frame while the tab is up: undo and redo bring back the words of the state they |
| 178 | /// land on; a timeline that is in no state we know leaves the transcript stale. |
| 179 | pub(crate) fn sync_transcript(&mut self) { |
| 180 | if !self.transcript_open || self.transcript.words.is_none() { |
| 181 | return; |
| 182 | } |
| 183 | let Some(now) = self.timeline_stamp() else { |
| 184 | return; |
| 185 | }; |
| 186 | if now == self.transcript.stamp { |
| 187 | return; |
| 188 | } |
| 189 | let t = &mut self.transcript; |
| 190 | match t.states.iter().find(|(s, _)| *s == now) { |
| 191 | Some((_, words)) => { |
| 192 | t.words = Some(words.clone()); |
| 193 | t.stamp = now; |
| 194 | t.stale = false; |
| 195 | } |
| 196 | None => t.stale = true, |
| 197 | } |
| 198 | // Whatever was selected belonged to the words before; a stale transcript keeps none. |
| 199 | t.anchor = None; |
| 200 | t.dragging = false; |
| 201 | } |
| 202 | |
| 203 | pub(crate) fn open_transcript(&mut self, cx: &mut Context<Self>) { |
| 204 | self.close_card(); |
| 205 | self.transcript_open = true; |
| 206 | self.transcript.followed.set(None); |
| 207 | cx.notify(); |
| 208 | } |
| 209 | |
| 210 | /// The playhead's frame. |
| 211 | pub(crate) fn transcript_frame(&self) -> u32 { |
| 212 | self.session.as_ref().map_or(0, |s| (s.now() * self.fps).floor().max(0.0) as u32) |
| 213 | } |
| 214 | |
| 215 | // ---- listening ---------------------------------------------------------------- |
| 216 | |
| 217 | /// Listens to the whole mix on a thread, with progress and Cancel on the tab. |
| 218 | pub(crate) fn start_transcribing(&mut self, cx: &mut Context<Self>) { |
| 219 | if self.transcript.running.is_some() { |
| 220 | return; |
| 221 | } |
| 222 | let fail = |this: &mut Self, why: String, cx: &mut Context<Self>| { |
| 223 | this.transcript.status = why.clone(); |
| 224 | this.notify_user(why.into()); |
| 225 | cx.notify(); |
| 226 | }; |
| 227 | if let Err(why) = speech::stt_bin() { |
| 228 | return fail(self, why, cx); |
| 229 | } |
| 230 | let Some((model, device)) = speech_target(self.session.as_ref()) else { |
| 231 | return fail(self, "No speech model is installed".into(), cx); |
| 232 | }; |
| 233 | if let Err(why) = speech::available(device) { |
| 234 | return fail(self, why, cx); |
| 235 | } |
| 236 | let Some(session) = self.session.as_ref() else { |
| 237 | return; |
| 238 | }; |
| 239 | let heard = match speech::prepare(session, Hear::Mix) { |
| 240 | Ok(h) => h, |
| 241 | Err(why) => return fail(self, why, cx), |
| 242 | }; |
| 243 | let job = Arc::new(Job::default()); |
| 244 | let id = self.transcript.runs.wrapping_add(1); |
| 245 | self.transcript.runs = id; |
| 246 | self.transcript.running = Some(Running { what: "Transcribing", job: job.clone(), id }); |
| 247 | self.transcript.status.clear(); |
| 248 | let work = cx.background_executor().spawn({ |
| 249 | let job = job.clone(); |
| 250 | async move { speech::listen(heard, &model, device, "auto", true, &job) } |
| 251 | }); |
| 252 | cx.spawn(async move |this, cx| { |
| 253 | let heard = work.await; |
| 254 | this.update(cx, |this, cx| this.finish_transcribing(id, heard, cx)).ok(); |
| 255 | }) |
| 256 | .detach(); |
| 257 | // The bar moves while the thread works: repaint until this run is over. |
| 258 | cx.spawn(async move |this, cx| { |
| 259 | loop { |
| 260 | cx.background_executor().timer(std::time::Duration::from_millis(200)).await; |
| 261 | let live = this |
| 262 | .update(cx, |this, cx| { |
| 263 | let live = this.transcript.running.as_ref().is_some_and(|r| r.id == id); |
| 264 | cx.notify(); |
| 265 | live |
| 266 | }) |
| 267 | .unwrap_or(false); |
| 268 | if !live { |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | }) |
| 273 | .detach(); |
| 274 | cx.notify(); |
| 275 | } |
| 276 | |
| 277 | fn finish_transcribing(&mut self, id: u64, heard: Result<Vec<Spoken>, String>, cx: &mut Context<Self>) { |
| 278 | if self.transcript.running.as_ref().is_none_or(|r| r.id != id) { |
| 279 | return; |
| 280 | } |
| 281 | self.transcript.running = None; |
| 282 | match heard { |
| 283 | Ok(words) if words.is_empty() => self.transcript.status = "No speech found".into(), |
| 284 | Ok(words) => { |
| 285 | if let Some(stamp) = self.timeline_stamp() { |
| 286 | self.transcript.states.clear(); |
| 287 | self.transcript.anchor = None; |
| 288 | self.transcript.adopt(stamp, words); |
| 289 | } |
| 290 | } |
| 291 | Err(e) if e == speech::CANCELLED => self.transcript.status = "Cancelled".into(), |
| 292 | Err(e) => { |
| 293 | self.transcript.status = e.clone(); |
| 294 | self.notify_user(e.into()); |
| 295 | } |
| 296 | } |
| 297 | cx.notify(); |
| 298 | } |
| 299 | |
| 300 | /// Fetches the speech model the project chose (see [`speech_download`]) with progress, off the |
| 301 | /// UI thread. Nothing is started while one is already coming. |
| 302 | pub(crate) fn start_model_download(&mut self, cx: &mut Context<Self>) { |
| 303 | if self.transcript.download.is_some() { |
| 304 | return; |
| 305 | } |
| 306 | let (model, device) = speech_download(self.session.as_ref()); |
| 307 | let job = Arc::new(Job::default()); |
| 308 | let id = self.transcript.runs.wrapping_add(1); |
| 309 | self.transcript.runs = id; |
| 310 | self.transcript.download = Some((job.clone(), id)); |
| 311 | self.transcript.status.clear(); |
| 312 | let work = cx.background_executor().spawn({ |
| 313 | let job = job.clone(); |
| 314 | async move { speech::download_model(&model, device, &job) } |
| 315 | }); |
| 316 | cx.spawn(async move |this, cx| { |
| 317 | let done = work.await; |
| 318 | this.update(cx, |this, cx| { |
| 319 | if this.transcript.download.as_ref().is_none_or(|(_, d)| *d != id) { |
| 320 | return; |
| 321 | } |
| 322 | this.transcript.download = None; |
| 323 | match done { |
| 324 | Ok(()) => this.notify_user("Speech model installed — Transcribe is ready".into()), |
| 325 | Err(e) if e == speech::CANCELLED => this.transcript.status = "Download cancelled".into(), |
| 326 | Err(e) => this.transcript.status = format!("Download failed: {e}"), |
| 327 | } |
| 328 | cx.notify(); |
| 329 | }) |
| 330 | .ok(); |
| 331 | }) |
| 332 | .detach(); |
| 333 | // The bar moves while the thread works: repaint until this download is over. |
| 334 | cx.spawn(async move |this, cx| { |
| 335 | loop { |
| 336 | cx.background_executor().timer(std::time::Duration::from_millis(200)).await; |
| 337 | let live = this |
| 338 | .update(cx, |this, cx| { |
| 339 | cx.notify(); |
| 340 | this.transcript.download.as_ref().is_some_and(|(_, d)| *d == id) |
| 341 | }) |
| 342 | .unwrap_or(false); |
| 343 | if !live { |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | }) |
| 348 | .detach(); |
| 349 | cx.notify(); |
| 350 | } |
| 351 | |
| 352 | pub(crate) fn cancel_model_download(&mut self, cx: &mut Context<Self>) { |
| 353 | if let Some((job, _)) = self.transcript.download.take() { |
| 354 | job.cancel.store(true, std::sync::atomic::Ordering::Relaxed); |
| 355 | self.transcript.status = "Download cancelled".into(); |
| 356 | } |
| 357 | cx.notify(); |
| 358 | } |
| 359 | |
| 360 | /// Opens the folder the speech models live in with the desktop's own file manager. |
| 361 | pub(crate) fn open_models_folder(&mut self, cx: &mut Context<Self>) { |
| 362 | let dir = speech::models_dir(); |
| 363 | let _ = std::fs::create_dir_all(&dir); |
| 364 | match std::process::Command::new("xdg-open").arg(&dir).stdin(std::process::Stdio::null()).spawn() { |
| 365 | Ok(_) => {} |
| 366 | Err(_) => self.notify_user(format!("Models go in {}", dir.display()).into()), |
| 367 | } |
| 368 | cx.notify(); |
| 369 | } |
| 370 | |
| 371 | pub(crate) fn cancel_transcribing(&mut self, cx: &mut Context<Self>) { |
| 372 | if let Some(running) = self.transcript.running.take() { |
| 373 | running.job.cancel.store(true, std::sync::atomic::Ordering::Relaxed); |
| 374 | self.transcript.status = "Cancelled".into(); |
| 375 | } |
| 376 | cx.notify(); |
| 377 | } |
| 378 | |
| 379 | // ---- reading ------------------------------------------------------------------ |
| 380 | |
| 381 | /// A press on word `i`: it starts a selection (shift extends the last one) and, unextended, |
| 382 | /// takes the playhead to the word. |
| 383 | pub(crate) fn press_word(&mut self, i: usize, extend: bool, cx: &mut Context<Self>) { |
| 384 | let Some(frame) = self.transcript.words.as_ref().and_then(|w| w.get(i)).map(|w| w.start_f) else { |
| 385 | return; |
| 386 | }; |
| 387 | let t = &mut self.transcript; |
| 388 | match (extend, t.anchor) { |
| 389 | (true, Some(_)) => t.head = i, |
| 390 | _ => { |
| 391 | t.anchor = Some(i); |
| 392 | t.head = i; |
| 393 | t.dragging = true; |
| 394 | let at = f64::from(frame) / self.fps; |
| 395 | self.seek(at, cx); |
| 396 | } |
| 397 | } |
| 398 | cx.notify(); |
| 399 | } |
| 400 | |
| 401 | pub(crate) fn drag_word(&mut self, i: usize, cx: &mut Context<Self>) { |
| 402 | if self.transcript.dragging && self.transcript.head != i { |
| 403 | self.transcript.head = i; |
| 404 | cx.notify(); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | pub(crate) fn release_word(&mut self, cx: &mut Context<Self>) { |
| 409 | if self.transcript.dragging { |
| 410 | self.transcript.dragging = false; |
| 411 | cx.notify(); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /// Moves to the next (or previous) match: selects the word, takes the playhead there and |
| 416 | /// scrolls its paragraph into view. The first step lands on the first match after the playhead |
| 417 | /// (before it, going back); stepping past either end wraps round. |
| 418 | pub(crate) fn step_match(&mut self, forward: bool, cx: &mut Context<Self>) { |
| 419 | let frame = self.transcript_frame(); |
| 420 | let list = self.transcript.match_list(); |
| 421 | let Some(words) = self.transcript.words.as_ref() else { |
| 422 | return; |
| 423 | }; |
| 424 | let pos = match self.transcript.search_at.and_then(|c| list.iter().position(|&i| i == c)) { |
| 425 | Some(p) if forward => (p + 1) % list.len(), |
| 426 | Some(p) => (p + list.len() - 1) % list.len(), |
| 427 | None if list.is_empty() => return, |
| 428 | None if forward => list.iter().position(|&i| words[i].start_f > frame).unwrap_or(0), |
| 429 | None => list.iter().rposition(|&i| words[i].start_f < frame).unwrap_or(list.len() - 1), |
| 430 | }; |
| 431 | let i = list[pos]; |
| 432 | let secs = f64::from(words[i].start_f) / self.fps; |
| 433 | self.transcript.search_at = Some(i); |
| 434 | self.transcript.anchor = Some(i); |
| 435 | self.transcript.head = i; |
| 436 | self.scroll_transcript_to(i); |
| 437 | self.seek(secs, cx); |
| 438 | } |
| 439 | |
| 440 | /// The query changed: the current match is the first one, scrolled into view (the playhead |
| 441 | /// and the selection stay where they are while typing). |
| 442 | pub(crate) fn search_changed(&mut self) { |
| 443 | let first = self.transcript.match_list().first().copied(); |
| 444 | self.transcript.search_at = first; |
| 445 | if let Some(i) = first { |
| 446 | self.scroll_transcript_to(i); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /// Scrolls the paragraph holding word `i` into view. |
| 451 | fn scroll_transcript_to(&self, i: usize) { |
| 452 | if let Some(p) = self.transcript.paragraphs(self.fps).iter().position(|&(a, b)| (a..=b).contains(&i)) { |
| 453 | self.transcript.scroll.scroll_to_item(p); |
| 454 | self.transcript.followed.set(Some(p)); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /// Empties the search box, and with it the matches. |
| 459 | pub(crate) fn clear_search(&mut self) { |
| 460 | self.transcript.search.clear(); |
| 461 | self.transcript.search_at = None; |
| 462 | } |
| 463 | |
| 464 | // ---- editing ------------------------------------------------------------------ |
| 465 | |
| 466 | /// Cuts the selected words out of every lane, closing the hole: one undo step. |
| 467 | pub(crate) fn delete_selected_words(&mut self, cx: &mut Context<Self>) { |
| 468 | let Some((lo, hi)) = self.transcript.selection() else { |
| 469 | return; |
| 470 | }; |
| 471 | if self.transcript.stale { |
| 472 | return; |
| 473 | } |
| 474 | let Some(words) = self.transcript.words.clone() else { |
| 475 | return; |
| 476 | }; |
| 477 | let Some(session) = self.session.as_mut() else { |
| 478 | return; |
| 479 | }; |
| 480 | let end = (session.timeline_duration() * self.fps).round() as u32; |
| 481 | let pad = (0.06 * self.fps).round() as u32; |
| 482 | let Some(span) = speech::word_span(&words, lo, hi, pad, end) else { |
| 483 | return; |
| 484 | }; |
| 485 | let lanes = session.lanes(); |
| 486 | let before = session.timeline_duration(); |
| 487 | let n = hi - lo + 1; |
| 488 | let line = match speech::cut_spans(session, &[span], &lanes) { |
| 489 | Ok(regions) => { |
| 490 | let shorter = before - session.timeline_duration(); |
| 491 | let kept = speech::remap_words(&words, ®ions); |
| 492 | self.finish_word_edit(kept); |
| 493 | format!( |
| 494 | "{n} word{} cut — {} shorter · undo {}", |
| 495 | if n == 1 { "" } else { "s" }, |
| 496 | secs_label(shorter), |
| 497 | self.keymap.display(ActionId::Undo) |
| 498 | ) |
| 499 | } |
| 500 | Err(e) => format!("Nothing cut — {e}"), |
| 501 | }; |
| 502 | self.notify_user(line.into()); |
| 503 | cx.notify(); |
| 504 | } |
| 505 | |
| 506 | /// Cuts every filler word out of every lane: one undo step. |
| 507 | pub(crate) fn remove_transcript_fillers(&mut self, cx: &mut Context<Self>) { |
| 508 | let Some(words) = self.transcript.words.clone() else { |
| 509 | return; |
| 510 | }; |
| 511 | if self.transcript.stale { |
| 512 | return; |
| 513 | } |
| 514 | let Some(session) = self.session.as_mut() else { |
| 515 | return; |
| 516 | }; |
| 517 | let found = creator::fillers_in(session, &words); |
| 518 | if found.is_empty() { |
| 519 | self.notify_user("No filler words".into()); |
| 520 | return cx.notify(); |
| 521 | } |
| 522 | let before = session.timeline_duration(); |
| 523 | let line = match creator::cut_fillers_regions(session, &found) { |
| 524 | Ok(regions) => { |
| 525 | let shorter = before - session.timeline_duration(); |
| 526 | let n = found.len(); |
| 527 | self.finish_word_edit(speech::remap_words(&words, ®ions)); |
| 528 | format!( |
| 529 | "{n} filler{} cut — {} shorter · undo {}", |
| 530 | if n == 1 { "" } else { "s" }, |
| 531 | secs_label(shorter), |
| 532 | self.keymap.display(ActionId::Undo) |
| 533 | ) |
| 534 | } |
| 535 | Err(e) => format!("Nothing cut — {e}"), |
| 536 | }; |
| 537 | self.notify_user(line.into()); |
| 538 | cx.notify(); |
| 539 | } |
| 540 | |
| 541 | /// What follows a cut: the words re-mapped become the transcript, the picture reseeks. |
| 542 | fn finish_word_edit(&mut self, kept: Vec<Spoken>) { |
| 543 | self.selected.clear(); |
| 544 | self.reset_after_reseek(); |
| 545 | self.mark_dirty(); |
| 546 | self.transcript.anchor = None; |
| 547 | if let Some(stamp) = self.timeline_stamp() { |
| 548 | self.transcript.adopt(stamp, kept); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // ---- keys --------------------------------------------------------------------- |
| 553 | |
| 554 | /// The tab's own keys, asked ahead of the keymap. The search field owns the keyboard once it |
| 555 | /// was clicked into; otherwise the tab answers only while it has focus, and only the strokes |
| 556 | /// that edit its words (Delete, Backspace, Esc, arrows over a selection, Ctrl+A): Space, S and |
| 557 | /// the rest still reach the timeline. `true` when the stroke was taken. |
| 558 | pub(crate) fn transcript_key(&mut self, key: &str, ctrl: bool, shift: bool, alt: bool, focused: bool, cx: &mut Context<Self>) -> bool { |
| 559 | if !self.transcript_open { |
| 560 | return false; |
| 561 | } |
| 562 | // Typing ends when the focus goes elsewhere (a click on the timeline). |
| 563 | if self.transcript.search_edit && !focused { |
| 564 | self.transcript.search_edit = false; |
| 565 | } |
| 566 | if self.transcript.search_edit { |
| 567 | match key { |
| 568 | // Esc clears what was typed; with nothing typed it leaves the field. |
| 569 | "escape" if self.transcript.search.is_empty() => self.transcript.search_edit = false, |
| 570 | "escape" => self.clear_search(), |
| 571 | "enter" => self.step_match(!shift, cx), |
| 572 | "backspace" => { |
| 573 | self.transcript.search.pop(); |
| 574 | self.search_changed(); |
| 575 | } |
| 576 | // Any other stroke is the search box's too: it is not passed on to the words or the keymap. |
| 577 | _ => { |
| 578 | if let Some(c) = crate::menus::typed(key).filter(|_| !ctrl && !alt) { |
| 579 | self.transcript.search.push(c); |
| 580 | self.search_changed(); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | cx.notify(); |
| 585 | return true; |
| 586 | } |
| 587 | if !focused || alt { |
| 588 | return false; |
| 589 | } |
| 590 | let n = self.transcript.words.as_ref().map_or(0, Vec::len); |
| 591 | let sel = self.transcript.selection(); |
| 592 | let taken = match (key, ctrl) { |
| 593 | ("delete" | "backspace", false) => { |
| 594 | self.delete_selected_words(cx); |
| 595 | true |
| 596 | } |
| 597 | ("a", true) if n > 0 => { |
| 598 | self.transcript.anchor = Some(0); |
| 599 | self.transcript.head = n - 1; |
| 600 | true |
| 601 | } |
| 602 | ("escape", false) if !self.transcript.search.is_empty() => { |
| 603 | self.clear_search(); |
| 604 | true |
| 605 | } |
| 606 | ("escape", false) if sel.is_some() => { |
| 607 | self.transcript.anchor = None; |
| 608 | true |
| 609 | } |
| 610 | ("left" | "right", false) if sel.is_some() && n > 0 => { |
| 611 | let step = if key == "left" { -1i64 } else { 1 }; |
| 612 | let to = (self.transcript.head as i64 + step).clamp(0, n as i64 - 1) as usize; |
| 613 | self.transcript.head = to; |
| 614 | if !shift { |
| 615 | self.transcript.anchor = Some(to); |
| 616 | let frame = self.transcript.words.as_ref().map_or(0, |w| w[to].start_f); |
| 617 | self.seek(f64::from(frame) / self.fps, cx); |
| 618 | } |
| 619 | true |
| 620 | } |
| 621 | _ => false, |
| 622 | }; |
| 623 | if taken { |
| 624 | cx.notify(); |
| 625 | } |
| 626 | taken |
| 627 | } |
| 628 | } |