Sonic Pi for standalone installations

I have used Sonic Pi in a headless mode quite a lot, and also auto-booting into a running sonic pi music file. I have used the following techniques to achieve this.

First, one of the easiest ways to get it to autoplay something, as Sam has pointed out, is to put your code in the init.rb file. This is useful, but ALWAYS runs every time you boot Sonic Pi, and you have to remove the code to stop this.

To start Sonic Pi automatically there are two techniques, which depend upon whether you want to run it without starting the GUI environment or want to start it from the (normal) gui environment.
In the latter case setup autoboot to Sonic Pi as follows:
Open a terminal window:

cd ~
mkdir -p .config/autostart
cp /usr/share/applications/sonic-pi.desktop   .config/autostart

Make sure that the Raspberry Pi is set to boot to the desktop (logged in) (you can change this using the Raspberry Pi Configuration program in Preferences on the main menu if necessary). Reboot your Raspberry PI and it should boot into Sonic Pi and run the code in the init.rb file.

The second method is a little more involved, but allows Sonic Pi to autoboot and run without having to start the desktop from a command line environment, and so uses fewer resources. Also why start the desktop if you are going to run headless, which is where I use this technique.

To get this to work, you first need to install xvfb. This is a package which lets you run a virtual frame buffer to fool the program (in this case Sonic Pi) into thinking it has a normal desktop environment.

sudo apt-get update
sudo apt-get install xvfb

Now you set up a script which will start sonic-pi running using xvfb, which can be run from the command line. You can create the script using the nano editor from the command line. I set mine up in Desktop folder, but you can place it anywhere you like.

cd ~/Desktop
nano startsp.sh

Type in the contents of the file:

#!/bin/sh
Xvfb :1 & xvfb-run sonic-pi 2 >/dev/null &

Then quit and save the file typing ctrl+X followed by Y. If you check the file typing cat startsp.sh you should see

#!/bin/sh
Xvfb :1 & xvfb-run sonic-pi 2 >/dev/null &

on the screen. Now set this executable by typing chmod +x startsp.sh

This generates the file to start Sonic PI. To get it to execute use crontab as follows: type crontab -e

(If this is the first time you have used this, select nano as the editor when prompted)
scroll down to the end of the file and add the line:
@reboot /home/pi/Desktop/startup.sh &

Then exit the file using ctrl+X followed by Y to resave the file

If you now set your Raspberry Pi to boot to the CLI logged in as user pi, then it will automatically boot into a running version of Sonic Pi. However you have no means of directly accessing the normal buffers in Sonic Pi which store your programs. There are two solutions to this:

  1. Use the init.rb file to hold the code you want to run.
  2. install the sonic-pi-cli gem which will let you send commands to sonic pi including using the run_file command which you can utilise to play any sonic pi file.

I use this in my Sonic Pi JukeBox program which utilises the technique above and on which you can get more details here

Finally you can inhibit either of the techniques used here to run Sonic Pi as follows for the Desktop autoboot just remove the sonic-pi.desktop file from the autostart folder for the cli boot, edit the crontab file using crontab -e and just put a # comment symbol before the @reboot line you added. You can remove the # again if you want to reinstate it.

11 Likes