foxygit / doom Log in
commit a84bfaefd8ded67dff17cf97e047196fefb440dc
Author:     Roman Fomin <rfomin@gmail.com>
AuthorDate: Mon Dec 6 16:16:39 2021 +0700
Commit:     Roman Fomin <rfomin@gmail.com>
CommitDate: Mon Dec 6 16:16:39 2021 +0700

    win midi: Reset pitch bend range when stopping song
---
 src/i_winmusic.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/i_winmusic.c b/src/i_winmusic.c
index 7a95e413..e3796359 100644
--- a/src/i_winmusic.c
+++ b/src/i_winmusic.c
@@ -407,6 +407,29 @@ void I_WIN_StopSong(void)
         hPlayerThread = NULL;
     }

+    for (i = 0; i < MIDI_CHANNELS_PER_TRACK; ++i)
+    {
+        DWORD msg = 0;
+
+        // RPN sequence to adjust pitch bend range (RPN value 0x0000)
+        msg = MIDI_EVENT_CONTROLLER | i | 0x65 << 8 | 0x00 << 16;
+        midiOutShortMsg((HMIDIOUT)hMidiStream, msg);
+        msg = MIDI_EVENT_CONTROLLER | i | 0x64 << 8 | 0x00 << 16;
+        midiOutShortMsg((HMIDIOUT)hMidiStream, msg);
+
+        // reset pitch bend range to central tuning +/- 2 semitones and 0 cents
+        msg = MIDI_EVENT_CONTROLLER | i | 0x06 << 8 | 0x02 << 16;
+        midiOutShortMsg((HMIDIOUT)hMidiStream, msg);
+        msg = MIDI_EVENT_CONTROLLER | i | 0x26 << 8 | 0x00 << 16;
+        midiOutShortMsg((HMIDIOUT)hMidiStream, msg);
+
+        // end of RPN sequence
+        msg = MIDI_EVENT_CONTROLLER | i | 0x64 << 8 | 0x7F << 16;
+        midiOutShortMsg((HMIDIOUT)hMidiStream, msg);
+        msg = MIDI_EVENT_CONTROLLER | i | 0x65 << 8 | 0x7F << 16;
+        midiOutShortMsg((HMIDIOUT)hMidiStream, msg);
+    }
+
     mmr = midiStreamStop(hMidiStream);
     if (mmr != MMSYSERR_NOERROR)
     {