redact_composer_musical/
timing.rs

1#[cfg(feature = "redact-composer")]
2use redact_composer_core::derive::Element;
3
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7/// A musical time signature as a combination of beats per bar, and beat length.
8#[derive(Debug, Copy, Clone)]
9#[cfg_attr(feature = "redact-composer", derive(Element))]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11pub struct TimeSignature {
12    /// The number of beats per bar.
13    pub beats_per_bar: i32,
14    /// How many ticks a beat represents.
15    pub beat_length: i32,
16}
17
18impl TimeSignature {
19    /// Length of a bar in ticks.
20    pub fn bar(&self) -> i32 {
21        self.beats_per_bar * self.beat_length
22    }
23
24    /// The length of `n` bars in ticks.
25    pub fn bars(&self, n: i32) -> i32 {
26        self.bar() * n
27    }
28
29    /// Length of a beat in ticks.
30    pub fn beat(&self) -> i32 {
31        self.beat_length
32    }
33
34    /// The length of `n` beats in ticks.
35    pub fn beats(&self, n: i32) -> i32 {
36        self.beat() * n
37    }
38
39    /// Length of a half-beat in ticks.
40    pub fn half_beat(&self) -> i32 {
41        self.beat_length / 2
42    }
43
44    /// The length of `n` half-beats in ticks.
45    pub fn half_beats(&self, n: i32) -> i32 {
46        self.half_beat() * n
47    }
48
49    /// Length of a triplet in ticks. (2/3 of a beat)
50    pub fn triplet(&self) -> i32 {
51        self.beat_length * 2 / 3
52    }
53
54    /// The length of `n` triplets in ticks.
55    pub fn triplets(&self, n: i32) -> i32 {
56        self.triplet() * n
57    }
58
59    /// Length of a half-triplet in ticks. (1/3 of a beat)
60    pub fn half_triplet(&self) -> i32 {
61        self.beat_length / 3
62    }
63
64    /// The length of `n` half-triplets in ticks.
65    pub fn half_triplets(&self, n: i32) -> i32 {
66        self.half_triplet() * n
67    }
68
69    /// Length of a quarter-beat in ticks.
70    pub fn quarter_beat(&self) -> i32 {
71        self.beat_length / 4
72    }
73
74    /// The length of `n` quarter-beats in ticks.
75    pub fn quarter_beats(&self, n: i32) -> i32 {
76        self.quarter_beat() * n
77    }
78
79    /// Length of an eighth-beat in ticks.
80    pub fn eighth_beat(&self) -> i32 {
81        self.beat_length / 8
82    }
83
84    /// The length of `n` eighth-beats in ticks.
85    pub fn eighth_beats(&self, n: i32) -> i32 {
86        self.eighth_beat() * n
87    }
88}