Skip to content

Commit

Permalink
Fix parsing of SMF files with sysex messages
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexCharlton committed Dec 15, 2024
1 parent a6d5f19 commit ebd0fbc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ impl TrackEvent {
let p = time_offset + len_offset + 1;
ctx.is_smf_sysex = true;
let (event, event_len) = SystemExclusiveMsg::from_midi(&v[p..], ctx)?;
if event_len != len as usize {
// event_length does not include the terminating 0xF7 byte, while len is the length of the entire message
if event_len != len as usize + 1 {
return Err(ParseError::Invalid("Invalid system exclusive message"));
}
Ok((
Expand All @@ -515,7 +516,7 @@ impl TrackEvent {
event: MidiMsg::SystemExclusive { msg: event },
beat_or_frame,
},
p + event_len,
p + len as usize,
))
}
0x7 => {
Expand All @@ -524,7 +525,7 @@ impl TrackEvent {
ctx.is_smf_sysex = false;
let (event, event_len) = MidiMsg::from_midi_with_context(&v[p..], ctx)?;

if event_len != len as usize {
if event_len != len as usize + 1 {
return Err(ParseError::Invalid("Invalid system exclusive message"));
}
Ok((
Expand All @@ -533,7 +534,7 @@ impl TrackEvent {
event,
beat_or_frame,
},
p + event_len,
p + len as usize,
))
}
0xF => {
Expand Down
Binary file added tests/breaking-the-law.mid
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/file_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,12 @@ fn test_score_file() {
assert!(deserialize_result.is_ok());
assert_eq!(deserialize_result.unwrap(), expected);
}

#[test]
#[cfg(feature = "file")]
fn test_smf_file_with_sysex() {
let test_file = include_bytes!("./breaking-the-law.mid");

let deserialize_result = MidiFile::from_midi(test_file);
assert!(deserialize_result.is_ok());
}

0 comments on commit ebd0fbc

Please sign in to comment.