DEV Community

Cover image for Create Music with Code
Harsh Mehta
Harsh Mehta

Posted on • Edited on

Create Music with Code

Hello 👋🏼

I was always interested in music but somehow I just couldn't get myself to explore those costly tools with complex UI. Until a few weeks ago, I found something really interesting. I found out about a tool that compiles code into Music! I tested it out and was instantly hooked. It's really fun and even if you're not that much into making music, you can kill some great time with Sonic Pi. So, here's a blog on how to get started!

What's Sonic Pi?

Sonic Pi is a code-based music creation and performance tool.
This basically means that you can create, mix music by code. Sonic Pi is built on Ruby and is Open Source.
You can download Sonic Pi from their website. You can download it for Windows, macOS and Raspberry Pi as well.

Basic Music Theory

Before we start, I'll be covering some basic Music theory which is very easy to understand so don't worry.

Notes

The piano diagram below shows the various piano notes that piano keys usually correspond to. The ones with # are the sharp notes and the ones with the symbol that looks like a b are the flat notes. Keys, wherever multiple notes are written, can be referenced as those notes. Meaning that C# and Db are the same key.

You can test what these notes usually sound on piano on this virtual piano. Make sure you enable the "Letter Notes" so that you know which note you are playing by pressing the key.

Numbers, Notes and Octaves.

Every note along with its octave represents a number, depending on the number of keys on a keyboard. An Octave is an interval between musical pitch and another with double its frequency. So, numbers or notes belonging to one octave will have the same frequency and those with different octave numbers will have different frequencies. Here is a cheat sheet I've taken from MeHackit's Sonic Pi tutorial.

Coding on Sonic Pi

Once you open Sonic Pi you'll probably be able to understand most of the interface but Sonic Pi's documentation has a UI guide that I'm attaching below for your reference.

Playing notes

We'll start by playing a note and this is how you play a note. In your editor, just type play :c and hit Run.

Congratulations! You've created and played your first music :p!

You can also change the octaves in the notes like so
play :c2 which will play the C note at the 2nd octave. You can experiment with different notes and octaves yourself. You can also refer to the cheat sheet provided above.

You can also play a note by its number. If you look for the note C at 4th octave you'll find the number 60. So, playing :c4 is the same as playing the 60th note number. To play a note number on Sonic Pi, just type play and the corresponding note:

play 60
Enter fullscreen mode Exit fullscreen mode

Notice how we did not include the colon as we did in the play :c command. When we play a number, we don't have to use any colon.

Changing the Synth

One thing you'll notice, that these notes sound very different than a normal piano. That's because Sonic Pi uses a default synthesizer and we can change the synth with the use_synth command. A bunch of synth options will appear in a list like so:

Alt Text

I'm using the piano synth and after that, all my notes will sound like a piano

use_synth :piano
play :c4
Enter fullscreen mode Exit fullscreen mode

Awesome right? You can try playing with different synth options available to you by Sonic Pi.

Sleep

We just played a single note up until now, but what if we want to play more than one note? I can just play another note below my first note, simple right?

play :c4
play :c2
Enter fullscreen mode Exit fullscreen mode

No. If you tried it yourself you will know what's wrong. The problem is that Sonic Pi executes lines of code quickly. So technically, before my first note even finishes playing, my second note will start which is almost instant. So what's the workaround? For that, we have the sleep command.

play :c4
sleep 1
play :c2
Enter fullscreen mode Exit fullscreen mode

Once the first note is played, the sleep command will tell Sonic Pi to stop for a second and then play the next note. The time is in seconds so if you type sleep 0.25, it will sleep for a Quarter of a second.

Parameters

You can tweak notes by changing their different parameters. You can add parameters to your notes separated by a comma.

Alt Text

You'll see a bunch of parameter options that we can use. We'll go through a few of them.

Pan

With the pan parameter, you can change the direction of your sound. For example, you can play your sound on the right or left or in the middle or anywhere in-between left and right. The value ranges from -1 to 1. -1 being extreme left and 1 being extreme right.

play :c3, pan: -1 #plays the note on left
sleep 1
play :c3, pan: 1 #plays the note on right
sleep 1
play :c3, pan: 0 #plays the note in the middle(default)
sleep 1
play :c3, pan: -0.25 #plays the note a little bit to the left
Enter fullscreen mode Exit fullscreen mode

Amp

With the amp parameter, you can change the amp of your notes. Which basically means you can change the volume.

play :c4, amp: 2
Enter fullscreen mode Exit fullscreen mode

If you run the above snippet, you'll hear the volume has increased. The default amp value is 1 and it starts from 0. If you put amp: 0, the volume will be 0 and you won't be able to hear anything.

Samples

We'll explore something really interesting called samples. Samples are basically pre-recorded sounds that you can use in your music. Sonic Pi has around 130 of such samples and also gives you the option to use external samples that aren't available in Sonic Pi. You can play different samples using the sample keyword and you'll get a list of samples you can select from.

Alt Text

There are a lot of different cool samples that you can explore. For example, the following sample will give you a kick or a beat sound.

sample :bd_haus
Enter fullscreen mode Exit fullscreen mode

Just like the play command, samples also have parameters that you can use to tweak how your samples sound. You can play with different samples and different parameters.

Loops

That was nice, in fact, it was so nice that we'd like to play it over and over again. So rather than playing the sample on every line, which will take me forever to do so, we can use the loop command given to us by Sonic Pi. The syntax goes something like this:

loop do
  #code that you want to loop
end
Enter fullscreen mode Exit fullscreen mode

Note that every loop must have at least one sleep command. Otherwise, Sonic Pi will try to play everything at once, which will completely cause it to break and will throw you an error about the loop not sleeping or syncing.

loop do
  sample :bd_haus
  sleep 1
end
Enter fullscreen mode Exit fullscreen mode

As you might have guessed, the above code snippet will play the bd_haus sample every second over and over again.

Done and Dusted?

That's about it for now! Play around with notes, samples, loops, and try changing different parameters and get creative! Remember, it's all fun and games. There is no such thing as a right and a wrong approach in art! It all depends on your creativity.

Part 2

In Part 2, I have covered the basics of live_loop, adding effects, and using randomness in your music! Do check out if you like this part :)

Contact Me

You can contact me if you have any doubts and you can also let me know on social media and do give a feedback! :D

Stay Home and Stay Safe :) <3

Top comments (0)