Skip to main content

MtcDecoder

Struct MtcDecoder 

Source
pub struct MtcDecoder { /* private fields */ }
Expand description

Turns MIDI Time Code messages into checked positions.

Feed every complete MIDI message with the time it arrived. Quarter frames form a position only when all eight pieces arrive consecutively in one direction, so a locate, a lost or repeated piece, a direction change or joining mid-cycle never yields a position that mixes two cycles. Full frames reset the cycle and yield a Motion::Located position.

Times are durations since any fixed instant the caller chooses, such as a MIDI backend’s timestamp or a monotonic clock. Use one clock per decoder. The decoder never reads a clock, allocates or blocks, so it can run inside a MIDI callback.

use core::time::Duration;
use smpte_timecode::{Direction, FrameRate, Motion, MtcDecoder, MtcEncoder, Timecode};

let start = Timecode::parse("01:00:00:00", FrameRate::FPS_25)?;
let mut encoder = MtcEncoder::new(start, Direction::Forward)?;
let mut decoder = MtcDecoder::new();
let mut events = Vec::new();
for (index, message) in encoder.by_ref().take(8).enumerate() {
    let at = Duration::from_millis(10 * index as u64);
    events.extend(decoder.feed(&message, at)?);
}
assert_eq!(events.len(), 1);
assert_eq!(events[0].timecode.to_string(), "01:00:00:02");
assert_eq!(events[0].motion, Motion::Forward);

Implementations§

Source§

impl MtcDecoder

Source

pub const DEFAULT_QUARTER_FRAME_TIMEOUT: Duration

The default longest gap between consecutive quarter frames of one cycle.

Source

pub const DEFAULT_STALE_AFTER: Duration

The default age after which the last position is LockState::Stale.

Source

pub const fn new() -> Self

A decoder with the default limits and no rate hint.

Source

pub const fn with_quarter_frame_timeout(self, timeout: Duration) -> Self

Sets the longest gap between consecutive quarter frames that still continues a cycle. A longer gap starts a new cycle.

Source

pub const fn with_stale_after(self, stale_after: Duration) -> Self

Sets the age after which MtcDecoder::lock_state reports LockState::Stale.

Source

pub const fn set_rate_hint(&mut self, hint: Option<FrameRate>)

Sets the rate the source is known to run at, such as a DAW session rate.

The two-bit rate code cannot tell 29.97 from 30, 23.976 from 24, or mark 30 as drop-frame. While the hint has the same nominal frame count as the incoming code, positions use the hint. When they differ, the stream wins, because reading its frames at another count would shift positions or reject frame numbers the hint cannot hold.

Source

pub fn feed( &mut self, message: &[u8], at: Duration, ) -> Result<Option<MtcEvent>, MtcError>

Decodes one complete MIDI message received at at.

Returns a position when the message completes a quarter-frame cycle or is a full frame, and None for a partial cycle or a message that is not MIDI Time Code.

§Errors

MtcError::MalformedMessage for a quarter frame or full frame with the wrong shape, and MtcError::InvalidTimecode when a complete message names a position that does not exist. A malformed message discards the partial cycle. An invalid complete position consumes the cycle as a valid one would. Neither error replaces or refreshes the last valid position.

Source

pub const fn lock_state(&self, now: Duration) -> LockState

Whether the decoder is following a source at now, on the same clock as MtcDecoder::feed.

Source

pub const fn reset(&mut self)

Discards any partial cycle and the last position, as when the source changes. Limits and the rate hint stay.

Trait Implementations§

Source§

impl Clone for MtcDecoder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MtcDecoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MtcDecoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.