Skip to content

Commit

Permalink
Remove limit on MIDI device numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
softins committed Dec 4, 2024
1 parent 82182e9 commit b2d502c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
29 changes: 11 additions & 18 deletions src/sound/midi-win/midi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 ) );
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/sound/midi-win/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -42,8 +39,8 @@ class CMidi
void MidiStop();

protected:
int iMidiDevs;
HMIDIIN hMidiIn[MAX_MIDI_DEVS]; // windows handles
int iMidiDevs;
QVector<HMIDIIN> vecMidiInHandles; // windows handles

static void CALLBACK MidiCallback ( HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2 );
};
Expand Down

0 comments on commit b2d502c

Please sign in to comment.