The state of MIDI support on Android
The current state of Midi support on Android is depressing. They ripped out javax.sound.midi, so all libraries that depend on this are not working. (Things like JFugue for example).
Fortunately the MediaPlayer does support the playback of midi files. So if you want to have dynamic audio, you need to create your own midi file writing library, or find an existing one. Midi is quite an old beast, so it takes some effort to learn this. Fortunately there are lot of examples and info for the MIDI file format.
Android seems to use Sonivox for MIDI handling in the MediaPlayer. From what I understand it’s a embedded synthesizer that is integrated in Android. One of the things it support is the Jet file format. It’s some kind of wrapper around midi that you can use to “dynamically” playback music. It’s however totally depended on manually generated midi input that you need to edit on your PC to turn into the Jet format. So no pushing midi events to a stream or something like that. The only thing you can do is repeat sections and turn tracks on and off. Bummer.
So, currently on Android there is no way to dynamically generate realtime MIDI. We can’t simple send midi-commands to a synthesizer that then realtime plays it back.
However…. According to the Sonivox EAS (Embedded Audio Synthesis) API Reference (found in the git tree of android), Sonivox does support Midi Streaming! That means that the low-level software stack of android does have the support for it. But it’s not exposed to us poor developers. I also don’t know if it’s possible to use the NDK to just use these C(++) API’s. I don’t have the C knowledge for that, and the NDK docs don’t mention them as supported/stable.
So, there may be a change that you can indeed use the C API’s, but that means you need to learn C/C++. Could be usefull, but well, why can’t we get it in Java in the SDK?
To resolve this I have opened a feature request on the google issue tracker to request for exposing these API’s in Java. If you are in any way interested in this feature, please leave a comment, star it, rate it, whatever it takes to let Google know that we need better MIDI support!
Now back to hacking.
Personally I solved the dynamic midi generation issue as follows: programmatically generate a midi file, write it to the device storage, initiate a mediaplayer with the file and let it play. This is fast enough if you just need to play a dynamic midi sound. I doubt it’s useful for creating user controlled midi stuff like sequencers, but for other cases it’s great.