Music Sprite Programming
Senior 1 · ICT
Music Sprite Programming
Lesson 3 of 5
25 min
A song is built from patterns: a drum rhythm that repeats, a line of notes that comes back in every chorus. Computers are very good at repeating patterns exactly, which is why so much music today is made with software. In this lesson you will use Scratch's sound tool kit to play drums and notes, choose instruments, control the speed and the volume, and program a complete tune.
In Scratch 3 the Sound category holds the blocks for recorded sounds and volume. The drum, note, instrument and tempo blocks are in the Music extension: click the Add Extension button in the bottom-left corner of the screen and choose Music. (In the older Scratch 2, all of these blocks were in the Sound category.)
| Block | Where | What it does |
|---|---|---|
| play sound ( ) until done / start sound ( ) | Sound | Play a recorded sound (Lesson 1) |
| stop all sounds | Sound | Silences every sound at once |
| set volume to (100) % | Sound | Sets the loudness: 0 is silent, 100 is full volume |
| change volume by (-10) | Sound | Makes the sprite's sounds quieter (or louder with a positive number) |
| play drum ((1) Snare Drum) for (0.25) beats | Music | Plays one hit of a percussion sound; there are 18 to choose from |
| rest for (0.25) beats | Music | Plays silence, a gap in the rhythm |
| play note (60) for (0.5) beats | Music | Plays one musical note on the current instrument |
| set instrument to ((1) Piano) | Music | Chooses the instrument used by play note; there are 21, from piano and guitar to marimba and steel drum |
| set tempo to (60) | Music | Sets the speed in beats per minute |
Drums, notes and rests are measured in beats. The tempo is the number of beats per minute (bpm), so it sets how long one beat lasts:
Worked example
At tempo 120, a script plays play drum ((1) Snare Drum) for (0.5) beats 8 times. Total beats: 8 × 0.5 = 4 beats. Each beat lasts 0.5 seconds, so the rhythm takes 4 × 0.5 = 2 seconds. A faster tempo makes the same music finish sooner.
In play note, each musical note has a number. Number 60 is middle C, and each step of 1 is the next key on a piano, white or black. The white keys from middle C are:
| Note | C | D | E | F | G | A | B | C (higher) |
|---|---|---|---|---|---|---|---|---|
| Number | 60 | 62 | 64 | 65 | 67 | 69 | 71 | 72 |
Adding 12 plays the same note one octave higher (60 → 72); subtracting 12 plays it an octave lower (67 → 55).
The first half of the old French round Frère Jacques (known in English as Brother John) uses only C, D, E, F and G:
The first loop plays C D E C twice ("Frère Jacques, Frère Jacques"); the second plays E F G twice ("Dormez-vous? Dormez-vous?"), holding G for 2 beats. repeat saves writing the same four blocks again. Length: 2 × 4 = 8 beats, plus 2 × (1 + 1 + 2) = 8 beats, so 16 beats. At 120 bpm each beat is 0.5 seconds, so the tune lasts 16 × 0.5 = 8 seconds. Try changing the instrument to (18) Steel Drum or (19) Marimba.
One pass of the loop lasts 0.5 + 0.5 + 0.25 + 0.25 = 1.5 beats, so 4 passes last 4 × 1.5 = 6 beats. At 90 bpm, 90 beats take 60 seconds, so 6 beats take 6 × 60 ÷ 90 = 4 seconds. The volume is 80%, a little below full.
A fade-out. After the loop, repeat (10) containing change volume by (-10) and play drum ((2) Bass Drum) for (0.5) beats makes each hit quieter than the last: 70%, 60% … down to 0%, which is silent.
Tip
Three numbers to remember: 60 is middle C, +12 is one octave up, and seconds per beat = 60 ÷ tempo. To find how long music lasts, first add up the beats, then multiply by the seconds per beat.
How long is one beat at tempo 30? At tempo 60?
Solution: 60 ÷ 30 = 2 seconds; 60 ÷ 60 = 1 second. A lower tempo means longer beats and slower music.
Which note number is G one octave above note 67?
Solution: 67 + 12 = 79.
At tempo 120, a loop repeats 6 times: play drum ((2) Bass Drum) for (0.5) beats, then rest for (0.5) beats. How many seconds does it last?
Solution: One pass: 0.5 + 0.5 = 1 beat. Six passes: 6 beats. At 120 bpm one beat is 0.5 seconds, so 6 × 0.5 = 3 seconds.
A sprite's volume is 100%. It runs repeat (4) containing change volume by (-15). What is the volume now?
Solution: 100 − 4 × 15 = 100 − 60 = 40%.
Keza's drum loop plays with the Music extension, but when she adds play note (60) for (1) beats she hears a piano, not a flute. What is missing?
Solution: A set instrument to ((12) Flute) block before the note. Without it, play note uses the piano, the first instrument.
Summary
Sources and credits
The block table, the tune and drum-loop scripts, the timing calculations and the practice items were written by Ganzaa. Frère Jacques is a traditional tune in the public domain.
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.
when green flag clicked
set instrument to ((1) Piano)
set tempo to (120)
repeat (2)
play note (60) for (1) beats
play note (62) for (1) beats
play note (64) for (1) beats
play note (60) for (1) beats
end
repeat (2)
play note (64) for (1) beats
play note (65) for (1) beats
play note (67) for (2) beats
endwhen green flag clicked
set tempo to (90)
set volume to (80) %
repeat (4)
play drum ((2) Bass Drum) for (0.5) beats
play drum ((1) Snare Drum) for (0.5) beats
play drum ((6) Closed Hi-Hat) for (0.25) beats
play drum ((6) Closed Hi-Hat) for (0.25) beats
end