nom_midi/types/
note.rs

1//! The note enum and associated helper methods
2
3use std::mem;
4
5/// A note representable in a 7 bit unsigned int. The subscript 's' to a note means sharp. The
6/// subscript 'n' to an octave means negate, so `Cs2n` = C# in octave -2.
7///
8/// Because it only uses the least significant 7 bits, any value can be interpreted as either an i8
9/// or a u8 for free (as the representation is the same in both)
10///
11/// This implements both From<u8>, Into<u8>, From<i8> and Into<i8> so the names can be completely
12/// ignored if prefered
13#[repr(u8)]
14#[derive(Debug, PartialEq, Copy, Clone)]
15pub enum Note {
16    C1n = 0x00,
17    Cs1n = 0x01,
18    D1n = 0x02,
19    Ds1n = 0x03,
20    E1n = 0x04,
21    F1n = 0x05,
22    Fs1n = 0x06,
23    G1n = 0x07,
24    Gs1n = 0x08,
25    A1n = 0x09,
26    As1n = 0x0A,
27    B1n = 0x0B,
28    C0 = 0x0C,
29    Cs0 = 0x0D,
30    D0 = 0x0E,
31    Ds0 = 0x0F,
32    E0 = 0x10,
33    F0 = 0x11,
34    Fs0 = 0x12,
35    G0 = 0x13,
36    Gs0 = 0x14,
37    /// Start of 88-note piano keyboard range
38    A0 = 0x15,
39    As0 = 0x16,
40    B0 = 0x17,
41    C1 = 0x18,
42    Cs1 = 0x19,
43    D1 = 0x1A,
44    Ds1 = 0x1B,
45    E1 = 0x1C,
46    F1 = 0x1D,
47    Fs1 = 0x1E,
48    G1 = 0x1F,
49    Gs1 = 0x20,
50    A1 = 0x21,
51    As1 = 0x22,
52    B1 = 0x23,
53    /// Start of 5 octave synth range
54    C2 = 0x24,
55    Cs2 = 0x25,
56    D2 = 0x26,
57    Ds2 = 0x27,
58    E2 = 0x28,
59    F2 = 0x29,
60    Fs2 = 0x2A,
61    G2 = 0x2B,
62    Gs2 = 0x2C,
63    A2 = 0x2D,
64    As2 = 0x2E,
65    B2 = 0x2F,
66    C3 = 0x30,
67    Cs3 = 0x31,
68    D3 = 0x32,
69    Ds3 = 0x33,
70    E3 = 0x34,
71    F3 = 0x35,
72    Fs3 = 0x36,
73    G3 = 0x37,
74    Gs3 = 0x38,
75    A3 = 0x39,
76    As3 = 0x3A,
77    B3 = 0x3B,
78    /// Middle C
79    C4 = 0x3C,
80    Cs4 = 0x3D,
81    D4 = 0x3E,
82    Ds4 = 0x3F,
83    E4 = 0x40,
84    F4 = 0x41,
85    Fs4 = 0x42,
86    G4 = 0x43,
87    Gs4 = 0x44,
88    A4 = 0x45,
89    As4 = 0x46,
90    B4 = 0x47,
91    C5 = 0x48,
92    Cs5 = 0x49,
93    D5 = 0x4A,
94    Ds5 = 0x4B,
95    E5 = 0x4C,
96    F5 = 0x4D,
97    Fs5 = 0x4E,
98    G5 = 0x4F,
99    Gs5 = 0x50,
100    A5 = 0x51,
101    As5 = 0x52,
102    B5 = 0x53,
103    C6 = 0x54,
104    Cs6 = 0x55,
105    D6 = 0x56,
106    Ds6 = 0x57,
107    E6 = 0x58,
108    F6 = 0x59,
109    Fs6 = 0x5A,
110    G6 = 0x5B,
111    Gs6 = 0x5C,
112    A6 = 0x5D,
113    As6 = 0x5E,
114    B6 = 0x5F,
115    /// end of 5 octave synth range
116    C7 = 0x60,
117    Cs7 = 0x61,
118    D7 = 0x62,
119    Ds7 = 0x63,
120    E7 = 0x64,
121    F7 = 0x65,
122    Fs7 = 0x66,
123    G7 = 0x67,
124    Gs7 = 0x68,
125    A7 = 0x69,
126    As7 = 0x6A,
127    B7 = 0x6B,
128    /// end of 88-note piano keyboard range
129    C8 = 0x6C,
130    Cs8 = 0x6D,
131    D8 = 0x6E,
132    Ds8 = 0x6F,
133    E8 = 0x70,
134    F8 = 0x71,
135    Fs8 = 0x72,
136    G8 = 0x73,
137    Gs8 = 0x74,
138    A8 = 0x75,
139    As8 = 0x76,
140    B8 = 0x77,
141    C9 = 0x78,
142    Cs9 = 0x79,
143    D9 = 0x7A,
144    Ds9 = 0x7B,
145    E9 = 0x7C,
146    F9 = 0x7D,
147    Fs9 = 0x7E,
148    G9 = 0x7F,
149}
150
151impl From<u8> for Note {
152    #[inline(always)]
153    fn from(n: u8) -> Note {
154        unsafe { mem::transmute(n & 0x7f) }
155    }
156}
157
158impl From<Note> for u8 {
159    #[inline(always)]
160    fn from(note: Note) -> u8 {
161        note as u8
162    }
163}
164
165impl From<i8> for Note {
166    #[inline(always)]
167    fn from(n: i8) -> Note {
168        unsafe { mem::transmute(n & 0x7f) }
169    }
170}
171
172impl From<Note> for i8 {
173    #[inline(always)]
174    fn from(note: Note) -> Self {
175        note as i8
176    }
177}
178
179impl From<Note> for usize {
180    fn from(note: Note) -> Self {
181        (note as u8) as usize
182    }
183}