DEV Community

Cover image for Create Music with Code: 2
Harsh Mehta
Harsh Mehta

Posted on

Create Music with Code: 2

Well, it's been a long time since I wrote Create Music With Code and a lot of people seemed interested in a second part. Unfortunately, I didn't get the time, up until now!
So here goes!

Where We Left Off

Last time we left off with loops. We learned how to loop basically anything that you want to play for like an infinite amount of time.

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

The above code snippet will play the bd_haus sample every second over and over again. This makes sense from a programming point of view, but since we cannot exit from this loop, it makes no sense to just play one loop and ignore the rest of the code. Good for us that Sonic Pi has live_loop for that!

Live Loops

live_loop is a way to loop your music and also play other code while you're at it. Moreover, you can play multiple live loops at the same time. A better alternative to loop don't you think? Yes, in fact, I've never used loop at all :p!

So why learn loop in the first place? In my opinion, loop is a good way to teach the basics of programming to kids, or maybe someone who is new to programming itself, how loops work in other programming languages, which I think is one of the bases of any programming language out there.

Enough talk, let's cut to the chase.
A live_loop looks very similar to loop

live_loop :beat do
  sample :bd_haus
  sleep 1
end
sleep 3
play :c4
Enter fullscreen mode Exit fullscreen mode

This will play the loop and after 3 seconds, it will play the play :c4 command. You'll notice how the live_loop still keeps on playing and that's the best part about it.
You can notice that it's a bit different than the loop. I've added :beat after its syntax. In order to declare a live_loop you need to name it

live_loop :[any_name] do
   #your music
end
Enter fullscreen mode Exit fullscreen mode

There's actually a very good reason why we name a live_loop, but I'm not covering it in this article. What's even more interesting is that live_loop gives you the power to change it while it's running!
In the previous code, try changing anything in the live_loop while it's still running. for example

live_loop :beat do
  sample :bd_sone
...
Enter fullscreen mode Exit fullscreen mode

This probably won't make much sense if you're reading this but when you try changing anything in a live_loop all the changes will reflect even when you are still running the code. Which is amazing.

Using Effects

In Sonic Pi, you can even add Effects or FX to almost anything that you play. You can add FX by using the following syntax:

with_fx :[selected_fx] do
    #code that you want to add the FX to
end
Enter fullscreen mode Exit fullscreen mode

You'll get a drop-down list as soon as you write with_fx which will give a list of available effects you can use.

List

Let's try out the echo FX in a live_loop and see how it sounds.

live_loop :tune do
  with_fx :echo do
    play :c3
    sleep 1
  end
end
Enter fullscreen mode Exit fullscreen mode

You'll notice that the command play :c3 echoes. You can use anything and give it an FX. For example, you can even use samples, like so:

live_loop :beat do
  with_fx :reverb do
    sample :bd_haus
    sleep 1
  end
end
Enter fullscreen mode Exit fullscreen mode

The reverb FX adds a bit of "room effect" to your music.

Randomization

Sonic Pi has some built-in functions that can add randomness to your music. One such function is the rrand function which will give a random value between two numbers. The syntax goes something like this:

rrand(min,max)
Enter fullscreen mode Exit fullscreen mode

Providing min and max basically defines the range between two numbers. So let's check it out

play rrand(60, 95)
Enter fullscreen mode Exit fullscreen mode

Run it and you'll notice it'll play 86.251 as a random value. But hold on, how come that every time you press Run, it plays the same note over and over again?

The answer is that it is not truly random, it’s pseudo-random. Sonic Pi will give you random-like numbers in a repeatable manner. This is very useful for ensuring that the music you create on your machine sounds identical to everybody else’s machine - even if you use some randomness in your composition.

The above reasoning is taken from Sonic Pi's official documentation and I don't think that I can explain any better than this.

What's interesting is that if you play it twice it will generate a different note the second time. For example, check this out:

play rrand(60,95)
sleep 1
play rrand(60,95)
Enter fullscreen mode Exit fullscreen mode

Run it and you'll notice that it plays a new note the second time. So basically, a new pseudo-random number is generated if you use it again.
You can take a live_loop to generate random notes every time.

live_loop :melody do
  play rrand(60,90)
  sleep 0.5
end
Enter fullscreen mode Exit fullscreen mode

This will play a different note every time it enters the loop.

Done and Dusted?

That's about it for now! Play around with live loops, FX, random numbers, try using multiple live loops, adding random numbers in different sample 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.

Missed Part 1?

I have covered the extreme basics of Sonic Pi in my Part 1, in case you missed it, do check it out.

Contact Me

You can contact me if you have any doubts and you can also let me know on social media if you'd like a part 3. :D

Top comments (0)