Sunday, March 30, 2014

Android Midi Apps development

I am working on a new project which requires developing an android application to access a USB midi interface to receive and send MIDI messages. Being almost new to Android as I am, I would expose here the different steps I followed to get a basic demo to work.

Installing Android SDK

I installed the ADT bundle from here: https://developer.android.com/sdk/index.html?hl=sk
Process was straight forward: download, extract and run.

I also tried the new Android Studio, but had some issues migrating existing projects, so I decided to give that up.

If it is your first Android App, I recommend going through the building your first app.

Building the Android MIDI USB package sample Application

The package I used for using USB MIDI devices is this one: https://github.com/kshoji/USB-MIDI-Driver

The package comes with a test application that logs the MIDI activity and can do passthrough of the MIDI traffic (send the incoming MIDI to the output MIDI device).

I built the application and installed it on my android device. Using a USB on the go cable a MIDI to USB adapter and a MIDI keyboard I had it working. For future development of my own app, that process seemed slow and messy, so I investigated other alternatives. I first tried using the AVD, the android emulator that comes with the SDK, but somehow that was running very slow on my machine and I found out I can not access the laptop's USB devices from my emulator, so it was not a good choice. I decided to install an android-x86 virtual machine.

Installing an Android-x86 Virtual Machine 

That got me an Android-x86 Virtual Machine in VirtualBox. Next step was configuring the network and configuring the USB access on my Virtual Machine.

Network Configuration: I used the "bridged adapter" option, which makes the virtual machine behave as a new device connected to the local network. Once the network configuration is as desired, the next step was to set up adb to be able to access the virtual machine over the network. To do that, the first step is to find the ip address of the android virtual machine. Doing "ALT+F1" you can get into the linux a console terminal and once there doing "netcfg" you can find the IP address. From the host system you can do "adb connect ip_address:5555" and then you will be able to run adb commands just as you would do with a device connected to the computer, including running you applications directly from Eclipse.

USB passthrough from Host to Guest machine: This is what I did to get access to the USB MIDI device from my android virtual machine:
1. Install the Virtual Box extension pack
2. In the Machine USB settings select "Enable USB Controller" and "Enable USB 2.0 (EHCI) Controller"
3. In the host machine run "usermod bill -a -G vboxusers", replacing bill with your user name, and log out and log in again for the change to take effect.

And that is all, I run again my MIDI sample application, now on my virtual machine, and connected my USB MIDI cable and my MIDI keyboard and had the application working correctly (slow, but working).