From b2d502cab0b95036c2afcedc3bebce447f82df04 Mon Sep 17 00:00:00 2001 From: Tony Mountifield Date: Wed, 4 Dec 2024 16:41:35 +0000 Subject: [PATCH] Remove limit on MIDI device numbers --- src/sound/midi-win/midi.cpp | 29 +++++++++++------------------ src/sound/midi-win/midi.h | 7 ++----- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/sound/midi-win/midi.cpp b/src/sound/midi-win/midi.cpp index 3a06bf7932..ef4c6a9aff 100644 --- a/src/sound/midi-win/midi.cpp +++ b/src/sound/midi-win/midi.cpp @@ -43,24 +43,20 @@ void CMidi::MidiStart() /* Get the number of MIDI In devices in this computer */ iMidiDevs = midiInGetNumDevs(); - if ( iMidiDevs > MAX_MIDI_DEVS ) - { - iMidiDevs = MAX_MIDI_DEVS; - } qInfo() << qUtf8Printable ( QString ( "- MIDI devices found: %1" ).arg ( iMidiDevs ) ); // open all connected MIDI devices and set the callback function to handle incoming messages for ( int i = 0; i < iMidiDevs; i++ ) { - MIDIINCAPS mic; + HMIDIIN hMidiIn; // windows handle + MIDIINCAPS mic; // device name and capabilities MMRESULT result = midiInGetDevCaps ( i, &mic, sizeof ( MIDIINCAPS ) ); if ( result != MMSYSERR_NOERROR ) { qWarning() << qUtf8Printable ( QString ( "! Failed to identify MIDI input device %1. Error code: %2" ).arg ( i ).arg ( result ) ); - hMidiIn[i] = 0; continue; // try next device, if any } @@ -69,43 +65,40 @@ void CMidi::MidiStart() if ( !selMIDIDevice.isEmpty() && selMIDIDevice != midiDev ) { qInfo() << qUtf8Printable ( QString ( " %1: %2 (ignored)" ).arg ( i ).arg ( midiDev ) ); - hMidiIn[i] = 0; continue; // try next device, if any } qInfo() << qUtf8Printable ( QString ( " %1: %2" ).arg ( i ).arg ( midiDev ) ); - result = midiInOpen ( &hMidiIn[i], i, (DWORD_PTR) MidiCallback, 0, CALLBACK_FUNCTION ); + result = midiInOpen ( &hMidiIn, i, (DWORD_PTR) MidiCallback, 0, CALLBACK_FUNCTION ); if ( result != MMSYSERR_NOERROR ) { qWarning() << qUtf8Printable ( QString ( "! Failed to open MIDI input device %1. Error code: %2" ).arg ( i ).arg ( result ) ); - hMidiIn[i] = 0; continue; // try next device, if any } - result = midiInStart ( hMidiIn[i] ); + result = midiInStart ( hMidiIn ); if ( result != MMSYSERR_NOERROR ) { qWarning() << qUtf8Printable ( QString ( "! Failed to start MIDI input device %1. Error code: %2" ).arg ( i ).arg ( result ) ); - midiInClose ( hMidiIn[i] ); - hMidiIn[i] = 0; + midiInClose ( hMidiIn ); continue; // try next device, if any } + + // success, add it to list of open handles + vecMidiInHandles.append ( hMidiIn ); } } void CMidi::MidiStop() { // stop MIDI if running - for ( int i = 0; i < iMidiDevs; i++ ) + for ( int i = 0; i < vecMidiInHandles.size(); i++ ) { - if ( hMidiIn[i] != 0 ) - { - midiInStop ( hMidiIn[i] ); - midiInClose ( hMidiIn[i] ); - } + midiInStop ( vecMidiInHandles.at ( i ) ); + midiInClose ( vecMidiInHandles.at ( i ) ); } } diff --git a/src/sound/midi-win/midi.h b/src/sound/midi-win/midi.h index 5c9ba65301..0bdd56f4b8 100644 --- a/src/sound/midi-win/midi.h +++ b/src/sound/midi-win/midi.h @@ -27,9 +27,6 @@ #include "../../util.h" #include "../../global.h" -// Max number of Windows native MIDI interfaces to support -#define MAX_MIDI_DEVS 8 - /* Classes ********************************************************************/ class CMidi { @@ -42,8 +39,8 @@ class CMidi void MidiStop(); protected: - int iMidiDevs; - HMIDIIN hMidiIn[MAX_MIDI_DEVS]; // windows handles + int iMidiDevs; + QVector vecMidiInHandles; // windows handles static void CALLBACK MidiCallback ( HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2 ); };