thestk / rtmidi

A set of C++ classes that provide a common API for realtime MIDI input/output across Linux (ALSA & JACK), Macintosh OS X (CoreMIDI) and Windows (Multimedia)
Other
979 stars 269 forks source link

Mapping to inconsistent device names upon each boot #281

Open gary-desroches opened 2 years ago

gary-desroches commented 2 years ago

Summary: Is there something in the API I'm missing, or is it not possible to tell the difference between 2 devices of the same model?

I have an application I develop/debug on Windows 10 and run on Ubuntu 21 on a Raspberry Pi 4 Model B. The Pi is meant to be a headless appliance that runs the program at startup. The program uses a config file to map commands received from an input device, and can either pass it through or send a different message to 1 or more output devices. I use device names to match the devices. If the device names are correct, everything works.

My problem is that on both Windows and Linux, each time the machine is booted, there is a chance the device names will be different even if the USB devices were connected to the same ports at boot. It seems more likely to occur on Ubuntu 21 than Windows 10. If I could guarantee that every device was a different model USB to MIDI adapter, then I could ignore the numbers at the end, but that is not the case.

Here is an example when running on Ubuntu 21.

    Reading config file...
    Using Linux ALSA as the MIDI API.
    Input port count: 5
    Port 0: Midi Through:Midi Through Port-0 14:0
    Port 1: USB Uno MIDI Interface:USB Uno MIDI Interface MIDI 1 20:0
    Port 2: MIDIPLUS TBOX 2x2:MIDIPLUS TBOX 2x2 Midi Out 1 24:0
    Port 3: MIDIPLUS TBOX 2x2:MIDIPLUS TBOX 2x2 Midi Out 2 24:1
    Port 4: USB Uno MIDI Interface:USB Uno MIDI Interface MIDI 1 28:0
    Output port count: 5
    Port 0: Midi Through:Midi Through Port-0 14:0
    Port 1: USB Uno MIDI Interface:USB Uno MIDI Interface MIDI 1 20:0
    Port 2: MIDIPLUS TBOX 2x2:MIDIPLUS TBOX 2x2 Midi Out 1 24:0
    Port 3: MIDIPLUS TBOX 2x2:MIDIPLUS TBOX 2x2 Midi Out 2 24:1
    Port 4: USB Uno MIDI Interface:USB Uno MIDI Interface MIDI 1 28:0

The last time this machine booted, the numbers at the end were 14:0, 16:0, 20:0, 20:1, and 24:0.

insolace commented 2 years ago

The USB MIDI spec allows devices to include a descriptor string for their serial number, but it is optional and therefore device specific. In my experience, most devices do not report a serial, as it is extra work for the manufacturer to serialize the devices when they are programmed. However, if the MIDI device reports a serial number then there are methods to read that data, however I do not believe this is implemented in RtMidi. The OS may use the serial to keep the numbering consistent, but I do not believe this is consistently implemented (MacOS reports identical port names, but applications like Ableton Live number multiple devices).

For devices that do not report serial numbers, Windows tends to go by the order that the devices enumerate. From your log, I would assume that ALSA is doing something similar. I would be very surprised if the M-Audio reports a serial #.

gary-desroches commented 2 years ago

Thanks. I modified it to strip off the suffix and bought another MIDI USB cable. If I find the time in the future, I'll take a look at the serial number stuff. It would be a nice feature for devices that support it.