use super::error::TimeError;
use super::tick::ClockTick;
use crate::time::SystemClock;
use std::fmt;
pub trait ClockSource: Send + Sync + fmt::Debug {
fn next_tick(&mut self, block_size: usize) -> ClockTick;
fn sample_rate(&self) -> f32;
fn start(&mut self) -> Result<(), TimeError> {
Ok(())
}
fn stop(&mut self) -> Result<(), TimeError> {
Ok(())
}
fn is_running(&self) -> bool {
true
}
fn current_position(&self) -> u64 {
0
}
}
impl ClockSource for SystemClock {
fn next_tick(&mut self, block_size: usize) -> ClockTick {
self.next_tick(block_size)
}
fn sample_rate(&self) -> f32 {
self.sample_rate
}
}
#[cfg(test)]
mod tests {
use crate::time::SystemClock;
#[test]
fn test_clock_source_trait() {
let mut clock = SystemClock::with_sample_rate(44100.0);
assert_eq!(clock.sample_rate, 44100.0);
let tick = clock.next_tick(64);
assert_eq!(tick.sample_pos, 0);
assert_eq!(tick.samples_since_last, 64);
}
}