Problem detecting if MIDI controller has been disconnected

Godot Version

4.3

Question

I’m programming a game that requires a MIDI controller (like a MIDI piano) to be plugged into the computer via USB. I currently have the game programmed to detect if the MIDI controller is connected when the game starts (in the _ready() function), but I want to change it so that the game can dynamically detect when a MIDI controller has been connected or disconnected at any time while the game is running (i.e. in the _process() function). That way the user has the freedom to disconnect their MIDI controller and reconnect it, and the game will still work.

When the game starts, it calls OS.open_midi_inputs() to initialize the MIDI driver, and I can then call OS.get_connected_midi_inputs() to return an array referencing what’s connected, if anything. I noticed that unplugging my MIDI controller doesn’t clear the array returned by OS.get_connected_midi_inputs(), meaning that I don’t have a way to tell when a MIDI controller has actually been disconnected, because no data changes. Plugging the MIDI controller back into the computer while the game is running doesn’t register as a connected device. If I can’t tell when a MIDI controller has been disconnected, I can’t tell when it has been reconnected either.

I’m a bit stuck at this point because I’m not sure how to have Godot successfully check for updates to the MIDI controller beyond what OS.get_connected_midi_inputs() is capable of telling. Does anyone know how I might dynamically update the array provided by OS.get_connected_midi_inputs() when the MIDI controller is disconnected?

From the docs

“By default, Godot does not detect MIDI devices. You need to call OS.open_midi_inputs, first. You can check which devices are detected with OS.get_connected_midi_inputs, and close the connection with OS.close_midi_inputs.”

Not sure if it has some answers.

Also check the Asset Library for MIDI. See if any assets can give you ideas.