midi_types/
note.rs

1//! The MIDI note type represent midi note numbers
2
3/// Represents a midi note number
4///
5/// # Note
6/// * 12-tone english named note constants are calculated with 0 corresponding to C-2 and 127 to
7///   G8, C4 is 72
8#[derive(Debug, PartialEq, Eq, Copy, Clone)]
9#[cfg_attr(feature = "defmt", derive(defmt::Format))]
10pub struct Note(u8);
11
12#[allow(
13    non_upper_case_globals,
14    missing_docs,
15    clippy::identity_op,
16    clippy::erasing_op
17)]
18impl Note {
19    pub const C2m: Self = Self::new(0 * 12 + 0);
20    pub const Cs2m: Self = Self::new(0 * 12 + 1);
21    pub const D2m: Self = Self::new(0 * 12 + 2);
22    pub const Ds2m: Self = Self::new(0 * 12 + 3);
23    pub const E2m: Self = Self::new(0 * 12 + 4);
24    pub const F2m: Self = Self::new(0 * 12 + 5);
25    pub const Fs2m: Self = Self::new(0 * 12 + 6);
26    pub const G2m: Self = Self::new(0 * 12 + 7);
27    pub const Gs2m: Self = Self::new(0 * 12 + 8);
28    pub const A2m: Self = Self::new(0 * 12 + 9);
29    pub const As2m: Self = Self::new(0 * 12 + 10);
30    pub const B2m: Self = Self::new(0 * 12 + 11);
31
32    pub const C1m: Self = Self::new(1 * 12 + 0);
33    pub const Cs1m: Self = Self::new(1 * 12 + 1);
34    pub const D1m: Self = Self::new(1 * 12 + 2);
35    pub const Ds1m: Self = Self::new(1 * 12 + 3);
36    pub const E1m: Self = Self::new(1 * 12 + 4);
37    pub const F1m: Self = Self::new(1 * 12 + 5);
38    pub const Fs1m: Self = Self::new(1 * 12 + 6);
39    pub const G1m: Self = Self::new(1 * 12 + 7);
40    pub const Gs1m: Self = Self::new(1 * 12 + 8);
41    pub const A1m: Self = Self::new(1 * 12 + 9);
42    pub const As1m: Self = Self::new(1 * 12 + 10);
43    pub const B1m: Self = Self::new(1 * 12 + 11);
44
45    pub const C0: Self = Self::new(2 * 12 + 0);
46    pub const Cs0: Self = Self::new(2 * 12 + 1);
47    pub const D0: Self = Self::new(2 * 12 + 2);
48    pub const Ds0: Self = Self::new(2 * 12 + 3);
49    pub const E0: Self = Self::new(2 * 12 + 4);
50    pub const F0: Self = Self::new(2 * 12 + 5);
51    pub const Fs0: Self = Self::new(2 * 12 + 6);
52    pub const G0: Self = Self::new(2 * 12 + 7);
53    pub const Gs0: Self = Self::new(2 * 12 + 8);
54    pub const A0: Self = Self::new(2 * 12 + 9);
55    pub const As0: Self = Self::new(2 * 12 + 10);
56    pub const B0: Self = Self::new(2 * 12 + 11);
57
58    pub const C1: Self = Self::new(3 * 12 + 0);
59    pub const Cs1: Self = Self::new(3 * 12 + 1);
60    pub const D1: Self = Self::new(3 * 12 + 2);
61    pub const Ds1: Self = Self::new(3 * 12 + 3);
62    pub const E1: Self = Self::new(3 * 12 + 4);
63    pub const F1: Self = Self::new(3 * 12 + 5);
64    pub const Fs1: Self = Self::new(3 * 12 + 6);
65    pub const G1: Self = Self::new(3 * 12 + 7);
66    pub const Gs1: Self = Self::new(3 * 12 + 8);
67    pub const A1: Self = Self::new(3 * 12 + 9);
68    pub const As1: Self = Self::new(3 * 12 + 10);
69    pub const B1: Self = Self::new(3 * 12 + 11);
70
71    pub const C2: Self = Self::new(4 * 12 + 0);
72    pub const Cs2: Self = Self::new(4 * 12 + 1);
73    pub const D2: Self = Self::new(4 * 12 + 2);
74    pub const Ds2: Self = Self::new(4 * 12 + 3);
75    pub const E2: Self = Self::new(4 * 12 + 4);
76    pub const F2: Self = Self::new(4 * 12 + 5);
77    pub const Fs2: Self = Self::new(4 * 12 + 6);
78    pub const G2: Self = Self::new(4 * 12 + 7);
79    pub const Gs2: Self = Self::new(4 * 12 + 8);
80    pub const A2: Self = Self::new(4 * 12 + 9);
81    pub const As2: Self = Self::new(4 * 12 + 10);
82    pub const B2: Self = Self::new(4 * 12 + 11);
83
84    pub const C3: Self = Self::new(5 * 12 + 0);
85    pub const Cs3: Self = Self::new(5 * 12 + 1);
86    pub const D3: Self = Self::new(5 * 12 + 2);
87    pub const Ds3: Self = Self::new(5 * 12 + 3);
88    pub const E3: Self = Self::new(5 * 12 + 4);
89    pub const F3: Self = Self::new(5 * 12 + 5);
90    pub const Fs3: Self = Self::new(5 * 12 + 6);
91    pub const G3: Self = Self::new(5 * 12 + 7);
92    pub const Gs3: Self = Self::new(5 * 12 + 8);
93    pub const A3: Self = Self::new(5 * 12 + 9);
94    pub const As3: Self = Self::new(5 * 12 + 10);
95    pub const B3: Self = Self::new(5 * 12 + 11);
96
97    pub const C4: Self = Self::new(6 * 12 + 0);
98    pub const Cs4: Self = Self::new(6 * 12 + 1);
99    pub const D4: Self = Self::new(6 * 12 + 2);
100    pub const Ds4: Self = Self::new(6 * 12 + 3);
101    pub const E4: Self = Self::new(6 * 12 + 4);
102    pub const F4: Self = Self::new(6 * 12 + 5);
103    pub const Fs4: Self = Self::new(6 * 12 + 6);
104    pub const G4: Self = Self::new(6 * 12 + 7);
105    pub const Gs4: Self = Self::new(6 * 12 + 8);
106    pub const A4: Self = Self::new(6 * 12 + 9);
107    pub const As4: Self = Self::new(6 * 12 + 10);
108    pub const B4: Self = Self::new(6 * 12 + 11);
109
110    pub const C5: Self = Self::new(7 * 12 + 0);
111    pub const Cs5: Self = Self::new(7 * 12 + 1);
112    pub const D5: Self = Self::new(7 * 12 + 2);
113    pub const Ds5: Self = Self::new(7 * 12 + 3);
114    pub const E5: Self = Self::new(7 * 12 + 4);
115    pub const F5: Self = Self::new(7 * 12 + 5);
116    pub const Fs5: Self = Self::new(7 * 12 + 6);
117    pub const G5: Self = Self::new(7 * 12 + 7);
118    pub const Gs5: Self = Self::new(7 * 12 + 8);
119    pub const A5: Self = Self::new(7 * 12 + 9);
120    pub const As5: Self = Self::new(7 * 12 + 10);
121    pub const B5: Self = Self::new(7 * 12 + 11);
122
123    pub const C6: Self = Self::new(8 * 12 + 0);
124    pub const Cs6: Self = Self::new(8 * 12 + 1);
125    pub const D6: Self = Self::new(8 * 12 + 2);
126    pub const Ds6: Self = Self::new(8 * 12 + 3);
127    pub const E6: Self = Self::new(8 * 12 + 4);
128    pub const F6: Self = Self::new(8 * 12 + 5);
129    pub const Fs6: Self = Self::new(8 * 12 + 6);
130    pub const G6: Self = Self::new(8 * 12 + 7);
131    pub const Gs6: Self = Self::new(8 * 12 + 8);
132    pub const A6: Self = Self::new(8 * 12 + 9);
133    pub const As6: Self = Self::new(8 * 12 + 10);
134    pub const B6: Self = Self::new(8 * 12 + 11);
135
136    pub const C7: Self = Self::new(9 * 12 + 0);
137    pub const Cs7: Self = Self::new(9 * 12 + 1);
138    pub const D7: Self = Self::new(9 * 12 + 2);
139    pub const Ds7: Self = Self::new(9 * 12 + 3);
140    pub const E7: Self = Self::new(9 * 12 + 4);
141    pub const F7: Self = Self::new(9 * 12 + 5);
142    pub const Fs7: Self = Self::new(9 * 12 + 6);
143    pub const G7: Self = Self::new(9 * 12 + 7);
144    pub const Gs7: Self = Self::new(9 * 12 + 8);
145    pub const A7: Self = Self::new(9 * 12 + 9);
146    pub const As7: Self = Self::new(9 * 12 + 10);
147    pub const B7: Self = Self::new(9 * 12 + 11);
148
149    pub const C8: Self = Self::new(10 * 12 + 0);
150    pub const Cs8: Self = Self::new(10 * 12 + 1);
151    pub const D8: Self = Self::new(10 * 12 + 2);
152    pub const Ds8: Self = Self::new(10 * 12 + 3);
153    pub const E8: Self = Self::new(10 * 12 + 4);
154    pub const F8: Self = Self::new(10 * 12 + 5);
155    pub const Fs8: Self = Self::new(10 * 12 + 6);
156    pub const G8: Self = Self::new(10 * 12 + 7);
157
158    /// The minimum note value
159    pub const MIN: Self = Self::C2m;
160    /// The maximum note value
161    pub const MAX: Self = Self::G8;
162
163    /// Create a new `Note`
164    ///
165    /// # Arguments
166    /// * `val` - the note number value
167    ///
168    /// # Note
169    /// * The `val` will be clamped so it is in the 0..127 valid range
170    #[must_use]
171    pub const fn new(val: u8) -> Self {
172        Self(if val > 127 { 127 } else { val })
173    }
174}
175
176impl From<u8> for Note {
177    fn from(note: u8) -> Self {
178        debug_assert!(note <= 127);
179        Self::new(note)
180    }
181}
182
183impl From<Note> for u8 {
184    fn from(value: Note) -> Self {
185        value.0
186    }
187}
188
189#[cfg(test)]
190mod tests {
191    use super::*;
192    #[test]
193    fn note_conv() {
194        assert_eq!(127u8, Note::G8.into());
195        assert_eq!(127u8, Note::MAX.into());
196        assert_eq!(0u8, Note::MIN.into());
197        assert_eq!(0u8, Note::C2m.into());
198    }
199}