Sonic Pi is a free live coding synthesizer, which can be installed onto your Raspberry Pi to create generative compositions. It uses a very simple programming interface to allow you to generate your own sounds and musical pieces.
The official website for the application is http://sonic-pi.net/.
An example of the types of sound that can be generated can be found at http://sonic-pi.net/#examples.
The very first example at the site generates some bell sounds. The code to generate this is shown here:
loop do sample :perc_bell, rate: (rrand 0.125, 1.5) sleep rrand(0, 2) end
As you can see, the minimal code they implemented on the example site generates some very interesting sounds.
We will now set up Sonic Pi and start experimenting with the programming code to generate all sorts of sounds.
Raspbian Jessie comes pre-installed with Sonic Pi. If you are using an earlier version of the Raspbian O/S, or find it is not installed, you can acquire it using apt-get
:
sudo apt-get install sonic-pi
Once the installation is finished, you are now ready to start experimenting.
Log in to the Raspberry Pi desktop and locate sonic-pi
. You will need to run this via the graphical desktop as it cannot be run from the command line.
The application can be found under Start | Programming | Sonic Pi.
The desktop application should now load. The following screenshot shows the GUI where you can write your programs:
Along the top of the application, there are a number of buttons that can be used to perform tasks such as play and stop the code running in the buffer window.
The Sonic Pi application comes with a tutorial which guides you through the application's menus, function calls, and syntax.
These can be located in the bottom left of the GUI.
Click on the Tutorial link and you will find a menu displayed.
In the center of the screen is the buffer window, where we noted our code will be written.
Let's try out a very simple example that illustrates how the menu and buffer window interact. In the buffer window, find the following comment:
#Welcome to Sonic Pi v2.6
Add the following command under it:
play 80
Plug in your speakers or headphones if they aren't already, and then press the Run button located in the top menu.
You should now hear a note being played. If you wish, try changing the value 80
to another integer such as 83
and press Run again. This time you will hear a different sound.
This is one of the most basic applications you can write in Sonic Pi.
Let's now try some more advanced experiments.
Starting your first program is very simple, as you saw; you can play a note just by typing play
followed by an integer.
Let's now take a look at another example like the one at the beginning of this section from the Sonic Pi website.
You may see that in the bottom-left window of the screen there is a list of examples. You can experiment with these to see what the different types of command in Sonic Pi do.
We are going to choose an example called Pentatonic Beeps from the Sonic Pi website:
with_fx :reverb, mix: 0.2 do loop do play scale(:Eb2, :major_pentatonic, num_octaves: 3).choose, release: 0.1, amp: rand sleep 0.1 end end
Paste this code into the buffer window. If you wish, you can select a different buffer tab, such as Buffer 1
.
This program constructs a simple loop with a pause, which executes continuously. Each time it executes, it plays notes from a major pentatonic scale.
To hear the effect in action, press Run.
You should hear the audio playing; it incorporates both scales and chords. To stop it, press the Stop button in the top menu.
If you wish to save any of your programs, you can do this using the Save button.
Let's try out one more program. This time, we will use the Ocean Waves example as a base. You can grab this either from the website or the examples list in Sonic Pi. The code is also displayed as follows:
with_fx :reverb, mix: 0.5 do loop do s = synth [:bnoise, :cnoise, :gnoise].choose, amp: rrand(0.5, 1.5), attack: rrand(0, 4), sustain: rrand(0, 2), release: rrand(1, 3), cutoff_slide: rrand(0, 3), cutoff: rrand(60, 80), pan: rrand(-1, 1), pan_slide: 1, amp: rrand(0.5, 1) control s, pan: rrand(-1, 1), cutoff: rrand(60, 115) sleep rrand(2, 3) end end
Add this to another of the buffer windows and press the Run button.
You should hear a sound that reminds you of ocean waves.
This is achieved through combining different types of noise. Let's edit this program to include the sound of a buoy bell. Start by stopping the program.
Add the following two lines above sleep rrand(2, 3)
in the preceding code:
play 80 play 83
These two notes will both play in quick succession and sound like they are almost playing together.
Try starting the application again. You should now hear a periodic bell-like chime.
If you want to export this to an external file such as a WAV, so you can play it independently of the Sonic Pi application, this is very simple.
Click the Rec button while your program is executing.
A recording will start until you click the Rec button again to stop it. You will then be given the option to save the file to your hard disk.
If you wish to save the program, click the Save button.
This concludes our introduction to Sonic Pi. You can read more about it using the tutorials mentioned earlier in this section. These will guide you through writing your own advanced projects.