Plog App Admin
Dashboard
Plogs
Entries
Back to Site
Edit Entry
Plog
Hitachi MQ-25
USB MIDI Host
Yamaha Dx7 Resto
Jasper Synth
FluidSynth on Pi Zero
Title
IsLearning
Slug
URL-friendly name (e.g., "my-entry-title")
Content
<h1 class="text-2xl font-bold mt-1 text-text-100">Connecting a MIDI Keyboard to FluidSynth on a Headless Raspberry Pi Zero</h1> <p class="whitespace-pre-wrap break-words">Here's a step-by-step guide to connect your MIDI keyboard to FluidSynth on a headless Raspberry Pi Zero:</p> <h2 class="text-xl font-bold text-text-100 mt-1 -mb-0.5">1. Install Required Software</h2> <p class="whitespace-pre-wrap break-words">First, update your system and install FluidSynth and ALSA utilities:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash"><span class="token">sudo</span> <span class="token">apt</span> update <span class="token">sudo</span> <span class="token">apt</span> upgrade -y <span class="token">sudo</span> <span class="token">apt</span> <span class="token">install</span> fluidsynth fluid-soundfont-gm alsa-utils -y</code></pre> </div> </div> <h2 class="text-xl font-bold text-text-100 mt-1 -mb-0.5">2. Connect Your MIDI Keyboard</h2> <p class="whitespace-pre-wrap break-words">Connect your MIDI keyboard to the Raspberry Pi Zero. Since the Pi Zero has limited USB ports, you might need:</p> <ul class="[&:not(:last-child)_ul]:pb-1 [&:not(:last-child)_ol]:pb-1 list-disc space-y-1.5 pl-7"> <li class="whitespace-normal break-words">A USB OTG adapter if your keyboard connects via USB</li> <li class="whitespace-normal break-words">A USB hub if you need additional ports</li> <li class="whitespace-normal break-words">A MIDI-to-USB adapter if your keyboard has standard 5-pin MIDI ports</li> </ul> <h2 class="text-xl font-bold text-text-100 mt-1 -mb-0.5">3. Check if the MIDI Keyboard is Detected</h2> <p class="whitespace-pre-wrap break-words">Check if your MIDI keyboard is recognized:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash">aconnect -i <span class="token"># Lists input MIDI devices</span> lsusb <span class="token"># Lists USB devices</span></code></pre> </div> </div> <p class="whitespace-pre-wrap break-words">Your MIDI keyboard should appear in one or both outputs.</p> <h2 class="text-xl font-bold text-text-100 mt-1 -mb-0.5">4. Configure Audio Output</h2> <p class="whitespace-pre-wrap break-words">Since you're using a headless setup, configure the audio output. The Pi Zero can output audio through:</p> <ul class="[&:not(:last-child)_ul]:pb-1 [&:not(:last-child)_ol]:pb-1 list-disc space-y-1.5 pl-7"> <li class="whitespace-normal break-words">3.5mm audio jack</li> <li class="whitespace-normal break-words">HDMI (if connected)</li> <li class="whitespace-normal break-words">USB audio interface</li> </ul> <p class="whitespace-pre-wrap break-words">Check your available audio devices:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash">aplay -l</code></pre> </div> </div> <h2 class="text-xl font-bold text-text-100 mt-1 -mb-0.5">5. Start FluidSynth</h2> <p class="whitespace-pre-wrap break-words">Run FluidSynth with appropriate settings:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash">fluidsynth -a alsa -o audio.alsa.device<span class="token">=</span>hw:0 -m alsa_seq -g <span class="token">1</span> /usr/share/sounds/sf2/FluidR3_GM.sf2</code></pre> </div> </div> <p class="whitespace-pre-wrap break-words">Replace <code class="bg-text-200/5 border border-0.5 border-border-300 text-danger-000 whitespace-pre-wrap rounded-[0.4rem] px-1 py-px text-[0.9rem]">hw:0</code> with your audio device if different.</p> <h2 class="text-xl font-bold text-text-100 mt-1 -mb-0.5">6. Connect MIDI Input to FluidSynth</h2> <p class="whitespace-pre-wrap break-words">In a separate SSH session (keep FluidSynth running in the first):</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash">aconnect -i <span class="token"># Find your MIDI keyboard's port number (e.g., 24:0)</span> aconnect -o <span class="token"># Find FluidSynth's port number (e.g., 128:0)</span> aconnect <span class="token">24</span>:0 <span class="token">128</span>:0 <span class="token"># Connect them (replace with your actual port numbers)</span></code></pre> </div> </div> <h2 class="text-xl font-bold text-text-100 mt-1 -mb-0.5">7. Create an Automated Startup Script</h2> <p class="whitespace-pre-wrap break-words">For a headless setup, you'll want FluidSynth to start automatically. Create a systemd service file:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash"><span class="token">sudo</span> <span class="token">nano</span> /etc/systemd/system/fluidsynth.service</code></pre> </div> </div> <p class="whitespace-pre-wrap break-words">Add the following content:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code>[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</code></pre> </div> </div> <p class="whitespace-pre-wrap break-words">Enable and start the service:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash"><span class="token">sudo</span> systemctl <span class="token">enable</span> fluidsynth.service <span class="token">sudo</span> systemctl start fluidsynth.service</code></pre> </div> </div> <h2 class="text-xl font-bold text-text-100 mt-1 -mb-0.5">8. Create a MIDI Connection Script</h2> <p class="whitespace-pre-wrap break-words">Create a script to automatically connect your MIDI keyboard when plugged in:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash"><span class="token">sudo</span> <span class="token">nano</span> /usr/local/bin/connect_midi.sh</code></pre> </div> </div> <p class="whitespace-pre-wrap break-words">Add:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash"><span class="token shebang">#!/bin/bash</span> <span class="token assign-left">KEYBOARD</span><span class="token">=</span><span class="token">$(</span><span class="token">aconnect -i </span><span class="token">|</span> <span class="token">grep</span><span class="token"> -i </span><span class="token">"your keyboard name"</span> <span class="token">|</span> <span class="token">awk</span> <span class="token">'{print $2}'</span> <span class="token">|</span> <span class="token">cut</span><span class="token"> -d</span><span class="token">':'</span><span class="token"> -f1</span><span class="token">)</span> <span class="token assign-left">FLUID</span><span class="token">=</span><span class="token">$(</span><span class="token">aconnect -o </span><span class="token">|</span> <span class="token">grep</span><span class="token"> -i </span><span class="token">"FLUID"</span> <span class="token">|</span> <span class="token">awk</span> <span class="token">'{print $2}'</span> <span class="token">|</span> <span class="token">cut</span><span class="token"> -d</span><span class="token">':'</span><span class="token"> -f1</span><span class="token">)</span> <span class="token">if</span> <span class="token">[</span> -n <span class="token">"</span><span class="token">$KEYBOARD</span><span class="token">"</span> <span class="token">]</span> <span class="token">&&</span> <span class="token">[</span> -n <span class="token">"</span><span class="token">$FLUID</span><span class="token">"</span> <span class="token">]</span><span class="token">;</span> <span class="token">then</span> aconnect <span class="token">$KEYBOARD</span>:0 <span class="token">$FLUID</span>:0 <span class="token">echo</span> <span class="token">"Connected MIDI keyboard to FluidSynth"</span> <span class="token">fi</span></code></pre> </div> </div> <p class="whitespace-pre-wrap break-words">Make the script executable:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash"><span class="token">sudo</span> <span class="token">chmod</span> +x /usr/local/bin/connect_midi.sh</code></pre> </div> </div> <h2 class="text-xl font-bold text-text-100 mt-1 -mb-0.5">9. Set Up UDEV Rules for Auto-Connection</h2> <p class="whitespace-pre-wrap break-words">Create a udev rule to run the connection script when your MIDI keyboard is plugged in:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash"><span class="token">sudo</span> <span class="token">nano</span> /etc/udev/rules.d/99-midi-keyboard.rules</code></pre> </div> </div> <p class="whitespace-pre-wrap break-words">Add (replace the vendor and product IDs with those of your keyboard from <code class="bg-text-200/5 border border-0.5 border-border-300 text-danger-000 whitespace-pre-wrap rounded-[0.4rem] px-1 py-px text-[0.9rem]">lsusb</code>):</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code>ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", RUN+="/usr/local/bin/connect_midi.sh"</code></pre> </div> </div> <p class="whitespace-pre-wrap break-words">Reload udev rules:</p> <div class="relative group/copy rounded-lg"> <div class="sticky opacity-0 group-hover/copy:opacity-100 top-2 py-2 h-12 w-0 float-right"> <div class="absolute right-0 h-8 px-2 items-center inline-flex"> <div class="relative"> </div> </div> </div> <div class="text-text-500 text-xs p-3.5 pb-0">bash</div> <div class=""> <pre class="code-block__code !my-0 !rounded-lg !text-sm !leading-relaxed"><code class="language-bash"><span class="token">sudo</span> udevadm control --reload-rules</code></pre> </div> </div>
External Link 1
External Link 1
Back to List