modern-midi icon indicating copy to clipboard operation
modern-midi copied to clipboard

Bug when parsing midi with running status

Open jcelerier opened this issue 5 years ago • 0 comments

Hello, so, I'm working on a fork of this lib & rtmidi (https://github.com/jcelerier/RtMidi17), but I found a bug when parsing midi files with running status so it may be of interest for you :

here: https://github.com/ddiakopoulos/ModernMIDI/blob/master/src/midi_file_reader.cpp#L160

say that you have for instance the following midi bytes sequence:

 00 99 23 50 00 31 50 77 23 00
|-----------|--------|--------|
      1          2       3

1 is a note on, channel 10, note 35, velocity 80 2 is a note on, channel 10, note 49, velocity 80 3 is a note on, channel 10, note 35, velocity 0 which starts a bit later

now, after parsing the first note:

  • parseEvent is called with dataStart pointing on the second byte of 2 (0x31) since it's the first bit of the midi channel event
  • type = *dataStart++, hence dataStart points on the third byte of 2 (0x50) and type == 0x31
  • we enter the running status check :
if (((uint8_t) type & 0x80) == 0) 
{
  event->m->data[0] = (uint8_t) type;
  type = lastEventTypeByte;
}

after this, the data stored in the event looks like :

[31, 00, 00]

-> the type is wrong, and event->m->data[1] is never set !

Instead, it should be :

if (((uint8_t) type & 0x80) == 0)
{
  event->m->data[0] = (uint8_t) lastEventTypeByte;
  event->m->data[1] = (uint8_t) type;
  type = lastEventTypeByte;
}

after which the data looks like

[99, 31, 00]

and the switch can work correctly afterwards

jcelerier avatar Jul 28 '18 11:07 jcelerier