Activity: Making Music (Intermediate)

Use the Rovers speaker to make it play songs. Learn to combine the world of music and robotics by going through sheet music and coding the Rover to play each note.

In the intermediate course, we will focus on making the code from the beginner guide more efficient.

Activity Demonstration

Basics of Music:

When it comes to reading sheet music for this task, we will be using two pieces of information from each note. The tone of the note and how long the note is played. The diagrams for music theory and the example song have been added underneath, but for further information on reading music, head over to ‘Activity: Making Music (Beginner)’.

Code:

We will switch to the text code editor for the intermediate and advanced music activities as some of the following functions are easier to build in that editor. If you wish to see the block version of the code, paste the complete code at the bottom of this activity into the text editor and then switch to block view in the IDE.

Constant - A constant is a variable that never changes when the code is run. In python, we write this in all capitals with underscores between words.

Lists/Arrays - A list is an object that can contain many variables inside it. Lists can be looped through to access each of the elements.

Credit: Srijan kumar Samanta

An array or list is an object that can contain multiple variables. As seen above, the image displays a list containing six strings, each representing a colour. You can put any data type in a list, and in python, you can also have data of different types in the same list. Each position in the list has an ‘index’, which is like the address of that position. Indexes start at 0 and can be used to tell the list which element you would like to read.

To get a complete understanding of lists in python, we recommend checking out the introduction to lists by W3Schools:

We will first make a ‘note’ list containing two elements, as seen below. Index 0 contains the tone of the note (C on octave 4). Index 1 contains the length of the note. A note is not inherent to python but something we have defined for this activity.

note = [NOTE.C4, CROTCHET]

The idea for this code is to create a list that contains several of the above ‘notes’. The list containing the notes will represent the whole song. We will then loop through every element in the song list and play each note for the correct note length in order.

1) Setting the Constants:

Now we need to start setting up the framework in our code to make music. We’ll begin by setting constants for each of the note lengths. A constant is a value used repeatedly throughout the code that never changes, which is perfect for what we need here. As all the notes are multiples of the Crotchet, we can use the diagram above and some simple multiplication to set all the constants. We also need to decide on an octave to use, so make a comment to remember this. For this example, we’ll use octave 4.

CROTCHET = 0.5
MINUM = 2 * CROTCHET
SEMIBREVE = 4 * CROTCHET
# Octave 4

2) Making the Song:

song = [
  [NOTES.E4, CROTCHET],
  [NOTES.D4, CROTCHET],
  [NOTES.C4, CROTCHET],
  [NOTES.D4, CROTCHET],
  [NOTES.E4, CROTCHET],
  [NOTES.E4, CROTCHET],
  [NOTES.E4, MINUM],
  [NOTES.D4, CROTCHET],
  [NOTES.D4, CROTCHET],
  [NOTES.D4, MINUM],
  [NOTES.E4, CROTCHET],
  [NOTES.G4, CROTCHET],
  [NOTES.G4, MINUM]
  ]

This part can take a little while, but the idea is to make a new list called ‘song’ and fill it with lists containing a note and note length. To use a note in the text editor, type ‘NOTES’ in all capitals, followed by the note and the octave number. We’ve done the first line of the song for reference.




3) Playing the Song:

for note in song:
  Sounds.playNote(note[0])
  delay(note[1])

Now to play the music, we can use a loop to cycle through each ‘note’ list in the song. ‘note[0]’ is the first index in the ‘note’ list, which is the tone. ‘note[1]’ is the next index in the ‘note’ list, which is the duration of the note.

Complete Code:

The complete code here will play the first line of the song. Try and fill in the rest of the song yourself!

CROTCHET = 0.5
MINUM = 2 * CROTCHET
SEMIBREVE = 4 * CROTCHET
# Octave 4

song = [
  [NOTES.E4, CROTCHET],
  [NOTES.D4, CROTCHET],
  [NOTES.C4, CROTCHET],
  [NOTES.D4, CROTCHET],
  [NOTES.E4, CROTCHET],
  [NOTES.E4, CROTCHET],
  [NOTES.E4, MINUM],
  [NOTES.D4, CROTCHET],
  [NOTES.D4, CROTCHET],
  [NOTES.D4, MINUM],
  [NOTES.E4, CROTCHET],
  [NOTES.G4, CROTCHET],
  [NOTES.G4, MINUM]
  ]
  
for note in song:
  Sounds.playNote(note[0])
  delay(note[1])

Up Next:

Up for a challenge? The advanced course will take you through how to process data through strings. This technique will be more complex but allows you to create songs faster and add more options to your music.

Related Posts

Previous
Previous

Connections in 3D Printing

Next
Next

Showcase: Rover and Arduino Line Follow