Raspberry Pi as USB/Bluetooth MIDI host

Here are some instructions for setting up a Raspberry Pi 3B or a Raspberry Pi 4B as a MIDI host for USB or Bluetooth musical equipment. The purpose is to build an as-simple-as-possible device with no user interface that can be switched on and off by simply connecting/disconnecting to/from a power source.

For easy instructions with a pre-made image, use this page

What we need

The steps are the following:

  1. Prepare the microSD card
  2. Install required packages
  3. Add read-only mode
  4. Configure boot and udev scripts
  5. Configure Bluetooth optional
  6. Configure the LCD display optional

Preparing the microSD card

Post-install packages

Configuring automatic MIDI connection

Enabling read-only mode

Once the setup is finished, it's advisable to make the raspberry read-only to avoid the possibility of microSD data corruption when powering off by unplugging it directly from the power source.


git clone https://gitlab.com/larsfp/rpi-readonly
cd rpi-readonly
sudo ./setup.sh

Once your setup is tested (try plugging and unplugging various MIDI devices, and test connection with the command aconnect -l), you can turn on readonly mode with the command ro. Now you can safely power off the unit by unplugging it from the power source.

In order to modify files, you can turn readonly mode off with the command rw. Just remember to put it back to ro before logging out.

optional MIDI Bluetooth setup

Bluetooth setup involves recompiling the standard GNU/Linux bluetooth stack bluez to enable optional support for the alsa audio toolset. Remember to switch to rw mode before performing the following operations, and to switch back to ro when finished.

git clone https://github.com/oxesoft/bluez
sudo apt-get install -y autotools-dev libtool autoconf
sudo apt-get install -y libasound2-dev
sudo apt-get install -y libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev
cd bluez
./bootstrap
./configure --enable-midi --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var
make
sudo make install

You should now be able to test Bluetooth MIDI with sudo btmidi-server -v -n "RPi Bluetooth" (change RPi Bluetooth to anything you like) and by discovering Bluetooth MIDI devices with an app that supports that.

Create file sudo nano /etc/udev/rules.d/44-bt.rules with the following content:


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

Reload udev configuration and daemon with the following commands:


sudo udevadm control --reload
sudo service udev restart

Create a startup file for starting btmidi-server as daemon: sudo nano /lib/systemd/system/btmidi.service


[Unit]
Description=MIDI Bluetooth connect
After=bluetooth.target sound.target multi-user.target
Requires=bluetooth.target sound.target

[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/home/pi
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=btmidi
Restart=always
ExecStart=/usr/bin/btmidi-server -n "RP4-Bluetooth"

[Install]
WantedBy=multi-user.target

Enable the service:


sudo systemctl daemon-reload
sudo systemctl enable btmidi.service
sudo systemctl start btmidi.service    

Revert to readonly mode with command ro, and reboot (sudo reboot) to test that everything is working.

LCD for seeing active MIDI connections

I'm using a 128x64 1.3' OLED like this (any SSD1306 driver based OLED should work). This script is based on the Adafruit_Python_SSD1306 library.