This post follows a first one on basics of Arch onto a Raspberry Pi. Now we will see how to have high quality, low latency sound and a MIDI keyboard on this beautiful but still useless system.
MIDI keyboard with JACK
To reliably output sound using a MIDI keyboard and have a reasonable latency, consider dropping pulseaudio, installing and setting up JACK, and setting up fluidsynth to use JACK.
Disable pulseaudio's autostart with systemd
Run as the relevant user :
$ systemctl --user mask pulseaudio.socket
Setup JACK
First install the jack2 package, and enable it at login with :
$ systemctl --user enable jack@lowlatency.service
The @ argument sets a JACK configuration file that will be searched in the
following places, first match wins :
~/.config/jack/lowlatency.conf
/etc/jack/lowlatency.conf
If you use a DAC, drop a second conf file in ~/.config/jack/MYDAC.conf. For
instance HifiBerry-style DACs are identified as device hw:sndrpihifiberry.
In this case create ~/.config/jack/hifiberry.conf as follows, and instead of
the former, enable jack@hifiberry.service :
JACK_DEFAULT_SERVER="default"
JACK_OPTIONS=""
DRIVER="alsa"
DEVICE="hw:sndrpihifiberry"
DRIVER_SETTINGS="-n2 -p128 -r48000"
Note that JACK_OPTIONS is empty ; all the useful options go in
DEVICE_SETTINGS. I suppose it's a bug of the JACK service file.
More about DACs at the end of the post.
Setup fluidsynth
Install the fluidsynth package ; it comes with a default user service that
will connect to the JACK service if you tell it to in its conf file
~/.config/fluidsynth. First do :
$ systemctl --user enable fluidsynth
You will need a soundfont in /usr/share/soundfonts/default.sf2. I actually
made it a symbolical link to a soundfont file in my home dir. If you're
desperate about soundfonts install the soundfont-fluid package.
JACK2 is said to need package a2jmidid to detect MIDI keyboards. I've noticed
that in fact fluidsynth was very capable of detecting them on its own with the
option -o midi.autoconnect=1, be it at boot or hotplugged.
A useful feature is that it can read a command file with the -f option ; an
arguably preferable strategy is to create and load one in
/usr/share/soundfonts/fluidsynth.command. Finally my ~/.config/fluidsynth
reads as follows :
SOUND_FONT=/usr/share/soundfonts/default.sf2
OTHER_OPTS="-a jack -j -f /usr/share/soundfonts/fluidsynth.command -o midi.autoconnect=1 -o synth.polyphony=64"
Its systemd service's default target used to be "multi-user" whereas it should
have been "default" (bug resolved as of version 2.1.5-2). One can now enable it
with
On older versions of the program, you had to do instead : systemctl --user add-wants default fluidsynth
Autostart
If you want the systemd user services started at boot do as root :
# loginctl enable-linger YOURUSERNAME
Deal with latency
To optimise latency, you will want to use a realtime process for jackd.
You can follow instructions for Arch to have the right to make your
process prioritary on system tasks. Eventually, you should belong to the
realtime group. After that jackd should detect it's able to run a realtime
process and proceed. You can check that it does with
journalctl --user -u jack@hifiberry -e
(replace hifiberry with the config name you gave at the beginning of this
post). The -e option jumps at the end of the logs.
I was only able to use the alsa-seq MIDI plugin with JACK, but some people report more effectiveness with jack's own plugin. You can check Archwiki's JACK page for detailed instructions.
Another Archwiki page on professional audio mentions a
-Xalsarawmidi option in JACK used with a2jmidid to enhance MIDI jitter.
Use an external sound card
For Phat DACs, follow these instructions. What's a Phat ? It's a small cardboard that fits above a Raspberry Pi, plugged into its IO pins. And a DAC ? Digital to Analog Converter - in other words, an audio card.