Is there a typical way to detect if a MidiInput and a MidiOutput belong to the same physical device?
I am trying to create a class that represents a physical midi controller that has it’s input and output connected and show it as a single entry in my device list instead of using 2 lists one for inputs and one for outputs.
Right, you can observe which input and output ports show up when you plug in a particular MIDI device, and set things up as needed for that specific case. But still I am curious about how others approach this in code, for the general case problem asked by the OP:
Given the MidiDeviceInfo.name of an input device, I have approached this by iterating through MidiOutput::getAvailableDevices and trying:
Look for an exact name match between input and output ports.
See if the input port name ends with “In”, if so try removing that substring and replacing it with “Out”, and then look for a matching output port name.
This has covered the cases for the MIDI devices I have here on hand to test with. What other Midi input/Midi output matching schemes have people tried?
Korg names their input ports (on their more recent hw) KBD/KNOB and their output ports SOUND. Ableton also do some funny stuff with Push2 port naming - there’s some other ones but I can’t think of them at the mo.
The main point here is that unless you can go through every piece of hardware and test, you can’t reliably do this and need to allow people to select MIDI in and MIDI out ports (I guess you could try the patterns and only enable MIDI output selector if your code fails - but you’re still going to have to allow people to select this manually)
Indeed, checking the device names is the easiest way to go but not very reliable, I have a Korg Triton Ex and a Korg Pa1000 and they both have the suffixes “KEYBOARD” for input port and “SOUND” for the output.
Meanwhile, the range of Keyboards I’m looking to target with my app is somewhat limited, I can probably hard code all the names in some helper class, but then, what if I have 2 keyboards of the same model hooked at the same time? That would create some confusion! Unless the system magically distinguishes them from each other by adding an enumeration or something like that, but I have no means to test this unless I buy another Pa1000 and wait for my divorce papers
I have been looking around and found a neat trick in this open source project:
The problem is that I am new to c++ and this code goes way over my head but what I could understand is the following:
Send’s a device Id request sysex to the MidiOut device one by one
Listens for the callback which has a MidiInput parameter
the replying MidiInput gets associated with the tested MidiOutput
I am still trying to decipher the code though as the project is a bit complicated with a lot if moving parts. If one of you gurus can help simplify this to fit into my little brain with a simpler example I would be greatly thankful. Also, I am hoping I can refresh my device list on the fly if a new device gets hooked up or removed.
Have a look here at this thread, where I just added some more info. In short, you can distinguish between 2 devices of the same model with MidiDeviceInfo::identifier, but not in any persistent way:
But that said, I’d point out that on macOS anyways, you can rename your MIDI devices in the Audio MIDI Setup app, and give different names to different devices of the same model, and that unique naming will be persistent.