Skip to content

Commit

Permalink
Update sorting to allow for empty messages and to sort controllers be…
Browse files Browse the repository at this point in the history
…tter automatically.
  • Loading branch information
craigsapp committed Apr 18, 2018
1 parent 82baf1a commit 732faa3
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src-library/MidiEventList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,34 +554,50 @@ int eventcompare(const void* a, const void* b) {
// aevent sequencing state occurs before bevent
// see MidiEventList::markSequence()
return -1;
} else if (aevent[0] == 0xff && aevent[1] == 0x2f) {
} else if (aevent.getP0() == 0xff && aevent.getP1() == 0x2f) {
// end-of-track meta-message should always be last (but won't really
// matter since the writing function ignores all end-of-track messages
// and writes its own.
return +1;
} else if (bevent[0] == 0xff && bevent[1] == 0x2f) {
} else if (bevent.getP0() == 0xff && bevent.getP1() == 0x2f) {
// end-of-track meta-message should always be last (but won't really
// matter since the writing function ignores all end-of-track messages
// and writes its own.
return -1;
} else if (aevent[0] == 0xff && bevent[0] != 0xff) {
} else if (aevent.getP0() == 0xff && bevent.getP0() != 0xff) {
// other meta-messages are placed before real MIDI messages
return -1;
} else if (aevent[0] != 0xff && bevent[0] == 0xff) {
} else if (aevent.getP0() != 0xff && bevent.getP0() == 0xff) {
// other meta-messages are placed before real MIDI messages
return +1;
} else if (((aevent[0] & 0xf0) == 0x90) && (aevent[2] != 0)) {
} else if (((aevent.getP0() & 0xf0) == 0x90) && (aevent.getP2() != 0)) {
// note-ons come after all other types of MIDI messages
return +1;
} else if (((bevent[0] & 0xf0) == 0x90) && (bevent[2] != 0)) {
} else if (((bevent.getP0() & 0xf0) == 0x90) && (bevent.getP2() != 0)) {
// note-ons come after all other types of MIDI messages
return -1;
} else if (((aevent[0] & 0xf0) == 0x90) || ((aevent[0] & 0xf0) == 0x80)) {
} else if (((aevent.getP0() & 0xf0) == 0x90) || ((aevent.getP0() & 0xf0) == 0x80)) {
// note-offs come after all other MIDI messages (except note-ons)
return +1;
} else if (((bevent[0] & 0xf0) == 0x90) || ((bevent[0] & 0xf0) == 0x80)) {
} else if (((bevent.getP0() & 0xf0) == 0x90) || ((bevent.getP0() & 0xf0) == 0x80)) {
// note-offs come after all other MIDI messages (except note-ons)
return -1;
} else if (((aevent.getP0() & 0xf0) == 0xb0) && ((bevent.getP0() & 0xf0) == 0xb0)) {
// both events are continuous controllers. Sort them by controller number
if (aevent.getP1() > bevent.getP1()) {
return +1;
} if (aevent.getP1() < bevent.getP1()) {
return -1;
} else {
// same controller number, so sort by data value
if (aevent.getP2() > bevent.getP2()) {
return +1;
} if (aevent.getP2() < bevent.getP2()) {
return -1;
} else {
return 0;
}
}
} else {
return 0;
}
Expand Down

1 comment on commit 732faa3

@craigsapp
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit updates the sort function for two things:

(1) change from operator[] to getP*() functions. This allows the event list to be sorted even when the expected number of bytes is not correct in the midi messages. Also this allows for sorting tracks when there is an empty message in the list.

(2) the function now sorts controllers with the same timestamp according to controller number (lower numbers first), and if still a tie, then by controller value (lower values first). This helps place pedal up messages before pedal down messages (and similar cases for other binary controllers).

Please sign in to comment.