AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Music that repeats

Week 14 · Unit 6: Be a musician · 3 lessons
2

Same tune, fewer steps

Computing 4: Unit 6, pp. 74–89
GoalCompare two algorithms that make the same music and say which one is better.
New words
compare · to look at two things and say what is the same and what is differentefficient · using fewer steps or less time to do the same jobphrase · a short group of notes that is played more than once
Explanation

Two different algorithms can make exactly the same music. The rain tune has a phrase of three notes: G, E and C. It is played four times. Algorithm 1 writes out all twelve notes, so it has 14 steps. Algorithm 2 writes the phrase once and uses a repeat, so it has only 6 steps. Both play the same sounds for the same time. Algorithm 2 is better, because it is shorter, easier to read and easier to change. To change a note you change one block instead of four. Choose the loop to match the job: use repeat for a set number of times and forever for music that goes on until stopped.

StepInstruction
1Start program when the sprite is clicked
2Play note 67 (G) for 0.5 beats
3Play note 64 (E) for 0.5 beats
4Play note 60 (C) for 1 beat
5Repeat steps 2 to 4 three more times
6Stop program
Worked examples
when this sprite clickedplay note67for0.5beatsplay note64for0.5beatsplay note60for1beatsrepeat4
This is Algorithm 2 as a Scratch program. The phrase is written once inside a repeat (4) block. All four rounds sound the same.
Algorithm 1: every note writtenAlgorithm 2: with repeat
Steps146
Notes played1212
Time8 seconds8 seconds
To change the first notechange 4 blockschange 1 block
Compare the two algorithms for the rain tune.
JobBest loop
play a jingle exactly 3 timesrepeat (3)
play background music until the game is closedforever
play a drum until a key is pressedrepeat until
The best loop depends on the job.
Class activity

Shorten it (with a friend): write a rhythm of 12 beats on paper as 12 separate steps, for example: tap, tap, clap, tap, tap, clap, tap, tap, clap, tap, tap, clap. Count the steps. Now write the same rhythm using one repeat. Count the steps again. Perform both versions. Do they sound the same? Which one is easier to read? Which one would be easier to change?

Practice
1
A tune plays the notes A, B and C one after another and does this 3 times. How many steps does the algorithm have a) with every note written out, b) with a repeat? Count the start and stop steps.
2
Do these two programs sound the same? Which one is shorter?
Program Pwhenclickedplay note60for0.5beatsplay note62for0.5beatsplay note60for0.5beatsplay note62for0.5beatsProgram Qwhenclickedplay note60for0.5beatsplay note62for0.5beatsrepeat2
3
Which loop fits best? a) play a jingle exactly 5 times, b) play music in the background until the game ends.
4
This algorithm should play D and F exactly 3 times, but it plays them 4 times. Which step must change and how?
StepInstruction
1Start program when the sprite is clicked
2Play note 62 (D) for 1 beat
3Play note 65 (F) for 1 beat
4Repeat steps 2 to 3 three more times
5Stop program