How to Make Your Own Music Using Simple Coding | Sonic Pi Tutorial

Sonic Pi is one of those software that places the stepping stone for a revolution. We are going to see that in our how to make your own music using simple coding tutorial today.

Coding is everywhere. It was the past, present and it is the future. I wasn’t surprised this day would come when they would attach notes to keys and commands, and we would derive music from it. I wouldn’t be surprised when AI would help us create a new piece of music by simply listening to the music in our head, and try to imitate it. I already see it happening.

That sounds like a dream right there! Doesn’t it? How to make your own music using simple coding! What if I tell you it is possible using Sonic Pi?

Doesn’t that, I don’t know, make you inquisitive? Well to drive away that curiosity, I will provide you a step by step tutorial on how to download Sonic Pi, how to install it, and how to start creating your own music using simple basic coding commands.

meme for how to make your own music using simple coding tutorial

Well that brings us to the cardinal question:

What is Sonic Pi?

Before we learn how to make your own music using simple coding let us understand what Sonic Pi is.

Sonic Pi is a live coding environment that is based on the language Ruby. So if you don’t know that language, don’t worry! The commands that you are going to learn are a piece of cake. You will know.

It was developed by Sam Aaron in the University of Cambridge Computer Laboratory. The initial release was in the year 2012. So you can imagine how far behind have we been in popularizing it.

How to Download Sonic Pi

Learning how to make your own music using simple coding starts with the very basic stuff. Downloading Sonic Pi. Let us see that first.

Step 1: Go to the following link from where you can download Sonic Pi:

https://sonic-pi.net/

It is also the official website of Sonic Pi.

Download Sonic Pi from the official website

Step 2: Based on the system you are using click on either Windows, Mac or Raspberry. I am using Windows so I am going to just go ahead and click on that.

Step 3: The next screen shows you the types of installers you have got. If you want a portable app that you can carry in your USB stick, you can choose the first one.

get sonic pi for windows

I need something that stays with me on my machine, so I am going to click on the second one.

With that you would notice that your download has already started.

downlading how to make your own music using simple coding

How to Install Sonic Pi

Step 4: Once the download finishes you have to click on it to initiate installation.

The next screen would be the agreement page. If you agree, select the checkbox that says “I accept”.

sonic pi license agreement

Then click on Install.

With that the installation would begin which would look something like this:

installing sonic pi

Step 5: Once the installation concludes, this is what you will see:

finished installation

Just click on Finish, with the Launch Sonic Pi checkbox checked.

How to Make Your Own Music Using Sonic Pi

Proceeding with the next section in how to make your own music using simple coding now.

With that Sonic Pi will open on your system.

first screen in how to make your own music using simple coding

The first thing to look at would be the UI of the software. This is the screen that you must be witnessing once Sonic Pi opens:

UI editor of Sonic Pi

The best part Sonic Pi offers lurks right down below. A hands on tutorial! If you just go through all the things they say, you will get a proper hang of how things are run in the Editor.

I will just post some basic commands that I understood myself, to help you get started.

Your First Sound

Type the following in the editor:

play 70

See! such a simple command. 70 is assigned to a piano note, and we are basically asking Sonic Pi to play the note that is against 70.

Once you are ready, click on Run. You can alternatively make it a habit to use the shortcut Alt + R.

first beep in how to make your own music using simple coding

Whoa! Where did that come from?

Wasn’t that sound good?

If you notice the right hand screen of the editor you will notice the log. It also gives you information like when does the command starts execution, when does it complete it, for how many times it was run, how long did it take it to run and which synthesizing server was used etc.

output in how to make your own music using simple coding tutorial

Pretty straightforward, right?

Go ahead and change that value to whatever you want. Play around with:

play 60

You would notice when you run the above, the sound suddenly becomes lesser pitched. Low pitches are directly associated with the left side of the pianos. If you want a pitch that’s higher increase the number and you are basically playing on the right side of the piano.

For those who know their music, 60 is basically the C of the 4th octave on a piano. If you want to play the next button, increase 60 by 1. So 61 would be equivalent to pressing the black key, and so on.

You can also club different notes by simply appending the code with more of such lines. Like this:

play 60
play 50
play 70

Now when you press Alt + R, you will notice that you all three of the notes have been played together. So it is like pressing the three buttons at the same time.

How do you separate them? Like one after the other? That’s where sleep comes into picture.

Your First Melody – Sleep

To separate them out use the sleep command. If you add the following in between the above notes:

sleep 1

It will basically add a pause of one beat. Let us use this in our example from above:

play 60
sleep 1
play 50
sleep 1
play 70

Press Alt + R and see the magic.

How was that?

You are becoming a musician already!

You can play around with different sleep values, for example you want to increase the tempo, just replace 1 with 0.5 and play. For e.g.

play 60
sleep 0.5
play 50
sleep 0.5
play 70

Press Alt + R and see the result for yourself.

Isn’t that cool?

But why is our music getting limited. If we were to make a song, would we end up writing that much of code? That would be stupid right, and exhausting too.

Don’t worry! We can play the above in a loop, so you don’t have to write the above music thousands of time. A simple loop will make sure it runs on repeat.

The Concept of Loops

If you want the above code to be run say, three times, instead of copy-pasting the same stuff over and over again, you can simply write:

3.times do

Don’t forget to use ‘end’ at the last line to finish off the loop otherwise, you will get a runtime error.

So if we use a loop in our above example, it would become:

3.times do
play 60
sleep 0.5
play 50
sleep 0.5
play 70
end

Go ahead and press Alt + R and see your code run three times.

Now what if you wanted your code to run for like 100000 times, are you going to write, 100000.times against your code? That would be daft. Sonic Pi has given a better solution. It is called loop.

You just have to add ‘loop do’ before you start coding.

loop do

Remember to end it using an ‘end’ syntax. Also, remember to add a sleep in order for the loop to actually work.

So if we try to apply the same in our above example:

loop do
  play 60
  sleep 0.5
  play 50
  sleep 0.5
  play 70
end

Hard to follow? Try that on an easier code:

loop do
play 60
sleep 1
end

To stop the loop you can click on the stop button availabe on the top bar:

stop

or use the shortcut key Alt + S.

Samples

Another one of those amazing topics I want to discuss in Sonic Pi is of samples. These are ready-made pieces of music that you can use to get the song you want.

Here’s a list of main samples that Sonic Pi provides:

  • :ambi_
  • :bass_
  • :elec_
  • :perc_
  • :guit_
  • :drum_
  • :misc_
  • :bd_

In each one of these types you get tons of options.

To try a sample, just type ‘sample’ and press space bar and you would be suggested with all the samples that Sonic Pi has.

samples

Let’s go with the first one.

ambi choir option in how to make your own music using simple coding

Press Alt + R to play and you get what this choir is doing. Basically playing a choir. But that felt weird. You can always play it in a loop by using the same loop logic learned above.

Let’s do that on this:

loop do
sample :ambi_choir
sleep 1
end

Remember adding a sleep would actually make the loop work.

Run it using Alt + R once again, and see for yourself.

Cool! Right?

Goof around by adding some beats or guitar to create something cool.

For e.g.

loop do
sample :drum_bass_hard
sleep 1
sample :guit_e_fifths
sleep 1
end

You get the idea? Right?

How do you Save and Load in Sonic Pi

If you have done a whole lot of coding and want to share your code with other Sonic Pi users, or maybe want to come back to what you are working on later, this method will come in handy:

Step 1: Once you are ready with your file, Click on File > Save. You can alternatively press Alt + Shift + S or even click here:

Let us stick to the fundamental method though.

save option

A dialog box would open. Here as you can see in the “Save as type” section you can save your file in different formats. So you can save it as a buffer, text, or ruby format. Go ahead and do that.

buffer files

I will save one as buffer to show you how it works.

Step 2: Give a file name to it. Then click on Save.

give a file name to your creation

Step 3: Now if you want someone to access it or maybe you want to load it at a later stage, you can simply open Sonic Pi and then go to File > Load.

load option in menu

or you can simply click here:

load button

Step 4: Navigate to the same location where your file is, and select it. Then click on Open.

giving any name

Your created file will open:

the same file received in how to make your own music using simple coding

How do you Record and Use Your Creation

Once you have created something you want to be able to share it with the world right?

To be able to do that, Sonic Pi provides you with the record feature.

Step 1: Once you are done creating, you click on File > Start Recording. The shortcut to do that is Alt + Shift + R.

start recording option

Alternatively you can directly click on the Record button here:

record button

Step 2: Then play your creation using Alt + S. Once the whole thing plays, you click on the Record button once again or press Alt + Shift + R again.

final saving in how to make your own music using simple coding tutorial

A dialog box would open asking you to save your recording.

NOTE: Sonic Pi lets you save your file in .wav format. If you want the file to be saved in some other format, you can always do that using other third party online converters.

Step 3: Provide a name in the File Name text box. Then click on Save button.

That’s it! You have just created music for your song.

If you like our how to make your own music using simple coding tutorial, check out other entertaining and cool stuff are we have.

Let us know what you have created using Sonic Pi in the comments section below. We would love to work with you on something cool.

Thanks.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

Leave a Reply