New Tricks on the Block

Patterns

Transcript

So far you have made your melodies and basslines by writing play and sleep commands under one another. There's nothing wrong with that but you might have noticed that longer melodies can get quite difficult to follow. This short melody barely gets started and it already takes up 16 lines of code. But hey, there's another way to write melodies in Sonic Pi! You can use a command called play_pattern_timed. And remember whenever you encounter a new command it's a good idea to check out its help file by moving the text cursor over the command and pressing ctrl+i. The help file tells me that play_pattern_timed expects two arguments: a list of notes and a list of times. But what's a list? In Sonic Pi a list is like a container where you can store things. In this case you put the notes you want to play in one list and the sleep values between them to another. Lists are a handy way to store information and you can find them in most of the programming languages. To make a list you need square brackets. I'll place my notes inside the brackets and then I separate them with commas. When the notes are inside I close the brackets. I need another list for the sleep values between the notes. A comma must be used to separate the note list from the sleep list. And the sleep values go in the same order as they are in the melody. And pay attention here! I use periods for the decimal numbers, but you have to use comma to separate values. So, don't get those mixed up. Now let's play the melody. It should sound more or less the same. Oho! Well looks like I put the wrong note there but otherwise it's almost the same. If my sleep list is shorter than the note list, it will loop back to the beginning until all the notes have been played. You can see here that the same pattern of sleep values is repeating twice. So here I have that and here it's basically the same thing again. I can take advantage of the looping of the list and I can remove half of these sleep values. Now it's a lot nicer to read already. And it still sounds the same. You can also use this feature to test different rhythms for your melody. If there is only one sleep value all the notes will be played with the same rhythm. Try using play_pattern_timed to play back a melody. Start a new melody or use some earlier melody you've made with play and sleep commands.

A list is a way to store a number of items in a single collection. In Sonic Pi a list consists of three main things:

[element, element2, element3]

Square brackets mark the beginning and end of the list. The items inside the list are called elements and can be for example numbers or symbols. The elements of the list are separated by a comma.

play_pattern_timed needs two lists to make sound. First a list of notes and then a list of sleep values. These two lists need to be separated by a comma.

play_pattern_timed [:C, :D, :Eb], [0.5,1]

Do this
Use the play_pattern_timed command to play back a melody. You can either start a totally new melody or convert an old one you have already made!

What will happen if the list of sleep values is shorter than the list of notes?

Educator notes

Lists are a great way to organise things. Some learners might ask why you would use play_pattern_timed instead of just writing play and sleep. In programming we like to avoid redundancy. Writing the play and sleep commands every time is not as efficient as writing two lists for them.

For a melody with just play and sleep values, play_pattern_timed is the better choice. You can also give the same options for all the values in a list.

However, using individual play commands gives more flexibility because you can define options for each command.

The important thing is to familiarise learners with the concept and syntax of lists. It will be very useful especially in future projects. In most programming languages list-like structures are used almost all the time.


Staying Organised


Do this

If your code has very long lines do this:

Make your code easier to read by splitting long lines into separate lines. Make sure your code is still running after your cleanup!


Educator notes

Whenever there is something that can be separated with a comma, the code can also be separated to a new line. Although it is great to be organised, too much separation can make the code confusing and hard to read.

play_pattern_timed [60, 63,
 63, 65,67, 69],
[0.25, 0.5, 0.25],
amp: 0.75,
pan: -1,
attack: 0.1,
release: 0.1

There is not really any point in splitting the melody in the middle. The code will work just fine, but splitting it to different lines has made it more difficult to understand.


Extra: About Note Lengths in Patterns

Copy this code to an empty buffer in Sonic Pi.

play 60, release: 0.1
sleep 0.5
play 64, release: 0.1
sleep 0.5
play 67, release: 0.1
sleep 0.5
play_pattern_timed [60, 64, 67], [0.5], release: 0.1

Did you notice that the release works differently in the pattern? You might find it strange, but actually the release time is 0.1 in both. play_pattern_timed sets the default value for sustain: based on the list of sleep values. Sustain sets how long the note will stay at full volume. By default, play has a sustain value of 0. Try setting the sustain: option for play_pattern_timed to 0 to get the same effect as with play!

play_pattern_timed [60, 64, 67], [0.5], release: 0.1, sustain: 0