From 4ccaf57d9d2ca3c603ce967cc8c288844f928912 Mon Sep 17 00:00:00 2001 From: Dan Muller Date: Sun, 16 Oct 2022 15:45:26 -0400 Subject: [PATCH] drop() no longer calls midiOutReset Fixes #122 On Windows, midiOutReset was being called before midiOutClose, presumably to cancel any pending output. This caused extra MIDI commands to be sent. Instead, we now call midiOutClose repeatedly until it's no longer blocked by pending output. --- src/backend/winmm/mod.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/backend/winmm/mod.rs b/src/backend/winmm/mod.rs index 3ec59a6..4f8e599 100644 --- a/src/backend/winmm/mod.rs +++ b/src/backend/winmm/mod.rs @@ -21,7 +21,7 @@ use windows::Win32::Media::Audio::{ midiInOpen, midiInPrepareHeader, midiInReset, midiInStart, midiInStop, midiInUnprepareHeader, midiOutClose, midiOutGetDevCapsW, midiOutGetNumDevs, midiOutLongMsg, midiOutOpen, - midiOutPrepareHeader, midiOutReset, midiOutShortMsg, + midiOutPrepareHeader, midiOutShortMsg, midiOutUnprepareHeader, midiInMessage, midiOutMessage, HMIDIIN, HMIDIOUT, MIDIHDR, MIDIINCAPSW, MIDIOUTCAPSW, CALLBACK_FUNCTION, CALLBACK_NULL, MIDIERR_NOTREADY, MIDIERR_STILLPLAYING, @@ -533,9 +533,12 @@ impl MidiOutputConnection { impl Drop for MidiOutputConnection { fn drop(&mut self) { - unsafe { - midiOutReset(self.out_handle); - midiOutClose(self.out_handle); + loop { + let result = unsafe { midiOutClose(self.out_handle) }; + if result == MIDIERR_STILLPLAYING { + sleep(Duration::from_millis(1)); + continue; + } else { break; } } } }