midi_event/types/
note.rs

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