1. Play us a tune
Connect and ssh
After following Andrew's quick instructions to get the Pi Zero chatting over USB, I could use ssh to shell into
email@example.com on my Mac.
This was great but the Pi Zero couldn't talk to the internet so downloading new software wasn't going to work. The fix was easy, just go to System Preferences → Sharing → Internet Sharing and make sure that the Pi Zero is ticked in the "To computers using:" list. The Pi Zero appears as "RNDIS/Ethernet Gadget".
I rebooted the Pi Zero and when I reconnected to it over ssh it could access the internet. Magic!
Now that ethernet over USB was working I wanted to try MIDI. I don't really know much about it, apart from that it's a standard for enabling music devices to communicate.
The USB Gagdet ID for MIDI is
g_midi so, I changed the
g_ether part of the Pi Zero SD card's
g_midi and restarted.
I dug up some WebMIDI example pages on the internet, as Chrome now allows you to access USB MIDI devices plugged into your computer.
This Web audio MIDI Synth demo listed the "MIDI gadget" in the UI which was exciting!
MIDI means no ethernet
Switching to the MIDI gadget means that you can no longer get access to the Pi Zero over the USB ethernet. So everything that follows is the result of making changes to the Pi Zero in ethernet mode and adding the changes to
/etc/rc.local so they'd run on startup and then switching back to MIDI mode to see if they worked.
This is very time consuming and the next post has some ways to solve that. Which are also time consuming to set-up but ultimately enable more iterative development.
Now the Pi Zero was being recognised by the Mac as a MIDI input device, it was time figure out how to get the Pi Zero to actually output MIDI.
The gadget testing document is actually a treasure trove 🎁 of useful information for testing the various USB gadget drivers. There's a section called "Testing the MIDI function" which has various command examples. The
aplaymidi command, part of the
alsa-utils package is the key.
aplaymidi -l lists MIDI devices.
On the Pi Zero this gave me:
pi@raspberrypi:~ $ aplaymidi -l Port Client name Port name 14:0 Midi Through Midi Through Port-0 16:0 f_midi f_midi
You can use the
16:0 to tell
aplaymidi to play a MIDI file.
$ aplaymidi -p 16:0 filename.mid
So, off to the internet to find a MIDI file to play!
With the MIDI file found I wrote a quick shell script to play it on startup, since in MIDI gadget mode I can't be ssh'd in too.
The script is very simple:
while true; do aplaymidi --list echo "Playing" aplaymidi --port 16:0 /boot/test.mid echo "Finished" done
I added it to
date >> /home/pi/midi.log /home/pi/midi.sh >> /home/pi/midi.log &
I restarted the Pi Zero in MIDI mode, opening the WebMIDI synth demo and… no music 🚫🎶🚫.
Which was really frustrating. But as I don't really know what kind of music a MIDI file contains I wasn't so sure the synth would be able to play it.
Apple GarageBand however sprung into life when I opened it and created an empty project. The MIDI file was coming through the USB, into Garage Band and was played by (I assume) Siri on a tiny piano under the keyboard - Success!⚠️ '/projects/images/pi-zero-gadget/1-garage-band.gif' not found
Back to Web MIDI
I discovered later on that if you refresh the Web MIDI synth after the Pi Zero starts sending the MIDI messages then you get some aggressive distorted noise being played. Yes!
But not being able to see which MIDI messages (if any) are being sent by the Pi Zero is quite annoying.
I found this great free app called MIDI Monitor that shows you all the messages being sent and received.
I think this is going to come in handy when trying to get the Pi Zero to send custom messages rather than just playing an existing MIDI file.