Skip to content

Commit

Permalink
Ensured that all MIDI events played via SMF player get processed (#80)
Browse files Browse the repository at this point in the history
Previously, some events may be occasionally dropped from the queue before
sending the terminal all-sound-off sequence.
  • Loading branch information
sergm committed Jan 13, 2022
1 parent 3486106 commit af501b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion mt32emu_qt/NEWS.txt
Expand Up @@ -16,7 +16,7 @@ HEAD:
counts, rendered the channel status grid looking weird. Additionally, this helps the Qt
layout calculator to work correctly with a high-dpi UI scaling on some systems.
* Fixed the standard MIDI file player occasionally failing to send all-sound-off sequence
when stopping playback. (#80)
when stopping playback; also, ensured that all played MIDI events get processed. (#80)

2021-05-22:

Expand Down
16 changes: 8 additions & 8 deletions mt32emu_qt/src/mididrv/SMFDriver.cpp
Expand Up @@ -24,9 +24,9 @@

static const MasterClockNanos MAX_SLEEP_TIME = 200 * MasterClock::NANOS_PER_MILLISECOND;

static void sendAllSoundOff(SynthRoute *synthRoute, bool resetAllControllers) {
static void sendAllSoundOff(SynthRoute *synthRoute, bool resetAllControllers, bool discardMidiBuffers) {
if (synthRoute->getState() != SynthRouteState_OPEN) return;
if (resetAllControllers) {
if (discardMidiBuffers) {
synthRoute->discardMidiBuffers();
} else {
synthRoute->flushMIDIQueue();
Expand Down Expand Up @@ -83,7 +83,7 @@ void SMFProcessor::run() {
if (driver->pauseProcessing) {
if (!paused) {
paused = true;
sendAllSoundOff(synthRoute, false);
sendAllSoundOff(synthRoute, false, false);
}
usleep(MAX_SLEEP_TIME / MasterClock::NANOS_PER_MICROSECOND);
MasterClockNanos delay = MasterClock::getClockNanos() - nanosNow;
Expand All @@ -97,17 +97,17 @@ void SMFProcessor::run() {
MasterClockNanos seekNanosSinceStart = totalSeconds * seekPosition * MasterClock::NANOS_PER_MILLISECOND;
MasterClockNanos currentNanosSinceStart = currentNanos - startNanos;
MasterClockNanos lastEventNanosSinceStart = currentNanosSinceStart - midiEvents.at(currentEventIx).getTimestamp() * midiTick;
bool resetAllControllers;
bool rewind;
if (seekNanosSinceStart < lastEventNanosSinceStart || seekNanosSinceStart == 0) {
midiTick = parser.getMidiTick();
emit driver->tempoUpdated(0);
currentEventIx = 0;
currentNanosSinceStart = midiEvents.at(currentEventIx).getTimestamp() * midiTick;
resetAllControllers = true;
rewind = true;
} else {
resetAllControllers = false;
rewind = false;
}
sendAllSoundOff(synthRoute, resetAllControllers);
sendAllSoundOff(synthRoute, rewind, rewind);
seek(synthRoute, midiEvents, currentEventIx, currentNanosSinceStart, seekNanosSinceStart);
nanosNow = MasterClock::getClockNanos();
startNanos = nanosNow - seekNanosSinceStart;
Expand Down Expand Up @@ -144,7 +144,7 @@ void SMFProcessor::run() {
break;
}
}
sendAllSoundOff(synthRoute, true);
sendAllSoundOff(synthRoute, true, false);
emit driver->playbackTimeChanged(0, 0);
qDebug() << "SMFDriver: processor thread stopped";
driver->deleteMidiSession(session);
Expand Down

0 comments on commit af501b1

Please sign in to comment.