GoalFollow an algorithm that repeats and predict what a music program will play.
New words
forever loop · a loop that repeats its steps until someone clicks the stop buttonrepeat loop · a loop that repeats its steps a set number of timesvolume · how loud a sound is, from 0 (silent) to 100 (loudest)note number · the number that tells Scratch which note to play, such as 60 for C
Explanation
An algorithm for music lists the sounds in order. A forever loop repeats its steps again and again until someone clicks stop. A repeat loop repeats them a set number of times. To predict the music, follow the steps one at a time, as the computer does. Take care with “repeat steps 2 to 4 two more times”. The steps have already played once, so you hear them three times in all. Volume is a number from 0 to 100, and Scratch never goes above 100 or below 0. At the usual tempo, one beat lasts one second.
Step
Instruction
1
Start program when the b key is pressed
2
Play note 72 (high C) for 0.5 beats
3
Play note 67 (G) for 0.5 beats
4
Rest for 1 beat
5
Repeat steps 2 to 4 two more times
6
Play the crash cymbal for 1 beat
7
Stop program
Worked examples
Here is the school-bell algorithm as a Scratch program. The repeat block runs its blocks 3 times in all: the first time and two more times. So you hear 6 notes and then the cymbal. One round lasts 0.5 + 0.5 + 1 = 2 beats, so the whole tune lasts 3 × 2 + 1 = 7 seconds.
A forever loop never stops by itself. This program plays G for half a beat, then rests half a beat, over and over. One round takes 1 second, so in 10 seconds it plays 10 notes. It only stops when someone clicks the stop button.
The algorithm says
Steps are played
repeat once more
2 times in all
repeat two more times
3 times in all
repeat three more times
4 times in all
repeat (5) block
5 times in all
Be careful with the words “more times”. The steps have already been played once.
Class activity
Body music (with family): make an algorithm for a rhythm. 1 Start. 2 Clap. 3 Clap. 4 Stamp your foot. 5 Repeat steps 2 to 4 two more times. 6 Stop. Perform it while somebody reads the steps. How many claps do you make in all? How many stamps? Now change step 5 to “repeat forever” and let your partner say “Stop!” after 15 seconds. How is this different?
Practice
1
Follow the drum algorithm. How many times is the snare drum played? How long does the whole program take?
Step
Instruction
1
Start program when the green flag is clicked
2
Play the bass drum for 1 beat
3
Play the snare drum for 0.5 beats
4
Play the snare drum for 0.5 beats
5
Repeat steps 2 to 4 three more times
6
Stop program
Steps 2 to 4 are played 4 times in all (once and three more times). Each round has 2 snares, so the snare is played 8 times. Each round is 1 + 0.5 + 0.5 = 2 beats, so the program takes 4 × 2 = 8 seconds.
2
In the drum algorithm of question 1, step 5 is changed to “Repeat steps 2 to 4 forever”. How many times is the bass drum played in the first 20 seconds? How does the music stop now?
One round takes 2 seconds, so in 20 seconds there are 10 rounds and 10 bass drum beats. The music only stops when someone clicks the stop button.
3
Which script stops by itself, and when?
Script B stops by itself after 3 seconds (6 notes of half a second). Script A goes on until someone clicks the stop button.
4
Predict the music. Does it get louder or quieter? At what volume is the last note played, and what is the volume at the end?
It gets quieter. The notes are played at 100, 75, 50 and 25, so the last note is at volume 25. After the last change the volume is 0.