Raspberry Pi as a "no hassle" Midi host

Post fully complete "how to" guides and tutorials here. This is a great place to get feedback on stuff you might put in the wiki.

Moderators: khz, MattKingUSA

Post Reply
User avatar
Established Member
Posts: 705
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland

Raspberry Pi as a "no hassle" Midi host

Post by Linuxmusician01 »

Ever wanted to connect a USB Midi keyboard to a DIN Midi synth? Well, you need a Midi host for that. You can use your laptop and even an Android phone (see this video on Youtube). Or you can cough up 45 Euro for the Midi host from Hobbytronics (needs battery power or a poer adapter).

I found a website that describes how to turn a Raspbery Pi that runs your average basic Raspbian into a hassle free Midi host! You do not have to connect the Pi to a monitor and keyboard and type in commands. It wil "auto-sense" that you've inserted Midi cables in the Pi and connect them (via the command aconnect). What you need is a Pi, some battery power (e.g. a 4 X AA battery to USB power thingy from Aliexpress, € 2.25) and a USB to DIN cable (link Aliexpress, € 4). On this website the procedure is described and the author refers to this website from where he got the Ruby script. However, he made a typo in copying the Ruby script, so I'll post my procedure here.

1. sudo apt-get install ruby git

2. Configuring automatic Midi connection. Issue the command sudo nano /usr/local/bin/connectall.rb and copy the following content:

Code: Select all


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

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"
3. Issue the command:

Code: Select all

sudo chmod +x /usr/local/bin/connectall.rb
You can test the auto-connection with command connectall.rb and checking results with aconnect -l.

4. Configure automatic MIDI connection/disconnection on USB device connect/disconnect:

Code: Select all

sudo nano /etc/udev/rules.d/33-midiusb.rules
Copy the following content and save the file:

Code: Select all

 ACTION=="add|remove", SUBSYSTEM=="usb", DRIVER=="usb", RUN+="/usr/local/bin/connectall.rb"

5. Issue the following commands:

Code: Select all

sudo udevadm control --reload
sudo service udev restart
6. Configure MIDI connection at system boot:

Code: Select all

sudo nano /lib/systemd/system/midi.service
Copy the following content and save the file

Code: Select all

Description=Initial USB MIDI connect


7. Issue the following commands:

Code: Select all

sudo systemctl daemon-reload
sudo systemctl enable midi.service
sudo systemctl start midi.service
8. To test automatic connection, you can use the command aconnect -l

P.S. Your Pi will still "behave" like your average basic Pi running Raspbian so no reason not to do this. :)

Post Reply