Making Music With Code
Making Music With Code
An Introduction to
Sonic Pi
***This isn’t a tutorial per-say, but to more-so show you how the app works, to get
you excited, and to give examples on the possibilities. The app itself has a strong
and in depth tutorial at length you should follow if you wish to!***
Sonic Pi can also be played with and updated live, allowing one to potentially
change their recordings on the fly by changing their code, or even play a live event
with other musicians as a drum machine or synthesizer.
Getting Started
Navigating the program is simple, and laid out for the user with a great tutorial
guide. While the program can end up being as complicated as you would like it to
be, it really is a typical example of easy to use and hard to master.
Here you can see the lay out of your music creator. Let’s break it down fo ra
second.
A) These are your play controls. They do exactly what they sound like they should!
Play your code, stop it, record it… It is your fundamental control of your music
output.
B) These control your editor. These less so control how you edit, and more-so your
editing experience. Let you change the size of your code editor, show wavelength
feedback, etc.
C) Info and help is exactly what it sounds like. These sections are robust and great
for beginners, of which I found myself constantly referring to.
D) The code editor is where you write your code to create your music. It’s
essentially your instrument! This we will get into more depth on later.
E) The prefs panel sets your preferences, such as if you want your output to be
mono or stereo, and how you want to deal with your volume controls.
F) The log viewer is interesting. It is a live update of all the loops and code being
played, showing you in real time how the music is passing.
G) The help system is what comes up when you ask for it with the help button
(section C). It is extremely… well… helpful and I suggest you use it often!
H) Scope viewer is where you see the visual feedback, such as the wavelengths, in
real time as they are generated.
Checking out a first simple live loop, lets take a look at the code below:
Writing this into the code editor (D above), it seems to look like ruby code with its
own syntax. Interesting! Here we have a simple live loop where we can have
the sample (in this case a sample synth, called bd_haus) at a rate of 1 (this is
tempo or rhythmic speed of the music) called, followed by a sleep of 1. The sleep
here is the time in-between notes being played. This is a very simple starting point
and no where nears shows the power of Sonic Pi, but it does show a starting point
in how to interact with the program. Below is an example of how the code sounds
once its played:
Here we have a simple repeating beat at the same frequency. We made our first
beat! Cool? Not really. Conceptually, yes, but lets make something more
interesting.
HELPFUL TIP — As I got further into using Sonic Pi, I realized there are more
similarities to its syntax within Ruby than I initially thought! Using a # to comment
out lines works within the program as well, which not only allows you to add
comments within your lines of music, but also allows one to single out a line by
commenting out others and hearing it in isolation!
Real Notes
This is great an all, but what do these arbitrary numbers mean? It may be great as
you begin, or if you don’t have a background in music yourself, but the best way to
really understand how to make notes work together in harmony is to use real note
signifiers. Sonic Pi lets you write out your code simply using “play #” to identify a
pitch, or you can write the actual notes themselves in symbol syntax!
Oooooh! Check it out; we wrote a C, D, E note progression that easily! With a few
sleeps in between (once again, these delay the following note, and the number
signifies the length of delay), we can hear ourselves a nice little three note diddy :)
Here we take in two arguments to our play_pattern_timed chord, of which are the
E3 scale at m13, and set at the parameters of 0.25 and 0.5 for timing. This signifies
the scale, whether its minor or major, and the parameters tell you the different
timing in-between the notes. Let’s listen.
Here you can hear the lag between the two with the 0.5 timing, and it having an
“and” beat with the 0.25. After we have made a simple arpeggio and added some
timing to go along with our notes, we can see even with very simple sound
manipulation Sonic Pi has the ability to go far and make very complicated music
that is only limited to your creativity.
Adding Beats
Awww yisssss ELECTRONIC MUSIC working with SYNTHS making BEATS oh
yeahh! What is the first thing on your mind when you hear this? Obviously drum
machines!
This is where Sonic Pi gets really fun and frees up your space to mess with built in
drum machines and beats. As this is just an introduction, we can start simple here
and go into depth in a future article. To start, let’s take a look at a simple drum beat
that Sonic Pi has already available as a sample for you.
Easy syntax. Look familiar ruby users? Its a loop! The loop allows one to do just
that; loop the music to your hearts content. You can choose the looping duration,
but here I simply have a never ending loop, followed by the sample provided in the
program (:loop_amen), and the sample duration being simply that of the length of
the sample.
One can also choose the parameters of the loop, making the loop go a certain
amount of times over again using ruby syntax. To do so, you add “#. times do”
before your loop to signify how many times you would like to to repeat. (i.e.
50.times do).
Randomized Beats
Great, we made a drum beat by simply asking Sonic Pi to use one of its samples
(tysm Sonic Pi so nice of you) to get started. But is there a way to make it
a little more our own?
A great way to do this is by randomizing your samples. Take a look at the code
below:
Whoa! This looks a little complicated. Thats ok! This is simply to show you WHAT
Sonic Pi can do, and to be excited about making music with it. Above we can see
that we have our live loop of the beat slicer. This has a slice index of “random
index 8”, where it with slice a certain size of the sample, choose where to start and
finish based on the sample size, and and choose how often to sleep in-between.
This can be a little confusing. Here is a visual representation of the code written
above in wavelength form:
A) The sample drum beat. This is what we used before. This is one iteration of the
drum beat split up into 8 sections.
B) Here we can see the beat split up and assigned values (0–7, as we indicate them
as index numbers, and in ruby, index starts on 0 instead of 1).
C) Now that we have the size and amount of chunks out of the sample that we want
and signified in our code above, we randomize them to different places, durations,
speeds, etc to make our own randomized sample!
This randomizes every the sample restarts, making each loop unique, and gives
the sample an almost jazz like quality to its randomization.
And with a free recording functionality, everything you can make can be saved and
shared with the world!
Sonic Pi is a great example of mixing creativity with code, and makes one excited
to use both sides of their brain. Download the application at https://sonic-
pi.net/ and get started :)