Skip to main content

aura_midi/
message.rs

1//! Channel / system MIDI message (sample-accurate payload only).
2
3/// High nibble of status byte (channel messages use low nibble as channel).
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
5#[repr(u8)]
6pub enum MidiStatus {
7    NoteOff = 0x80,
8    NoteOn = 0x90,
9    PolyAftertouch = 0xA0,
10    ControlChange = 0xB0,
11    ProgramChange = 0xC0,
12    ChannelPressure = 0xD0,
13    PitchBend = 0xE0,
14    /// System messages (0xF0..); channel field is unused.
15    System = 0xF0,
16}
17
18impl MidiStatus {
19    #[must_use]
20    pub const fn from_status_byte(status: u8) -> Self {
21        match status & 0xF0 {
22            0x80 => Self::NoteOff,
23            0x90 => Self::NoteOn,
24            0xA0 => Self::PolyAftertouch,
25            0xB0 => Self::ControlChange,
26            0xC0 => Self::ProgramChange,
27            0xD0 => Self::ChannelPressure,
28            0xE0 => Self::PitchBend,
29            _ => Self::System,
30        }
31    }
32}
33
34/// One MIDI message. Channel is `0..=15` for channel voice messages.
35#[derive(Clone, Copy, Debug, PartialEq, Eq)]
36pub struct MidiMessage {
37    pub status: MidiStatus,
38    /// Wire channel `0..=15`. Ignored for [`MidiStatus::System`].
39    pub channel: u8,
40    pub data1: u8,
41    pub data2: u8,
42}
43
44impl MidiMessage {
45    #[must_use]
46    pub const fn raw(status_byte: u8, data1: u8, data2: u8) -> Self {
47        let status = MidiStatus::from_status_byte(status_byte);
48        let channel = if matches!(status, MidiStatus::System) {
49            0
50        } else {
51            status_byte & 0x0F
52        };
53        Self {
54            status,
55            channel,
56            data1,
57            data2,
58        }
59    }
60
61    #[must_use]
62    pub const fn note_on(channel: u8, note: u8, velocity: u8) -> Self {
63        Self {
64            status: MidiStatus::NoteOn,
65            channel: channel & 0x0F,
66            data1: note & 0x7F,
67            data2: velocity & 0x7F,
68        }
69    }
70
71    #[must_use]
72    pub const fn note_off(channel: u8, note: u8, velocity: u8) -> Self {
73        Self {
74            status: MidiStatus::NoteOff,
75            channel: channel & 0x0F,
76            data1: note & 0x7F,
77            data2: velocity & 0x7F,
78        }
79    }
80
81    #[must_use]
82    pub const fn control_change(channel: u8, controller: u8, value: u8) -> Self {
83        Self {
84            status: MidiStatus::ControlChange,
85            channel: channel & 0x0F,
86            data1: controller & 0x7F,
87            data2: value & 0x7F,
88        }
89    }
90
91    #[must_use]
92    pub const fn pitch_bend(channel: u8, value_14: u16) -> Self {
93        let v = if value_14 > 0x3FFF { 0x3FFF } else { value_14 };
94        Self {
95            status: MidiStatus::PitchBend,
96            channel: channel & 0x0F,
97            data1: (v & 0x7F) as u8,
98            data2: ((v >> 7) & 0x7F) as u8,
99        }
100    }
101
102    #[must_use]
103    pub const fn program_change(channel: u8, program: u8) -> Self {
104        Self {
105            status: MidiStatus::ProgramChange,
106            channel: channel & 0x0F,
107            data1: program & 0x7F,
108            data2: 0,
109        }
110    }
111
112    #[must_use]
113    pub const fn channel_pressure(channel: u8, pressure: u8) -> Self {
114        Self {
115            status: MidiStatus::ChannelPressure,
116            channel: channel & 0x0F,
117            data1: pressure & 0x7F,
118            data2: 0,
119        }
120    }
121
122    /// Status byte with channel in the low nibble (system → 0xF0).
123    #[must_use]
124    pub const fn status_byte(self) -> u8 {
125        match self.status {
126            MidiStatus::System => 0xF0,
127            other => (other as u8) | (self.channel & 0x0F),
128        }
129    }
130
131    #[must_use]
132    pub const fn is_note_on(self) -> bool {
133        matches!(self.status, MidiStatus::NoteOn) && self.data2 > 0
134    }
135
136    #[must_use]
137    pub const fn is_note_off(self) -> bool {
138        matches!(self.status, MidiStatus::NoteOff)
139            || (matches!(self.status, MidiStatus::NoteOn) && self.data2 == 0)
140    }
141
142    #[must_use]
143    pub const fn note_number(self) -> Option<u8> {
144        match self.status {
145            MidiStatus::NoteOn | MidiStatus::NoteOff | MidiStatus::PolyAftertouch => {
146                Some(self.data1)
147            }
148            _ => None,
149        }
150    }
151
152    /// 14-bit pitch bend `0..=16383` (center = 8192).
153    #[must_use]
154    pub const fn pitch_bend_value(self) -> Option<u16> {
155        if matches!(self.status, MidiStatus::PitchBend) {
156            Some((self.data1 as u16) | ((self.data2 as u16) << 7))
157        } else {
158            None
159        }
160    }
161}
162
163#[cfg(test)]
164mod tests {
165    use super::*;
166
167    #[test]
168    fn note_on_roundtrip_status() {
169        let m = MidiMessage::note_on(3, 60, 100);
170        assert!(m.is_note_on());
171        assert_eq!(m.note_number(), Some(60));
172        assert_eq!(m.status_byte(), 0x93);
173        assert_eq!(MidiMessage::raw(m.status_byte(), m.data1, m.data2), m);
174    }
175
176    #[test]
177    fn note_on_zero_vel_is_note_off() {
178        let m = MidiMessage::note_on(0, 48, 0);
179        assert!(m.is_note_off());
180        assert!(!m.is_note_on());
181    }
182
183    #[test]
184    fn pitch_bend_14bit() {
185        let m = MidiMessage::pitch_bend(0, 8192);
186        assert_eq!(m.pitch_bend_value(), Some(8192));
187    }
188}