Skip to content

Commit

Permalink
Add support for multiple devices
Browse files Browse the repository at this point in the history
  • Loading branch information
softins committed Dec 1, 2024
1 parent f058359 commit 09d7397
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
49 changes: 32 additions & 17 deletions src/sound/asio/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,34 +1239,49 @@ int64_t CSound::Flip64Bits ( const int64_t iIn )
// Windows Native MIDI support
void CSound::MidiStart()
{
midiPort = 0; // might want to make this settable, Windows allocates device numbers in order

MMRESULT result = midiInOpen ( &hMidiIn, midiPort, (DWORD_PTR) MidiCallback, 0, CALLBACK_FUNCTION );

if ( result != MMSYSERR_NOERROR )
/* Get the number of MIDI In devices in this computer */
iMidiDevs = midiInGetNumDevs();
if ( iMidiDevs > MAX_MIDI_DEVS )
{
qWarning() << "! Failed to open MIDI input device. Error code: " << result;
hMidiIn = 0;
return;
iMidiDevs = MAX_MIDI_DEVS;
}

result = midiInStart ( hMidiIn );
if ( result != MMSYSERR_NOERROR )
// printf("Found %d MIDI device%s\n", iMidiDevs, iMidiDevs == 1 ? "" : "s");

// open all connected MIDI devices and set the callback function to handle incoming messages
for ( int i = 0; i < iMidiDevs; i++ )
{
qWarning() << "! Failed to start MIDI input. Error code: " << result;
midiInClose ( hMidiIn );
hMidiIn = 0;
return;
MMRESULT result = midiInOpen ( &hMidiIn[i], i, (DWORD_PTR) MidiCallback, 0, CALLBACK_FUNCTION );

if ( result != MMSYSERR_NOERROR )
{
qWarning() << "! Failed to open MIDI input device. Error code: " << result;
hMidiIn[i] = 0;
return;
}

result = midiInStart ( hMidiIn[i] );

if ( result != MMSYSERR_NOERROR )
{
qWarning() << "! Failed to start MIDI input. Error code: " << result;
midiInClose ( hMidiIn[i] );
hMidiIn[i] = 0;
return;
}
}
}

void CSound::MidiStop()
{
// stop MIDI if running
if ( hMidiIn != 0 )
for ( int i = 0; i < iMidiDevs; i++ )
{
midiInStop ( hMidiIn );
midiInClose ( hMidiIn );
if ( hMidiIn[i] != 0 )
{
midiInStop ( hMidiIn[i] );
midiInClose ( hMidiIn[i] );
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/sound/asio/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ class CSound : public CSoundBase
char* cDriverNames[MAX_NUMBER_SOUND_CARDS];

// Windows native MIDI
HMIDIIN hMidiIn; // windows handle
UINT midiPort;
#define MAX_MIDI_DEVS 4

int iMidiDevs;
HMIDIIN hMidiIn[MAX_MIDI_DEVS]; // windows handles

void MidiStart();
void MidiStop();
Expand Down

0 comments on commit 09d7397

Please sign in to comment.