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: MattKingUSA, khz

Post Reply
User avatar
Linuxmusician01
Established Member
Posts: 1503
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 734 times
Been thanked: 130 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: 1503
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 734 times
Been thanked: 130 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.
User avatar
Largos
Established Member
Posts: 609
Joined: Mon Oct 05, 2020 12:21 pm
Has thanked: 70 times
Been thanked: 178 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: 1503
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 734 times
Been thanked: 130 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: 2041
Joined: Tue Feb 16, 2016 6:56 am
Location: Kangasala, Finland
Has thanked: 369 times
Been thanked: 207 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

User avatar
Linuxmusician01
Established Member
Posts: 1503
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 734 times
Been thanked: 130 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. :)
RichieC
Posts: 2
Joined: Fri Jan 05, 2024 1:17 pm
Been thanked: 2 times

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

Post by RichieC »

This is very cool.. but I'm having a slight issue..

If I login to the pi and start the service manually everything works fine, but I can't get the service to start automatically when the pi boots up.

During start up it does say the the service is started successfully but it doesn't work..

Any ideas what the problem could be?

User avatar
bluebell
Established Member
Posts: 1903
Joined: Sat Sep 15, 2012 11:44 am
Location: Saarland, Germany
Has thanked: 111 times
Been thanked: 114 times

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

Post by bluebell »

Maybe the service is started too early.

There is a brute force method. Ugly, but in most cases effective. Use /etc/crontab:

Code: Select all

@reboot root sleep 10; command1; command2;

Change command1 and command2 to the commands to stop and start the service.

Linux – MOTU UltraLite AVB – Qtractor – http://suedwestlicht.saar.de/

User avatar
autostatic
Established Member
Posts: 1994
Joined: Wed Dec 09, 2009 5:26 pm
Location: Beverwijk, The Netherlands
Has thanked: 32 times
Been thanked: 104 times
Contact:

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

Post by autostatic »

You actually don't need the service. It does nothing but running the Ruby script once so all connected MIDI devices get detected on startup. After that udev takes over for detecting new devices. Quickest fix would be to unplug a MIDI device and plug it in again to have everything detected again. And does the Ruby script issue an error code when it fails? If so then the systemd service file could be improved by adding restart directives.

User avatar
Linuxmusician01
Established Member
Posts: 1503
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 734 times
Been thanked: 130 times

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

Post by Linuxmusician01 »

autostatic wrote: Sat Jan 06, 2024 10:20 am

You actually don't need the service. It does nothing but running the Ruby script once so all connected MIDI devices get detected on startup. After that udev takes over for detecting new devices. Quickest fix would be to unplug a MIDI device and plug it in again to have everything detected again. And does the Ruby script issue an error code when it fails? If so then the systemd service file could be improved by adding restart directives.

Yep. What @autostatic said. You can also try automatically start the Ruby script another way so you know it's started when Linux is "ready"/completely booted. If you have a fast recent Pi and you're booting it into a GUI you can try it, for instance in XFCE, via Settings > System > Session and startup. But on the other hand, that might be the same as starting it via the SystemD service... :oops:

RichieC
Posts: 2
Joined: Fri Jan 05, 2024 1:17 pm
Been thanked: 2 times

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

Post by RichieC »

Actually.. turns out it does work... I just wasn't waiting long enough for it to start.. I'm running this on an old pi (a 1b) so it's not the fastest .. takes a couple of minutes for everything to start working.. but it works!

Thanks for the replies

Post Reply