Sonic Pi for standalone installations


#1

Hi there!

I’m working on a headless sound installation and I would love to use Sonic Pi for it.
The ideal situation would be having an option to save a Sonic Pi project as an executable that auto runs when you open it.

Has anyone tried something similar? Is this something you’d like to be able to do?


#2

Hi @guardabrazo,

this is definitely something I’d like to see supported in the future :slight_smile:

Currently we have an init file you can write which will auto-execute on launch. If you navigate into your ~/.sonic-pi/ folder and open up the init.rb file you’ll see something like this:

# Sonic Pi init file
# Code in here will be evaluated on launch.

If you place Sonic Pi code in there, it will be automatically executed when you start Sonic Pi.

Does this help you get started?


#3

Hi @samaaron! Thanks for the quick reply…

It definitely looks promising! I’ll try it later and get back here to share the results.


#4

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.


How to autorun sonic-pi?
Sonic Pi for video games question
#5

Wow @robin.newman , super useful answer! I can’t wait to test everything you said.

I’ll get back here to show you the results!


#6

I’m not sure if it runs it better or not, but Sonic-Pi-Tool allows you to boot sonic-pi headless via sonic-pi-tool start-server so you wouldn’t have to make your own shortcut.

Only thing that I know may be an issue is you have to install Rust/Cargo and compile it


#7

Tried both methods. The sonic-pi-tool one does work but it throws a number of errors and never completes so, with a sonic-pi-tool start-server; sonic-pi-tool eval-file script.rb (going from memory on this syntax, but it should be right), it never reaches the file. Naming my file init.rb and placing it in the right folder does work. And adding the sonic-pi.desktop file to the .config/autostart does work.

My script makes Sonic Pi into a synthesizer (takes MIDI signal to control a really long note). Especially useful in headless mode for a compact setup to bring to a jam or some such. Added two little beeps at the start of that script so that, when it’s done loading, Sonic Pi warns me and it’s then possible to just play without worrying about a thing.

But, then, the experience is even more seamless with the MODEP emulation of the MOD Duo, that Blokes has made for the pisound HAT. This is a real “it just works” experience and the synths, though limited, are quite decent.


#8

I’ve made a python port of Sonic-Pi-Tool (available here) in case you want to use it without installing Rust.

I think the reason sonic-pi-tool start-server; sonic-pi-tool eval-file script.rb doesn’t work is that start-server runs in the foreground, so it never completes (until you kill the Sonic Pi server). You’d need to run it in the background with something like sonic-pi-tool start-server & sonic-pi-tool eval-file script.rb (maybe with a sleep in between the two commands to give the server time to start up).

Another option might be to run them both as separate autostart apps (probably including a sleep at the start of the eval-file one again to give Sonic Pi time to start up).