Skip to main content

rill_core/time/
clock.rs

1use std::fmt;
2use std::sync::atomic::{AtomicU64, Ordering};
3
4use super::tick;
5
6/// High-precision system clock for sample-accurate timing.
7///
8/// Provides sample-accurate timing for signal processing.
9/// Uses atomic operations for thread safety without locks.
10pub struct SystemClock {
11    /// Sample rate of the audio system (Hz).
12    pub sample_rate: f32,
13    /// Global sample position (atomically updated).
14    position: AtomicU64,
15    /// Current BPM stored as raw f64 bits for atomic access.
16    bpm: AtomicU64,
17}
18
19impl SystemClock {
20    /// Create a new system clock at the given sample rate and initial BPM.
21    pub fn new(sample_rate: f32, initial_bpm: f64) -> Self {
22        Self {
23            sample_rate,
24            position: AtomicU64::new(0),
25            bpm: AtomicU64::new(initial_bpm.to_bits()),
26        }
27    }
28
29    /// Create a new system clock with a default BPM of 120.
30    pub fn with_sample_rate(sample_rate: f32) -> Self {
31        Self::new(sample_rate, 120.0)
32    }
33
34    /// Advance the clock by `block_size` samples and return the clock tick.
35    pub fn next_tick(&mut self, block_size: usize) -> tick::ClockTick {
36        let samples = block_size as u32;
37        let pos = self.position.fetch_add(samples as u64, Ordering::Relaxed);
38
39        tick::ClockTick {
40            sample_pos: pos,
41            samples_since_last: samples,
42            is_new_block: true,
43            sample_rate: self.sample_rate,
44            tempo: Some(self.bpm() as f32),
45        }
46    }
47
48    /// Return the current BPM value.
49    pub fn bpm(&self) -> f64 {
50        f64::from_bits(self.bpm.load(Ordering::Relaxed))
51    }
52
53    /// Set the BPM value atomically.
54    pub fn set_bpm(&self, bpm: f64) {
55        self.bpm.store(bpm.to_bits(), Ordering::Relaxed);
56    }
57
58    /// Return the current sample position.
59    pub fn position(&self) -> u64 {
60        self.position.load(Ordering::Relaxed)
61    }
62
63    /// Reset the sample position to zero.
64    pub fn reset(&self) {
65        self.position.store(0, Ordering::Relaxed);
66    }
67}
68
69impl fmt::Debug for SystemClock {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        f.debug_struct("SystemClock")
72            .field("sample_rate", &self.sample_rate)
73            .field("position", &self.position.load(Ordering::Relaxed))
74            .field("bpm", &self.bpm())
75            .finish()
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_system_clock() {
85        let mut clock = SystemClock::new(44100.0, 120.0);
86
87        let tick = clock.next_tick(64);
88        assert_eq!(tick.sample_pos, 0);
89        assert_eq!(tick.samples_since_last, 64);
90        assert_eq!(tick.sample_rate, 44100.0);
91        assert_eq!(tick.tempo, Some(120.0));
92
93        let tick = clock.next_tick(64);
94        assert_eq!(tick.sample_pos, 64);
95    }
96}