Variables
A useful thing to do in your code is to create names for things. Sonic
Pi makes this very easy, you write the name you wish to use, then an =
then the thing you want to remember:
main_loop = :loop_amen
Here, we’ve ‘remembered’ the symbol :loop_amen
in the variable
sample_name
. We can now use sample_name
everywhere we might have
used :loop_amen
. For example:
main_loop = :loop_amen
sample main_loop
There are three main reasons for using variables in Sonic Pi: communicating meaning, managing repetition and capturing the results of things.
Communicating Meaning
When you write code it’s easy to just think you’re telling the computer to do stuff, so as long as the computer understands it’s OK. However, it’s important to remember that it’s not just the computer that reads the code. Other people may read it and try and understand what’s going on. Also, you’re likely to read your code in the future and try and understand what’s going on. Although it might seem obvious to you now - it might not be obvious to others or even your future self!
One way to help others understand what your code is doing is to write comments (as we saw in a previous section). Another is to use meaningful variable names. Look at this code:
sleep 1.7533
Why does it use the number 1.7533
? Where did this number come from?
What does it mean? However, look at this code:
loop_amen_duration = 1.7533
sleep loop_amen_duration
Now, it’s much clearer what 1.7533
menas - it’s the duration of the
sample :loop_amen
. Of course, you might say why not simply write:
sleep sample_duration(:loop_amen)
Which, of course, is a very nice way of communicating the intent of the code.
Managing Repetition
Often you see a lot of repetition in your code and when you want to change things, you have to change it in a lot of places. Take a look at this code:
sample :loop_amen
sleep sample_duration(:loop_amen)
sample :loop_amen, rate: 0.5
sleep sample_duration(:loop_amen, rate: 0.5)
sample :loop_amen
sleep sample_duration(:loop_amen)
We’re doing a lot of things with :loop_amen
! What if we wanted to hear
what it sounded like with another loop sample such as
:loop_garzul
. We’d have to find and replace all :loop_amen
s with
:loop_garzul
. That might be fine if you have lots of time - but what
if you’re performing on stage? Sometimes you don’t have the luxury of
time - especially if you want to keep people dancing.
What if you’d written your code like this:
sample_name = :loop_amen
sample sample_name
sleep sample_duration(sample_name)
sample sample_name, rate: 0.5
sleep sample_duration(sample_name, rate: 0.5)
sample sample_name
sleep sample_duration(sample_name)
Now, that does exactly the same as above (try it). However, it gives us
the ability to just change one line sample_name = :loop_amen
to
sample_name = :loop_garzul
and we change it in many places with the
magic of variables.
Capturing Results
Finally, a good motivation for using variables is to capture the results of things. For example, you may wish to do things with the duration of a sample:
sd = sample_duration(:loop_amen)
We can now use sd
anywhere we need the duration of the :loop_amen
sample.
Perhaps more importantly it allows us to capture the result of a call to
play
or sample
:
s = play 50, release: 8
Now we have caught and remembered s
as a variable, it allows us to
control the synth as it’s running:
s = play 50, release: 8
sleep 2
control s, note: 62
We’ll look into controlling synths in more detail in a later section.