git ssb

0+

dinoworm 🐛 / metronome



Commit 9cf1052d129942a85af116c81a7f960330143ad6

clock time

Michael Williams committed on 5/13/2018, 6:58:07 AM
Parent: 4ba4ca13e7973216b4183f63e28393f41614ba18

Files changed

src/clock.rschanged
src/main.rschanged
src/clock.rsView
@@ -8,80 +8,145 @@
88
99 pub type Nanos = u64;
1010 pub type Ticks = u64;
1111 pub type Beats = u64;
12-pub type Measures = u64;
12 +pub type Bars = u64;
1313
1414 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;
1616 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;
1919
20-#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
21-pub struct TimeSignature {
20 +#[derive(Clone, Copy, Debug, Hash)]
21 +pub struct ClockSignature {
2222 nanos_per_beat: u64, // tempo
23- clicks_per_beat: u64, // meter: notes per beat
23 + ticks_per_beat: u64, // meter
2424 beats_per_bar: u64 // meter
2525 }
2626
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 + }
3038 }
3139
3240 pub fn nanos_per_tick (&self) -> u64 {
3341 (self.nanos_per_beat / self.ticks_per_beat) as u64
3442 }
3543
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
3946 }
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 + }
4063 }
4164
4265 #[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)]
4385 pub struct Clock {
44- time: Time,
45- time_signature: TimeSignature
86 + start_instant: Instant,
87 + tick_instant: Instant,
88 + signature: ClockSignature
4689 }
4790
4891 pub struct ClockMessage {
4992 }
5093
5194 impl Clock {
52- pub fn new (time_signature: TimeSignature) -> Self {
95 + pub fn new (signature: ClockSignature) -> Self {
96 + let start_instant = Instant::now();
97 +
5398 Self {
54- time: Instant::now(),
55- time_signature
99 + start_instant,
100 + tick_instant: start_instant,
101 + signature
56102 }
57103 }
58104
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)
61107 }
62108
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 +
63121 // https://github.com/BookOwl/fps_clock/blob/master/src/lib.rs
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))
69127 };
70- self.time = Instant::now();
71- diff_nanos
128 +
129 +
130 + self.tick_instant = Instant::now();
131 +
132 + diff
72133 }
73134 }
74135
136 +fn duration_to_nanos (duration: Duration) -> Nanos {
137 + duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as Nanos
138 +}
139 +
75140 /*
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
78143 }
79144
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
82147 }
83148
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
86151 }
87152 */
src/main.rsView
@@ -67,17 +67,13 @@
6767 }
6868
6969 fn clock () {
7070 spawn(move|| {
71- let time_signature = clock::TimeSignature {
72- nanos_per_beat: BEATS_PER_MINUTE / SECONDS_PER_MINUTE * NANOS_PER_SECOND,
73- ticks_per_beat: DEFAULT_TICKS_PER_BEAT,
74- beats_per_measure: DEFAULT_BEATS_PER_MEASURE
75- };
76- let mut clock = clock::Clock::new(time_signature);
71 + let signature = clock::ClockSignature::new(60_f64);
72 + let mut clock = clock::Clock::new(signature);
7773 loop {
7874 clock.tick();
79- println!("{:?}", clock.get_time());
75 + println!("{:?}", clock.time());
8076 }
8177 });
8278 }
8379

Built with git-ssb-web