108 lines
3.2 KiB
Rust
108 lines
3.2 KiB
Rust
use fader_core::{MidiChannel, MidiData};
|
|
use thiserror::Error;
|
|
|
|
/// Press/release semantics normalized across note and CC buttons.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum ButtonEdge {
|
|
Press,
|
|
Release,
|
|
}
|
|
|
|
/// Supported MIDI 1.0 channel messages.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum MidiMessage {
|
|
ControlChange {
|
|
channel: MidiChannel,
|
|
number: MidiData,
|
|
value: MidiData,
|
|
},
|
|
Note {
|
|
channel: MidiChannel,
|
|
number: MidiData,
|
|
velocity: MidiData,
|
|
edge: ButtonEdge,
|
|
},
|
|
}
|
|
|
|
/// Malformed bytes for a message type that fader_master otherwise supports.
|
|
#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
|
|
pub enum DecodeError {
|
|
#[error("empty MIDI message")]
|
|
Empty,
|
|
#[error("MIDI message starts with data byte 0x{0:02x}; running status is unsupported")]
|
|
MissingStatus(u8),
|
|
#[error("MIDI status 0x{status:02x} requires {expected} bytes, received {actual}")]
|
|
Length {
|
|
status: u8,
|
|
expected: usize,
|
|
actual: usize,
|
|
},
|
|
#[error("MIDI byte {position} contains a status bit: 0x{value:02x}")]
|
|
InvalidData { position: usize, value: u8 },
|
|
#[error("unsupported MIDI status reached the channel decoder: 0x{0:02x}")]
|
|
UnsupportedStatus(u8),
|
|
}
|
|
|
|
/// Decodes supported channel messages and ignores unrelated MIDI traffic.
|
|
///
|
|
/// Clock, active-sensing, SysEx, program-change, pressure, and pitch messages
|
|
/// are deliberately outside the first controller profile and return `None`.
|
|
pub fn decode_message(bytes: &[u8]) -> Result<Option<MidiMessage>, DecodeError> {
|
|
let Some(&status) = bytes.first() else {
|
|
return Err(DecodeError::Empty);
|
|
};
|
|
if status & 0x80 == 0 {
|
|
return Err(DecodeError::MissingStatus(status));
|
|
}
|
|
|
|
match status & 0xf0 {
|
|
0x80 | 0x90 | 0xb0 => decode_three_byte(status, bytes).map(Some),
|
|
_ => Ok(None),
|
|
}
|
|
}
|
|
|
|
fn decode_three_byte(status: u8, bytes: &[u8]) -> Result<MidiMessage, DecodeError> {
|
|
if bytes.len() != 3 {
|
|
return Err(DecodeError::Length {
|
|
status,
|
|
expected: 3,
|
|
actual: bytes.len(),
|
|
});
|
|
}
|
|
let first = data_byte(bytes[1], 1)?;
|
|
let second = data_byte(bytes[2], 2)?;
|
|
let channel = MidiChannel::new(u16::from(status & 0x0f) + 1)
|
|
.map_err(|_| DecodeError::MissingStatus(status))?;
|
|
match status & 0xf0 {
|
|
0x80 => Ok(MidiMessage::Note {
|
|
channel,
|
|
number: first,
|
|
velocity: second,
|
|
edge: ButtonEdge::Release,
|
|
}),
|
|
0x90 => Ok(MidiMessage::Note {
|
|
channel,
|
|
number: first,
|
|
velocity: second,
|
|
edge: if second.get() == 0 {
|
|
ButtonEdge::Release
|
|
} else {
|
|
ButtonEdge::Press
|
|
},
|
|
}),
|
|
0xb0 => Ok(MidiMessage::ControlChange {
|
|
channel,
|
|
number: first,
|
|
value: second,
|
|
}),
|
|
_ => Err(DecodeError::UnsupportedStatus(status)),
|
|
}
|
|
}
|
|
|
|
fn data_byte(value: u8, position: usize) -> Result<MidiData, DecodeError> {
|
|
if value & 0x80 != 0 {
|
|
return Err(DecodeError::InvalidData { position, value });
|
|
}
|
|
MidiData::new(u16::from(value)).map_err(|_| DecodeError::InvalidData { position, value })
|
|
}
|