Setting up Sonic Pi with Qsynth on a Raspberry Pi (or other Linux host)

Setting up Qsynth, qjackctl and sonic pi on Raspberry Pi
Also probably applicable to other linux machines

These are concise notes with some photos of the various configuration screens.

Before installing anything do

sudo apt update
sudo upgrade

on Raspbian (Buster) you need to add your own Sonic Pi built from source
The so called default SP 3.1 on the latest Raspbian Buster  2019-09-26 does NOT support midi or OSC. You can see this by looking at the IO tab in the Preferences. The list of midi devices will always be blank, or at the log files in ~/.sonic-pi/log which show errors for the erlang and two osmid logs showing that they are missing.

I have an install package (script driven not .deb file) which installs a full 3.1 version. This can be installed alongside the supplied SP or you can remove that first using sudo apt remove sonic-pi. You can purge the extra packages it notifies if you wish. I usually leave the supplied version there and just add my own You can then install your own version of SP.

On the Main menu select Preferences->Main Menu Editor to add qjackctl to the menu in the sound section. It is already installed. Just not active on the menu

sudo apt install qsynth

This should also install fluid-soundfount-gm to /usr/share/sounds/sf2/FluidR3_GM.sf2

You can also install fluid-soundfont-gs separately if you wish using

sudo apt installfluid-soundfont-gs

It places the file in /usr/share/sounds/sf2/FluidR3_GS.sf2

Start Qsynth from the Menu (in the Sounds section)

click on setup.
In the Midi tab In the audio tab settings

In the Soundfonts tab If you don’t see FluidR3_GM.sf2 listed click open and open it.
You can open other sound fonts as well if you wish, but leave the GM font in the top position (use the up down arrows if necessary) Note if you don’t open the sound font you wont hear anything!

Settings tab just gives info. No changes to make. Click OK to close and say yes to the Restart query. (If OK is dimmed you haven’t made any changes so just click cancel.)
There is an article here you may find useful with further details on Qsynth installation

Assuming you have a full Sonic Pi 3.1 or 3.2dev installed, make sure it is NOT running. Because SP does not kill any jackd that it sets up, ie if you have run it without qjackctl, In that case check that there are no instances running using

killall jackd

Start qjackctl from the menu->sound section
click setup. For a Pi4 with no additional sound card adjust the settings as follows:
Settings tab. Parameters: (This gives a latency of 64msec. You can try different settings but make sure the sample rate is the same for Qsynth and Qjackctl)

Advanced tab: Don’t change any of the other tabs.
Save by clicking OK, or if OK is grey (you haven’t changed anything) click Cancel.

Click the Green Start button to fire up jackd

Click the Connect tab

Now start Sonic Pi running. When it starts, you will see SuperCollider added to the Connect audio window. (Bring it to the front if it is obscured) The SuperCollider Output port should be shown connected to the System input port.
Drag from the qsynth output to the SuperCollider input. That connects its audio output to Sonic Pi where it can be accessed using the live_audio command. Alternatively, you can drag from qsynth to system, in which case the audio can be played directly, and is not fed back to Sonic Pi. That is why I disconnected the auto connect when setting up Qsynth above.
If you want to change the connects, highlight the ends of the connection by clicking for example qsynth and supercollider then click Disconnect (NOT Disconnect All) and the highlighted connection will be removed.

Now switch to the Alsa tab. Drag a connection from Midi_Through to FLUID Synth. This will route midi from the visible “midi_through_port-0” shown in Sonic Pi to the Fluid synth input in Qsynth.

That completes the setup. Now you can set the output midi port in Sonic Pi using:
use_midi_defaults port: “midi_through_port-0”

you can set the instrument for all channels using
midi_pc 6 for example which will set all channels to a harpsichord voice.

To get the audio back into Sonic Pi use a command like
audio_live :qsynth,stereo: true

Note. If you restart qsynth you will loose both the audio and midi connections set up in the connection panel of qjackctl. It is possible to set up the Patchbay to remember connections, but because I am always altering things I do not find this all that useful, as it is quick and easy to remake the connections in the connection panel.

Here is a suitable program to test out the setup in Sonic Pi 3.1 or 3.2dev

#midi test program
#set midi port, channel and velocity factor
use_midi_defaults port:"midi_through_port-0",vel_f: 0.7,channel:1
#enable audio input from qsynth with some reveb applied
with_fx :reverb, room: 0.5,mix: 0.7 do
  live_audio :qsynth,stereo: true
end
#select a different instrument every 32 notes
live_loop :selectInstrument do
  n=tick%118
  midi_pc n
  sleep 3.2
end

#select random notes from the pentatonic minor scale to play
live_loop :playmidi do
  midi scale(:c2,:minor_pentatonic,num_octaves:4).choose,sustain: 0.1
  sleep 0.1
end

Enjoy using midi on Sonic Pi with the help of Qsynth