subx_cli/core/sync/dialogue/segment.rs
1//! Dialogue segment representing either speech or silence interval.
2//!
3//! This struct stores timing and confidence information for detected
4//! dialogue or silence regions in audio tracks.
5//!
6//! # Examples
7//!
8//! ```rust
9//! use subx_cli::core::sync::dialogue::segment::DialogueSegment;
10//! let speech = DialogueSegment::new_speech(0.0, 1.5);
11//! ```
12
13/// Represents a segment of audio containing dialogue or silence.
14///
15/// Used by the audio synchronization system to identify speech patterns
16/// in audio files for subtitle timing alignment.
17#[derive(Debug, Clone)]
18pub struct DialogueSegment {
19 /// Start time of the segment in seconds
20 pub start_time: f64,
21 /// End time of the segment in seconds
22 pub end_time: f64,
23 /// Whether this segment contains speech
24 pub is_speech: bool,
25 /// Confidence level of the speech detection (0.0 to 1.0)
26 pub confidence: f32,
27}
28
29impl DialogueSegment {
30 /// Creates a new `DialogueSegment` representing a speech interval.
31 pub fn new_speech(start: f64, end: f64) -> Self {
32 Self {
33 start_time: start,
34 end_time: end,
35 is_speech: true,
36 confidence: 1.0,
37 }
38 }
39
40 /// Creates a new `DialogueSegment` representing a silence interval.
41 pub fn new_silence(start: f64, end: f64) -> Self {
42 Self {
43 start_time: start,
44 end_time: end,
45 is_speech: false,
46 confidence: 1.0,
47 }
48 }
49
50 /// Returns the duration of the segment in seconds.
51 pub fn duration(&self) -> f64 {
52 self.end_time - self.start_time
53 }
54
55 /// Determines if this segment overlaps with another segment.
56 pub fn overlaps_with(&self, other: &DialogueSegment) -> bool {
57 self.start_time < other.end_time && self.end_time > other.start_time
58 }
59
60 /// Merge with other segments of the same type, updating boundaries and confidence
61 pub fn merge_with(&mut self, other: &DialogueSegment) {
62 if self.is_speech == other.is_speech && self.overlaps_with(other) {
63 self.start_time = self.start_time.min(other.start_time);
64 self.end_time = self.end_time.max(other.end_time);
65 self.confidence = (self.confidence + other.confidence) / 2.0;
66 }
67 }
68}
69
70/// Silence segment data structure for additional processing or analysis.
71///
72/// Represents periods of silence in audio that can be used for
73/// subtitle timing adjustments and synchronization optimization.
74#[derive(Debug, Clone)]
75pub struct SilenceSegment {
76 /// Start time of the silence segment in seconds
77 pub start_time: f64,
78 /// End time of the silence segment in seconds
79 pub end_time: f64,
80 /// Duration of the silence segment in seconds
81 pub duration: f64,
82}
83
84impl SilenceSegment {
85 /// Creates a new silence segment with the specified time range.
86 pub fn new(start: f64, end: f64) -> Self {
87 Self {
88 start_time: start,
89 end_time: end,
90 duration: end - start,
91 }
92 }
93
94 /// Check if this silence segment exceeds minimum length
95 pub fn is_significant(&self, min_duration: f64) -> bool {
96 self.duration >= min_duration
97 }
98}