You can play music in Scratch by adding the Music extension. This will add the following new blocks:
Play a melody
Put the following blocks together to make a simple melody:

Do you recognise it? It is the opening melody from the song ‘You Are My Sunshine’.
Change the instrument
The default instrument is a piano (1). The block set instrument to allows to chose among 21 different instruments.

Now let’s try our simple melody with different instruments.
Create a variable called instrument. Let’s add a way to change the instrument with the left/right keys.

Play a drum
The block play drum allows to play one of 18 different drums.

To create a rhythm for our music we could combine 4 quarter beats in a loop and repeat 16 times. Try this example:

Convert musical notation into music on Scratch
Let’s complete the melody for You Are My Sunshine. Below is the musical notation for the melody.

We can work out the notes and duration of each note from the music notation. There are up to 4 notes per beat in this tune and the the majority of the notes have a duration of ¼ of a beat. In the notation above, these are the black ones with a line attached (♩), also known as quarter notes. Some notes are held longer, there are some notes that last for half of a beat (these are the white ones with line attached 𝅗𝅥), also known as half notes. There is one note that lasts ¾ of a beat (the symbol is same as for a half note but followed by a dot 𝅗𝅥.). A whole note has a duration of one beat and is marked as a white note symbol with no line attached (𝅝). A rest is when there is no note played during part of a beat. A quarter beat rest has the symbol 𝄽 and a half beat rest has the symbol 𝄼. In music the notes are named by the letters A, B, C, D, E, F, G. After G it repeats again from A but at one octave higher. There can also be notes in between these notes. In our tune above there is one such note, A# (A sharp) on the first line. A# is slightly higher than a regular A, it is in between an A and a B. Note that as well as sharps we can also have flats (marked with the symbol ♭) which means a slightly lower note. B♭ is slightly lower than a regular B, it is in between A and B so it is actually the same note as A#. These ‘in between’ notes are the black keys on a piano keyboard.
In Scratch the notes are given numbers between 0 and 130. You can easily find the notes using the dropdown keyboard that is displayed when you use the ‘play note’ block.
On the notation diagram above, the Scratch note numbers are written in red. Also the duration for each note is written underneath. So now we have enough information to write a Scratch program to play the entire melody. Go ahead and complete the melody in Scratch. Here’s the start, but you have enough information to finish it.

Include a drum beat
If you added the code for the complete melody to the same project where you added the drum beat shown previously, you should notice that the drum beat goes along with the melody and lasts for the same duration (16 beats). You can experiment with varying the drum beat slightly during the song. e.g.

Using Lists for the notes
A more flexible way to set up the music is to use lists instead of joining multiple play note blocks together. This may allow us to enter our notation more quickly and we could use the same code for playing back different songs that we store in different lists. As an example, let’s set up our ‘You Are My Sunshine’ song using lists. We could create a list of just the notes but this is not the only information that we need to play a tune, we also need to know the duration for each note. We could use a separate list for the duration for each note OR we could just put everything into the same list – each note followed by the duration. Instead of writing decimal fractions, e.g. 0.25 for a quarter of a beat, we can simplify by using whole numbers. For this song, a quarter note is the smallest duration so we can use 1 to represent a quarter beat duration, 2 to represent half beat duration, etc. We would convert our code to a list like this:

As well as notes we also have rests so we can represent a rest using a dash ‘-‘ (minus). Go ahead and create a list containing all the notes and durations for the song.
Next we will need to write some code to play our song from the list. The code needs to go through the list in steps of 2 getting the note and then the next item in the list which is duration. We will store note and duration in variables and then use the play note block to play the note for the required duration. We also need to use the rest block instead of play note block is a rest is required instead of a note. To keep track of where we are in the list we will need a variable, this is often called ‘index’ or just ‘i’. Go ahead and make 3 variables, note, duration and i:

Then set up the following blocks to play the song from the list:


