Raspberry Pi USB MIDI Host

Posted on Apr 23, 2024

This guide exists to help you create a USB MIDI router out of a Raspberry Pi so that you can connect USB MIDI devices together easily. If you add a battery to your Raspberry Pi, like in my Otto project, you can connect these devices anywhere!

The information from this guide largely comes from Fabio Barbon’s guide at Neumatic Studio. I also relied on my previous guide on how to set up an Orange Pi USB MIDI router.

I provide a preconfigured 32 bit Raspberry Pi OS Legacy (Debian Bullseye) image that can be flashed to an 8GB or larger microSD card. This image is created using the step by step instructions below it.

This should work on any Raspberry Pi capable of running 32 bit Raspberry Pi OS.

It has been tested in a Raspberry Pi Zero 2W as well as a Raspberry Pi 2B+

Usage

Insert the flashed microSD card in your Raspberry Pi and turn it on. Give the device ~10-30 seconds to boot (depend on speed of Pi and microSD card) and then plug in your USB MIDI devices. They should be automatically connected to each other and transmitting/receiving data in both directions.

When done, simply remove the power to the Pi. The image is set to read only and there is no risk to corrupting the card as there will be no writes to interrupt.

Rinse and repeat as needed.

Preconfigured image

The latest preconfigured image can be found at my Otto project on github, at this link.

To flash the image, download the latest release zip file and extract the .img file inside. Then write that image to an 8GB or larger microSD card. You can use Rufus or Win32 Disk Imager for Windows, or dd on linux/mac.

Step by step instructions

  1. Download the Raspberry Pi Imager and set up the microSD card. Choose the Legacy 32 Lite image (no desktop is needed). You will need to enter in a username/password, set up wifi, and enable SSH access (on the second tab) in order to finish the setup. Setting the hostname can also be helpful.
  2. Insert the microSD in your Raspberry Pi and boot it up. It will take some time to connect to your WIFI, you can watch your router’s DHCP logs to see what IP it pulls or try to ping the hostname you set until you see a response.
  3. Connect to the Pi using SSH
  4. Perform sudo apt update and sudo apt upgrade to update all current packages
  5. Perform sudo apt install git ruby to install required packages
  6. Create and edit the connection script sudo nano /usr/local/bin/connectall.rb Paste the following code as the contents:
#!/usr/bin/ruby
#

t = `aconnect -i -l`
ports = []
t.lines.each do |l|
  /client (\d*)\:/=~l
  port = $1
  # we skip empty lines and the "Through" port
  unless $1.nil? || $1 == '0' || /Through/=~l
    ports << port
    #names << name
  end  
end

ports.each do |p1|
  ports.each do |p2|
    unless p1 == p2 # probably not a good idea to connect a port to itself
      system  "aconnect #{p1}:0 #{p2}:0"
    end
  end
end
  1. Save and exit the file (Ctrl+X, then Y, then enter)
  2. Set permissions on the file sudo chmod +x /usr/local/bin/connectall.rb
  3. Connect one or more USB devices to otto and issue connectall.rb to perform a test connection Check the results with aconnect -l, it should show connected devices
  4. Create a udev rule to run the script when USB devices are connected sudo nano /etc/udev/rules.d/33-midiusb.rules Paste the following as contents:
ACTION=="add|remove", SUBSYSTEM=="usb", DRIVER=="usb", RUN+="/usr/local/bin/connectall.rb"
  1. Restart the udev service to enable sudo udevadm control --reload and sudo service udev restart
  2. Create a systemd service to configure any attached devices at boot sudo nano /lib/systemd/system/midi.service Paste the following as contents:
[Unit]
Description=Initial USB MIDI connect

[Service]
ExecStart=/usr/local/bin/connectall.rb

[Install]
WantedBy=multi-user.target
  1. Reload the systemctl daemon sudo systemctl daemon-reload
  2. Enable sudo systemctl enable midi.service the service.
  3. Reboot the device sudo reboot

At this point USB MIDI routing should be working as expected, you can plug and unplug devices as desired and they should be automatically connected to each other.

In order to prevent microSD card corruption you will want to set it up as read-only.

  1. Reconnect to the device via SSH
  2. Clone the rpi-readonly git git clone https://gitlab.com/larsfp/rpi-readonly
  3. Enter the directory cd rpi-readonly and run the setup sudo ./setup.sh

When setup is complete your install will be set to read only mode. You are now ready to make music!

If you want to make changes in the future you can turn read/write mode on with the command rw

When done making changes you can turn read-only mode back on using the command ro


rm