MIDI over USB

Hmmm if it works with the JUCE demo then have a look at how the AudioDeviceManager opens the midi device. There isn’t a whole lot of fancy code there.

yeah, just looking at that now - doesn’t seem to be anything special as you say…

Ok, so I think I have a handle on this and why my stuff is not working and the demo does.
When building a standalone, this code gets executed at startup for OSX and Android:

#if JUCE_IOS || JUCE_ANDROID
void timerCallback() override
{
auto newMidiDevices = MidiInput::getDevices();


for (auto& newDevice : newMidiDevices)
if (! lastMidiDevices.contains (newDevice))
deviceManager.setMidiInputEnabled (newDevice, true);
}
#endif

It opens all the MIDI devices it can find. Any calls to MidiInput::OpenDevice() on Android will then fail - seems to be ok on iOS - maybe this is because iOS MIDI is mulit-ported like OSX and Android isn’t like Windows? Anyway, I commented out this code and my code now works as expected. So it seems on Android that we have to go through the DeviceManager created at startup. As the JuceDemo is not running the standalone code, it does not see this issue.

I’m not sure how to get hold of the DeviceManager class that lives inside the standalone filter window from my plugin inside my code to use it - can you tell me how?

I guess ideally, if we want to manage MIDI devices ourselves, without using the DeviceManager class, it would be nice to be able to tell the Android build not to go and open all the MIDI input devices at startup so that the rest the same behaviour is seen across all platforms. Or maybe there’s a call on DeviceManager to tell it to close everything - haven’t checked yet

Yes that’s spot on. I forgot about that. We had a similar issue with ROLI’s NOISE app on Android: you can only open midi devices once.

So the standalone target is a simple standalone app to get you going quickly. It will simply connect all midi devices to your AudioProcessor - so without even opening any MidiDevices in your own code, you should be getting the midi into your AudioProcessor via the processBlock callback. You don’t need to use any AudioDeviceManager or anything. Check-out the JuceDemoPlugin or the AUv3Synth samples.

If you need something more custom [1] then you may need to roll your own custom app for your plugin. See option number 2 in this post for more information:

[1] Let us know why the midi handling of the default standalone target app does not work for you. Maybe we can add it to JUCE.

Ok, I see. I’ll have a read-through that post about how to roll my own Standalone class.

"Let us know why the midi handling of the default standalone target app does not work for you. Maybe we can add it to JUCE"
The app only works with certain devices and as such performs a handshake for before allowing MIDI processing - this involves checking the that input and output for a device can be opened and doing a sysex device enquiry, so it’s not general MIDI handling.

Input pretty much sorted now, thx for all the help! Not getting any output now, but this one is the same in the demo app. Tried on a couple of different synths and the same. Running demo on iPad works as expected.

I’ve connected Bluetooth MIDI to a Yamaha MD-BT01 and see the same behaviour - input ok,output nothing. Tested Bluetooth MIDI on iPad and both in/out work.

I’ve used:


to make sure the USB cables are working properly.

Be sure to restart your Android device from time to time. The MIDI backend of Android is a bit buggy. See this thread:

and this bugreport:

https://issuetracker.google.com/issues/62150024

1 Like

hi, just restarted and still the same behaviour. The MIDI keyboard app works but no output from the JUCE app.

“Can you also try this with the MidiTest example app: JUCE/examples/MidiTest?”

Just tried this and it works - seeing what differences there are…

So in the Juce Demo app I was selecting MIDI output on the settings page, then selecting it again on the MIDI i/o page which silently causes the 2nd open to fail on Android and therefore no output. Only opening the output on the MIDI i/o page works ok. Guess I must have something similar in my code.

Yeah, just tried the BLOCKS examples in the example folder and all seems to work fine.

Heh. My app is working fine this morning after the suggested reboot. Guess things are still a bit flaky as you say!

Hi Fabian, did you have a look at this page?

https://source.android.com/devices/audio/midi

Yes I did but this is only for platform implementers. In any case, it’s only supported after Android M. I quote from the site:

Beginning with the Android 6.0 (Marshmallow) release, device makers can enable optional MIDI support in the platform.

… and JUCE fully supports MIDI for Android M and higher.

ok, thx for checking. Still a bit confused as I have a number of MIDI apps that work on 19 so it must be possible. I guess this is something we’ll have to explore and implement ourselves

Hi Fabian, just getting around to looking at this (rather than the commenting out of code). So, in order to change the behaviour of timerCallback() in StandalonePluginHolder, which is what I need to not automatically open all the MIDI ins on Android, it seems as though I need to do the following:

  1. Copy juce_StandaloneApp to my own class.
  2. Derive a new class from StandalonePluginHolder to be able to override timerCallback().
  3. Derive a new class from StandaloneFilterWindow to be able to create my own version of StandalonePluginHolder
  4. create new version of the StandaloneApp from juce_CreateApplication() after setting the JUCE_USE_CUSTOM_PLUGIN_STANDALONE_APP=1 flag.

Does that sound about right? thx

Yes, sounds about right. Are you doing all of this to only avoid JUCE opening the ports? If yes, I’m sure we can add a flag to the StandalonePluginHolder to change this behaviour.

thx for confirming, just wanted to make sure I wasn’t going off down the wrong path.

yes, this is just to avoid opening the ports, so a flag would be great if it’s no trouble to add.

OK, I’ve added a preprocessor flag JUCE_DONT_AUTO_OPEN_MIDI_DEVICES_ON_MOBILE to avoid the standalone plug-in opening the midi devices. This is on develop with commit 0b2822f.

brill - thanks!

1 Like