usbd_midi/message/
notes.rs

1//! Enum representing the MIDI notes.
2
3use crate::message::data::u7::U7;
4use crate::message::data::FromOverFlow;
5use num_enum::TryFromPrimitive;
6
7/// A simple enum type that represents all the midi 'notes'.
8///
9/// Note the flat versions are associated constants
10/// but can be referenced like Note::Bb3
11/// C1m is the C-1
12#[allow(missing_docs)]
13#[derive(Debug, Copy, Clone, TryFromPrimitive, Eq, PartialEq)]
14#[repr(u8)]
15pub enum Note {
16    C1m,
17    Cs1m,
18    D1m,
19    Ds1m,
20    E1m,
21    F1m,
22    Fs1m,
23    G1m,
24    Gs1m,
25    A1m,
26    As1m,
27    B1m,
28    C0,
29    Cs0,
30    D0,
31    Ds0,
32    E0,
33    F0,
34    Fs0,
35    G0,
36    Gs0,
37    A0,
38    As0,
39    B0,
40    C1,
41    Cs1,
42    D1,
43    Ds1,
44    E1,
45    F1,
46    Fs1,
47    G1,
48    Gs1,
49    A1,
50    As1,
51    B1,
52    C2,
53    Cs2,
54    D2,
55    Ds2,
56    E2,
57    F2,
58    Fs2,
59    G2,
60    Gs2,
61    A2,
62    As2,
63    B2,
64    C3,
65    Cs3,
66    D3,
67    Ds3,
68    E3,
69    F3,
70    Fs3,
71    G3,
72    Gs3,
73    A3,
74    As3,
75    B3,
76    C4,
77    Cs4,
78    D4,
79    Ds4,
80    E4,
81    F4,
82    Fs4,
83    G4,
84    Gs4,
85    A4,
86    As4,
87    B4,
88    C5,
89    Cs5,
90    D5,
91    Ds5,
92    E5,
93    F5,
94    Fs5,
95    G5,
96    Gs5,
97    A5,
98    As5,
99    B5,
100    C6,
101    Cs6,
102    D6,
103    Ds6,
104    E6,
105    F6,
106    Fs6,
107    G6,
108    Gs6,
109    A6,
110    As6,
111    B6,
112    C7,
113    Cs7,
114    D7,
115    Ds7,
116    E7,
117    F7,
118    Fs7,
119    G7,
120    Gs7,
121    A7,
122    As7,
123    B7,
124    C8,
125    Cs8,
126    D8,
127    Ds8,
128    E8,
129    F8,
130    Fs8,
131    G8,
132    Gs8,
133    A8,
134    As8,
135    B8,
136    C9,
137    Cs9,
138    D9,
139    Ds9,
140    E9,
141    F9,
142    Fs9,
143    G9,
144    Gs9,
145}
146
147impl From<Note> for u8 {
148    fn from(val: Note) -> Self {
149        val as u8
150    }
151}
152
153impl From<Note> for U7 {
154    fn from(value: Note) -> U7 {
155        let byte = value as u8;
156        U7::from_overflow(byte)
157    }
158}
159
160#[allow(missing_docs)]
161impl Note {
162    #[allow(non_upper_case_globals)]
163    pub const Db1m: Note = Note::Cs1m;
164    #[allow(non_upper_case_globals)]
165    pub const Eb1m: Note = Note::Ds1m;
166    #[allow(non_upper_case_globals)]
167    pub const Gb1m: Note = Note::Fs1m;
168    #[allow(non_upper_case_globals)]
169    pub const Ab1m: Note = Note::Gs1m;
170    #[allow(non_upper_case_globals)]
171    pub const Bb1m: Note = Note::As1m;
172    #[allow(non_upper_case_globals)]
173    pub const Db0: Note = Note::Cs0;
174    #[allow(non_upper_case_globals)]
175    pub const Eb0: Note = Note::Ds0;
176    #[allow(non_upper_case_globals)]
177    pub const Gb0: Note = Note::Fs0;
178    #[allow(non_upper_case_globals)]
179    pub const Ab0: Note = Note::Gs0;
180    #[allow(non_upper_case_globals)]
181    pub const Bb0: Note = Note::As0;
182    #[allow(non_upper_case_globals)]
183    pub const Db1: Note = Note::Cs1;
184    #[allow(non_upper_case_globals)]
185    pub const Eb1: Note = Note::Ds1;
186    #[allow(non_upper_case_globals)]
187    pub const Gb1: Note = Note::Fs1;
188    #[allow(non_upper_case_globals)]
189    pub const Ab1: Note = Note::Gs1;
190    #[allow(non_upper_case_globals)]
191    pub const Bb1: Note = Note::As1;
192    #[allow(non_upper_case_globals)]
193    pub const Db2: Note = Note::Cs2;
194    #[allow(non_upper_case_globals)]
195    pub const Eb2: Note = Note::Ds2;
196    #[allow(non_upper_case_globals)]
197    pub const Gb2: Note = Note::Fs2;
198    #[allow(non_upper_case_globals)]
199    pub const Ab2: Note = Note::Gs2;
200    #[allow(non_upper_case_globals)]
201    pub const Bb2: Note = Note::As2;
202    #[allow(non_upper_case_globals)]
203    pub const Db3: Note = Note::Cs3;
204    #[allow(non_upper_case_globals)]
205    pub const Eb3: Note = Note::Ds3;
206    #[allow(non_upper_case_globals)]
207    pub const Gb3: Note = Note::Fs3;
208    #[allow(non_upper_case_globals)]
209    pub const Ab3: Note = Note::Gs3;
210    #[allow(non_upper_case_globals)]
211    pub const Bb3: Note = Note::As3;
212    #[allow(non_upper_case_globals)]
213    pub const Db4: Note = Note::Cs4;
214    #[allow(non_upper_case_globals)]
215    pub const Eb4: Note = Note::Ds4;
216    #[allow(non_upper_case_globals)]
217    pub const Gb4: Note = Note::Fs4;
218    #[allow(non_upper_case_globals)]
219    pub const Ab4: Note = Note::Gs4;
220    #[allow(non_upper_case_globals)]
221    pub const Bb4: Note = Note::As4;
222    #[allow(non_upper_case_globals)]
223    pub const Db5: Note = Note::Cs5;
224    #[allow(non_upper_case_globals)]
225    pub const Eb5: Note = Note::Ds5;
226    #[allow(non_upper_case_globals)]
227    pub const Gb5: Note = Note::Fs5;
228    #[allow(non_upper_case_globals)]
229    pub const Ab5: Note = Note::Gs5;
230    #[allow(non_upper_case_globals)]
231    pub const Bb5: Note = Note::As5;
232    #[allow(non_upper_case_globals)]
233    pub const Db6: Note = Note::Cs6;
234    #[allow(non_upper_case_globals)]
235    pub const Eb6: Note = Note::Ds6;
236    #[allow(non_upper_case_globals)]
237    pub const Gb6: Note = Note::Fs6;
238    #[allow(non_upper_case_globals)]
239    pub const Ab6: Note = Note::Gs6;
240    #[allow(non_upper_case_globals)]
241    pub const Bb6: Note = Note::As6;
242    #[allow(non_upper_case_globals)]
243    pub const Db7: Note = Note::Cs7;
244    #[allow(non_upper_case_globals)]
245    pub const Eb7: Note = Note::Ds7;
246    #[allow(non_upper_case_globals)]
247    pub const Gb7: Note = Note::Fs7;
248    #[allow(non_upper_case_globals)]
249    pub const Ab7: Note = Note::Gs7;
250    #[allow(non_upper_case_globals)]
251    pub const Bb7: Note = Note::As7;
252    #[allow(non_upper_case_globals)]
253    pub const Db8: Note = Note::Cs8;
254    #[allow(non_upper_case_globals)]
255    pub const Eb8: Note = Note::Ds8;
256    #[allow(non_upper_case_globals)]
257    pub const Gb8: Note = Note::Fs8;
258    #[allow(non_upper_case_globals)]
259    pub const Ab8: Note = Note::Gs8;
260    #[allow(non_upper_case_globals)]
261    pub const Bb8: Note = Note::As8;
262    #[allow(non_upper_case_globals)]
263    pub const Db9: Note = Note::Cs9;
264    #[allow(non_upper_case_globals)]
265    pub const Eb9: Note = Note::Ds9;
266    #[allow(non_upper_case_globals)]
267    pub const Gb9: Note = Note::Fs9;
268    #[allow(non_upper_case_globals)]
269    pub const Ab9: Note = Note::Gs9;
270}
271
272#[cfg(test)]
273mod tests {
274
275    use super::*;
276    macro_rules! note_test {
277        ($($id:ident:$value:expr,)*) => {
278            $(
279                #[test]
280                fn $id() {
281                    let (input,expected) = $value;
282                    assert_eq!(input as u8, expected);
283                }
284            )*
285        }
286    }
287
288    //These test mainly prove we are generating all the sharps/flats
289    //(as the same number) correctly.
290    note_test! {
291            note_c1m:   (Note::C1m,0),
292            note_cs1m:  (Note::Cs1m,1),
293            note_db1m:  (Note::Db1m,1),
294            note_d1m:   (Note::D1m,2),
295            note_ds1m:  (Note::Ds1m,3),
296            note_eb1m:  (Note::Eb1m,3),
297            note_e1m:   (Note::E1m,4),
298            note_f1m:   (Note::F1m,5),
299            note_fs1m:  (Note::Fs1m,6),
300            note_gb1m:  (Note::Gb1m,6),
301            note_g1m:   (Note::G1m,7),
302            note_gs1m:  (Note::Gs1m,8),
303            note_ab1m:  (Note::Ab1m,8),
304            note_a1m:   (Note::A1m,9),
305            note_as1m:  (Note::As1m,10),
306            note_bb1m:  (Note::Bb1m,10),
307            note_b1m:   (Note::B1m,11),
308            note_c0:    (Note::C0,12),
309            note_cs0:   (Note::Cs0,13),
310            note_db0:   (Note::Db0,13),
311            note_d0:    (Note::D0,14),
312            note_ds0:   (Note::Ds0,15),
313            note_eb0:   (Note::Eb0,15),
314            note_e0:    (Note::E0,16),
315            note_f0:    (Note::F0,17),
316            note_fs0:   (Note::Fs0,18),
317            note_gb0:   (Note::Gb0,18),
318            note_g0:    (Note::G0,19),
319            note_gs0:   (Note::Gs0,20),
320            note_ab0:   (Note::Ab0,20),
321            note_a0:    (Note::A0,21),
322            note_as0:   (Note::As0,22),
323            note_bb0:   (Note::Bb0,22),
324            note_b0:    (Note::B0,23),
325            note_c1:    (Note::C1,24),
326            note_cs1:   (Note::Cs1,25),
327            note_db1:   (Note::Db1,25),
328            note_d1:    (Note::D1,26),
329            note_ds1:   (Note::Ds1,27),
330            note_eb1:   (Note::Eb1,27),
331            note_e1:    (Note::E1,28),
332            note_f1:    (Note::F1,29),
333            note_fs1:   (Note::Fs1,30),
334            note_gb1:   (Note::Gb1,30),
335            note_g1:    (Note::G1,31),
336            note_gs1:   (Note::Gs1,32),
337            note_ab1:   (Note::Ab1,32),
338            note_a1:    (Note::A1,33),
339            note_as1:   (Note::As1,34),
340            note_bb1:   (Note::Bb1,34),
341            note_b1:    (Note::B1,35),
342            note_c2:    (Note::C2,36),
343            note_cs2:   (Note::Cs2,37),
344            note_db2:   (Note::Db2,37),
345            note_d2:    (Note::D2,38),
346            note_ds2:   (Note::Ds2,39),
347            note_eb2:   (Note::Eb2,39),
348            note_e2:    (Note::E2,40),
349            note_f2:    (Note::F2,41),
350            note_fs2:   (Note::Fs2,42),
351            note_gb2:   (Note::Gb2,42),
352            note_g2:    (Note::G2,43),
353            note_gs2:   (Note::Gs2,44),
354            note_ab2:   (Note::Ab2,44),
355            note_a2:    (Note::A2,45),
356            note_as2:   (Note::As2,46),
357            note_bb2:   (Note::Bb2,46),
358            note_b2:    (Note::B2,47),
359            note_c3:    (Note::C3,48),
360            note_cs3:   (Note::Cs3,49),
361            note_db3:   (Note::Db3,49),
362            note_d3:    (Note::D3,50),
363            note_ds3:   (Note::Ds3,51),
364            note_eb3:   (Note::Eb3,51),
365            note_e3:    (Note::E3,52),
366            note_f3:    (Note::F3,53),
367            note_fs3:   (Note::Fs3,54),
368            note_gb3:   (Note::Gb3,54),
369            note_g3:    (Note::G3,55),
370            note_gs3:   (Note::Gs3,56),
371            note_ab3:   (Note::Ab3,56),
372            note_a3:    (Note::A3,57),
373            note_as3:   (Note::As3,58),
374            note_bb3:   (Note::Bb3,58),
375            note_b3:    (Note::B3,59),
376            note_c4:    (Note::C4,60),
377            note_cs4:   (Note::Cs4,61),
378            note_db4:   (Note::Db4,61),
379            note_d4:    (Note::D4,62),
380            note_ds4:   (Note::Ds4,63),
381            note_eb4:   (Note::Eb4,63),
382            note_e4:    (Note::E4,64),
383            note_f4:    (Note::F4,65),
384            note_fs4:   (Note::Fs4,66),
385            note_gb4:   (Note::Gb4,66),
386            note_g4:    (Note::G4,67),
387            note_gs4:   (Note::Gs4,68),
388            note_ab4:   (Note::Ab4,68),
389            note_a4:    (Note::A4,69),
390            note_as4:   (Note::As4,70),
391            note_bb4:   (Note::Bb4,70),
392            note_b4:    (Note::B4,71),
393            note_c5:    (Note::C5,72),
394            note_cs5:   (Note::Cs5,73),
395            note_db5:   (Note::Db5,73),
396            note_d5:    (Note::D5,74),
397            note_ds5:   (Note::Ds5,75),
398            note_eb5:   (Note::Eb5,75),
399            note_e5:    (Note::E5,76),
400            note_f5:    (Note::F5,77),
401            note_fs5:   (Note::Fs5,78),
402            note_gb5:   (Note::Gb5,78),
403            note_g5:    (Note::G5,79),
404            note_gs5:   (Note::Gs5,80),
405            note_ab5:   (Note::Ab5,80),
406            note_a5:    (Note::A5,81),
407            note_as5:   (Note::As5,82),
408            note_bb5:   (Note::Bb5,82),
409            note_b5:    (Note::B5,83),
410            note_c6:    (Note::C6,84),
411            note_cs6:   (Note::Cs6,85),
412            note_db6:   (Note::Db6,85),
413            note_d6:    (Note::D6,86),
414            note_ds6:   (Note::Ds6,87),
415            note_eb6:   (Note::Eb6,87),
416            note_e6:    (Note::E6,88),
417            note_f6:    (Note::F6,89),
418            note_fs6:   (Note::Fs6,90),
419            note_gb6:   (Note::Gb6,90),
420            note_g6:    (Note::G6,91),
421            note_gs6:   (Note::Gs6,92),
422            note_ab6:   (Note::Ab6,92),
423            note_a6:    (Note::A6,93),
424            note_as6:   (Note::As6,94),
425            note_bb6:   (Note::Bb6,94),
426            note_b6:    (Note::B6,95),
427            note_c7:    (Note::C7,96),
428            note_cs7:   (Note::Cs7,97),
429            note_db7:   (Note::Db7,97),
430            note_d7:    (Note::D7,98),
431            note_ds7:   (Note::Ds7,99),
432            note_eb7:   (Note::Eb7,99),
433            note_e7:    (Note::E7,100),
434            note_f7:    (Note::F7,101),
435            note_fs7:   (Note::Fs7,102),
436            note_gb7:   (Note::Gb7,102),
437            note_g7:    (Note::G7,103),
438            note_gs7:   (Note::Gs7,104),
439            note_ab7:   (Note::Ab7,104),
440            note_a7:    (Note::A7,105),
441            note_as7:   (Note::As7,106),
442            note_bb7:   (Note::Bb7,106),
443            note_b7:    (Note::B7,107),
444            note_c8:    (Note::C8,108),
445            note_cs8:   (Note::Cs8,109),
446            note_db8:   (Note::Db8,109),
447            note_d8:    (Note::D8,110),
448            note_ds8:   (Note::Ds8,111),
449            note_eb8:   (Note::Eb8,111),
450            note_e8:    (Note::E8,112),
451            note_f8:    (Note::F8,113),
452            note_fs8:   (Note::Fs8,114),
453            note_gb8:   (Note::Gb8,114),
454            note_g8:    (Note::G8,115),
455            note_gs8:   (Note::Gs8,116),
456            note_ab8:   (Note::Ab8,116),
457            note_a8:    (Note::A8,117),
458            note_as8:   (Note::As8,118),
459            note_bb8:   (Note::Bb8,118),
460            note_b8:    (Note::B8,119),
461            note_c9:    (Note::C9,120),
462            note_cs9:   (Note::Cs9,121),
463            note_db9:   (Note::Db9,121),
464            note_d9:    (Note::D9,122),
465            note_ds9:   (Note::Ds9,123),
466            note_eb9:   (Note::Eb9,123),
467            note_e9:    (Note::E9,124),
468            note_f9:    (Note::F9,125),
469            note_fs9:   (Note::Fs9,126),
470            note_gb9:   (Note::Gb9,126),
471            note_g9:    (Note::G9,127),
472            note_gs9:   (Note::Gs9,128),
473            note_ab9:   (Note::Ab9,128),
474    }
475}