Sonic Pi with HiFiberry/IQAudio DACs

Both HiFiBerry DAC, DAC+ and IQAudio DAC+ suffer from the problem that jackd, the JACK Audio Connection Kit daemon, doesn’t work with an i2s-connected device.

However the latest 4.1x Linux kernel that has been built for the Raspberry Pi does support memory mapped access for i2s devices. If you upgrade to this, your DAC HAT will work successfully with Sonic Pi.

Upgrade the Linux kernel

sudo rpi-update
(sudo apt-get install rpi-update if you don’t have this already installed)

One word of warning: Linux kernel 4 may cause problems with other drivers you may have installed on your Pi, so it would be better to work initially on a spare SD card.

Get Sonic Pi to recognise the DAC

When you install one of these cards it sets itself up as the default card in location hw: 0 Sonic Pi is hard wired to use this slot (via jackd) and as a result you can’t get any sound out of Sonic Pi when one of these cards is installed.

There is a work-around which will let you restore Sonic Pi using the built-in sound on the Pi, but unfortunately there is no solution at present to enable Sonic Pi to use the card directly. You have to tell it to use card slot 1 instead of 0 by modifying Sonic Pi.

Assuming you are using the normal installed version of Sonic Pi and not one you have compiled yourself, start up an LXTerminal window in the GUI or access the Pi by SSH, then:

cd /opt/sonic-pi/app/server/sonicpi/lib/sonicpi

Make a backup copy of the file you are about to modify in case of accident:

sudo cp scsynthexternal.rb scsynthexternal.rb.bak

Edit the relevant file:

sudo nano scsynthexternal.rb

When the file has opened find the relevant line using Ctrl+W and search for alsa. You should find a line:

sys("jackd -R -T -p 32 -d alsa -n 3 -p 2048 -r 44100& "


Note: On my system it read:

sys("jackd -R -p 32 -d alsa -d hw:#{audio_card} -n 3 -p 2048 -i 2 -o 2 -r 44100& ")
and below that:
sys("jackd -R -T -p 32 -d alsa -n 3 -p 2048 -r 44100 -i 2 -o 2& ")


Move to the right just after alsa and type a space followed by -d hw:1,0. Make sure there is a space before the -n. The new line should read

sys("jackd -R -T -p 32 -d alsa -d hw:1,0 -n 3 -p 2048 -r 44100& ")

Save the modification and exit:

Ctrl+X
Y
Return

Hopefully Sonic Pi will now start. (If you have previously tried to start it unaltered you may need to restart the Pi to kill off old processes.)

Get Sonic Pi to use the external DAC

After upgrading the system, you will now have additional entries in the /boot/overlays/ folder in your boot folder. You must add one of these to config.txt. To list them:

ls /boot/overlays/

Now sudo nano /boot/config.txt and add at the end:

dtoverlay=i2s-mmap
dtoverlay=hfberry-dacplus
(or whichever is the relevant overlay for your setup)

You must have at least Sonic Pi 2.9 to use an external DAC. To make sure everything is up-to-date:

sudo apt-get upgrade
sudo apt-get update
sudo reboot

aplay -l should list your sound interfaces. Look for the correct sound card in slot 0.

Now Sonic Pi will run

You will find Sonic Pi in the -> Programming menu. It takes a while to initiate itself, but eventually should run using the installed DAC.

If it doesn’t work, try using Audio Jack Control from the Preferences menu and visually try to start jackd.

sudo apt-get install qjackd if this is not already on your system. Check in the Setup tab that you are addressing the right interface, or by selecting the sound card with alsamixer.

If Sonic Pi still doesn’t run…

https://github.com/samaaron/sonic-pi/issues/1231

Most likely a copy of the sonic-pi server was still running, or perhaps the gui client crashed or didn’t shut down cleanly. If you try ps aux | grep sonic-pi-sever
You should get something like this:

factoid  10892  0.5  1.2 1069952 49104 pts/5   Sl+  21:58   0:01 ruby /home/factoid/src/ruby/sonic-pi/app/gui/qt/../../../app/server/bin/sonic-pi-server.rb  
factoid  11010  0.0  0.0  14232   948 pts/7    S+   22:02   0:00 grep --color=auto sonic-pi-server

In which case you need to do a kill 10892 (the number I typed matches the number in the 2nd column of the ruby /path/to/sonic-pi-server.rb) to force a shutdown of the server, which should free up that port, and you can just launch Sonic Pi again.

Or, you can just try signing out and back in of your account, or rebooting your computer, whichever you are most comfortable with.

Source: https://groups.google.com/forum/#!topic/sonic-pi/BvkSTW0Cqnk