From c347b6133365dcf1b7da4e77890b20d04d6cfba4 Mon Sep 17 00:00:00 2001 From: Malte Voos Date: Fri, 5 Dec 2025 15:35:38 +0100 Subject: implement machine translation; various fixes and refactorings --- src/subtitles/state.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/subtitles/state.rs (limited to 'src/subtitles/state.rs') diff --git a/src/subtitles/state.rs b/src/subtitles/state.rs new file mode 100644 index 0000000..6b1ebda --- /dev/null +++ b/src/subtitles/state.rs @@ -0,0 +1,63 @@ +use crate::{ + subtitles::{SUBTITLE_TRACKS, StreamIndex}, + translation::TRANSLATIONS, + util::Tracker, +}; + +#[derive(Default)] +pub struct SubtitleState { + pub stream_ix: Option, + pub last_started_cue_ix: Tracker>, + pub last_ended_cue_ix: Tracker>, +} + +#[derive(Clone, Copy, Debug)] +pub struct CueAddress(pub StreamIndex, pub usize); + +impl SubtitleState { + pub fn active_cue(&self) -> Option { + if let Some(stream_ix) = self.stream_ix { + match (*self.last_started_cue_ix, *self.last_ended_cue_ix) { + (None, _) => None, + (Some(started_ix), None) => Some(CueAddress(stream_ix, started_ix)), + (Some(started_ix), Some(ended_ix)) => { + if started_ix > ended_ix { + Some(CueAddress(stream_ix, started_ix)) + } else { + None + } + } + } + } else { + None + } + } + + pub fn is_dirty(&self) -> bool { + self.last_started_cue_ix.is_dirty() || self.last_ended_cue_ix.is_dirty() + } + + pub fn reset(&mut self) { + self.last_started_cue_ix.reset(); + self.last_ended_cue_ix.reset(); + } + + pub fn set_stream_ix(&mut self, stream_ix: Option) { + self.stream_ix = stream_ix; + self.last_started_cue_ix.set(None); + self.last_ended_cue_ix.set(None); + } +} + +impl CueAddress { + pub fn resolve_text(&self) -> String { + SUBTITLE_TRACKS.read().get(&self.0).unwrap().texts[self.1].clone() + } + + pub fn resolve_translation(&self) -> Option { + TRANSLATIONS + .read() + .get(&self.0) + .and_then(|tln| tln.get(self.1).cloned()) + } +} -- cgit 1.4.1