Entry Details

Plog
FluidSynth on Pi Zero
Title
Fluid Synth MIDI Keyboard
Is Mini
False
Slug
fluid-synth-midi-keyboard
Content

Connecting a MIDI Keyboard to FluidSynth on a Headless Raspberry Pi Zero

Here's a step-by-step guide to connect your MIDI keyboard to FluidSynth on a headless Raspberry Pi Zero:

1. Install Required Software

First, update your system and install FluidSynth and ALSA utilities:

 
bash
sudo apt update
sudo apt upgrade -y
sudo apt install fluidsynth fluid-soundfont-gm alsa-utils -y

2. Connect Your MIDI Keyboard

Connect your MIDI keyboard to the Raspberry Pi Zero. Since the Pi Zero has limited USB ports, you might need:

  • A USB OTG adapter if your keyboard connects via USB
  • A USB hub if you need additional ports
  • A MIDI-to-USB adapter if your keyboard has standard 5-pin MIDI ports

3. Check if the MIDI Keyboard is Detected

Check if your MIDI keyboard is recognized:

 
bash
aconnect -i  # Lists input MIDI devices
lsusb        # Lists USB devices

Your MIDI keyboard should appear in one or both outputs.

4. Configure Audio Output

Since you're using a headless setup, configure the audio output. The Pi Zero can output audio through:

  • 3.5mm audio jack
  • HDMI (if connected)
  • USB audio interface

Check your available audio devices:

 
bash
aplay -l

5. Start FluidSynth

Run FluidSynth with appropriate settings:

 
bash
fluidsynth -a alsa -o audio.alsa.device=hw:0 -m alsa_seq -g 1 /usr/share/sounds/sf2/FluidR3_GM.sf2

Replace hw:0 with your audio device if different.

6. Connect MIDI Input to FluidSynth

In a separate SSH session (keep FluidSynth running in the first):

 
bash
aconnect -i  # Find your MIDI keyboard's port number (e.g., 24:0)
aconnect -o  # Find FluidSynth's port number (e.g., 128:0)
aconnect 24:0 128:0  # Connect them (replace with your actual port numbers)

7. Create an Automated Startup Script

For a headless setup, you'll want FluidSynth to start automatically. Create a systemd service file:

 
bash
sudo nano /etc/systemd/system/fluidsynth.service

Add the following content:

 
[Unit]
Description=FluidSynth Daemon
After=sound.target

[Service]
ExecStart=/usr/bin/fluidsynth -a alsa -o audio.alsa.device=hw:0 -m alsa_seq -g 1 -l -s /usr/share/sounds/sf2/FluidR3_GM.sf2
User=pi

[Install]
WantedBy=multi-user.target

Enable and start the service:

 
bash
sudo systemctl enable fluidsynth.service
sudo systemctl start fluidsynth.service

8. Create a MIDI Connection Script

Create a script to automatically connect your MIDI keyboard when plugged in:

 
bash
sudo nano /usr/local/bin/connect_midi.sh

Add:

 
bash
#!/bin/bash
KEYBOARD=$(aconnect -i | grep -i "your keyboard name" | awk '{print $2}' | cut -d':' -f1)
FLUID=$(aconnect -o | grep -i "FLUID" | awk '{print $2}' | cut -d':' -f1)

if [ -n "$KEYBOARD" ] && [ -n "$FLUID" ]; then
  aconnect $KEYBOARD:0 $FLUID:0
  echo "Connected MIDI keyboard to FluidSynth"
fi

Make the script executable:

 
bash
sudo chmod +x /usr/local/bin/connect_midi.sh

9. Set Up UDEV Rules for Auto-Connection

Create a udev rule to run the connection script when your MIDI keyboard is plugged in:

 
bash
sudo nano /etc/udev/rules.d/99-midi-keyboard.rules

Add (replace the vendor and product IDs with those of your keyboard from lsusb):

 
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", RUN+="/usr/local/bin/connect_midi.sh"

Reload udev rules:

 
bash
sudo udevadm control --reload-rules
External Link 1
External Link 2
Edit Back to List