oximedia-core 0.1.2

Core types and traits for OxiMedia
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//! Media timing primitives: `MediaTime`, `TimeRange`, and `MediaTimeCalc`.
//!
//! Provides lightweight time representation using integer numerator/denominator
//! pairs for frame-accurate arithmetic without floating-point rounding errors.

#![allow(dead_code)]

use std::fmt;

/// A media timestamp expressed as `ticks / time_base`.
///
/// Avoids floating-point representation to preserve frame accuracy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MediaTime {
    /// Tick count (numerator).
    pub ticks: i64,
    /// Time base denominator (e.g. 90000 for MPEG, 48000 for audio).
    pub time_base: u64,
}

impl MediaTime {
    /// Creates a `MediaTime` at exactly zero.
    #[must_use]
    pub const fn zero(time_base: u64) -> Self {
        Self {
            ticks: 0,
            time_base,
        }
    }

    /// Creates a `MediaTime` from whole seconds.
    ///
    /// # Panics
    ///
    /// Panics if `time_base` is zero.
    #[must_use]
    #[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
    pub fn from_secs(secs: f64, time_base: u64) -> Self {
        assert!(time_base > 0, "time_base must be non-zero");
        let ticks = (secs * time_base as f64).round() as i64;
        Self { ticks, time_base }
    }

    /// Converts this `MediaTime` to seconds as a 64-bit float.
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn to_secs(&self) -> f64 {
        self.ticks as f64 / self.time_base as f64
    }

    /// Adds an offset in ticks (same time base) and returns a new `MediaTime`.
    #[must_use]
    pub const fn add_offset(&self, offset_ticks: i64) -> Self {
        Self {
            ticks: self.ticks + offset_ticks,
            time_base: self.time_base,
        }
    }

    /// Returns `true` if this time is strictly before `other`.
    ///
    /// Both times must share the same time base; otherwise this compares
    /// the raw tick values which may be misleading.
    #[must_use]
    pub fn is_before(&self, other: &Self) -> bool {
        if self.time_base == other.time_base {
            self.ticks < other.ticks
        } else {
            self.to_secs() < other.to_secs()
        }
    }

    /// Returns `true` if this time is strictly after `other`.
    #[must_use]
    pub fn is_after(&self, other: &Self) -> bool {
        other.is_before(self)
    }

    /// Returns the absolute difference between two `MediaTime` values in seconds.
    #[must_use]
    pub fn abs_diff_secs(&self, other: &Self) -> f64 {
        (self.to_secs() - other.to_secs()).abs()
    }

    /// Rescales this `MediaTime` to a different time base.
    ///
    /// # Panics
    ///
    /// Panics if `new_base` is zero.
    #[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
    #[must_use]
    pub fn rescale(&self, new_base: u64) -> Self {
        assert!(new_base > 0, "time_base must be non-zero");
        let new_ticks =
            (self.ticks as f64 * new_base as f64 / self.time_base as f64).round() as i64;
        Self {
            ticks: new_ticks,
            time_base: new_base,
        }
    }
}

impl fmt::Display for MediaTime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:.6}s", self.to_secs())
    }
}

/// A half-open time range `[start, end)` in the same time base.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeRange {
    /// Inclusive start.
    pub start: MediaTime,
    /// Exclusive end.
    pub end: MediaTime,
}

impl TimeRange {
    /// Creates a new `TimeRange`.
    ///
    /// # Panics
    /// Panics if `start` is after `end`.
    #[must_use]
    pub fn new(start: MediaTime, end: MediaTime) -> Self {
        assert!(
            !end.is_before(&start),
            "TimeRange: start must not be after end"
        );
        Self { start, end }
    }

    /// Returns the duration of this range in seconds.
    #[must_use]
    pub fn duration(&self) -> f64 {
        self.end.to_secs() - self.start.to_secs()
    }

    /// Returns `true` if `time` falls within `[start, end)`.
    #[must_use]
    pub fn contains(&self, time: &MediaTime) -> bool {
        !time.is_before(&self.start) && time.is_before(&self.end)
    }

    /// Returns `true` if this range overlaps with `other`.
    #[must_use]
    pub fn overlaps(&self, other: &Self) -> bool {
        self.start.is_before(&other.end) && other.start.is_before(&self.end)
    }

    /// Returns the overlap range between `self` and `other`, if any.
    #[must_use]
    pub fn intersection(&self, other: &Self) -> Option<Self> {
        let start_secs = self.start.to_secs().max(other.start.to_secs());
        let end_secs = self.end.to_secs().min(other.end.to_secs());
        if end_secs <= start_secs {
            return None;
        }
        let tb = self.start.time_base;
        Some(Self::new(
            MediaTime::from_secs(start_secs, tb),
            MediaTime::from_secs(end_secs, tb),
        ))
    }
}

impl fmt::Display for TimeRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}, {})", self.start, self.end)
    }
}

/// Helper for common PTS/DTS calculations.
///
/// Converts presentation timestamps to decode timestamps given a constant
/// B-frame delay (measured in codec time-base ticks).
#[derive(Debug, Clone, Copy)]
pub struct MediaTimeCalc {
    /// Number of B-frames in the codec's lookahead (determines PTS→DTS offset).
    pub b_frame_delay: u32,
    /// Codec time base.
    pub time_base: u64,
}

impl MediaTimeCalc {
    /// Creates a `MediaTimeCalc` with the given parameters.
    #[must_use]
    pub const fn new(b_frame_delay: u32, time_base: u64) -> Self {
        Self {
            b_frame_delay,
            time_base,
        }
    }

    /// Converts a PTS (presentation timestamp in ticks) to a DTS (decode
    /// timestamp in ticks) by subtracting the B-frame delay.
    #[must_use]
    pub fn pts_to_dts(&self, pts: MediaTime) -> MediaTime {
        let delay = i64::from(self.b_frame_delay);
        pts.add_offset(-delay)
    }

    /// Converts a DTS back to a PTS by adding the B-frame delay.
    #[must_use]
    pub fn dts_to_pts(&self, dts: MediaTime) -> MediaTime {
        let delay = i64::from(self.b_frame_delay);
        dts.add_offset(delay)
    }

    /// Returns the minimum DTS offset in seconds caused by the B-frame delay.
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn dts_offset_secs(&self) -> f64 {
        f64::from(self.b_frame_delay) / self.time_base as f64
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_zero() {
        let t = MediaTime::zero(90_000);
        assert_eq!(t.ticks, 0);
        assert_eq!(t.to_secs(), 0.0);
    }

    #[test]
    fn test_from_secs_to_secs() {
        let t = MediaTime::from_secs(1.0, 90_000);
        assert!((t.to_secs() - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_from_secs_half() {
        let t = MediaTime::from_secs(0.5, 90_000);
        assert!((t.to_secs() - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_add_offset() {
        let t = MediaTime::from_secs(1.0, 90_000);
        let t2 = t.add_offset(90_000);
        assert!((t2.to_secs() - 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_is_before() {
        let t1 = MediaTime::from_secs(1.0, 90_000);
        let t2 = MediaTime::from_secs(2.0, 90_000);
        assert!(t1.is_before(&t2));
        assert!(!t2.is_before(&t1));
    }

    #[test]
    fn test_is_after() {
        let t1 = MediaTime::from_secs(1.0, 90_000);
        let t2 = MediaTime::from_secs(2.0, 90_000);
        assert!(t2.is_after(&t1));
        assert!(!t1.is_after(&t2));
    }

    #[test]
    fn test_abs_diff_secs() {
        let t1 = MediaTime::from_secs(1.0, 90_000);
        let t2 = MediaTime::from_secs(3.0, 90_000);
        assert!((t1.abs_diff_secs(&t2) - 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_rescale() {
        let t = MediaTime::from_secs(1.0, 90_000);
        let t2 = t.rescale(48_000);
        assert!((t2.to_secs() - 1.0).abs() < 1e-4);
    }

    #[test]
    fn test_display() {
        let t = MediaTime::from_secs(1.5, 90_000);
        let s = format!("{t}");
        assert!(s.contains("1.5"));
    }

    #[test]
    fn test_time_range_duration() {
        let s = MediaTime::from_secs(0.0, 90_000);
        let e = MediaTime::from_secs(2.0, 90_000);
        let r = TimeRange::new(s, e);
        assert!((r.duration() - 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_time_range_contains() {
        let s = MediaTime::from_secs(1.0, 90_000);
        let e = MediaTime::from_secs(5.0, 90_000);
        let r = TimeRange::new(s, e);
        let mid = MediaTime::from_secs(3.0, 90_000);
        assert!(r.contains(&mid));
        assert!(!r.contains(&MediaTime::from_secs(0.5, 90_000)));
        // End is exclusive
        assert!(!r.contains(&e));
    }

    #[test]
    fn test_time_range_overlaps() {
        let r1 = TimeRange::new(
            MediaTime::from_secs(0.0, 90_000),
            MediaTime::from_secs(4.0, 90_000),
        );
        let r2 = TimeRange::new(
            MediaTime::from_secs(2.0, 90_000),
            MediaTime::from_secs(6.0, 90_000),
        );
        assert!(r1.overlaps(&r2));
    }

    #[test]
    fn test_time_range_no_overlap() {
        let r1 = TimeRange::new(
            MediaTime::from_secs(0.0, 90_000),
            MediaTime::from_secs(2.0, 90_000),
        );
        let r2 = TimeRange::new(
            MediaTime::from_secs(3.0, 90_000),
            MediaTime::from_secs(5.0, 90_000),
        );
        assert!(!r1.overlaps(&r2));
    }

    #[test]
    fn test_time_range_intersection() {
        let r1 = TimeRange::new(
            MediaTime::from_secs(0.0, 90_000),
            MediaTime::from_secs(4.0, 90_000),
        );
        let r2 = TimeRange::new(
            MediaTime::from_secs(2.0, 90_000),
            MediaTime::from_secs(6.0, 90_000),
        );
        let inter = r1.intersection(&r2).expect("intersection should exist");
        assert!((inter.start.to_secs() - 2.0).abs() < 1e-4);
        assert!((inter.end.to_secs() - 4.0).abs() < 1e-4);
    }

    #[test]
    fn test_time_range_intersection_none() {
        let r1 = TimeRange::new(
            MediaTime::from_secs(0.0, 90_000),
            MediaTime::from_secs(2.0, 90_000),
        );
        let r2 = TimeRange::new(
            MediaTime::from_secs(3.0, 90_000),
            MediaTime::from_secs(5.0, 90_000),
        );
        assert!(r1.intersection(&r2).is_none());
    }

    #[test]
    fn test_pts_to_dts_no_delay() {
        let calc = MediaTimeCalc::new(0, 90_000);
        let pts = MediaTime::from_secs(1.0, 90_000);
        let dts = calc.pts_to_dts(pts);
        assert_eq!(dts, pts);
    }

    #[test]
    fn test_pts_to_dts_with_delay() {
        let calc = MediaTimeCalc::new(2, 90_000);
        let pts = MediaTime {
            ticks: 100,
            time_base: 90_000,
        };
        let dts = calc.pts_to_dts(pts);
        assert_eq!(dts.ticks, 98);
    }

    #[test]
    fn test_dts_to_pts_roundtrip() {
        let calc = MediaTimeCalc::new(3, 90_000);
        let pts = MediaTime::from_secs(5.0, 90_000);
        let dts = calc.pts_to_dts(pts);
        let pts2 = calc.dts_to_pts(dts);
        assert_eq!(pts, pts2);
    }

    #[test]
    fn test_dts_offset_secs() {
        let calc = MediaTimeCalc::new(2, 90_000);
        let offset = calc.dts_offset_secs();
        assert!((offset - 2.0 / 90_000.0).abs() < 1e-12);
    }
}

// ---------------------------------------------------------------------------
// Rich media time using Rational time base
// ---------------------------------------------------------------------------

use crate::types::Rational;

/// Standard time base: 1/90000 (MPEG / H.264 / VP9 default).
pub const TB_90K: Rational = Rational {
    num: 1,
    den: 90_000,
};

/// Standard time base: 1/44100 (CD-quality audio).
pub const TB_44100: Rational = Rational {
    num: 1,
    den: 44_100,
};

/// Standard time base: 1/48000 (broadcast audio).
pub const TB_48K: Rational = Rational {
    num: 1,
    den: 48_000,
};

/// Standard time base: 1/1000 (millisecond precision).
pub const TB_1K: Rational = Rational { num: 1, den: 1_000 };

/// A media presentation timestamp with an associated [`Rational`] time base.
///
/// `pts` is measured in units of `time_base`; the wall-clock time is
/// `pts * time_base.num / time_base.den` seconds.
///
/// # Examples
///
/// ```
/// use oximedia_core::media_time::{PtsMediaTime, TB_90K};
///
/// let t = PtsMediaTime::new(90_000, TB_90K);
/// assert!((t.to_secs() - 1.0).abs() < 1e-9);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PtsMediaTime {
    /// Raw tick count (presentation timestamp).
    pub pts: i64,
    /// Time base as a rational fraction (e.g. `1/90000`).
    pub time_base: Rational,
}

impl PtsMediaTime {
    /// The canonical zero timestamp in a 1/90000 time base.
    pub const ZERO: Self = Self {
        pts: 0,
        time_base: TB_90K,
    };

    /// Creates a new `PtsMediaTime`.
    #[must_use]
    pub fn new(pts: i64, time_base: Rational) -> Self {
        Self { pts, time_base }
    }

    /// Converts seconds to ticks in the given time base and returns the
    /// resulting `PtsMediaTime`.
    #[must_use]
    #[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
    pub fn from_secs(secs: f64, time_base: Rational) -> Self {
        // ticks = secs / (num/den) = secs * den / num
        let ticks = (secs * time_base.den as f64 / time_base.num as f64).round() as i64;
        Self {
            pts: ticks,
            time_base,
        }
    }

    /// Returns the wall-clock time in seconds.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn to_secs(&self) -> f64 {
        self.pts as f64 * self.time_base.to_f64()
    }

    /// Returns the wall-clock time in whole milliseconds (truncated toward
    /// zero).
    #[must_use]
    #[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
    pub fn to_ms(&self) -> i64 {
        (self.to_secs() * 1000.0) as i64
    }

    /// Rescales this timestamp to `new_base`, preserving the wall-clock
    /// instant as accurately as possible.
    ///
    /// Uses 128-bit intermediate arithmetic to prevent overflow.
    #[must_use]
    #[allow(clippy::cast_possible_truncation)]
    pub fn rebase(&self, new_base: Rational) -> Self {
        // new_pts = pts * (old_num / old_den) / (new_num / new_den)
        //         = pts * old_num * new_den / (old_den * new_num)
        let scale_num = i128::from(self.time_base.num) * i128::from(new_base.den);
        let scale_den = i128::from(self.time_base.den) * i128::from(new_base.num);
        let half = scale_den / 2;
        let new_pts = ((i128::from(self.pts) * scale_num + half) / scale_den) as i64;
        Self {
            pts: new_pts,
            time_base: new_base,
        }
    }

    /// Returns `true` if the presentation timestamp is non-negative (valid
    /// for presentation).
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.pts >= 0
    }

    /// Adds a [`MediaDuration`] to this timestamp.
    ///
    /// Both operands must share the same time base; if they differ the
    /// duration is first rebased.
    #[must_use]
    pub fn add_duration(&self, d: MediaDuration) -> Self {
        let d_rebased = d.rebase(self.time_base);
        Self {
            pts: self.pts + d_rebased.ticks,
            time_base: self.time_base,
        }
    }

    /// Returns the difference `self - other` as a [`MediaDuration`] in
    /// `self`'s time base.
    #[must_use]
    pub fn subtract(&self, other: &Self) -> MediaDuration {
        let other_rebased = other.rebase(self.time_base);
        MediaDuration {
            ticks: self.pts - other_rebased.pts,
            time_base: self.time_base,
        }
    }
}

/// A duration expressed in [`Rational`] time-base ticks.
#[derive(Debug, Clone, Copy)]
pub struct MediaDuration {
    /// Raw tick count.
    pub ticks: i64,
    /// Time base.
    pub time_base: Rational,
}

impl MediaDuration {
    /// Converts seconds to ticks and returns the resulting `MediaDuration`.
    #[must_use]
    #[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
    pub fn from_secs(secs: f64, time_base: Rational) -> Self {
        let ticks = (secs * time_base.den as f64 / time_base.num as f64).round() as i64;
        Self { ticks, time_base }
    }

    /// Returns the duration in seconds.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn to_secs(&self) -> f64 {
        self.ticks as f64 * self.time_base.to_f64()
    }

    /// Rescales this duration to `new_base`.
    #[must_use]
    #[allow(clippy::cast_possible_truncation)]
    pub fn rebase(&self, new_base: Rational) -> Self {
        let scale_num = i128::from(self.time_base.num) * i128::from(new_base.den);
        let scale_den = i128::from(self.time_base.den) * i128::from(new_base.num);
        let half = scale_den / 2;
        let new_ticks = ((i128::from(self.ticks) * scale_num + half) / scale_den) as i64;
        Self {
            ticks: new_ticks,
            time_base: new_base,
        }
    }
}

#[cfg(test)]
mod tests_pts_media_time {
    use super::*;

    #[test]
    fn test_zero_constant() {
        assert_eq!(PtsMediaTime::ZERO.pts, 0);
        assert_eq!(PtsMediaTime::ZERO.time_base, TB_90K);
    }

    #[test]
    fn test_new_and_to_secs() {
        let t = PtsMediaTime::new(90_000, TB_90K);
        assert!((t.to_secs() - 1.0).abs() < 1e-9);
    }

    #[test]
    fn test_from_secs() {
        let t = PtsMediaTime::from_secs(2.5, TB_90K);
        assert!((t.to_secs() - 2.5).abs() < 1e-6);
    }

    #[test]
    fn test_to_ms() {
        let t = PtsMediaTime::from_secs(1.5, TB_1K);
        assert_eq!(t.to_ms(), 1500);
    }

    #[test]
    fn test_rebase_90k_to_1k() {
        let t = PtsMediaTime::new(90_000, TB_90K);
        let r = t.rebase(TB_1K);
        assert_eq!(r.pts, 1000);
        assert!((r.to_secs() - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_rebase_1k_to_44100() {
        let t = PtsMediaTime::from_secs(1.0, TB_1K);
        let r = t.rebase(TB_44100);
        assert_eq!(r.pts, 44_100);
    }

    #[test]
    fn test_is_valid() {
        assert!(PtsMediaTime::new(0, TB_90K).is_valid());
        assert!(PtsMediaTime::new(100, TB_90K).is_valid());
        assert!(!PtsMediaTime::new(-1, TB_90K).is_valid());
    }

    #[test]
    fn test_add_duration() {
        let t = PtsMediaTime::new(0, TB_1K);
        let d = MediaDuration::from_secs(1.5, TB_1K);
        let t2 = t.add_duration(d);
        assert_eq!(t2.pts, 1500);
    }

    #[test]
    fn test_add_duration_different_base() {
        let t = PtsMediaTime::new(90_000, TB_90K); // 1 second
        let d = MediaDuration::from_secs(0.5, TB_1K); // 0.5 seconds in 1k base
        let t2 = t.add_duration(d);
        assert!((t2.to_secs() - 1.5).abs() < 1e-4);
    }

    #[test]
    fn test_subtract() {
        let a = PtsMediaTime::new(3000, TB_1K);
        let b = PtsMediaTime::new(1000, TB_1K);
        let d = a.subtract(&b);
        assert!((d.to_secs() - 2.0).abs() < 1e-9);
    }

    #[test]
    fn test_media_duration_from_secs_to_secs() {
        let d = MediaDuration::from_secs(0.25, TB_90K);
        assert!((d.to_secs() - 0.25).abs() < 1e-6);
    }

    #[test]
    fn test_media_duration_rebase() {
        let d = MediaDuration::from_secs(1.0, TB_90K);
        let r = d.rebase(TB_48K);
        assert_eq!(r.ticks, 48_000);
    }

    #[test]
    fn test_tb_constants() {
        assert_eq!(TB_90K.den, 90_000);
        assert_eq!(TB_44100.den, 44_100);
        assert_eq!(TB_48K.den, 48_000);
        assert_eq!(TB_1K.den, 1_000);
    }
}