diff --git a/src-library/MidiEventList.cpp b/src-library/MidiEventList.cpp index e6b4e6c9..960747dd 100644 --- a/src-library/MidiEventList.cpp +++ b/src-library/MidiEventList.cpp @@ -204,7 +204,7 @@ int MidiEventList::push_back(MidiEvent& event) { // There are two models that can be done if two notes are overlapping // on the same pitch: the first note-off affects the last note-on, // or the first note-off affects the first note-on. Currently the -// first note-off affects the last note-on, but both methods could +// first note-off affects the first note-on, but both methods could // be implemented with user selectability. The current state of the // track is assumed to be in time-sorted order. Returns the number // of linked notes (note-on/note-off pairs). @@ -311,8 +311,8 @@ int MidiEventList::linkNotePairs(void) { key = mev->getKeyNumber(); channel = mev->getChannel(); if (noteons[channel][key].size() > 0) { - noteon = noteons[channel][key].back(); - noteons[channel][key].pop_back(); + noteon = noteons[channel][key].front(); + noteons[channel][key].erase(noteons[channel][key].begin()); noteon->linkEvent(mev); counter++; } diff --git a/src-programs/deleteOverlapps.cpp b/src-programs/deleteOverlapps.cpp new file mode 100644 index 00000000..966612a8 --- /dev/null +++ b/src-programs/deleteOverlapps.cpp @@ -0,0 +1,85 @@ +// +// Programmer: Tom M. +// Creation Date: Sat Nov 05 14:51:00 PST 2016 +// Last Modified: Sat Nov 05 14:51:00 PST 2016 +// Filename: midifile/src-programs/deleteOverlapps.cpp +// Web Address: https://github.com/craigsapp/midifile/blob/master/src-programs/deleteOverlapps.cpp +// Syntax: C++ +// +// Description: gets rid of overlapping note in a midi file, i.e. sets the note off event of an overlapping note right before +// note on event of a second note +// +// + +#include "MidiFile.h" +#include "Options.h" +#include +#include + +using namespace std; + +int main(int argc, char** argv) { + Options options; + options.process(argc, argv); + MidiFile midifile; + if (options.getArgCount() > 1) { + midifile.read(options.getArg(1)); + } + else + { + cout << "usage: infile.mid outfile.mid" << endl; + return -1; + } + + + cout << "TPQ: " << midifile.getTicksPerQuarterNote() << endl; + cout << "TRACKS: " << midifile.getTrackCount() << endl; + + + cout << "notepairs: " << midifile.linkNotePairs() << endl; + + midifile.absoluteTicks(); + + for (int track=0; track < midifile.getTrackCount(); track++) + { + cout << "\nTrack " << track << endl; + for (int event=0; event < midifile[track].size(); event++) + + { + MidiEvent* on1 = &midifile[track][event]; + if (on1->isNoteOn()) + { + MidiEvent* off1 = on1->getLinkedEvent(); + if(off1 != nullptr) + { + on1->unlinkEvent(); + off1->unlinkEvent(); + + for(int e=event; eisNoteOn() && + on1->getKeyNumber() == on2->getKeyNumber() && + on1->getChannel() == on2->getChannel() && + + on1->tick < on2->tick && + off1->tick >= on2->tick // they overlapp + ) + { + off1->tick = on2->tick-1; + } + + } + } + } + } + } + + midifile.sortTracks(); + midifile.write(options.getArg(2)); + + + return 0; +}