Setting up Sonic-Pi to work with a pisound midi/audio interface board.

I thought it would be useful to gather my experience of using the excellent pisound midi/audio interface board by blokas.io with Sonic Pi.

The samplerate, frames/period and number of periods/buffer I use in this article give about the lowest latency I have been able to achieve with Sonic Pi on a Raspberry Pi 3. If you are not worried about latency, and mainly using Sonic Pi for playback, you may want to use values which are less demanding, but at the expense of worse latency.
Such figures might be sample rate 48000, frames/period 4096, periods/buffer 3.  I use these when playing pieces that are very demanding of sonic pi, with several parts playing together. Latency for these settings is 256ms as opposed to 21.3ms for the settings used below

Although the pisound board works “out of the box” with Sonic Pi, that is only as far as sound output is concerned. Sonic Pi does not automatically connect the stereo input ports on the pisound board. This is because, by default the Raspberry Pi does not have any audio input ports, and it cannot predict which external cards/boards a user is likely to install.
There are two ways around this problem. The first in non-invasive and makes use of an external program `qjackct` (installed on the Pi) to launch and configure the jackd connector which enables the SuperCollider scsynth which is at the heart of the sounds Sonic Pi produces to talk to the outside world. The second invasive method which involves altering one of the files in Sonic Pi is to configure this file (scsynthexternal.rb) to accommodate the actual board setup (in this case PiSound) that you are using.

In both instances the first thing to do is to set PiSound as the default audio board. This is achieved by using the Audio Device Settings in Preferences on the main Sonic Pi menu. From the Sound Card popup menu select pisound (Alsa mixer) and set it as default, using the Make Default button.

For the non-invasive approach, first open a terminal window and type
“`
killall jackd
qjackctl &
“`
The qjackctl interace window will open up

CLick the Setup Button, then select the Settings Tab and within that the Parameters tab
adjust the parameters as follows:

Driver alsa
Realtime (ticked)
Sample Rate: 96000
Frames/Period: 1024
Periods/Buffer: 2
MIDI Driver: seq

Switch to the Advanced tab and adjust as follows:

Preset Name: (default)
Server Prefix: jackd
Port Maximum: 1024
Time (msec): 500
Audio: Duplex
Dither: None
Output Device: hw:pisound
Input Device: hw:pisound,0
Channels I/0 (both) 2
Latency I/O (default)
Startup Delay: 2 secs
“`
Now click `OK`
Click the `Start` button on the Jackd screen. All being well jackd will start running and the Stop button will show a bright red rectangle beside it.

Click the `Messages` button and select the `Status` tab
You should see values reflecting the Sample Rate 96000 Hz and the Buffer Size 1024 that we set earlier.
Leave qjackctl running and start Sonic Pi
When it has started, you should be able to hear sound from Sonic Pi as usual. Check using for example `play 72`
However you still have to connect the inputs to SuperCollider to use the PiSound input. To do this go back to the qjackctl interface and click on the Connect button.
You should see two entries on the left pane, `SuperCollider` and `system` and two entries on the right pane `SuperCollider` and `system`

These represent the output and input ports. You will see that the Supercollider output is connected to the system input, enabling aduio to go from Sonic Pi to your audio sysetm where you can hear it.
However the input to SuperCollider on the right hand side is disconnected. Use your mouse to drag a connection line diagonally from the system output (your microphone for example) to the SuperCollider input
Note you can expand each of these which makes it a bit clearer. You will see the two system capture ports connected to the two “in” ports in SuperCollider.

If you now connect an audio source to the PiSound board eg a guitar or microphone, you can run the program below to test it.

with_fx :compressor,amp: 4 do
live_audio :min # for a mono input
end

For a stereo input change the live_audio line to `live_audio :min,stereo: true`

The invasive method.

Using qjackctl is easy and it allows you to experiment with different settings for the sample rate, buffer size (frames/period) and number of periods/buffer. By default Sonic Pi uses 41000Hz for the sample rate, 2048 frames/period and 3 periods/buffer which is more conservative to suit the inbuilt audio card, but which gives a much larger latency. Blokas.io recommend that you only use 48000, 96000 and 192000 with pisound, otherwise it has to do resampling. 192000 is a bit too high for Sonic Pi so try the other two.
If you want to dispense with qjackctl you can alter one of the files in Sonic Pi, but you should only attempt this if you know what you are doing and are comfortable with hacking apps.

The file in question is named scsynthexternal.rb and it is a Ruby text file situated inside the app. The following changes apply to the current release version on Raspberry Pi which is 3.0.1
If you have installed Sonic Pi using apt-get install or are using an up to date raspbian stretch you will find the file at:

/opt/sonic-pi/app/server/sonicpi/lib/sonicpi/scsynthexternal.rb

It is saved as user root, so you have to access it using sudo. In a terminal type:

sudo leafpad

When the text editor opens navigate to the file listed above and open it.
First save a backup copy of the file. eg save as scsynthexternal.rb.backup
Then reopen the original and scroll to line 363 and 364 which show

# `jack_connect SuperCollider:in_1 system_capture_1`
# `jack_connect SuperCollider:in_2 system_capture_2`

alter these to read as follows:

`jack_connect SuperCollider:in_1 system:capture_1`
`jack_connect SuperCollider:in_2 system:capture_2`

Note there are TWO changes on each line
Then scroll to line 350 which reads:

jack_pid = spawn "jackd -R -p 32 -d alsa -d hw:#{audio_card} -n 3 -p 2048 -o2 -r 44100& "

put a # before the line to comment it out, and add an extra line below it.

#jack_pid = spawn "jackd -R -p 32 -d alsa -d hw:#{audio_card} -n 3 -p 2048 -o2 -r 44100& "
jack_pid = spawn "jackd -R -p 32 -d alsa -d hw:#(audio_card) -n 2 -p 1024 -o2 -i2 -r 96000& "

(note this will alter subsequent line numbers by 1 if you scroll back to the first change)

Save the file, replacing the original.
Exit the text editor, and try running Sonic Pi (without using qjackctl first)
All being well it will startup and you should be able to run the live_audio example.

One final point. You can’t use the volume slider on the Raspberry Pi Menu Bar with the PiSound board. Instead use the volume knob on the PiSound itself.

Problems. If you find Sonic Pi won’t start and that it times out with an error message, this can sometimes be because parts of the app are left running and interfere with subsequent attempts to start it. One drastic cure for this is to restart the Raspberry Pi. Alternatively you can open a terminal window and issue a series of commands to kill these sub programs.

You can try:

ls -ae

and look for any entries that contain sonic-pi, ruby, scsynth, jackd, m20 or o2m
if so try

killall sonic-pi
killall ruby
killall scysnth
killall jackd
killall m2o
killall o2m

You may get no process found responses, which is fine, but for any that you don’t get this then you have successfully stopped the offending app.
Now try restarting Sonic Pi.

I hope that this article will be useful to you if you have a pisound. You can also find help in the pisound community section at https://community.blokas.io/

Advertisements