Companion Generic MIDI plugin doesn't reconnect

Hi,

as outlined here: USB Midi doesn't reconnect on Linux / Pipewire · Issue #19 · bitfocus/companion-module-generic-midi · GitHub

The current version of the generic MIDI plugin does not reconnect to MIDI devices when the connection gets lost. It completely relies on the RTMidi stack to keep the logical MIDI device online. If the logical MIDI device closes the connection is only re-established when the “Save” button in the “Connections” tab of Companion is pressed or Companion is restarted, as that is the only times that the actual MIDI port is opened in software (in the constructor of the MIDI plugin class).

This can be verified in multiples ways:
On Arch Linux with Pipewire, where the logical device is closed when the hardware device is disconnected:

  • Start Companion
  • Set up MIDI device
  • Disconnect MIDI device
  • Reconnect MIDI device
  • See theres no more data coming in
  • Press “Save” or restart Companion
  • See the data is coming in again (without re-selecting the device)

On MacOS the logical device is kept but can also be removed:

  • Start Companion
  • Set up MIDI device
  • Disconnect MIDI device
  • Remove MIDI device from “Audio-MIDI Setup” in MacOS
  • Reconnect MIDI device
  • See theres no more data coming in
  • Press “Save” or restart Companion
  • See the data is coming in again (without re-selecting the device)

In my opinion the plugin should check on the state of the MIDI connection repeatedly and re-establish the connection when the device is available again. Restarting companion or going to the settings to reconnect the MIDI device is not a great solution.

Thanks for the attention.

Thank you for reporting this issue here, Normen, and for all your work in verifying the problem. It does seem like it might be an RTMidi issue, but since it can be reproduced on MacOS, I will do some testing to see where/why it fails and see where the best place to make a fix will be. Repeated checking feels like a bit of a band-aid, but if that’s the only way, then I will see what that takes.

As per the discussion about RTMidi theres apparently no consistent cross-platform way to detect disconnect events and thus the library doesn’t really support that. It does however manage reconnect to the hardware as long as the logical device isn’t removed so I take that with a grain of salt. I suppose its just not properly implemented.

From my own experience with RTMidi (albeit in golang) I can verify that its very dependent on how the OS handles the connection. For example on MacOS its near impossible to detect a disconnected MIDI device through the API, save for MIDI active sensing or actually removing it from the Audio-MIDI Panel in MacOS, which is when the API actually reports “not opened” (not before on hardware disconnect).