Skip to the content.

Chapter 1 - First beeps

First synth in Sonic Pi

We will make this oscillator into a synthesiser that we can use in Sonic Pi:

{SinOsc.ar(440, 0, 0.2)}.play;

If we run this definition in Super Collider we can get a synth that we can play in Sonic Pi. Notice that it is writing the synthdef to a default location in my home directory (on a Mac) where Sonic Pi will find it - you will need to put your home directory in whatever operating system you use into that path.

(SynthDef("myfirstsynth", {arg out_bus = 0;
     var note, envelope;
     envelope = Line.kr(0.1, 0.0, 1.0, doneAction: 2);
     note = SinOsc.ar(440, 0, envelope);
     Out.ar(out_bus, note);
}).writeDefFile("/Users/gordonguthrie/.synthdefs"))

To use it in Sonic Pi we need to do two things. Firstly we need to enable it in the GUI:

enable external synths

Nota Bene/Pay Attention the GUI enables both external synths and external FXs. This manual only covers synths but in theory you could write your own effects too.

Then in Sonic Pi we have to load it and then just use it as normal

load_synthdefs "/Users/gordonguthrie/.synthdefs"

use_synth(:myfirstsynth)

play 69

Play about with this. There are a couple of things to notice - the sound will always be coming from one side of the speakers. That’s a bit odd. And also as you change the note up and down it makes no difference. It just plays A4 that fades out in 1 second.

We will fix both of these things later on. But first lets breakdown the synth definition:

/*
Define the synth - give it a name "myfirstsynth"
let it take one argument: `out_bus`
*/

(SynthDef("myfirstsynth", {arg out_bus = 0;

     // define 2 variables
     var note, envelope;

     /*
     Create a sound envelope that goes from 0.1 to 0 in 1 second
     and when it has done that trigger an action
     that destroys the running instance of
     the synthesiser and frees all memory
     */
     envelope = Line.kr(0.1, 0.0, 1.0, doneAction: 2);

     // define the note we are going to play A4 at 440Hz and set the volume to be the envelope
     note = SinOsc.ar(440, 0, envelope);

     // send the new note to the output channel 0
     Out.ar(out_bus, note);
}).writeDefFile("/Users/gordonguthrie/.synthdefs"))

We have had to do a bit more work to get it to play nice with Sonic Pi. It has a few problems:

But it’s not all bad - the line that determines the length of the note also calls a self-destruct function that cleans up and frees resources - without it your computer would gradually fill up with unused instances of synthesisers consuming both memory and CPU and eventually would just crash.

In Chapter 3 we will gradually build up this synthesiser until it is a clone of the sine synthesiser that is built into Sonic Pi.

We will add the following functions and defaults:

and the following slide options:

But before we can build a proper synth we need to understand a lot of stuff:

Subsequent chapters of this book step through these in a systematic fashion.