src/clock.rsView |
---|
8 | 8 … | |
9 | 9 … | pub type Nanos = u64; |
10 | 10 … | pub type Ticks = u64; |
11 | 11 … | pub type Beats = u64; |
12 | | -pub type Measures = u64; |
| 12 … | +pub type Bars = u64; |
13 | 13 … | |
14 | 14 … | static SECONDS_PER_MINUTE: u64 = 60; |
15 | | -static NANOS_PER_SECOND: u64 = 1_000_000; |
| 15 … | +static NANOS_PER_SECOND: u64 = 1_000_000_000; |
16 | 16 … | static BEATS_PER_MINUTE: u64 = 60; |
17 | | -static DEFAULT_TICKS_PER_BEAT: u64 = 1; |
18 | | -static DEFAULT_BEATS_PER_MEASURE: u64 = 4; |
| 17 … | +static DEFAULT_TICKS_PER_BEAT: u64 = 16; |
| 18 … | +static DEFAULT_BEATS_PER_BAR: u64 = 4; |
19 | 19 … | |
20 | | -#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] |
21 | | -pub struct TimeSignature { |
| 20 … | +#[derive(Clone, Copy, Debug, Hash)] |
| 21 … | +pub struct ClockSignature { |
22 | 22 … | nanos_per_beat: u64, |
23 | | - clicks_per_beat: u64, |
| 23 … | + ticks_per_beat: u64, |
24 | 24 … | beats_per_bar: u64 |
25 | 25 … | } |
26 | 26 … | |
27 | | -impl TimeSignature { |
28 | | - pub fn new (beats_per_minute: f64) { |
29 | | - let beats_per_nano = ticks_per_minute |
| 27 … | +impl ClockSignature { |
| 28 … | + pub fn new (beats_per_minute: f64) -> Self { |
| 29 … | + let minutes_per_beat = 1_f64 / beats_per_minute; |
| 30 … | + let seconds_per_beat = minutes_per_beat * SECONDS_PER_MINUTE as f64; |
| 31 … | + let nanos_per_beat = seconds_per_beat * NANOS_PER_SECOND as f64; |
| 32 … | + |
| 33 … | + Self { |
| 34 … | + nanos_per_beat: nanos_per_beat as u64, |
| 35 … | + ticks_per_beat: DEFAULT_TICKS_PER_BEAT, |
| 36 … | + beats_per_bar: DEFAULT_BEATS_PER_BAR |
| 37 … | + } |
30 | 38 … | } |
31 | 39 … | |
32 | 40 … | pub fn nanos_per_tick (&self) -> u64 { |
33 | 41 … | (self.nanos_per_beat / self.ticks_per_beat) as u64 |
34 | 42 … | } |
35 | 43 … | |
36 | | - pub fn ticks_since (&self, diff_duration: Duration) -> u64 { |
37 | | - let total_nanos = diff_duration.as_secs() * 1_000_000_000 + diff_duration.subsec_nanos() as u64; |
38 | | - self.nanos_per_tick() - total_nanos |
| 44 … | + pub fn nanos_per_beat (&self) -> u64 { |
| 45 … | + self.nanos_per_beat |
39 | 46 … | } |
| 47 … | + |
| 48 … | + pub fn nanos_per_bar (&self) -> u64 { |
| 49 … | + self.nanos_per_beat() * self.beats_per_bar |
| 50 … | + } |
| 51 … | + |
| 52 … | + pub fn nanos_to_ticks (&self, nanos: Nanos) -> u64 { |
| 53 … | + (nanos / self.nanos_per_tick()) as u64 |
| 54 … | + } |
| 55 … | + |
| 56 … | + pub fn nanos_to_beats (&self, nanos: Nanos) -> u64 { |
| 57 … | + (nanos / self.nanos_per_beat()) as u64 |
| 58 … | + } |
| 59 … | + |
| 60 … | + pub fn nanos_to_bars (&self, nanos: Nanos) -> u64 { |
| 61 … | + (nanos / self.nanos_per_bar()) as u64 |
| 62 … | + } |
40 | 63 … | } |
41 | 64 … | |
42 | 65 … | #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] |
| 66 … | +pub struct ClockTime { |
| 67 … | + nanos: Nanos, |
| 68 … | + ticks: Ticks, |
| 69 … | + beats: Beats, |
| 70 … | + bars: Bars |
| 71 … | +} |
| 72 … | + |
| 73 … | +impl ClockTime { |
| 74 … | + pub fn new (nanos: Nanos, signature: ClockSignature) -> Self { |
| 75 … | + Self { |
| 76 … | + nanos, |
| 77 … | + ticks: signature.nanos_to_ticks(nanos), |
| 78 … | + beats: signature.nanos_to_beats(nanos), |
| 79 … | + bars: signature.nanos_to_bars(nanos) |
| 80 … | + } |
| 81 … | + } |
| 82 … | +} |
| 83 … | + |
| 84 … | +#[derive(Clone, Copy, Debug, Hash)] |
43 | 85 … | pub struct Clock { |
44 | | - time: Time, |
45 | | - time_signature: TimeSignature |
| 86 … | + start_instant: Instant, |
| 87 … | + tick_instant: Instant, |
| 88 … | + signature: ClockSignature |
46 | 89 … | } |
47 | 90 … | |
48 | 91 … | pub struct ClockMessage { |
49 | 92 … | } |
50 | 93 … | |
51 | 94 … | impl Clock { |
52 | | - pub fn new (time_signature: TimeSignature) -> Self { |
| 95 … | + pub fn new (signature: ClockSignature) -> Self { |
| 96 … | + let start_instant = Instant::now(); |
| 97 … | + |
53 | 98 … | Self { |
54 | | - time: Instant::now(), |
55 | | - time_signature |
| 99 … | + start_instant, |
| 100 … | + tick_instant: start_instant, |
| 101 … | + signature |
56 | 102 … | } |
57 | 103 … | } |
58 | 104 … | |
59 | | - pub fn get_time (&self) -> Time { |
60 | | - self.time |
| 105 … | + pub fn time (&self) -> ClockTime { |
| 106 … | + ClockTime::new(self.nanos_since_start(), self.signature) |
61 | 107 … | } |
62 | 108 … | |
| 109 … | + pub fn diff (&self) -> ClockTime { |
| 110 … | + ClockTime::new(self.nanos_since_tick(), self.signature) |
| 111 … | + } |
| 112 … | + |
| 113 … | + pub fn nanos_since_start (&self) -> Nanos { |
| 114 … | + duration_to_nanos(self.start_instant.elapsed()) |
| 115 … | + } |
| 116 … | + |
| 117 … | + pub fn nanos_since_tick (&self) -> Nanos { |
| 118 … | + duration_to_nanos(self.tick_instant.elapsed()) |
| 119 … | + } |
| 120 … | + |
63 | 121 … | |
64 | | - pub fn tick (&mut self) -> u64 { |
65 | | - let diff_duration = self.time.elapsed(); |
66 | | - let diff_nanos = self.time_signature.nanos_since(diff_duration); |
67 | | - if diff_nanos > 0 { |
68 | | - sleep(Duration::new(0, diff_nanos as u32)) |
| 122 … | + pub fn tick (&mut self) -> ClockTime { |
| 123 … | + let diff = self.diff(); |
| 124 … | + |
| 125 … | + if diff.nanos > 0 { |
| 126 … | + sleep(Duration::new(0, diff.nanos as u32)) |
69 | 127 … | }; |
70 | | - self.time = Instant::now(); |
71 | | - diff_nanos |
| 128 … | + |
| 129 … | + |
| 130 … | + self.tick_instant = Instant::now(); |
| 131 … | + |
| 132 … | + diff |
72 | 133 … | } |
73 | 134 … | } |
74 | 135 … | |
| 136 … | +fn duration_to_nanos (duration: Duration) -> Nanos { |
| 137 … | + duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as Nanos |
| 138 … | +} |
| 139 … | + |
75 | 140 … | |
76 | | -pub fn nanos_from_ticks (ticks: Ticks, time_signature: TimeSignature) -> Nanos { |
77 | | - ticks * time_signature.nanos_per_beat |
| 141 … | +pub fn nanos_from_ticks (ticks: Ticks, signature: ClockSignature) -> Nanos { |
| 142 … | + ticks * signature.nanos_per_beat |
78 | 143 … | } |
79 | 144 … | |
80 | | -pub fn ticks_from_beats (beats: Beats, time_signature: TimeSignature) -> Ticks { |
81 | | - beats * time_signature.ticks_per_beat |
| 145 … | +pub fn ticks_from_beats (beats: Beats, signature: ClockSignature) -> Ticks { |
| 146 … | + beats * signature.ticks_per_beat |
82 | 147 … | } |
83 | 148 … | |
84 | | -pub fn ticks_from_measure (measures: Measures, time_signature: TimeSignature) -> Ticks { |
85 | | - measures * time_signature.beats_per_measure * time_signature.ticks_per_beat |
| 149 … | +pub fn ticks_from_measure (measures: Measures, signature: ClockSignature) -> Ticks { |
| 150 … | + measures * signature.beats_per_measure * signature.ticks_per_beat |
86 | 151 … | } |
87 | 152 … | */ |