# Reviving a dead synth with a Raspberry Pi

I’ve never been much of a musician, but I had some fun with a Kawai K1-II for a number of years. And it seems that will go on even after it died.

It was kept around the house while we saved up for a a “real” piano, at which point it became somewhat redundant. It was starting to show its age, too, with odd pops and crackles in the synth audio, and it just wasn’t worth fixing at the time.

Fast forward ten years or so, and this weekend I brought it home again from my parents’ in hopes that it would be useful for the kids to practice their scales on. As it turns out, the synth has deteriorated further and can only make garbled noises.

I dug around the internet for clues and assumed the on-board battery was depleted, but after opening it up1 and having a look at the schematics I’m more partial to its (EP?)ROM being corrupted, given that an attempt at downloading the original factory patches had zero practical effect except for it generating moderately different garbled noises.

Fortunately the MIDI circuitry seems to be largely independent of the synth portion itself – after digging out an ancient MIDI to USB adapter that I had lying around for (literally) a decade and hooking it up to GarageBand, a few minutes of playing sufficed to confirm that the K1-II is still completely usable as a velocity-capable MIDI keyboard.

But I can’t realistically leave my kids alone with a Mac and expect them to behave, so I decided to set up a Raspberry Pi as an audio synth.

Since navigating the intricacies of Linux audio is a tremendous pain in the posterior, I resorted to another piece of ancient history to prototype the solution, and after fiddling about with some soundfonts2 and cursing whoever came up with the idiotic sfArk format, I eventually managed to get the Eee 901 running Qsynth well enough to sound like a moderately decent 80’s era piano.

But even that is a bit much for a kid to handle3, so it was time to downsize.

## Your Own Digital Harpsichord

Assuming you’re running Moebius (my preference) or Raspbian, it’s about as easy as the following:

sudo apt-get update
sudo apt-get install fluidsynth
# get things rolling with stock soundfont, boosting gain
# note that we're going straight to the hardware device to minimize latency
fluidsynth -i -s -a alsa -o audio.alsa.device=hw:0,0 -m alsa_seq /usr/share/sounds/sf2/FluidR3_GM.sf2 -g 1 &
# connect MIDI input to softsynth
aconnect 20:0 128:0
# fiddle with volume, interactively
alsamixer
# store alsamixer settings
sudo alsactl store 0


A few more minutes of fiddling, and I got it to work upon boot (which is fairly quick if you disable a bunch of services, including networking).

fluidsynth takes a configuration file as parameter to -f, so specifying a single-line file containing prog 0 6 made it come up as a harpsichord (after all, we do have a piano already). Alas, non-stock soundfonts tend to be a little too much for the CPU to handle, so I had to stick with the stock one.

Of course, given the Raspberry Pi‘s hardware limitations, you are well advised to use something other than the original model B’s rather noisy analog output (I used a Sennheiser USB sound card) and consider over-clocking your board (I set mine to 900MHz, and it helped a fair bit with audio quality).

Next week I’ll wire up a LED and a couple of switches to the GPIO so that we can fiddle with the volume (there was a reason I picked a harpsichord for now…) and we’ll see how it goes.

## Update: Automating The Process

I’ve since had to reproduce this twice to test other SD cards4 and settings, so here’s a fabric script that will set this up for you via SSH on a fresh Raspberry Pi installation:

from fabric.api import run
from fabric.context_managers import cd
from fabric.operations import put, sudo
from fabric.contrib.files import contains, exists, append, comment

# Standard preamble for all my Raspberry Pi provisioning scripts
sudo("apt-get update")
sudo("apt-get -y dist-upgrade")
# need to replace dropbear with OpenSSH so that file transfer works
sudo("apt-get -y install fluidsynth htop openssh-server")
sudo("apt-get -y remove dropbear")
sudo("rpi-update")

# set up mild overclocking
for line in ["gpu_mem=16", "force_turbo=1", "arm_freq=900", "core_freq=250", "sdram_freq=450", "over_voltage=2"]:
append("/boot/config.txt", line, use_sudo=True)
sudo("update-rc.d switch_cpu_governor defaults")

# set up Fluidsynth
append("/root/fluidsynth.cmd", "prog 0 6", use_sudo=True)
with cd("/etc"):
put("fluidsynth", "init.d", use_sudo=True, mode=0755)
sudo("update-rc.d fluidsynth defaults")
sudo("reboot")


…and the accompanying bash init script:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          fluidsynth
# Required-Start:
# Required-Stop:
# Should-Start:
# Default-Start:     2
# Default-Stop:      0 1 6
# Short-Description: MIDI Playback
# Description:       MIDI Playback
### END INIT INFO

do_start () {
fluidsynth -i -s -a alsa -o audio.alsa.device=hw:0,0 -m alsa_seq /usr/share/sounds/sf2/FluidR3_GM.sf2 -f /root/fluidsynth.cmd &
until aconnect 20:0 128:0
do
sleep 2
done
}

do_stop () {
# kill off any players (quietly)
killall -9 fluidsynth > /dev/null 2>&1
# disconnect the MIDI input
aconnect -x
}

case "\$1" in
start|"")
do_start
exit 0
;;
restart|reload|force-reload)
do_stop
do_start
exit 0
;;
stop)
do_stop
exit 0
;;
status)
exit 0
;;
*)
echo "Usage: fluidsynth [start|stop|restart]" >&2
exit 3
;;
esac


1. Cue rant about non-removable batteries and long-term maintenance of devices. Oh, wait↩︎

2. Incidentally, if you’re looking for a nice soundfont for other machines, I can recommend this one. It comes with a bunch of retrogaming MIDI files that are a lot of fun. Won’t play well on a Raspberry Pi, though. ↩︎

3. I’m pretty sure they’d be able to get it going, but I’m completely sure they’d never stop fiddling with all the available sounds and effects. ↩︎

4. As always, you’ll get a lot better performance altogether if you use a Class 10 card. I got it to work on a Class 4, but it takes significantly longer to boot. ↩︎