Skip to content

Commit

Permalink
fix ties in chords
Browse files Browse the repository at this point in the history
  • Loading branch information
mtwestra authored and dfober committed Oct 24, 2024
1 parent 7761a77 commit da61efb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/visitors/midicontextvisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ void midicontextvisitor::playNote (const notevisitor& note)
if (vel == notevisitor::kUndefinedDynamics) vel = fCurrentDynamics;

int tie = note.getTie();
int midiPitch = (int)note.getMidiPitch(); // Use raw MIDI pitch as map key
long date = note.inChord() ? fLastPosition : fCurrentDate;
if (note.isGrace()) { // grace notes
dur = fTPQ / 6; // have no duration - set to an arbitrary value
Expand All @@ -186,13 +187,16 @@ void midicontextvisitor::playNote (const notevisitor& note)
fMidiWriter->newNote(date, chan, note.getMidiPitch(), vel, dur);
}
else if (tie & StartStop::start) {
fPendingDuration += dur;
// Start of tie or continutation of tie - accumulate duration for this specific pitch
fPendingDurations[midiPitch] += dur;
return;
}
else if (tie == StartStop::stop) {
dur += fPendingDuration;
if (fPendingDurations.count(midiPitch)) { // check if we have a pending duration for this pitch
dur += fPendingDurations[midiPitch]; // add duration to pending duration for this pitch
fPendingDurations.erase(midiPitch); // clear pending duration
}
fMidiWriter->newNote(date, chan, note.getMidiPitch(), vel, dur);
fPendingDuration = 0;
}
}
// finally adjust the current date
Expand Down Expand Up @@ -253,7 +257,8 @@ void midicontextvisitor::visitEnd ( S_transpose& elt ) { fTranspose = fChromati
//________________________________________________________________________
void midicontextvisitor::visitStart ( S_part& elt )
{
fCurrentDate = fLastPosition = fPendingDuration = 0;
fCurrentDate = fLastPosition = 0;
fPendingDurations.clear();
fEndMeasureDate = fEndPartDate = 0;
fTranspose = 0;
fDivisions = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/visitors/midicontextvisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class EXP midicontextvisitor :
long fDivisions; // current division
long fCurrentDate; // current date
long fLastPosition; // last time position (used for chord)
long fPendingDuration; // pending duration (used for tied notes)
std::map<int, long> fPendingDurations; // map of pitch -> pending duration,(used for tied notes)
long fCurrentDynamics; // current dynamics ie MIDI velocity
long fTranspose; // current transpose value
long fTPQ; // ticks-per-quater value for date conversion
Expand Down

0 comments on commit da61efb

Please sign in to comment.