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
Linuxmusician01
Established Member
Posts: 930
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 178 times
Been thanked: 39 times

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 power 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

#!/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
  ports << port unless $1.nil? || $1 == '0' || /Through/=~l
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
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

[Unit]
Description=Initial USB MIDI connect

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

[Install]
WantedBy=multi-user.target
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. :)
Last edited by Linuxmusician01 on Mon Jan 10, 2022 10:36 am, edited 1 time in total.
dollar99
Posts: 1
Joined: Sun Jan 09, 2022 11:23 pm
Been thanked: 1 time

Re: Raspberry Pi as a "no hassle" Midi host

Post by dollar99 »

This is awesome. Also, to get it working on a non-Raspberry Pi Debian linux machine you might need to install aconnect with the [
apt-get install alsa-utils] command no brackets.
User avatar
Linuxmusician01
Established Member
Posts: 930
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 178 times
Been thanked: 39 times

Re: Raspberry Pi as a "no hassle" Midi host

Post by Linuxmusician01 »

dollar99 wrote: Sun Jan 09, 2022 11:26 pm This is awesome. Also, to get it working on a non-Raspberry Pi Debian linux machine you might need to install aconnect with the [
apt-get install alsa-utils] command no brackets.
Thanks for the info. It would be nice if USB worked properly on a R. Pi Zero because that's even cheaper and smaller. Unfortunately I couldn't get the Zero to play nice with a (powered) USB hub. I don't know what it is with the Zero but no matter what you do considering power supply it won't work properly with more than one USB device. Also a pity that the Pi's that you can buy nowadays aren't as cheap as the Pi (version 1) was back in the day: they take themselves too seriously.
Largos
Established Member
Posts: 183
Joined: Mon Oct 05, 2020 12:21 pm
Has thanked: 10 times
Been thanked: 40 times

Re: Raspberry Pi as a "no hassle" Midi host

Post by Largos »

https://www.raspberrypi.com/news/supply ... -increase/

it's intended to be a temporary price increase.
User avatar
Linuxmusician01
Established Member
Posts: 930
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 178 times
Been thanked: 39 times

Re: Raspberry Pi as a "no hassle" Midi host

Post by Linuxmusician01 »

Largos wrote: Mon Jan 10, 2022 12:23 pm https://www.raspberrypi.com/news/supply ... -increase/

it's intended to be a temporary price increase.
I know. But what I meant is that the "normal" (non-Covid) price of a Pi is also higher. The Pi 4 can do much more than the Pi 1 (the 1 could barely run a GUI). So it's only logical it's price increased.
tavasti
Established Member
Posts: 1428
Joined: Tue Feb 16, 2016 6:56 am
Location: Kangasala, Finland
Has thanked: 65 times
Been thanked: 58 times
Contact:

Re: Raspberry Pi as a "no hassle" Midi host

Post by tavasti »

PI is kind of overkill for such work, most likely smallest and cheapest arduino would be more cost optimized for such task.
Linux veteran & Novice musician

Latest track: https://www.youtube.com/watch?v=ycVrgGtrBmM
My music-related hobby business: https://chordpak.com/
User avatar
Linuxmusician01
Established Member
Posts: 930
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 178 times
Been thanked: 39 times

Re: Raspberry Pi as a "no hassle" Midi host

Post by Linuxmusician01 »

tavasti wrote: Mon Jan 10, 2022 4:29 pm PI is kind of overkill for such work, most likely smallest and cheapest arduino would be more cost optimized for such task.
I agree, tho I don't know anything about the Arduino. I'd love to see how that can be done. :)
Post Reply