Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deleteOverlapps #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src-library/MidiEventList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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++;
}
Expand Down
85 changes: 85 additions & 0 deletions src-programs/deleteOverlapps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// Programmer: Tom M. <[email protected]>
// 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 <iostream>
#include <iomanip>

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; e<midifile[track].size(); e++)
{
MidiEvent* on2 = &midifile[track][e];

if(on2 != NULL &&
on2->isNoteOn() &&
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;
}