qualia_audio/midi/message/
note.rs1use crate::types::AudioError;
7
8pub const STATUS_NOTE_OFF: u8 = 0x80;
10pub const STATUS_NOTE_ON: u8 = 0x90;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct NoteOn {
17 pub channel: u8,
19 pub note: u8,
21 pub velocity: u8,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub struct NoteOff {
28 pub channel: u8,
30 pub note: u8,
32 pub velocity: u8,
34}
35
36impl NoteOn {
37 pub fn new(channel: u8, note: u8, velocity: u8) -> Result<Self, AudioError> {
40 if channel > 15 || note > 127 || velocity > 127 {
41 return Err(AudioError::InvalidParameter);
42 }
43 Ok(Self {
44 channel,
45 note,
46 velocity,
47 })
48 }
49
50 #[inline]
52 pub fn to_bytes(self) -> [u8; 3] {
53 [
54 STATUS_NOTE_ON | (self.channel & 0x0F),
55 self.note,
56 self.velocity,
57 ]
58 }
59
60 pub fn parse(bytes: &[u8]) -> Result<Self, AudioError> {
65 if bytes.len() < 3 {
66 return Err(AudioError::MalformedAudio);
67 }
68 if bytes[0] & 0xF0 != STATUS_NOTE_ON {
69 return Err(AudioError::UnsupportedFormat);
70 }
71 if bytes[1] > 127 || bytes[2] > 127 {
72 return Err(AudioError::MalformedAudio);
73 }
74 Ok(Self {
75 channel: bytes[0] & 0x0F,
76 note: bytes[1],
77 velocity: bytes[2],
78 })
79 }
80
81 #[inline]
83 pub fn is_effectively_note_off(self) -> bool {
84 self.velocity == 0
85 }
86}
87
88impl NoteOff {
89 pub fn new(channel: u8, note: u8, velocity: u8) -> Result<Self, AudioError> {
91 if channel > 15 || note > 127 || velocity > 127 {
92 return Err(AudioError::InvalidParameter);
93 }
94 Ok(Self {
95 channel,
96 note,
97 velocity,
98 })
99 }
100
101 #[inline]
103 pub fn to_bytes(self) -> [u8; 3] {
104 [
105 STATUS_NOTE_OFF | (self.channel & 0x0F),
106 self.note,
107 self.velocity,
108 ]
109 }
110
111 pub fn parse(bytes: &[u8]) -> Result<Self, AudioError> {
113 if bytes.len() < 3 {
114 return Err(AudioError::MalformedAudio);
115 }
116 if bytes[0] & 0xF0 != STATUS_NOTE_OFF {
117 return Err(AudioError::UnsupportedFormat);
118 }
119 if bytes[1] > 127 || bytes[2] > 127 {
120 return Err(AudioError::MalformedAudio);
121 }
122 Ok(Self {
123 channel: bytes[0] & 0x0F,
124 note: bytes[1],
125 velocity: bytes[2],
126 })
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use super::*;
133
134 #[test]
135 fn golden_note_on_parse() {
136 let n = NoteOn::parse(&[0x90, 0x3C, 0x64]).unwrap();
138 assert_eq!(n.channel, 0);
139 assert_eq!(n.note, 60);
140 assert_eq!(n.velocity, 100);
141 }
142
143 #[test]
144 fn note_on_roundtrip() {
145 let n = NoteOn::new(9, 64, 127).unwrap();
146 assert_eq!(n.to_bytes(), [0x99, 64, 127]);
147 assert_eq!(NoteOn::parse(&n.to_bytes()).unwrap(), n);
148 }
149
150 #[test]
151 fn note_off_roundtrip() {
152 let n = NoteOff::new(0, 60, 0).unwrap();
153 assert_eq!(n.to_bytes(), [0x80, 60, 0]);
154 assert_eq!(NoteOff::parse(&n.to_bytes()).unwrap(), n);
155 }
156
157 #[test]
158 fn note_on_zero_velocity_is_note_off() {
159 assert!(NoteOn::new(0, 60, 0).unwrap().is_effectively_note_off());
160 assert!(!NoteOn::new(0, 60, 1).unwrap().is_effectively_note_off());
161 }
162
163 #[test]
164 fn out_of_range_rejected() {
165 assert_eq!(NoteOn::new(16, 0, 0), Err(AudioError::InvalidParameter));
166 assert_eq!(NoteOn::new(0, 128, 0), Err(AudioError::InvalidParameter));
167 assert_eq!(
168 NoteOn::parse(&[0x90, 0x3C]),
169 Err(AudioError::MalformedAudio)
170 );
171 assert_eq!(
172 NoteOn::parse(&[0x80, 0x3C, 0x64]),
173 Err(AudioError::UnsupportedFormat)
174 );
175 }
176}