Sonic Pi Internals
So you want to peek under the hood? Whether you're a seasoned hacker or a complete newbie, this overview will hopefully help you get the big picture of how Sonic Pi works its magic.
You've interacted plenty with the user interface of Sonic Pi (the GUI). The GUI is written in C++ and uses the Qt framework, since that's a nice way to build cross-platform graphical applications (RasPi, Mac, Windows, and Linux at last count). However, the GUI doesn't do any of the heavy lifting of running code or making sound. Instead, it communicates with...
The server is written in Ruby and starts in the background when Sonic Pi starts up. It's really the heart of Sonic Pi -- you could write a new front end for it and still have Sonic Pi (it's been done... Emacs is a popular choice for some reason!). The server is responsible for executing your song's code, and implements the vast array of functions that make Sonic Pi so well suited to making music.
The server is also home to the Time Machine, a truly amazing piece of technical wizardry that is responsible for keeping virtual time, allowing things to stay nicely musical even when different parts are moving at different speeds.
Finally, the server is a sort of zookeeper for SuperCollider, which is up next.
SuperCollider is the audio synthesis engine that makes Sonic Pi sound good. The server keeps track of all the details of active notes, synthesizer parameters, FX, samples, and so on, and feeding all of that data to SuperCollider as it needs it via SC's documented OSC API (http://doc.sccode.org/Reference/Server-Command-Reference.html). It runs in the background as
scsynth. SuperCollider is a third-party project with a life of its own, and is almost untouched by Sonic Pi developers. The only exception so far is on the Windows platform, where one patch to the network code is applied to keep it from aggravating the Windows Firewall.
Sonic Pi v3.1.0
Contains the main program code for the server and GUI:
- app/gui/qt - the main GUI source code, and build scripts (some are outdated)
- app/gui/html - contains the code for the currently not maintained html interface, which also utilises some clojurescript
- app/server/ruby/bin - contains the main ruby scripts that start the backend sonic pi server
- app/server/ruby/vendor - contains ruby gems, some of which are needed for sonic pi to run, others may be there in preparation for future updates
- app/server/ruby/lib/sonicpi - the backend server code, written in ruby
- app/server/ruby/test - a set of test scripts to test the server code and ruby gems
- app/server/erlang - erlang code for the server
bin contains the following files:
sonic-pi- a bash script which starts Sonic Pi when ran
sonic-pi-graffiti- a bash script that prints out the text art of the Sonic Pi logo
Most of the stuff in etc is used at runtime:
- buffers - contains a wav file that drives the random system
- doc - the source code for the documentation, only used at build time
- examples - the source code for the examples, also only used at build time
- samples - all the built-in audio samples
- snippets - hidden feature, but planned to be used in the future
- synthdefs - contains both compiled synths (required at runtime) and the designs to create them
- wavetables - not yet used