Detecting a physical midi controller's input & output

Greetings,

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.

Thanks,
Sam

can you say more details

The only way to do this is via the name - you need to understand how a particular MIDI device names its inputs and 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:

  1. Look for an exact name match between input and output ports.
  2. 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)

OK, point taken that input/output port naming is unpredictable and arbitrary, and that eventually attempts to automatically match them will fail.

Can you give an example of the Korg hardware you’re talking about? Naming a MIDI port “SOUND” is a terrible idea!

minilogue XD and Prologue both use that naming convention. i’m guessing the monologue and minilogue do aswell…

Thank you all for your input.

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 :stuck_out_tongue:

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:

The FindSynthOnMidiNetwork.cpp class inherits from a thread and does the following in the run() method:

  1. Open’s all MidiIns and hooks a callback to them.
  2. Loops over the MidiOutputs and…
  3. Send’s a device Id request sysex to the MidiOut device one by one
  4. Listens for the callback which has a MidiInput parameter
  5. 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.

Thanks!

1 Like

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.

Just from reading your description of the MidiKraft-base FindSynthOnMidiNetwork approach, I’d say it sounds clever, but this is going to be the catch:

…because not all MIDI devices respond to Identity Request messages.

1 Like

I can live with this, as I’m targeting a limited list of models and I believe all of them respond to an Id request sysex.

But I’m still finding it hard to apply this logic on a simple example.