ilass 2.1.0

Automatic Language-Agnostic Subtitle Synchronization (Library)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// This file is part of the Rust library and binary `ilass`.
//
// Copyright (C) 2017 kaegi
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

use std;
use std::cmp::{max, min, Ordering};
use std::ops::*;

/// Implements conversion to integer variables for TimeDelta and TimePoint.
macro_rules! impl_from {
    ($f:ty, $t:ty) => {
        impl From<$f> for $t {
            fn from(t: $f) -> $t {
                t.0 as $t
            }
        }
    };
}

/// This struct represents a time difference between two `TimePoints`.
/// Internally its an integer type.
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct TimeDelta(i64);

impl TimeDelta {
    /// No difference in time.
    pub fn zero() -> TimeDelta {
        TimeDelta(Default::default())
    }

    /// Smallest positive time difference the library can work with.
    pub fn one() -> TimeDelta {
        TimeDelta(1)
    }

    /// Create time delta as "TimeDelta::one() * v".
    pub fn from_i64(v: i64) -> TimeDelta {
        TimeDelta(v)
    }

    /// Return time difference as f64.
    pub fn as_f64(&self) -> f64 {
        self.0 as f64
    }

    /// Return time difference as f64.
    pub fn as_f32(&self) -> f32 {
        self.0 as f32
    }

    /// Return time difference as i64.
    pub fn as_i64(&self) -> i64 {
        self.0 as i64
    }
}

impl_from!(TimeDelta, i32);
impl_from!(TimeDelta, u32);
impl_from!(TimeDelta, i64);
impl_from!(TimeDelta, u64);

impl std::iter::Sum for TimeDelta {
    fn sum<I: Iterator<Item = TimeDelta>>(iter: I) -> TimeDelta {
        TimeDelta(iter.map(|d| d.0).sum())
    }
}

impl std::fmt::Display for TimePoint {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl std::fmt::Display for TimeDelta {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Add for TimeDelta {
    type Output = TimeDelta;
    fn add(self, rhs: TimeDelta) -> TimeDelta {
        TimeDelta(self.0 + rhs.0)
    }
}

impl AddAssign<TimeDelta> for TimeDelta {
    fn add_assign(&mut self, rhs: TimeDelta) {
        self.0 += rhs.0;
    }
}

impl Sub<TimeDelta> for TimeDelta {
    type Output = TimeDelta;
    fn sub(self, rhs: TimeDelta) -> TimeDelta {
        TimeDelta(self.0 - rhs.0)
    }
}

impl SubAssign<TimeDelta> for TimeDelta {
    fn sub_assign(&mut self, rhs: TimeDelta) {
        self.0 -= rhs.0;
    }
}

impl Mul<i64> for TimeDelta {
    type Output = TimeDelta;
    fn mul(self, rhs: i64) -> TimeDelta {
        TimeDelta(self.0 * rhs)
    }
}

impl MulAssign<i64> for TimeDelta {
    fn mul_assign(&mut self, rhs: i64) {
        self.0 *= rhs;
    }
}

impl Mul<TimeDelta> for i64 {
    type Output = TimeDelta;
    fn mul(self, rhs: TimeDelta) -> TimeDelta {
        TimeDelta(self * rhs.0)
    }
}

impl Neg for TimeDelta {
    type Output = TimeDelta;
    fn neg(self) -> TimeDelta {
        TimeDelta(-self.0)
    }
}

// //////////////////////////////////////////////////////////////////////////////////////////////////
// struct TimeSpan

/// Represents a timepoint in your own metric.
///
/// A timepoint is internally represented by an integer (because the align
/// algorithm needs discrete
/// time steps). You will have to choose your own metric: for example 1i64 means
/// 2ms. The internal algorithm does not use any non-user given `TimePoint`s
/// (so its interpretation is
/// up to you).
///
/// This is the reason this library works with `TimePoint` and `TimeDelta`: to
/// enforce
/// an absolute and delta relationship an a own metric.
///
/// The only way to create a new `TimePoint` is with `TimePoint::from({i64})`.
///
/// ```
/// use ilass::TimePoint;
///
/// let p = TimePoint::from(10);
///
/// // to get that i64 again
/// let i1: i64 = p.into();
/// let i2 = i64::from(p);
/// ```
///
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct TimePoint(i64);

impl TimePoint {
    /// Returns a f32 for the given time point.
    pub fn as_f32(self) -> f32 {
        self.0 as f32
    }

    /// Returns a f32 for the given time point.
    pub fn as_f64(self) -> f64 {
        self.0 as f64
    }

    /// Returns a i64 for the given time point.
    pub fn as_i64(self) -> i64 {
        self.0 as i64
    }
}

impl From<i64> for TimePoint {
    fn from(f: i64) -> TimePoint {
        TimePoint(f)
    }
}
impl_from!(TimePoint, i64);

impl Sub for TimePoint {
    type Output = TimeDelta;
    fn sub(self, rhs: TimePoint) -> TimeDelta {
        TimeDelta(self.0 - rhs.0)
    }
}

impl Add<TimeDelta> for TimePoint {
    type Output = TimePoint;
    fn add(self, rhs: TimeDelta) -> TimePoint {
        TimePoint(self.0 + rhs.0)
    }
}

impl AddAssign<TimeDelta> for TimePoint {
    fn add_assign(&mut self, rhs: TimeDelta) {
        self.0 += rhs.0;
    }
}

impl Sub<TimeDelta> for TimePoint {
    type Output = TimePoint;
    fn sub(self, rhs: TimeDelta) -> TimePoint {
        TimePoint(self.0 - rhs.0)
    }
}

impl SubAssign<TimeDelta> for TimePoint {
    fn sub_assign(&mut self, rhs: TimeDelta) {
        self.0 -= rhs.0;
    }
}

// //////////////////////////////////////////////////////////////////////////////////////////////////
// struct TimeSpan

/// Represents a time span from "start" (included) to "end" (excluded).
///
/// The constructors will ensure "start <= end", this condition will hold at
/// any given time.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct TimeSpan {
    /// The first time point of the time span (inclusive)
    pub start: TimePoint,

    /// The last time point of the time span (excluded)
    pub end: TimePoint,
}

impl TimeSpan {
    /// Create a new TimeSpan with `start` and `end`.
    ///
    /// # Examples
    /// ```rust
    /// use ilass::{TimeSpan, TimePoint};
    ///
    /// let t0 = TimePoint::from(0);
    /// let t10 = TimePoint::from(10);
    ///
    /// let ts = TimeSpan::new(t0, t10);
    /// ```
    ///
    /// # Panics
    ///
    ///
    /// This function asserts that `start` is less or equal `end`.
    ///
    /// ```rust,should_panic
    /// use ilass::{TimeSpan, TimePoint};
    ///
    /// let t0 = TimePoint::from(0);
    /// let t10 = TimePoint::from(10);
    ///
    /// // this will case a panic
    /// let ts = TimeSpan::new(t10, t0);
    /// ```
    #[inline]
    pub fn new(start: TimePoint, end: TimePoint) -> TimeSpan {
        assert!(start <= end);
        TimeSpan { start: start, end: end }
    }

    /// Create a new TimeSpan with `start` and `end`. This function will not
    /// panic on `end < start`, but
    /// swap the values before calling `TimeSpan::new()`.
    ///
    /// # Examples
    /// ```rust
    /// use ilass::{TimeSpan, TimePoint};
    ///
    /// let t0 = TimePoint::from(0);
    /// let t10 = TimePoint::from(10);
    ///
    /// let ts = TimeSpan::new_safe(t10, t0);
    /// assert!(ts.start() == t0 && ts.end() == t10);
    /// ```
    pub fn new_safe(start: TimePoint, end: TimePoint) -> TimeSpan {
        if end < start {
            TimeSpan::new(end, start)
        } else {
            TimeSpan::new(start, end)
        }
    }

    /// Mutates a `TimeSpan`s end.
    ///
    /// # Panics
    ///
    /// Will panic if `new_end` is less than current `start`.
    pub fn new_copy_with_end(self, new_end: TimePoint) -> TimeSpan {
        TimeSpan::new(self.start, new_end)
    }

    /// Returns the length of the `TimeSpan`.
    ///
    /// `len()` is zero, if and only if `start` is `end`.
    pub fn len(self) -> TimeDelta {
        self.end - self.start
    }

    /// Returns true if `start == end`.
    pub fn is_empty(self) -> bool {
        self.end == self.start
    }

    /// Returns the start point of the `TimeSpan`.
    #[inline(always)]
    pub fn start(self) -> TimePoint {
        self.start
    }

    /// Returns the end point of the `TimeSpan`.
    #[inline(always)]
    pub fn end(self) -> TimePoint {
        self.end
    }

    /// Returns one (of the possibly two) points in the center of the `TimeSpan`.
    pub fn half(self) -> TimePoint {
        TimePoint::from((self.start.as_i64() + self.end.as_i64()) / 2)
    }

    /// Returns true if `self` contains `TimeSpan` `other`.
    ///
    /// # Examples
    /// ```
    /// use ilass::{TimeSpan, TimePoint};
    /// ```
    pub fn contains(self, other: TimeSpan) -> bool {
        other.start >= self.start && other.end <= self.end
    }

    /// Returns the smallest difference between two `TimeSpan`s.
    ///
    /// ```
    /// use ilass::{TimeSpan, TimePoint, TimeDelta};
    ///
    /// let p = TimePoint::from(0);
    /// let d = TimeDelta::one();
    ///
    /// let ts1 = TimeSpan::new(p, p + 10 * d);
    /// let ts4 = TimeSpan::new(p + 20 * d, p + 100 * d);
    ///
    /// assert!(TimeSpan::fast_distance_to(ts1, ts1) == 0 * d);
    /// assert!(TimeSpan::fast_distance_to(ts1, ts4) == 10 * d);
    /// assert!(TimeSpan::fast_distance_to(ts4, ts1) == 10 * d);
    /// assert!(TimeSpan::fast_distance_to(ts4, ts4) == 0 * d);
    /// ```
    pub fn fast_distance_to(self, other: TimeSpan) -> TimeDelta {
        // self < other
        if self.end < other.start {
            other.start - self.end
        }
        // self > other
        else if self.start > other.end {
            self.start - other.end
        }
        // self and other overlap
        else {
            TimeDelta::zero()
        }
    }

    /// Returns the smallest difference between two `TimeSpan`s.
    pub fn get_overlapping_length(self, other: TimeSpan) -> TimeDelta {
        let start_max = max(self.start, other.start);
        let end_min = min(self.end, other.end);
        max(TimeDelta::zero(), end_min - start_max)
    }

    /// Scale start and end time point to zero by `scaling_factor`.
    pub fn scaled(self, scaling_factor: f64) -> TimeSpan {
        let new_start = TimePoint::from((self.start.as_f64() * scaling_factor) as i64);
        let new_end = TimePoint::from((self.end.as_f64() * scaling_factor) as i64);
        TimeSpan::new(new_start, new_end)
    }

    /// Compares two `TimeSpan`s by their start timepoint.
    pub fn cmp_start(self, other: TimeSpan) -> Ordering {
        self.start.cmp(&other.start)
    }

    /// Compares two `TimeSpan`s by their end timepoint.
    pub fn cmp_end(self, other: TimeSpan) -> Ordering {
        self.end.cmp(&other.end)
    }
}

impl Add<TimeDelta> for TimeSpan {
    type Output = TimeSpan;
    fn add(self, rhs: TimeDelta) -> TimeSpan {
        TimeSpan::new(self.start + rhs, self.end + rhs)
    }
}