I've been exploring the MIDI APIs available on macOS and Windows recently (Core MIDI and Windows Multimedia, respectively) and noticed:
On MacOS, after querying for the number of available sources, each source can be uniquely identified by checking the value of the property kMIDIPropertyUniqueID
.
On Win32, I can't find a clear alternative - it seems that an input device's "ID" is no more than the index into the list of connected devices, which could change at any time as new devices are connected and disconnected. Information in the capabilities structure one can request from a device is also not particularly distinctive, as using two of the same MIDI keyboard at once would result in two identical capability sets right down to the product name.
Is there some way to uniquely identify these devices in WinMM?
Short answer is no.
ID of a device in WinMM is indeed just an index in the devices list. So if you unplug one device and plug another one with the same properties (name, for example), you won't be able to determine whether the device you work with is the old one or new. Name, of course, can be the same, so that's not an option.
So from programming side there is no way to distinguish two MIDI devices with the same properties in Windows. In an app you can provide a button like "Refresh devices" or "Test device" to be up to date with current devices and to be able to determine which object in GUI corresponds to a MIDI device to.
As for macOS, kMIDIPropertyUniqueID
is an option, BUT there are nuances. From the documentation on the constant:
The system assigns unique IDs to all objects. You may set this property on virtual endpoints; however, doing so may fail if the ID isn’t unique.
So in some cases you can get not unique value. In my opinion, the most reliable way to identify a device in macOS is to use its reference. For example, when you get a source device with MIDIGetSource
you get MIDIEndpointRef
as the result. In fact it's a UInt32
number which is truly unique in the scope of the system.
So I in my .NET library DryWetMIDI rely on MIDIEndpointRef
to distinguish MIDI devices in macOS. Also it's possible to provide MIDI devices watching (adding/removing). But for Windows it's not possible unfortunately.