MidiOutput::getAvailableDevices() polling

What’s the best way to use MidiOutput::getAvailableDevices() to observe when a MIDI device has been plugged in/out of the computer?

Should I just use a timer and poll that method from a dedicated thread every few seconds, or is there a better way to figure out when a MIDI device becomes available / unavailable?

Does calling MidiOutput::getAvailableDevices() have the danger of freezing the application for a tiny fraction of a second if called from a different thread than where the main application is running and using MIDI devices?

I tried the above idea of polling in a dedicated juce::Thread and I got an assertion failure in juce_CoreMidi_mac.mm findDevice() method:

        // It seems that OSX can be a bit picky about the thread that's first used to
        // search for devices. It's safest to use the message thread for calling this.
        JUCE_ASSERT_MESSAGE_THREAD

Take a look at MidiDeviceListConnection. This will fire a callback whenever the device list changes.

2 Likes

Nice! Thank you, I’ll try that :slight_smile:

I have personally modified JUCE to make

MidiOutput::sendMessageNow (const MidiMessage& message)

return a bool.

Here’s the patch for JUCE 7
https://user.fm/files/v2-b70df06ac6628e0eaad6ace23a3e44a3/0001-Make-sendMessageNow-const-MidiMessage-return-false-i.patch

It’s easy to do and I think it would be cool if JUCE included it.

In this way, when a device disconnects (e.g. loose usb plug, usb port going to sleep, etc), you can detect the error the next time that you send a message to it and you can make it reconnect automatically.

1 Like