Skip to content

Commit

Permalink
Added support for SysEx in CodeIndexNumber::try_from_payload
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcebox committed Dec 22, 2024
1 parent 1bc9fc4 commit 5f08120
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/packet/code_index_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,36 @@ impl CodeIndexNumber {
0xC0 => Ok(Self::ProgramChange),
0xD0 => Ok(Self::ChannelPressure),
0xE0 => Ok(Self::PitchBendChange),
_ => Err(MidiPacketParsingError::InvalidPayloadStatus),
_ => {
if payload.len() > 1 && payload[1] == 0xF7 {
Ok(Self::SysexEnds2Bytes)
} else if payload.len() > 2 && payload[2] == 0xF7 {
Ok(Self::SysexEnds3Bytes)
} else if payload.len() == 1 {
Ok(Self::SingleByte)
} else if payload.len() > 2 {
Ok(Self::SysexStartsOrContinues)
} else {
Err(MidiPacketParsingError::InvalidPayloadSize)
}
}
}
} else {
match status {
0xF0 => {
if payload.len() > 1 && payload[1] == 0xF7 {
Ok(Self::SysexEnds2Bytes)
} else if payload.len() > 2 && payload[2] == 0xF7 {
Ok(Self::SysexEnds3Bytes)
} else if payload.len() > 2 {
Ok(Self::SysexStartsOrContinues)
} else {
Err(MidiPacketParsingError::InvalidPayloadSize)
}
}
0xF1 | 0xF3 => Ok(Self::SystemCommon2Bytes),
0xF2 => Ok(Self::SystemCommon3Bytes),
0xF6 => Ok(Self::SystemCommon1Byte),
0xF6 | 0xF7 => Ok(Self::SystemCommon1Byte),
0xF8 | 0xF9 | 0xFA | 0xFB | 0xFC | 0xFE | 0xFF => Ok(Self::SingleByte),
_ => Err(MidiPacketParsingError::InvalidPayloadStatus),
}
Expand Down Expand Up @@ -175,6 +198,17 @@ mod tests {
stop: ([0xFC], Ok(CodeIndexNumber::SingleByte)),
active_sensing: ([0xFE], Ok(CodeIndexNumber::SingleByte)),
system_reset: ([0xFF], Ok(CodeIndexNumber::SingleByte)),
sysex_starts: ([0xF0, 1, 2], Ok(CodeIndexNumber::SysexStartsOrContinues)),
sysex_starts_1byte: ([0xF0], Err(MidiPacketParsingError::InvalidPayloadSize)),
sysex_starts_2byte: ([0xF0, 1], Err(MidiPacketParsingError::InvalidPayloadSize)),
sysex_continues_1byte: ([1], Ok(CodeIndexNumber::SingleByte)),
sysex_continues_2bytes: ([1, 2], Err(MidiPacketParsingError::InvalidPayloadSize)),
sysex_continues_3bytes: ([1, 2, 3], Ok(CodeIndexNumber::SysexStartsOrContinues)),
sysex_ends_1byte: ([0xF7], Ok(CodeIndexNumber::SystemCommon1Byte)),
sysex_ends_2bytes: ([1, 0xF7], Ok(CodeIndexNumber::SysexEnds2Bytes)),
sysex_ends_3bytes: ([1, 2, 0xF7], Ok(CodeIndexNumber::SysexEnds3Bytes)),
sysex_2bytes: ([0xF0, 0xF7], Ok(CodeIndexNumber::SysexEnds2Bytes)),
sysex_3bytes: ([0xF0, 1, 0xF7], Ok(CodeIndexNumber::SysexEnds3Bytes)),
undefined_f4: ([0xF4], Err(MidiPacketParsingError::InvalidPayloadStatus)),
undefined_f5: ([0xF5], Err(MidiPacketParsingError::InvalidPayloadStatus)),
empty: ([], Err(MidiPacketParsingError::EmptyPayload)),
Expand Down

0 comments on commit 5f08120

Please sign in to comment.