staff/
time.rs

1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2pub enum DurationKind {
3    Quarter,
4    Half,
5    Whole,
6}
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub struct Duration {
10    pub kind: DurationKind,
11    pub is_dotted: bool,
12}
13
14impl Duration {
15    pub fn new(kind: DurationKind, is_dotted: bool) -> Self {
16        Self { kind, is_dotted }
17    }
18
19    pub fn beats(self, unit: u8) -> f64 {
20        let mut n = match self.kind {
21            DurationKind::Quarter => 4.,
22            DurationKind::Half => 2.,
23            DurationKind::Whole => 1.,
24        };
25        if self.is_dotted {
26            n *= 1.5;
27        }
28
29        unit as f64 / n
30    }
31}
32
33pub struct TimeSignature {
34    pub unit: DurationKind,
35    pub beats: u8,
36}
37
38impl TimeSignature {
39    pub fn new(unit: DurationKind, beats: u8) -> Self {
40        Self { unit, beats }
41    }
42}