Skip to main content

smpte_timecode/mtc/
mod.rs

1//! MIDI Time Code quarter-frame and full-frame messages.
2//!
3//! Rate codes map as 0 → 24, 1 → 25, 2 → 29.97 DF and 3 → 30. The encoder sends 23.976 as code 0,
4//! 30 DF as code 2 and 29.97 as code 3, because those rates have no code of their own. The
5//! decoder recovers them through [`MtcDecoder::set_rate_hint`].
6
7mod decoder;
8mod encoder;
9mod errors;
10
11pub use decoder::{LockState, MtcDecoder, MtcEvent};
12pub use encoder::MtcEncoder;
13pub use errors::MtcError;
14
15use crate::FrameRate;
16
17const QUARTER_FRAME: u8 = 0xF1;
18const SYSEX_START: u8 = 0xF0;
19const SYSEX_END: u8 = 0xF7;
20const UNIVERSAL_REAL_TIME: u8 = 0x7F;
21const ALL_DEVICES: u8 = 0x7F;
22const SUB_ID_TIME_CODE: u8 = 0x01;
23const SUB_ID_FULL_FRAME: u8 = 0x01;
24
25/// Which way quarter frames run.
26#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
27pub enum Direction {
28    /// Pieces 0 to 7, with time moving forward.
29    Forward,
30    /// Pieces 7 to 0, with time moving backward.
31    Reverse,
32}
33
34/// How a decoded position was reached.
35#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
36pub enum Motion {
37    /// A forward quarter-frame cycle. The position includes the two-frame cycle latency.
38    Forward,
39    /// A reverse quarter-frame cycle.
40    Reverse,
41    /// A full-frame message: a locate or a stopped position.
42    Located,
43}
44
45/// The rate a two-bit code names on its own.
46const fn stream_rate(code: u8) -> FrameRate {
47    match code {
48        0 => FrameRate::FPS_24,
49        1 => FrameRate::FPS_25,
50        2 => FrameRate::FPS_29_97_DF,
51        _ => FrameRate::FPS_30,
52    }
53}
54
55/// The two-bit code that carries `rate`, grouping rates by nominal frames and drop-frame counting.
56const fn rate_code(rate: FrameRate) -> Result<u8, MtcError> {
57    match (rate.nominal(), rate.is_drop_frame()) {
58        (24, _) => Ok(0),
59        (25, _) => Ok(1),
60        (30, true) => Ok(2),
61        (30, false) => Ok(3),
62        _ => Err(MtcError::NoRateCode),
63    }
64}