Controlling Sonic Pi from QLab – Use Sonic Pi to generate pre-show music for your designs. Sonic Pi, created by Sam Aaron at Cambridge University, is a fantastic live-coding music synth utilizing Ruby script and the SuperCollider synth engine. Originally developed for the Raspberry Pi platform, it is also available, free, on Windows, Mac OS X and desktop Linux. Utilizing script commands from QLab (as AppleScript cues) and a command line interface for Sonic Pi developed by Nick Johnstone called sonic-pi-cli, you can randomly call up pre-built instruments/pads/drones on a Pi connected to the audio system (you can also do this internally on the QLab host). With the script cues placed within a Fire All group triggered by a script, the Pi will create a reasonably random layering of sounds. The control script selects from the child cues within the group will only start cues who are not running (“whose running is false.”)

The Pi is connected via Ethernet to the QLab host, connected via SSH. You can install sonic-pi-cli via gem on a Mac.

As a simplified example, the following scripts call Ruby files from a directory located on the machine running both QLab and Sonic Pi. It requires that Sonic Pi is running before the call is made. All communication is local to the host machine. To run these same commands to a connected Pi, simply SSH into the Pi and then update the file pathway as necessary. The initial call will activate Terminal and keep the window hidden. All subsequent calls containing “in window 1” will keep the commands within the same window.

tell application "Terminal"
	do script "`sonic_pi \"run_file '~/Dropbox/Code/Ruby/SonicPi/Dark Swirl Ambient Generator.rb'\"`" 
end tell

To run additional buffers/scripts without opening new terminal windows, you can use:

tell application "Terminal"
	do script "`sonic_pi \"run_file '~/Dropbox/Code/Ruby/SonicPi/randomBells02.rb'\"`" in window 1
end tell

To stop playback of all buffers/scripts, use:

tell application "Terminal"
	do script "sonic_pi stop" in window 1
end tell

To select a random ruby file to playback, set all of the trigger scripts (those that call a specific .rb file) into a Fire All group. Create a script cue with the following:

tell application id "com.figure53.qlab.3" to tell front workspace
	set triggerCues to cues of cue "2000" whose armed is true  -- this is your group cue, change cue number as necessary
	try -- In case there aren't any possible cues
		set someCue to some item of triggerCues
		if armed of someCue is true then
			start someCue
		end if
		delay 0.1
		set armed of someCue to false
	end try
end tell

Set a remote trigger (a randomized wait time with a restart, etc) to run the script cue that points to the group of .rb scripts). This will ensure that files which have been called will be disarmed and not allowed to be selected again. You can comment out that safety if you want files to be called in multiple thread runs.