Lists

In this section we’ll take a look at a datastructure which is very useful - the list. We met it very briefly before in the section on randomisation when we randomly chose from a list of notes to play:

play choose([50, 55, 62])

In this section we’ll explore using lists to also represent both chords and scales. First let’s recap how we might play a chord. Remember that if we don’t use sleep, sounds all happen at the same time:

play 52
play 55
play 59

Let’s look at other ways to represent this code.


Chords

One option is to place all the notes in a list: [52, 55, 59]. Our friendly play function is smart enough to know how to play a list of notes. Try it:

play [52, 55, 59]

Ooh, that’s already nicer to read. Playing a list of notes doesn’t stop you from using any of the parameters as normal:

play [52, 55, 59], amp: 0.3

Of course, you can also use the traditional note names instead of the MIDI numbers:

play [:E3, :G3, :B3]

Now those of you lucky enough to have studied some music might recognise that chord as E Minor played in the 3rd octave.