Introduction: PiMiDi: a Raspberry Pi Midi Box, or How I Learned to Stop Worrying and Love MIDI
This is my second instructable on using the Raspberry Pi for digital audio, check out my first instructable for how to set up and control custom audio effects on a Raspberry Pi.
If you like messing with audio effects or making digital music, you might have heard of MIDI. For the rest of you savages, MIDI (Musical Instrument Digital Interface) is a standard method for communicating musical information between two or more electronics. I first heard of MIDI in the early 90s as a kid, thinking “What is this awful music playing on geocities webpages?” Synthesized music has improved a lot since then, with various controllers, synths and MIDI interfaces available to dubstep and warble to your heart’s content.
Enter the PiMiDi: A MIDI interface built on a Raspberry Pi. There are a few commercial USB MIDI interfaces on the market that would work with the Raspberry Pi, but since the Pi has built in UART (a serial hardware platform used by MIDI), I decided to make my own MIDI interface. If you’re interested in learning how to make your own, grab your materials and let’s get started!
Step 1: Materials Needed
You'll need a few things to get started:
- Raspberry Pi (any model should work, I used Model B)
- Hammond 1590BB alumin(i)um enclosure (or similar)
Electronic Components for wiring up MIDI to the Raspberry Pi:
- Prototyping PCB board (2.54mm/.1″ spacing, double-sided is best)
- PCB connector pins (26 for the Raspberry Pi GPIO)
- 7414 Hex Schmitt-Trigger Inverter (or equivalent: SN7414, 74HC14, etc.)
- An Opto-coupler (I used 6n138, others: 6n137,6n139,PC900*)
- A diode (like 1N4148, 1N914, or similar)
- A 470 Ohm and 1k Ohm resistor
- Five 220 Ohm resistors (1 for MIDI input, 2 for each MIDI output)
- One MIDI plug for each input/thru/output
- Wires to connect it all up
- (optional) 26-pin GPIO ribbon connector to connect Pi to the PCB
- (optional) LED with ~330 Ohm resistor
- (optional) a pushbutton switch
Tools you will need to get the job done
- Soldering iron and solder
- A drill with different bit sizes (1/8″, 1/4″, 5/8″)
- Some free MIDI music to keep you motivated
Step 2: From Schematic to PCB
After searching on the web for MIDI to Raspberry Pi schematics, I got a good start from siliconstuff.com and modified it to include a MIDI thru port and an LED with a controllable switch. (*note: if you only have a PC900 opto-coupler, you need to modify its pinout slightly according to the 2nd schematic).
Now, it's time to start placing parts on the PCB board. I've included a layout of two PCB designs, depending on whether you want to use a ribbon cable (like I did) or connect it directly to the Raspberry Pi header. If you don't have a double-sided protoboard, use the left layout and ribbon cable. If you're concerned about fitting it in a 1590BB box, use the layout on the right. Both layouts use the same connections, however the connector pins are reversed.
Since things aren't very clear on the layout, you can hover over components in the photo to see their values and other important details. MIDI connectors don't need to be directly soldered to the board if you have connector pins and 6 female-adapter wires.
Step 3: Drilling Holes in Alumin(i)um
Once you've got your electronics soldered properly onto the PCB, it's a good time to give your eyes a rest and work on the case for your PiMiDi. Start by laying your Raspberry Pi on the bottom lid of your stomp box. It should be placed with the USB jack, audio and RCA video connector closer to the sides. The Pi's SD card should just barely touch the other side of the box without going over the lid's inner edge. Using a sharpie marker, make a small mark on the lid where the two screw holes will hold it in place.
Next remove the Raspberry Pi and drill two 1/8" holes in the lid where you marked it. If you did it right, the holes should line up perfectly and you can mount your Pi onto the lid with two screws (and standoffs if you have them). I made a mistake drilling at an off angle and just drilled a second hole to line up better. Don't worry no one will see it when it's on the floor/table.
Check to see that your Pi fits nicely in the box with the lid almost completely closed, and take note of where the power cable comes into the side of the box. Drill a small hole (about 1/4") to allow your power cable to connect inside the box while it's closed. Drill 3 holes (about 5/8") on the side opposite to the audio connectors for the MIDI jacks. Also drill any holes you want for the audio, video, USB and ethernet ports.
Because I wanted to use both the USB and the ethernet port, I drilled a roughly square shape into one side of the box and then slowly filed it out with a hand file. I would recommend using a dremel tool or CNC machine if you have one, otherwise it'll look as "pretty" as mine does.
Test fit your MIDI jacks and all wiring before connecting the PCB board, allowing for extra room for each plug. Once you're satisfied connect the PCB to the Pi and wire up the MIDI jacks. The backs of the MIDI jacks should have #'s 1-5 on them, but if not the schematic shows the jack from the back side where you connect the PCB wires, not the face where you plug in a MIDI cable.
Step 4: Setting Up the PiMiDi Software
A little MIDI history here: MIDI first started back in the 80s when computers weren't as fast (or as prevalent) as they are now. They standardized the baud rate (how fast it communicates) at 31250 (one byte per 320ms). The 31250 baud is still used today in most MIDI devices, although there are other baud rates used for MIDI serial communication out there.
Back to the present: The Raspberry Pi accepts most baud rates, but (as of this post) it doesn't accept 31250 as a baud rate. So we need to modify the Pi's code by editing the UART clock which controls the serial baud rate on the Pi. Boot up your Pi, and type this in a terminal (minus the $ sign):
$ sudo nano /boot/config.txt
Add these two lines to the bottom of config.txt file:
It should look something like the above photo. Save (Ctrl+0) and exit (Ctrl+x). Next, edit the cmdline.txt and inittab.txt to disable accessing the Pi's shell from UART (leaving it open for MIDI access). Make a copy of each file in case you mess up:
$ sudo cp /boot/cmdline.txt /boot/cmdline.bak
$ sudo cp /etc/inittab /etc/inittab.bak
Edit the cmdline.txt first:
$ sudo nano /boot/cmdline.txt
Find and remove any mention of ttyAMA0,115200 such as “console=ttyAMA0,115200” and “kgdboc=ttyAMA0,115200”. It should still say "console=tty1" after you've edited it. Add bcm2708.uart_clock=3000000 to the first line in case it's not already there, so now it should look like mine:
dwc_otg.lpm_enable=0 bcm2708.uart_clock=3000000 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
Save and exit, then edit the inittab.txt
$ sudo nano /etc/inittab
Now look for the line at the bottom that says “ttyAMA0” and remove it or put a hash in front of it like this:
Save and exit, and then restart your Pi. Now the Pi should be able to receive and send communication at 31250 baud, but we need to install a program to handle the MIDI signals. Time to do a little ditty for TTYMidi, a bit of software that creates a serial to ALSA interface on your Pi. Type the following commands into the Pi terminal to download and install ttymidi:
$ sudo apt-get install libasound2-dev
$ tar -zxvf ttymidi.tar.gz
$ cd ttymidi/
$ sudo make install
Reboot the Pi, and anytime you type ttymidi -s /dev/ttyAMA0 -b 38400 -v & into the terminal; you should see incoming MIDI messages (assuming you have a MIDI controller and cable connected to the MIDI in jack). Congratulations! You now have MIDI input and output on your Raspberry Pi.
Step 5: What to Do With a MIDI Message
Once you've got MIDI messages coming and going on your Pi, you can do a lot with it. For example, play a synth piano using Fluidsynth. Install it by typing:
$ sudo apt-get install fluidsynth
You can now open two terminal windows and in one type:
$ fluidsynth -a alsa -m alsa_seq /usr/share/sounds/sf2/FluidR3_GM.sf2
and in the other type:
$ ttymidi -s /dev/ttyAMA0 -b 38400 -v &
$ aconnect -iol
$ aconnect 128:0 129:0
(128:0 is the ttymidi output, and 129:0 is the Fluidsynth input on my Pi, you might need to modify it to match your listings from the aconnect command). Now by connecting a MIDI keyboard controller and a speaker you have a Pi Synthesizer! To make it load on startup you can create a bash file like I did (or you can just download mine from this instructable).
$ nano midi.sh
Now type these 8 lines into the text editor:
tmux new-session -d -s my_midi -n fluidsynth
tmux new-window -t my_midi:1 -n ttymidi
tmux send-keys -t my_midi:0 "fluidsynth -a alsa -m alsa_seq /usr/share/sounds/sf2/FluidR3_GM.sf2" C-m
#use your specific port settings to connect ttymidi out to Fluidsynth
tmux send-keys -t my_midi:1 "aconnect 128:0 129:0" C-m
tmux select-window -t my_midi:fluidsynth
Save and exit, then type this to allow permissions:
$ sudo chmod 755 midi.sh
Finally, edit your startup file to make the midi bash run on startup:
$ sudo nano /etc/rc.local
In the rc.local file, add the line:
sudo sh /home/pi/midi.sh
Save, exit and reboot. Now you've got a PiMiDi Synth player! I didn't mention how to add a shutdown button with LED, but I'll include my shutdown.py script in case someone is interested in using it. Just copy it over, edit your startup file to include sudo python /home/pi/shutdown.py and restart to see if it worked. The light should blink 3 times and stay on. Push it for less than 3 seconds and it shuts down the Pi, press it longer and it reboots. Easy!
Or, use MIDI messages to control a Musical Melodyian MIDI Robot!
I hope you've enjoyed learning how to send and receive MIDI messages on a Raspberry Pi, as well as creating your own MIDI synth player to jam out on. Feel free to post comments/questions on here, or share videos of your MIDI synth in action! -Munch
We have a be nice policy.
Please be positive and constructive.