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.