pub struct Clock { /* private fields */ }Expand description
Named clock with a ClockDomain and a ClockChart, the unit of clock
math in this crate.
A clock converts between Instants and ClockIndexes, and mints the
kernel Tick / Expr forms that carry a clock index across the
runtime.
§Examples
use sim_kernel::Symbol;
use sim_lib_stream_clock::{Clock, ClockChart, ClockIndex};
let clock = Clock::frame(Symbol::new("audio"), 48_000)?;
assert!(matches!(clock.chart(), ClockChart::Frames { frames_per_second: 48_000 }));
let instant = clock.instant_for_index(ClockIndex::new(48_000))?;
assert_eq!(instant.numerator(), 1);
assert_eq!(instant.denominator(), 1);Implementations§
Source§impl Clock
impl Clock
Sourcepub fn frame(id: Symbol, frames_per_second: u64) -> Result<Self>
pub fn frame(id: Symbol, frames_per_second: u64) -> Result<Self>
Builds a frame clock in the ClockDomain::Sample domain.
Returns an error when frames_per_second is zero.
Sourcepub fn frame_with_domain(
id: Symbol,
domain: ClockDomain,
frames_per_second: u64,
) -> Result<Self>
pub fn frame_with_domain( id: Symbol, domain: ClockDomain, frames_per_second: u64, ) -> Result<Self>
Builds a frame clock in an explicit domain.
Returns an error when domain is ClockDomain::MidiTick (which
requires a MIDI chart) or when frames_per_second is zero.
Sourcepub fn midi(id: Symbol, tpq: u32, tempo_map: TempoMap) -> Result<Self>
pub fn midi(id: Symbol, tpq: u32, tempo_map: TempoMap) -> Result<Self>
Builds a MIDI clock in the ClockDomain::MidiTick domain.
Returns an error when tpq (ticks per quarter note) is zero.
Sourcepub fn domain(&self) -> ClockDomain
pub fn domain(&self) -> ClockDomain
Returns the clock’s domain.
Sourcepub fn chart(&self) -> &ClockChart
pub fn chart(&self) -> &ClockChart
Returns the clock’s timing chart.
Sourcepub fn index_for_instant(&self, instant: Instant) -> Result<IndexConversion>
pub fn index_for_instant(&self, instant: Instant) -> Result<IndexConversion>
Converts instant to this clock’s index, reporting whether the result
is exact via the returned IndexConversion.
Returns an error when the conversion arithmetic overflows or the index
does not fit in a u64.
Sourcepub fn instant_for_index(&self, index: ClockIndex) -> Result<Instant>
pub fn instant_for_index(&self, index: ClockIndex) -> Result<Instant>
Converts a clock index back to its Instant.
Returns an error when the conversion arithmetic overflows.
Sourcepub fn tick_for_index(&self, cx: &mut Cx, index: ClockIndex) -> Result<Tick>
pub fn tick_for_index(&self, cx: &mut Cx, index: ClockIndex) -> Result<Tick>
Interns index as datum content and returns the kernel Tick that
carries it on this clock.
The clock index is stored as a datum node referenced by content, so
kernel events keep carrying only Tick values. Returns an error when
interning into the datum store fails.
Sourcepub fn index_expr(&self, index: ClockIndex) -> Expr
pub fn index_expr(&self, index: ClockIndex) -> Expr
Builds the codec Expr extension form encoding index on this clock.