MIDI device onDisconnect?

Is there a way to register a callback for MIDI device disconnection events in the API?

My module (VCV Library - PitchGrid Microtonal Exquis Interface) is built for one specific MIDI controller to which it directly and automatically connects by looping over drivers and devices, identifying the right one by name. Every 2.5 seconds I am doing that loop to see if the device is still available, to detect a disconnect. However, I noticed that the loop is prohibitively expensive, in particular when using VCV Rack as a VST plugin. I could avoid the loop if I could listen to an onDisconnect event, but I could not find any info in the docs how I could achieve this. Can someone help out?

Rack (and the underlying support libraries rtmidi/rtaudio) do not expose any such event for device changes (adds are as important as removals). This results in poor behavior when devices are plugged and unplugged.

This problem has certainly created a lot of headaches for my MIDI-centered plugin.

All of the Rack OS platforms have a way to watch and be notified of device changes, so this is certainly technically possible, but Rack has not implemented such a thing. It generally relies on 3rd party cross-platform library support for graphics, input, and devices.

Worth opening a support ticket for.

1 Like

Not the answer I hoped for but very helpful. Thank you! Will contact support.

@peterjungx - I’m no help here but thanks for the example code in your plugin on digging through the midi device list and setting that. That’s something I’ve been meaning to add to my stuff, not just for midi but for audio devices as well. Right now I have the user manually setting midi and two audio devices. That’s quite annoying and not the UX one would want. I’d be excited to solve that but I was curious on a couple points. (aside: somewhat related thread where @pachde had some useful comments that I’m yet to act on: Save/restore of module with multiple audio interfaces on MacOS )

  • Platform dependency: the strings that get returned can be/are platform dependent (per related thread). MIDI seems more stable than Audio interfaces, so maybe you’re not impacted so much.
  • Do you maintain a way for the user to manually set the device if the auto-connection fails or should be overridden?
  • If a second device is connected, is that supported? Or if a second PitchGrid module is instantiated, how is that handled?

I don’t have good solutions for much of any of this. Things work now, seems like it could be a lot smoother though. And the disconnect issue, your original point, is a thing. When I disconnect the USB Audio devices on Linux the CPU goes through the roof. While it’s annoying it’s not a huge issue as that’s usually right before I exit VCV Rack.