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