You are on page 1of 217

i

Scratch Music Projects


ii
iii

Scratch Music Projects

Andrew R. Brown and S. Alex Ruthmann

1
iv

1
Oxford University Press is a department of the University of Oxford. It furthers
the University’s objective of excellence in research, scholarship, and education
by publishing worldwide. Oxford is a registered trade mark of Oxford University
Press in the UK and certain other countries.

Published in the United States of America by Oxford University Press


198 Madison Avenue, New York, NY 10016, United States of America.

© Oxford University Press 2020

All rights reserved. No part of this publication may be reproduced, stored in


a retrieval system, or transmitted, in any form or by any means, without the
prior permission in writing of Oxford University Press, or as expressly permitted
by law, by license, or under terms agreed with the appropriate reproduction
rights organization. Inquiries concerning reproduction outside the scope of the
above should be sent to the Rights Department, Oxford University Press, at the
address above.

You must not circulate this work in any other form


and you must impose this same condition on any acquirer.

Library of Congress Cataloging-​in-​Publication Data


Names: Brown, Andrew R, author. | Ruthmann, S Alex, author.
Title: Scratch music projects /​Andrew R Brown & S. Alex Ruthmann.
Description: New York : Oxford University Press, 2020. |
Includes bibliographical references and index.
Identifiers: LCCN 2019055798 (print) | LCCN 2019055799 (ebook) |
ISBN 9780199309269 (hardback) | ISBN 9780199309276 (paperback) |
ISBN 9780199309306 (epub) | ISBN 9780199309290 (updf)
Subjects: LCSH: Scratch (Computer program language) |
Computer music—​Instruction and study. | Computer programming.
Classification: LCC ML74.4.S37 B76 2020 (print) |
LCC ML74.4.S37 (ebook) | DDC 781.3/​45133—​dc23
LC record available at https://​lccn.loc.gov/​2019055798
LC ebook record available at https://​lccn.loc.gov/​2019055799

9 8 7 6 5 4 3 2 1

Paperback printed by Sheridan Books, Inc., United States of America


Hardback printed by Bridgeport National Bindery, Inc., United States of America
v

v 
CONTENTS
Foreword  •  vii
Acknowledgments  •  ix
Introduction  •  xi

1 Getting Started with Scratch  •  1

2 A Musical Keyboard  •  9

3 A Mouse Theremin  •  19

4 Making Your Own Sounds  •  26

5 Design Your Own Instrument  •  34

6 Musical Riffs  •  48

7 Musical Expression  •  61

8 Building the Band: Rhythm Guitar and Keyboard  •  73

9 Building the Band: Drums and Bass  •  87

10 Conducting the Arrangement  •  107

11 Making Games with Sound Effects  •  128

12 Generative Music  •  146

13 Video Interaction  •  165

14 Live Coding  •  175

Glossary  •  189
Index  •  193
vi
vii

vii 
FOREWORD
by Mitchel Resnick

Learn to Code + Code to Learn


Over the past decade, there has been a surge of interest in learning to code. Around
the world, there are more and more coding classes in schools, libraries, and community
centers, and also a growing number of books dedicated to helping people learn to code.
What makes Scratch Music Projects stand out is that it’s not just about Learning to
Code, but also about Coding to Learn. The book shows how you can use the Scratch pro-
gramming language to create a diverse range of interactive music projects. By bringing
together coding and music, the book opens up a wide range of learning opportunities. As
you experiment with the example projects in the book, you’ll learn important computa-
tional concepts and problem-​solving strategies. At the same time, you’ll learn new ways
to express yourself musically—​and you’ll gain a deeper understanding of core ideas un-
derlying musical composition and expression.
I love the way that Chapter 6 uses a riff from Led Zeppelin’s “Kashmir” as an entry
point into learning about looping structures in Scratch. The same chapter introduces the
computational concept of variables to enable an exploration of the musical idea of trans-
position. The following chapter integrates the computational concept of lists with the
musical idea of canons.
Throughout the book, music and coding are woven together: music provides a mean-
ingful and motivating entry path into coding, while coding provides an innovative and
engaging way to explore, experiment, and express yourself with music.
When my research group at the MIT Media Lab developed Scratch, we hoped that
it would be used not just to learn about coding, but to learn through coding. In Scratch
Music Projects, Andrew Brown and Alex Ruthmann have provided a great example of how
to support learning through coding. As you read through the book, I hope you’ll use it as a
launch pad for your own explorations into Scratch and music—​and that you’ll share your
projects in the Scratch online community. I can’t wait to see what you create!

Mitchel Resnick
Professor of Learning Research
Director, Lifelong Kindergarten Research Group
MIT Media Lab
viii
ix

ix 
ACKNOWLEDGMENTS

T hanks to Paul Young, David Harris, and an anonymous reviewer for their comments
and suggestions for improvement. Thanks also to the Scratch developer team, es-
pecially Eric Rosenbaum, for putting up with our feature requests and bug reports as
Scratch 3 evolved during the writing of this book. We very much appreciate the willing-
ness of the profiled musicians to share their experience and wisdom via the interviews.
Of course, without the support of our families during the writing of this book, the task
would have been impossible.
Andrew and Alex
x
xi

xi 
INTRODUCTION

T his book outlines a musical journey through Scratch. Scratch is an approachable com-
puter programming environment that contains a rich set of media features, such as
music and sound, both of which are explored here.
The book features a series of independent musical projects built in Scratch and guides
readers through the processes required to create each project. Readers will encounter
coding techniques and algorithmic music processes while completing the exercises. In
general, the projects are very interactive and encourage readers to make music through
playing and composing with each task.
The book is designed for beginner coders and musicians. As such, it introduces pro-
gramming techniques and musical concepts along the way as they arise. The projects
vary in complexity, commencing with foundational exercises and moving on to more ad-
vanced topics in the final projects. While there is a development of knowledge as a reader
works the whole book, each project is quite self-​contained, and they can be undertaken
in any order.
There are some formatting conventions used in this book to make it easier to read.

•​ Numbered coding steps are used to indicate the actions required to make each project.
•​ The names of code blocks, code values, and some technical terms are written in a
mono-​spaced font so they are easily recognized in the text.
•​ Boxes are used to highlight additional music or coding information that is optional for
completing each project.
•​ Opportunities for extended musical experimentation are marked with this musical
notation symbol (♫).

There is a companion website that supports this book. The completed projects, source
code, and additional materials can be downloaded from this site.

•​ A Scratch Studio: https://​scratch.mit.edu/​studios/​25117838/​

We hope that you enjoy creating these music projects in Scratch and that you will be
inspired to use your creativity to design and code musical projects that are built on the
examples provided here.
Andrew R. Brown and S. Alex Ruthmann, 2020
xii
xiii

Scratch Music Projects


xiv
1

1 1 

Getting Started with Scratch

This project introduces the Scratch programming environment and will get you making
music. We’ll explore playing notes and drum beats, selecting sounds, and setting the
tempo and volume.

1. Start Scratch on your computer by logging into https://​scratch.mit.edu.

Click on Create to begin a new project. The interface will look similar to Figure 1.1. The
area in the top-​right is the Stage; this is where projects run. Below the Stage are the avail-
able backgrounds and sprites (more on these later). There are three tabs on the top left.
Use these to navigate to the Code, Costumes, or Sounds editors.

2. Click on Sprite1 in the lower right-​hand corner of the screen to select it and then click
on the Code tab to show the code block editor.

On the left-​hand side of the screen are the code blocks, the building blocks of your projects.
In the middle is the editing area, where the code blocks are assembled into scripts—​
also known as computer programs or code—​where costumes are created or selected, and
sounds are recorded and manipulated.

Figure 1.1
The Scratch 3 web-​
based graphical user
interface.
2

Scratch Music Projects

Scratch is a great environment for exploring and making music with sound. In Scratch
2  you use the code blocks to write a script that makes things happen. Scripts are associated
with either the Stage background or individual sprites. A new project starts with a blank
background and one Cat sprite named Sprite1.
In Scratch 2, all of the Sound blocks are located in the Sound blocks menu. In Scratch
3 the Sound blocks are still located here, but the Music blocks are loaded as an additional
set, called an extension.

3. In Scratch 3, add the Music blocks extension. Click the Add Extension button (located
in the bottom-​left corner of the interface). The extension chooser window, similar to
Figure 1.2, should appear. Click to choose the Music blocks extension.

Figure 1.2
The block extension
chooser.

The Music blocks should appear like those shown in Figure 1.3. As you can see, there
are a number of blocks that we can work with to make and create music projects.
To begin with, we will start with the play drum block:

4. Drag a play drum block from the Music blocks area to the edit window and click on
it. It should appear like the one shown in Figure 1.4.

Click on the play drum block. You should hear the sound of a snare drum each
time you click the block. See if you can perform a steady beat by clicking on the play
drum block.
The number 1 and Snare Drum text refers to which drum sound is to be played;
in this case it will be a snare drum sound. The play drum block can play many drum
sounds.

5. Click on the small triangle to the right of (1) Snare Drum in the play drum block
to reveal a long list of drum sounds. Choose one to use.
3

Getting Started with Scratch

Figure 1.3
The Music blocks.
3 

Figure 1.4
The play drum block.

Experiment with different sounds by changing the name in the play drum block
and click the block to hear them.

Making an Interactive Drum Kit


Clicking on play drum blocks to play drum sounds is cool. But what’s more fun is using
Scratch to turn your computer keyboard into an interactive drum kit! Let’s create a script
to do that.

6. Click to select the Events blocks.

Just as the Music blocks are related to making sounds, the Events block area contains
code blocks that respond to or generate actions of the user or of the scripts.
The Event block we need first is the when [  ] key pressed block, as shown in
Figure 1.5. The default setting of this block is to respond to the space key, or spacebar, on
your computer keyboard.

7. Drag a when [space] key pressed block to the script edit area.
4

Scratch Music Projects

Figure 1.5
4  The when [space]
key pressed block.

If you click on the triangle to the right of the word space within the block, you will
get a long drop-​down list of keys on your keyboard. You can use this list to assign almost
any key on your computer keyboard to this block.
When this block is placed in the Code edit window, it waits for when the space key is
pressed and then tells any blocks connected below it to start. Now it’s time to connect the
Event block to a Music block.
In Scratch, blocks are connected together to form scripts that contain several steps.
As connectable blocks are moved closer together, they “click” into place.

8. Connect the play drum block to the bottom of the when [space] key pressed
block. Blocks will “click” as shown in Figure 1.6. When blocks are close together,
they will click into place; this is how you know they are connected. Connected blocks
become a script and when the top block is moved in the edit area the whole script
moves as a group.

Figure 1.6
Connecting blocks
together to form a
script.

Press the spacebar and the drum should sound! Now let’s create some more drum
sound triggers. Let’s create the script shown in Figure 1.7.

9. Drag two more when [ ] key pressed blocks onto the Code edit window and
change the default “space” key value to the “right arrow” and “left arrow” keys in the
different blocks.

10. Next, drag and connect a new play drum block beneath each of those when [ ]
key pressed blocks and set the values of the drum sounds to (2) Bass Drum
and (6) Closed Hi-​
Hat. (Remember the play drum block is in the Music
section of blocks. The Music blocks need to be added as an extension before they
are available.)

11. Finally, set the beats value (located on the right of each play drum block as shown in
Figure 1.6) to a very short duration by clicking on the value and typing in 0.01. Setting
this value enables you to perform repeated drum beats at a faster tempo.
5

Getting Started with Scratch

Tempo is the musical term for the speed of a piece of music. Music often has a sense 5 
of pulse or beat, and tempo is the speed of these. Tempo can be measured as the
time between beats, and often described as the number of beats per minute (BPM).
The default tempo in Scratch is 60 BPM.

Figure 1.7
An interactive drum kit
script.

Now, you are all set to begin performing each drum sound by simply pressing the associ-
ated key on your computer keyboard. Can you create a drumbeat? Take some time now to
create and extend your own interactive drum kit and jam out!

Sequencing Drum Blocks
In addition to triggering and performing drum sounds manually from the keyboard, you
can also stack drum sounds within a script to create a sequence, or series, of drum sounds
that make up a drum pattern.
Scratch will allow you to create some popular drum patterns, such as a classic rock
pattern with snare on beats 2 and 4. You may like to create a beat and then rap over it, or
swap and edit beats with other Scratch users.

A sequence is the mathematical term for an ordered list of items, in this case it refers
to a series of play drum blocks that make up a musical pattern or phrase.
6

Scratch Music Projects

12. Create a new script by dragging four play drum blocks onto the Code window and
6  connecting them together matching the blocks shown in Figure 1.8.

13. Set your drum sounds to match the values in Figure 1.8. Clicking on the triangle to
the right of the drum values will bring up a drop-​down list of possible drum sounds.
In different versions of Scratch these numbers and sounds may vary. If so, choose the
closest sounds to those listed here: (2) Bass Drum, (6) Closed Hi-​Hat, (1) Snare Drum,
(6) Closed Hi-​Hat.

Figure 1.8
A four-​step drum
sequence.

Click on the set of four blocks to hear the drum pattern. You should hear a standard
rock music drum beat with the bass/​kick drum on beat 1, hi-​hat on beat 2, snare on beat
3, and a hi-​hat on beat 4.
Placing the four play drum blocks together creates an ordered list, or sequence, of
blocks. When this script is clicked, each play drum block plays its selected drum sound
for the specified duration in beats. The sequence is regular because each play drum
block is set to 0.25 (one quarter) of a beat. Once one block is finished, the next block
in the sequence will execute. Clicking on a script will run it, and if already running, will
stop it.

Take some time at this point to create your own sequences of drum beats. Try
playing around with different durations and drum sounds. See what interesting sound
combinations and rhythmic patterns you can come up with.

Choose a Drum Costume
To help the visual appearance of your project match the sounds, change the Cat sprite
image (called a costume in Scratch) to a drum kit.

14. Click on the Costumes tab (see Figure 1.9) in the top left of the interface.

Figure 1.9
The Costumes tab.
7

Getting Started with Scratch

15. To select a new costume (visual icon) for the sprite, select Choose a Costume from the
button in the lower left of the interface (see Figure 1.10). 7 
Figure 1.10
The appearance of the
Choose a Costume
button when clicked.

16. A list of icons appears. Click on the Music button near the top to filter out all but the
music costumes. Select the drum kit costume by clicking on it. The sprite will change
from the Cat icon to the drum kit shown in Figure 1.11.

Figure 1.11
The drum kit costume.
8

Scratch Music Projects

Saving Your Project
8 
In the web-​based versions of Scratch, projects are saved to your account. If you don’t
have a Scratch account, create one now. Saving your projects is useful so you can re-
load them later, share them with others, or use them as the starting point for future
projects.

17. Create a Scratch account. Click on the Join Scratch button and complete the sign-​in
details in the dialog box that appears.

18. To save your Scratch project, once logged into your account, give your project a name
by typing into the field at the top of the Scratch interface between the Tutorials and
Share buttons. Your project will be automatically saved and a Project Page for it will be
created.

You may also (or instead) want to save a copy of your project to your local computer. If
so, then locate the File menu and choose Save to Your Computer. A project file will be
downloaded to your computer’s default download location.

Summary
In this project we have introduced the Scratch environment and shown how to easily
create interesting drum beats. The code we wrote created scripts that enable drum sounds
to be played manually using the computer keyboard or as a drum kit pattern by con-
necting blocks together as a sequence. Finally, we saw how to create a Scratch online ac-
count and save a project.
9

2 9 

A Musical Keyboard

Scratch includes sounds from a variety of instruments. In this project we’ll explore how
to turn our computer keyboard into a piano keyboard. Like we did in Chapter 1 when
triggering drum sounds, we’ll use the when [ ] key pressed block for interactions
with the computer keyboard. We’ll also use the Music blocks, which are an extension that
need to be loaded using the Add Extensions button in bottom left of the blocks panel.

Playing a Note
Let’s start by adding a Piano sprite to the stage and writing a script to trigger one note.
You can see the sprite menu and a completed sprite image in Figure 2.1.

1. Start a new project, add a new sprite and select the keyboard image for it.

There is a Keyboard image like the one shown in Figure 2.1 in the Scratch costume library.

(a) Figure 2.1
a) Selecting a new
sprite b) The Keyboard
sprite.

(b)
10

Scratch Music Projects

2. Select the Keyboard sprite and add a when [space] key pressed block to the
script area. Change the value of the key to 1.
10 
3. Drag a play note Music block (remember to load the music block extension) and
connect it below the when [1]‌key pressed block. Keep the value of the beats
to 0.25.

Your script should look like that in Figure 2.2 and you should now be able to press the 1
key and hear a piano tone.

Figure 2.2
A script that plays a
note when the number
1 is pressed.

The play note block is used for pitched instruments, like the piano. We’ll see later
how to choose a different instrument. Pitched instruments, unlike drums, have sounds
with a consistent tone enabling us to play melodies, bass lines, and so on. The play
note block can specify which note to play and how long to play it. The note’s pitch (how
high or low the note is) is represented as a number, like 60, and the duration (its length)
by a number, like 0.25. What do these numbers mean?

Notes on the piano (and in general) have pitch names, like C, D, E, F♯ (♯ means sharp),
and so on. These note names from A–​G are repeated up and down the keyboard.
Notes are also referred to by MIDI number from lowest to highest, with the number
60 being the note C in the middle of the keyboard. The note after C (which is C sharp)
is 61, the next note (D) is 62 and so on. Figure 2.3 shows a section of the keyboard
starting at middle C with the corresponding names and numbers.

Figure 2.3
Names and numbers
of notes on the piano
keyboard.
11

A Musical Keyboard

Scratch includes a graphical note picker, that appears when you click in the pitch inlet
of the play note [ ] for [ ] beats object. See the note picker in Figure 2.4. Click
on the piano keys to select the pitch and use the arrow icons to move the displayed range
11 
up or down an octave. For those familiar with the piano keyboard this can be a more in-
tuitive method of selecting note pitches.

Figure 2.4
The graphical note
picker.

Beat durations in the play note block are numbers that are multiples (or
divisions) of a beat, where the value of 1 equals one beat, 2 is two beats, 0.5 is half a
beat, 0.25 is quarter of the beat, and so on. The beat speed is measured in beats per
minute (BPM) so higher tempo numbers are faster. The block set tempo to [ ]
bpm is used to change the tempo. The default tempo is 60 bpm, where 1 beat is equal
to one second on a clock.

Adding Visual Elements
Making our sound play is not very hard, so let’s add some visual interest to our program.
We will display the pitch name and animate the Keyboard when the note is played using
some of the Looks blocks.

4. To display the pitch, click on the Looks button on the left-​hand block panel and add
a think [C]‌for [1] secs block to the bottom of the script. Set it to show the
letter C and for the duration to 1 second, as shown in Figure 2.5. When the 1 key is
pressed a thought bubble briefly appears.

Figure 2.5
Add a think block to
show the letter name
of the note played.
12

Scratch Music Projects

Try the script now. Experiment with different times for the beats and seconds and try the
12  say blocks instead of the think blocks. What is the difference? What do the show and
hide blocks in the Looks panel do?
Now let’s animate the piano. To do this we can use the set size to Looks block,
but we need to make it larger then set it back to normal size to get an animated effect.

5. Put a set size to block before the play note block and put 110 in the %
(percent) field.

6. Put a second set size to block after the think block and leave the % field at 100.

The final script should look like that in Figure 2.6. When you type the 1 key all three
actions should occur in quick succession.

Figure 2.6
A script to animate
the Keyboard sprite,
play a note, and show
the pitch.

Try the project now, and make sure that when you press the 1 key a note sounds, the
think bubble appears, and the sprite is animated.

Parallel Audio and Visual Scripts


This script works quite well, but try to play notes in quick succession. You may notice that
the triggering is not very responsive at high rates. This is a subtle problem but underpins
an important principle. In a script the blocks are actioned in order from top to bottom.
Blocks that take some time, like the play note block that takes 0.25 beats, and the
think block that takes 0.5 seconds, hold up the sequence of actions for that duration.
This means that the script takes some time to complete, and you can’t run it again until it
is done. Another way to put this is that pressing the key again does not restart the script
immediately—​you have to wait until it’s finished, and then start it again. So it’s helpful
to make key presses trigger scripts that take a very short amount of time, so you can re-
peat them quickly.
In order to speed up actions and their retrigger we can divide the actions into several
scripts and run them at the same time—​in parallel. For example, in Figure 2.7 the sound
13

A Musical Keyboard

and animation actions have been separated into two scripts, each of which is triggered by
the same key press.

7. Rearrange the blocks into two scripts as they appear in Figure 2.7.
13 

Figure 2.7
Two scripts each
triggered by the
same key.

Press the key now to trigger the note. You should find that it is more responsive to
rapid retriggering.

Duplicating Scripts
So far we have one note playing. Clearly it would be good to have more. We can duplicate
the scripts and change some values in the copies to create a second note trigger.

8. Right click on each script and select “duplicate.” Drag the copied script to a clear
location and click to keep it there. Change the when [ ] key pressed key to 2,
the play note pitch to 62, and the think block’s text field to D.

Keys 1 and 2 should now trigger different notes.

9. Repeat this process to create scripts for each of the numbers 3 to 7 so they trigger the
pitches of the remaining white notes shown in Figure 2.3.

Explore playing some melodies on your new seven-​note keyboard. If you need more notes,
you can add them. What melodies can you play? Can you play more than one note at a
time? Add an extra note for the next C, MIDI pitch 72. This should increase the number
of melodies you can play.
14

Scratch Music Projects

Adding a Background
We now have our musical computer keyboard. But let’s add a visual finishing touch by
14  placing a background onto the Stage as shown in Figure 2.8.

10. To add a background image to the Stage from Scratch’s image library, click on the
Backdrops tab in the bottom right side of the screen. Click the Choose a Backdrop
button in the bottom-​left part of the screen. Search for and select Theater 2. Select
the stage and return to the work environment. Position the piano on the stage to look
like it’s positioned correctly.

Figure 2.8
The Stage with its new
background image.

Another variation for the musical keyboard project is to change the instrument that
is played. You can change instruments with the set instrument to block. Create
a new script to change the instrument when the project is started like that shown in
Figure 2.9.

11. Under the Events category select and drag a when [green flag] clicked block
to the Scripts area, and attach a set instrument to Music block below it, and
choose a new instrument from its drop-​down menu.

Figure 2.9
A script to set a new
instrument sound.
15

A Musical Keyboard

Choose different instruments and click the Stop and green flag buttons to restart the pro-
ject to activate the choice. Explore the different instrument sounds. Which ones do you 15 
prefer? Do they respond differently? Can you find a different sprite image to match the
instrument you have selected?

Summary
In this project we have seen how to play notes on particular instrument sounds with
Scratch. Our scripts have triggered these notes with key presses on the computer key-
board. In addition, we have seen how images can be added to sprites and to the Stage to
make our programs look more interesting, and how these can be modified to become an-
imated as we interact with the program.
16

16 

Interview with Eric Rosenbaum
Eric Rosenbaum is a musician and programmer. He currently works with the Scratch Team
at MIT Media Lab, which includes work on music and sound. Previously, he worked with
Google Creative Lab and NYU Music Experience Design Lab on music learning technology
projects. In 2015, he completed a PhD at MIT Media Lab in the Lifelong Kindergarten
group with a dissertation entitled Explorations in Musical Tinkering. Before his time at
the Media Lab, Eric worked at MIT Teacher Education Program, creating learning games;
Concord Consortium, creating molecular dynamics simulations for kids; and Six Red
Marbles, creating animations for music education. He holds a master’s in technology in
education from Harvard Graduate School of Education, and a bachelor’s in psychology
and mind/​brain/​behavior from Harvard College.

How did you get into combining computers and music?


In college, I was studying how sound is processed in the brain, and I was also starting to
learn programming, and at the same time I was playing trombone with my friends in a
funk band called FinkFankFunk. So, late one night in the lab, when I was supposed to be
analyzing monkey sounds, I realized I could use the same programming tools to synthe-
size sounds instead. I made some beeps and bloops using Matlab. That set me on a path
of tinkering with music technology. A few years later I was especially inspired by working
with Professor Jeanne Bamberger, who was thinking deeply about both how children
think about music, and how we can create technological tools that take advantage of
everyone’s musical intuitions. Working with her, I  created a set of electronic musical
building blocks, and tried them out with children. Over the next few years, I built things
like an improvisation machine controlled by skin conductivity for museum visitors, a
little 8-​bit synthesizer box for live performance (accompanying a reading of the Epic of
Gilgamesh), and a musical LEGO robot hand that traveled the world in a museum exhibit.
Later, when I did my PhD at MIT Media Lab, I followed these same obsessions, and called
my dissertation Explorations in Musical Tinkering. During that time, I prototyped a music
app for the iPad that I called MelodyMorph, inspired by Jeanne Bamberger’s ideas. I also
co-​invented the Makey Makey invention kit that lets people create (among other things)
a piano out of bananas and high-​five triggered human synthesizers.

What are you doing now in computers and music?


I’m part of the team that develops Scratch, and my work includes designing and coding
lots of music-​related features. These have included curating new sound effects and sound
loops for the sound library (“dun dun DUNNNN!”), building the audio engine for Scratch
and adding the new pitch and pan effects, implementing the new version of the music
17

A Musical Keyboard

extension, and adding selection-​based editing features to the Scratch sound editor. It’s
so amazing to get the chance to see kids in person and in the online Scratch community
make music and sound using the features I’ve helped to create. I’ve also been lucky to
17 
have the freedom to do some experiments, including playing with new sound effects for
the sound editor (what would a “sparkles” effect sound like? Or “alien”? What else could
we add?), and designing new extensions related to sound and music (how could we ex-
pand the possibilities for sensing sound, or even synthesizing sound, with new blocks?).
Even if those weird ideas don’t make it into Scratch in the end, that kind of prototyping
always informs our design process and helps generate new ideas.
I also have some hobby projects in computer music that I  do for fun. I’ve been
exploring real-​time transformation of ambient sounds into musical landscapes (this gets
weird!). I’m also just getting started with using machine learning techniques to generate
new sounds. . . . Who knows where that will take me?!

Where can we find examples of your recent and current work?


The best place to go is my website, http://​ericrosenbaum.com. There you can find infor-
mation about my work with Scratch and Makey Makey. It also includes other projects,
like my iOS app Singing Fingers, which lets you finger paint with sound, and play back the
sound by touching your drawing. You can also find the projects I did with Google Creative
Lab: Chrome Music Lab, which is a set of browser-​based interactive music experiments
I helped to create, and Giorgio Cam, which uses computer vision to generate a song in
real-​time with lyrics about the objects you see around you. You can also check out my pro-
file on Scratch, where I’m known as ericr (scratch.mit.edu/​users/​ericr), where of course
I have lots of music projects, including an alliterative talking centaur band-​name gener-
ator (“Ladies and gentlemen . . . George and the Gym Gnome!”), an interactive beatbox
quartet, and my whole-​tone scale toy the “Monkifier.”

What role does coding play in your music activities?


I do some music-​making without coding, like improvising and making up songs at home
with my wife. I also play in an Afrobeat band. One area where these things cross over
a bit is in a performance series I  created called the Jazz Technology Workshop. For
those events, I bring together improvising jazz musicians with people who have created
music technology experiments, and combine them, live. I once even used it to try out
the auto-​conductor, a Scratch project I  created that conducts a group of improvisers.
We’ve also had a jazz drummer improvising with a mathematical rhythm synthesizer,
an upright bass player jamming with people generating sounds through measurements
of skin contact, and a whole group of musicians interacting with a real-​time calligraphy
visualization.

What tips or advice would you give young coders working with music?
It’s so important to find and trust your own voice. It took me many years to under-
stand that I didn’t need to try to make things that I thought I was supposed to make, or
what I thought other people wanted. Gradually I started being able to listen to my intu-
ition more. You can take those crazy ideas, even if they’re so weird it feels impossible to
18

Scratch Music Projects

describe them to other people, and trust that they’re worth putting energy into. If you
put that kind of energy into what you make, people will feel it and respond to it.
I’d also say: share early and often. Try showing what you’re making to people before
18 
you feel ready, in person or online. The enthusiasm and ideas you can get that way are
incredibly valuable, and often take projects in directions you might never otherwise find.
And try to keep learning new tools. There are so many programming languages and
frameworks and hardware platforms out there, it can be hard to choose, but I find that
each new tool that I learn expands how I think—​we often think through our tools, and
sometimes ideas come from having these new ways to think.
19

3 19 

A Mouse Theremin

In this project, we will explore how to control music using movements with the mouse or
trackpad. Gestures, such as hand and arm movements, are often used to control music.
We saw in Chapter 1 that key presses could be used to trigger sounds. What we learn
about mouse movements in this project can be applied to other sensors as well.
The theremin was an early electronic musical instrument that used two metal
antennae that sensed hand gestures to control the frequency and loudness of a simple
tone. It was named after its inventor, Léon Theremin, who developed it in the 1920s.
Figure 3.1 shows Alexandra Stepanoff playing the instrument in 1930. There are more
modern versions of the theremin available today, and in this project we will build a “dig-
ital theremin” that is inspired by the original.

Figure 3.1
A theremin being
played by Alexandra
Stepanoff in 1930.
A microphone is
nearby on a stand
to record the
performance. (IMAGE
is in the public
domain–​–​ https://​
commons.wikimedia.
org/​wiki/​File:Theramin-​
Alexandra-​Stepanoff-​
1930.jpg).
20

Scratch Music Projects

Our version of the theremin will consist of two imaginary antennae, one to the right
edge of the Stage and one along the bottom. When a hand-​object, or the mouse, is closer
to the right of the stage, the sound will be louder and when closer to the bottom it will
play a lower tone.
20 

Tracking Locations on the Stage


Let’s start by working out the coordinates of the Stage area.

1. Move a sprite around the Stage and observe the changing X and Y location values
displayed below the Stage.

A sprite is an object that appears on the Stage. A sprite has an image and can have
scripts associated with it. New sprites can be added to your project from the sprite
library, or can be based on an image you draw or import.

If the Stage is empty, then add a new sprite. For fun, you may want to let Scratch
pick a sprite for you using the “surprise” option in the Choose a Sprite menu, as shown
in Figure 3.2.

Figure 3.2
The sprite menu
includes an option
to make a surprise
selection.

Move the sprite by dragging it with the mouse. Its position on the stage has two
values: an X axis value (left-​right) and a Y axis value (up-​down) that align with the center
of the sprite. The center of the stage is position 0,0 and values increase (are positive) or
decrease (are negative) away from the center. The location values (coordinates) on the
stage range from −240 to 240 on the X axis and from −180 to 180 on the Y axis, as shown
in Figure 3.3.
21

A Mouse Theremin

Figure 3.3
The coordinate space
for the Stage.

21 

Okay, let’s use a script to get the mouse location and move the sprite to that location.
Select the sprite so we can create the script shown in Figure 3.4.

2. From the Motion blocks menu drag a go to x: [x value] y: [y value] block
to the scripts area.

3. From the Sensing blocks menu drag a mouse x and a mouse y block into the
corresponding slots in the go to block.

The shape of the mouse x and mouse y blocks are rounded rectangles. This indicates
that they fit into spaces in other blocks that have the same shape. As you look through
the blocks in Scratch you will notice they have various shapes. Pay attention to the
shapes as this is a visual hint about how they can fit together with each other.

4. Surround the go to x: [x value] y: [y value] block with a forever block
from the Control blocks menu.

5. Place a when [green flag] clicked block from the Events blocks menu on top
of the forever block to start the script.

Start the script by clicking on the flag icon and then as you move the mouse around
the screen the sprite should move to follow the mouse position.

The forever block creates a continuous loop that repeatedly calls the blocks inside
it. It is useful for when you want a behavior in your program that is constantly running.
The forever block is one of several repeat blocks in Scratch. Remember that the
blocks in a script execute one after the other in sequence, so the repeat blocks enable
the blocks inside them to be executed several times over and over in a loop. Some
repeat blocks can specify the number of loops (repeats) to do, while the forever
block loops continuously until you stop the project.
22

Scratch Music Projects

Figure 3.4
A script that makes a
sprite follow the mouse
location.

22 

Mapping the Mouse to Music


Now that we can use the mouse locations to move the sprite, the next task is to use the
mouse location to change the music. Let’s start by setting up a basic looping note player
using the forever block.

6. Click on the Stage edit area on the right of the Scratch interface, shown in Figure 3.5,
to select it.

Figure 3.5
The Stage edit area of
the Scratch interface.

With the Code tab selected, the Code edit area should be blank. Let’s add a script,
shown in Figure 3.6, to the stage.

Code scripts in Scratch are associated with either the stage or a sprite. We generally
add code to the stage for overall or background behavior that affects the whole pro-
gram and add code to sprites when we want to affect just their behavior.
23

A Mouse Theremin

7. Start with a when [green flag] clicked block and add a set instrument to
block and choose instrument 12 (flute).

8. Add a forever block and add set volume and play note blocks inside it.
23 
Figure 3.6
The starting script for
the mouse theremin.

When you start this script, a repeated note should be heard. A flute tone is used as
it is somewhat similar to the pure tone of the theremin. At present moving the mouse
does not create any change, but we’ll work on that next. Click the Stop button to halt the
playback.
There are two features of the music that the mouse position will control, the note
number (change the pitch up and down) and the volume (change the loudness). We will
assign the mouse x value (left-​right) to volume and the mouse y value (up-​down) to
pitch. The mouse x and mouse y blocks are Sensing blocks.
Assigning values, like the mouse position or volume, is common when coding and is
called “mapping the values.” However, the mapping is not straightforward because the
values from the mouse location are not in the correct range for pitch and volume. So we
will do some math to make this work.
The mouse x values range (left to right on the stage) from −240 to 240 (a total of
480 steps). The volume range is from 0 to 100 (a total of 100 steps). A formula to approx-
imately align these ranges is: volume = (mouse x + 240) /​ 5.

9. Create a script for the volume mapping formula using the + and /​blocks from the
Operator blocks menu, as shown in Figure 3.7. Drag and drop the blocks inside each
other the same way we did it to create the blocks in Figure 3.6. To insert a block, align
its left edge with space in the outer block; the space will highlight, and the dragged
block can be let go so it clicks into place.

The mouse y values (bottom to top of the stage) range from −180 to 180 (360 steps in
total). The effective MIDI pitch range for the flute sound is from 40 to 100 (a total of 60
steps). A formula to map these ranges is: pitch = mouse y /​6 + 72.
24

Scratch Music Projects

Figure 3.7
Formula for mapping
mouse x to volume.

24 

10. Create a script for the pitch mapping formula by using the /​and + blocks from the
Operator blocks menu, as shown in Figure 3.8.

Figure 3.8
Formula for mapping
mouse y to pitch.

11. Insert the mouse y formula into the play note block and the mouse x formula into
the set volume block. See Figure 3.9.

12. To achieve a faster musical response to mouse movements, shorten the play note
block beats duration to 0.25.

Figure 3.9
The complete mouse
theremin script.

The final script should look similar to that in Figure 3.9.

Start the project by clicking on the green flag button and explore how mouse movements
vary the musical attributes. To ensure that you fully understand the mapping formula,
take some time to experiment by altering some values to see the effect. Try some dif-
ferent instruments to see what other sonic outcomes can be created.
25

A Mouse Theremin

To add visual interest to the project add a new backdrop to the Stage. This might be
one of the library images or you might like to use an image of the theremin with some
text instructions overlaid. Figure 3.10 shows a stage background behind the default Cat
sprite.
25 
Figure 3.10
Cat sprite on a stage
backdrop.

Summary
In this project we have seen how the mouse location can be used to control music. In par-
ticular we’ve built a project, inspired by the theremin, where the pitch and loudness of
sounds are controlled by the vertical and horizontal mouse position on the Stage. Along
the way we’ve explored how to convert, or map, the mouse coordinate values to the ap-
propriate ranges for note pitch and volume.
26

26 
4
Making Your Own Sounds

In the previous projects we have played drum sounds and notes from instruments. In this
project we will explore playing sound and music files from the Scratch library and else-
where, and even recording your own sounds to use in projects.

Play a Sound from the Scratch Library


Scratch contains a selection of inbuilt sounds that can be added to your project.

1. Click the Sounds tab above the Scripts blocks.

2. Click on the Choose a Sound icon (located at the bottom left hand side of the
screen) to choose the Dog 1 sound from the library.

A new icon, like that in Figure 4.1, will appear for this sound in the list of currently loaded
sounds.

Figure 4.1
The Dog 1 sound icon
appears in the Sounds
tab when added.

The next stage is to add a Dog sprite to the project and write a script to link the dog
sound to it.

3. New projects usually include a Cat sprite. Delete the cat sprite by clicking on the cross
(X) in the top right corner of its icon (similar to the one shown in Figure 4.1) in the
Sprites area or by right-​clicking on the sprite icon and choosing Delete.
27

Making Your Own Sounds

4. Add a Dog 2 sprite. Click the Choose a Sprite icon and select Dog 2 from the
sprite library. The sprite list is quite long so it may help to select the Animals category
to show a smaller set of sprite images. Alternatively, type “Dog 2” into the search
panel.

5. Create the Play Sound script as shown in Figure 4.2. Select the Dog 2 sprite and click
27 
on the Code tab to show the script area where you can add blocks. Add the when
this sprite clicked block and attach below it the start sound (or play
sound until done) block. In the start sound block choose dog1 as the sound to
play.

Figure 4.2
A sketch to play a
sound when the sprite
is clicked.

The start sound block will begin the sound then immediately proceed on to the next
block while the sound plays. The play sound until done block will pause the script
while the sound plays and then proceed onto the next block.
Click on the Dog 2 sprite on the stage to trigger the sound.

Animate with Costume Changes
Some sprites include several variations of their image, called costumes. The costumes for
the Dog 2 sprite are shown in Figure 4.3 and you can see them in the Scratch editor by
clicking on the Costumes tab. In some cases, like with the Dog 2 sprite, these costumes
are designed to allow simple step-​frame animations by looping through the costumes.

Figure 4.3
The three costumes in
the Dog 2 sprite.

Add an animation loop to the existing sound playback script. The full script is shown
in Figure 4.4.
28

Scratch Music Projects

Figure 4.4
The sketch including
animation blocks.

28 

6. Add a repeat block at the end of the script and change the number of repeats to 9
(three times through the three costumes).

7. Insert a next costume block inside the repeat loop. This is found under the Looks
block menu.

8. Shift the position of the sprite slightly to simulate walking by inserting a move X
steps block inside the loop. This block is located under the Motion block menu. The
default distance of 10 steps should be fine.

9. To control the speed of animation, insert a wait X secs block (located in the
Control block menu) inside the repeat loop and change its duration value to 0.1.

The wait block suspends the execution of the script for the specified period of time, or
until some specified event has concluded. This can be useful for managing the timing
of events in a project or for ensuring that events do not overlap. Because the default as-
sumption in many programming languages is for code to execute as fast as possible, the
control of time can be an important feature when we don’t want this default behavior.

Click on the Dog 2 sprite on the stage to hear the bark and see the dog walk.
Let’s add a person sprite to the stage so that the dog appears to be running toward
someone.

10. Click Choose a Sprite and select the Kai character. Place the sprite on the right of the
stage so the dog can run toward it from the left.

Take some time to explore this script. See how the animation speed changes when
you alter the wait time. What happens if you remove the wait X seconds block? Can
you alter the repeat times to make the dog walk animation last longer? Can you make the
dog bark twice when clicked?
29

Making Your Own Sounds

Add Sound Effects
Scratch includes blocks that can alter sounds in various ways. We will use this feature to
vary the playback slightly in order to provide variations in the sound, like there would be
in real life.
29 
11. Change the pitch of the sound. Insert a set [pitch] effect to block in the script
just prior to the start sound block.

Listen to how this affects the pitch of the sound. Try different values for the change
amount. A value of 0 (zero) is the normal playback speed. Higher numbers increase the
pitch, negative numbers (e.g., −50) lower the pitch. Vary this value to change the bark to
one that sounds like a large or small dog.
In order to make subtle changes to each bark we can randomize the pitch change
effect.

The random operator chooses a value between the minimum and maximum value
each time the block is evaluated. The value chosen is unpredictable (unknown in ad-
vance), like the roll of dice for a board game. Randomness can be used to add variety
and surprise to projects.

12. Insert a pick random block into the set [pitch] effect to value field. Set
minimum and maximum values for the random number, like those shown in
Figure 4.5.

Figure 4.5
Add random
changes to a sound’s
playback pitch.

With this set effect to block in the script, the sound playback will vary slightly
on each click of the sprite. Use smaller and larger values for the pick random block
if you wish to exaggerate the effect. Try varying the minimum and maximum random
values to those you think sound best.

Record Your Own Sound


As well as using sounds and instruments from the library, Scratch enables the recording
and editing of original sounds for projects. In this section we show how to record sounds.
In the next section we explore ways of editing them.
Let’s record a sound to the Kai sprite we added earlier. Record your voice calling the
dog or whistling to it.

13. Click on the Kai sprite in the Sprites area of the Scratch interface, then click on the
Sounds tab to reveal Scratch’s sound editing area.
30

Scratch Music Projects

14. Hover over the speaker icon in the bottom-​left side of the screen and then click on the
Record microphone icon as shown in Figure 4.6.

Figure 4.6
Selecting Record.

30 

15. When ready, click Record, then make your dog call, and press Stop. A pop-​up
message may appear, asking for permission to use the microphone on your computer;
allow the use of the microphone.

16. Click Play to listen back to the recording. If okay, press Save. If not, press Re-​record
and repeat the process. Don’t worry about excess gaps either side of the recording;
these can be edited out.

Edit a Sound
The saved sound will be called recording1 by default. Click on the icon for re-
cording1 and the audio waveform will look something like Figure 4.7 where “fatter”
sections represent louder sounds.

Figure 4.7
An audio waveform
in the Scratch audio
display.

We need to “top and tail” the recording. That is, to trim the excess before and after the
voice audio section that we want to keep.

17. To edit the waveform, click and drag across the waveform. Start and end locators will
appear.
31

Making Your Own Sounds

18. Drag the edit points from either end as shown in Figure 4.8 to isolate just the section
of recording you want to keep. Press Play to audition the final result.

Figure 4.8
A waveform with trim
regions on either side. 31 

19. Click the Copy to New icon to create a new sound containing just the selected region.
Selected the newly created sound and rename it Dog Call by typing in the Sound
name box.

Now it’s time to assign the recorded sound to the Kai sprite character with a script.

20. Select the Kai sprite icon. Return to the scripts area by clicking on the Code tab.

21. Let’s create a script to play the sound. Add a when this sprite clicked block,
then a start sound block and change the sound to the Dog Call recording.

22. To provide some visual reinforcement, add a say [text] for [ ] sec button and
change the text to reflect the recorded voice, for example “Here, Puppy,” and the
duration to 1 second. The script should look like that in Figure 4.9.

Figure 4.9
The Kai call script.

Now clicking on the Kai character on the stage should play the voice recording and
clicking on the dog should play a bark and move the dog sprite. Play with the program to
make sure everything works as expected. Drag the dog sprite to the left of the stage as
required to reset the scene.
32

Scratch Music Projects

Remember that you can add a set [pitch] effect to block in the voice sound
playback script to change the pitch to be higher or lower. This might make the voice sound
better match the image.

32  Figure 4.10
The visually complete
project with playing-​
field backdrop.

Complete the scene visually by adding a backdrop to the stage.

23. Click the Choose a Backdrop button and select the playing field image. The stage
should now look similar to Figure 4.10.

Saving and Loading Sounds and Music


As a final touch, let’s add some background music to the scene. A great source for free
music and sound is http://​freesound.org. You will need to create a free account there to
download samples. You could also create your own music if you prefer. It will work best
to have a short musical fragment that can loop, because longer tracks take up much more
memory space in the project.

24. Click in the Stage area of the Scratch interface. Choose a background music track
and load it into the project. Go to the Sounds tab, select Upload Sound from
the Choose a Sound menu, as shown in Figure 4.11, and locate and load your
background music file.
33

Making Your Own Sounds

Figure 4.11
Selecting Upload
Sound from the
Choose a Sound menu.

33 

25. Create a looped playback script for the sound on the Stage. Select the Stage icon.
Choose the Blocks/​Scripts tab. Add a when [green flag] clicked block.
Connect a forever block below it. Insert a play sound [ ] until done block
inside the forever loop and choose the music sample from the block’s drop-​down
menu.

The script should look similar to that in Figure 4.12. When you click the project’s
Start flag, the music should continue until you click the Stop icon.

Figure 4.12
Loop background
music forever.

Summary
In this project we have seen how to add music and sounds to enhance an animated scene.
The project explored how to record and edit our own sounds in Scratch. The project trig-
gered this music and these sounds as part of an animated scene where a person and dog
were playing on a sports field. The use of audio elements was critical to making an en-
gaging interaction between the characters in the scene.
34

34 
5
Design Your Own Instrument

In this project we will create a virtual ukulele and add scripts to strum it with the mouse.
Along the way we will encounter a few new features of Scratch and introduce some new
scripting blocks including the important if [ ] then [ ] statement.

Sprite Orientation and Size


Load an image of a ukulele and resize it to appear as shown in Figure 5.1.

1. Download the ukulele image from this location: http://​explodingart.com/​smp/​images/​


5.1_​ukulele_​noun_​23306_​edited.png

Figure 5.1
The ukulele image
(based on a public
domain image by
Bernardo Carvalho).

2. Start a new project in Scratch and select the default Cat sprite. Navigate to the
Costume tab and select Upload Costume from the Choose a Costume button in the
bottom left corner of the Scratch interface, as shown in Figure 5.2. Select the ukulele
file from your hard drive. If you can’t use the ukulele image, select the Guitar—​a
costume from Scratch—and rotate it to direction 165.

3. Rename the sprite Ukulele, and delete the “Cat” costumes.


35

Design Your Own Instrument

Figure 5.2
Choose the option to
upload a new costume.

35 

Play the Instrument if the Mouse Is Touching It


We can “play” the instrument with the mouse, as though we were strumming it. A number
of methods for doing this will be introduced, each of them getting more sophisticated.
Let’s start with the simplest method: clicking on the sprite to trigger a sound by adding
another script to the Ukulele sprite.

4. Select the Ukulele sprite. Navigate to the Code tab. Load the Music extension blocks.

5. Start a new script with a when this sprite clicked block and after it add a set
instrument to music block and choose the Guitar sound. This will be quite suitable
for a ukulele. Add four play note music blocks with the pitch and duration values
shown in Figure 5.3.

Figure 5.3
Simple play script.
36

Scratch Music Projects

Clicking the ukulele image on the Stage should now play a chord. Give it a go.
While this works, it is not very expressive, so we will explore ways to use the mouse-​
drag gesture as though we were strumming the instrument.

6. To continue, delete the script we just created in Step 5.

36 
Deleting blocks from the Scripts area can be done in three ways. 1) Right-​click on the
block(s) and select Delete. 2) Drag the block(s) to the left, returning them to the Blocks
list area. 3) Click on the block or script and hit your computer’s Delete key. To delete
ALL blocks, right-​click on the Scripts area background and select Delete N Blocks
-​where N is the current number of blocks, or click on the blocks and press Delete on
your keyboard.

Draw Sprites for Strings


Thus far the ukulele has played a chord consisting of all four open strings. However, the
ukulele’s strings can be played independently, not only strummed to play a chord. We will
now add this capability to our project. To do so we need sprites for each string. These do
not exist in the sprite library, but fortunately Scratch allows us to create our own sprite
images in the costume editor, shown in Figure 5.4.

Figure 5.4
The costume editor in
Scratch 3 with a drawn
string image.

Notice that the strings in the ukulele image are black lines. We will create sprites that
mimic these and position them over the current image. So, we need to create a string
sprite that is simply a black line of the correct width and length.
37

Design Your Own Instrument

7. Choose the Paint option from the Costume menu to add a new sprite to the project.

8. Change the sprite name to “String 1” by editing the name in the Sprite area above
the sprite icons.

9. Select the new sprite icon, go to the Costumes tab, delete all but one of the
costumes, and click on the image for the final costume and delete it. Name the new
37 
costume String 1.

10. Draw a string in the costume editor using the line tool as shown in Figure 5.4 to match
the top string of the ukulele. Set the width of the line to 3 pixels and adjust the length
of the line to match the existing string length on the ukulele image.

The line need only be a simple horizontal line in the center of the image area. Use the
+ and −​magnifying buttons to scale the drawing area as required. Use the Select (arrow)
tool to resize and reposition the line. Make sure the drawn line intersects with the center
of the drawing area, marked with a small gray dot. You can monitor the drawing as it
appears on the stage. The image will appear in the default color, black.

11. Position the String 1 sprite over the associated string on the ukulele. Be careful to click
on the string rather than the ukulele when moving sprites around.

If you click on the ukulele it might hide the String 1 sprite. If this happens, then se-
lect the String 1 sprite, go to the Code tab, add a go to [front] layer block to the
code area and click on it to bring the String 1 sprite in front of the ukulele.
Later we will repeat this sprite creation process to create the remaining strings of
the ukulele. But for now, we’ll move on to playing the first string so that we complete the
processes for this one string to make it simpler to duplicate the others.

Play Strings
To play the string, we will use the touching [ ]? command in the Sensing blocks.

12. Add the script shown in Figure 5.5 to the string.

Figure 5.5
Script to play sound
when mouse touches
the string.
38

Scratch Music Projects

In the script in Figure 5.5 the set instrument block specifies the guitar sound.
The forever block continually loops. On each repeat the if [ ] then [ ] block uses
touching [  ]? to check if the mouse is over the sprite image. If so, then the play
note block is started.

38  The if [ ] then [ ] block is a conditional statement. That is, things happen when
particular conditions are true or false. This is an important and useful feature for
projects. The condition can be specified using one of the angle-​ended blocks, such as
touching or mouse down. There can be multiple conditions that must be met, such
as this and that or this or that. Scratch provides angle-​ended operator blocks for this
purpose. Finally, the if [ ] then [ ] else block provides two outcomes: one if the
condition is true, the other if not. The else actions are taken if the condition is not true.

Restart the project and drag the mouse across the string image to hear the new single
string sound being triggered.
You may have noticed that if the mouse is moved quickly over the string the sound
may not trigger, and if the mouse is held on the string it will repeatedly sound. We will re-
solve these issues later in the chapter. For now, let’s complete the code for our first string.

A Better Strumming Script
Using the scripts so far, strumming works best at slow speeds. Sometimes strings are
missed if the mouse position jumps over them when moved rapidly. In this section we
introduce a script that solves this problem.
A new variable called previous Y will store the vertical mouse position and an
additional script will be added to the Ukulele sprite. This script is shown in Figure 5.6.

13. In the Code tab click on the Make a Variable button and name the new variable
previous Y and choose “For all sprites.”

14. Add a script to the Ukulele sprite. Starting with when [green flag] clicked, add
a forever block and inside that add set [previous Y] to [mouse y] as shown in
Figure 5.6. The mouse y is a Sensing block.

Figure 5.6
The Ukulele sprite
script that sets the
variable previous Y
to the current mouse
y location.
39

Design Your Own Instrument

The script in Figure 5.6 uses a forever loop and a set [ ] to [ ] Variables block
to continually update the variable previous Y to the y position of the mouse. With the
previous Y variable showing on the Stage, start the project. You should see its value
updating as you move the mouse up and down the Stage area.

Variables hold a value. The value can be read or changed by scripts. Variables are 39 
“global,” that is, they can be accessed by scripts anywhere in a project. Variables are
created in the Data section of the Blocks (Scripts) area. When created there is a tick
box next to the variable; when this is ticked the current value of the variable is dis-
played in the top left corner of the Stage.

We next modify the when [green flag] clicked script to add code that checks for
upward and downward mouse movement over the string location. The complete strum-
ming script for String 1 is shown in Figure 5.8.

15. Update the code in the String 1 sprite as shown in Figure 5.8. Two if [ ] then
[ ] loops are inserted into the forever loop. The mouse and sprite y positions are
used in the < (less than) and > (greater than) blocks to check when they cross over.

The y position of the sprite also appears in the sprite information area of the Scratch
interface, as shown in Figure 5.7.

Figure 5.7
The Y position value
of a sprite is in the top
right corner of this
figure.

The script in Figure 5.8 starts when the green flag is clicked. The go to [front]
layer block ensure that the string is on top of the ukulele image. The forever block
repeatedly checks for mouse upward or downward “strumming” gestures that pass
the string position. Mouse movements are detected by the two nested if [  ] then
[  ] statements. These are the heart of the “better” strumming process. The first set
of if [  ] then [  ] block checks to see if the mouse has just passed over the string
from above to below. It looks for a downward mouse gesture by seeing if the current
mouse y position is less-​than (<) the line’s y position AND if the previous mouse po-
sition (previous Y) was greater-​than (>) the line’s. The play note blocks specify
40

Scratch Music Projects

Figure 5.8
The “better” string
strumming script.

40 

a short duration, 0.1 beats, so that the interaction is reasonably responsive. This tech-
nique reduces the problem of missing the string as the mouse passes quickly over it (in
an upward direction). The second if [ ] then [ ] block checks, similarly, for down-
ward motion.

This script is somewhat complex, so take some time to explore it and understand its
nuances. Especially tricky are the nested if [  ] then [  ] blocks and nested oper-
ator blocks (which include <, >, =, and, not). At times the logic of these can take some
deciphering. Also, explore the expressive musical potential of the final string plucking
simulation.

Animate the String
To add more visual interest to the project we can add a simple string animation to accom-
pany the sound. A background image will also enliven the look of the stage.

16. In the String 1 sprite, extend the script as shown in Figure 5.9. Use two change y
by blocks within each if [ ] then [ ] block to shift and later return the string
image location slightly (by one pixel, then back) so it looks like the string has been
plucked.

17. Use the Choose a Backdrop button in the bottom-​right corner of the Scratch interface
to select the Concert background. Hide the previous Y variable from the Stage
by deselecting the checkbox next to the variable name in the Code tab. The Stage
should now appear similar to that shown in Figure 5.10.

So far, we have a method of performing on one string of the ukulele instrument. Now
we are ready to duplicate these processes for the remaining strings for a richer perfor-
mance experience.
41

Design Your Own Instrument

Figure 5.9
A script that animates
the string as it plays
the sound.

41 

Figure 5.10
The Stage with
the Concert
backdrop added.

Create the Remaining Strings
A ukulele has four strings. We can duplicate String 1 to create sprites for the remaining
three strings. The position values and sounds will have to be changed for each string.
Strings on the ukulele can be tuned to various pitches. For this project we will follow
the standard tuning in C shown on the staff below where the top string is tuned to G (67),
42

Scratch Music Projects

the next to C (60), the next to E (64), and the fourth string to A (69). These pitches are
shown as notation in Figure 5.11.

Figure 5.11
The open string
tunings for a ukulele.

42 

To store the pitch values for each string we will use a list. In Scratch, lists are created
in the Variables section of the Code tab using the Make a List button.

Lists hold a sequence of values—​usually numbers or words. In this case we will create
a list of four values representing the pitch of each string on the ukulele. The data in
lists, like variables, can be changed. Lists can be of any length and items can be added
to lists or deleted from them. Lists ’ items can be accessed by scripts and items can be
stored in lists and read as required.

18. Create a new list with the Make a List button; name the list pitches. A new list block will
be added to the Blocks area. Like variables, lists have a check box that enables them to
be displayed on the Stage. This is on initially but, if not, make sure the box is checked.

19. Add pitch values to the pitches list. Use the + (plus) button in the list box on the Stage
to create a new item in the list and type 67 into it. Press return, or + again, to add a
second item to the list and type the value 60. Add two more items with the values 64
and 69. We now have the string pitch data in this list, like that shown in Figure 5.12.

Figure 5.12
The values in the
pitches list.
43

Design Your Own Instrument

Next we need to update the play note blocks so they access the values from the
pitches list.

20. Select the String 1 sprite and navigate the Code tab. Insert an item 1 of pitches
block from the List area into the play note blocks, as shown in Figure 5.13.

Figure 5.13 43 
A play note block
selecting from an item
in the pitches list.

This change should make no difference to the music at this stage. Later we’ll see that
it allows more flexibility in what the instrument can play. Test that moving the mouse
over the string still plays a note.
Finally, we can move onto creating the remaining string sprites for the ukulele.

21. Create the second string by duplicating String 1 and naming the new sprite String
2. Right-​click on the String 1 sprite block and select Duplicate. The name should
automatically update to be “String 2.” If it does not, change the name manually.

22. Navigate to the Costume tab and redraw the string to match the angle of the second
string. Use the Select (arrow) tool to delete the existing string and the Line tool to
redraw the new string image.

23. Update the when [green flag] clicked script of the String 2 sprite by changing
the play note pitch to item 2 of pitches and the number in the if [ ] then
[ ] condition to the y position of String 2. Remember to change values in both if
[ ] then [ ] loops.

Follow a similar procedure to add the third and fourth strings.

24. Repeat steps 21 to 23 to create the String 3 and String 4 sprites. Drag the string
sprites into location above the ukulele. Update the scripts to reflect the y position of
each sprite and set String 3 note to play item 3 of pitches and set String 4 note
to play item 4 of pitches.

Swipe the mouse across the strings to play them. You should hear a chord with each string
playing a different pitch. Try strumming slower and faster.

Change Chords
The scripts created so far play sounds at the open-​tuned string pitches. In order to play
different notes or chords, the pitch of each string will need to vary. By replacing the values
in the pitches list we can achieve this. To make those changes interactive, we’ll use key
presses detected by the when [ ] key pressed Events block.
44

Scratch Music Projects

25. Select the Ukulele sprite. Create a new script starting with the when [ ] key is
pressed block. Then add four replace item X of [pitches] with Y blocks,
where X is 1, 2, 3, or 4 and Y is 67, 60, 64, and 72. The script should look like that in
Figure 5.14.

Figure 5.14
44  The script to change
pitch values upon
key press.

26. Duplicate this script in Figure 5.14 twice more. Set one to respond to the f key and the
other to the g key. Change the pitch values for the f key script to 69, 60, 65, 69, and
those for the g key script to 67, 62, 67, 71.

Start the project and play the instrument now. As you press the keys c, f, or g, the
pitch of the sounding notes will change when you “pluck” the string with the mouse.
You can now play C major, F major, and G major chords on the instrument, which will
allow you to play many songs. You can add more chord change scripts to get even more
variety.

Summary
In this project we have simulated a ukulele that can be played using gestures with the
mouse and keyboard. Along the way we’ve seen how to draw our own Costumes for sprites
and how to vary notes by changing their pitch. To build this project, a number of coding
techniques have been employed, including conditional statements so our program can
make decisions. We have introduced variables and lists which are coding tools that allow
the storage of values that can change as the program runs.
45

45 

Interview with Nicole Carroll
Nicole Carroll is a composer, performer, sound designer, and builder. Her work spans in-
stallation, improvisation, and fixed media performance. She is active as a sound designer
and composer in theater, performs electronic music under the alias “n0izmkr,” and builds
custom synthesizers and performance sensor systems. Through her work, Nicole seeks
to reconcile the natural world with technology. Themes found in her work derive from
reflections on nature, occult philosophies, literature, and the human psyche. Nicole holds
a bachelor and masters of music in composition from and Arkansas State University
and Bowling Green State University, respectively. She completed a PhD in computer
music and multimedia at Brown University in Providence, Rhode Island. Her works have
been performed internationally in the United States, Mexico, Wales, Germany, Greece,
Australia, and China.

How did you get into combining computers and music?


I’ve been interested in electronic music since I  was quite young. I  credit Def Leppard,
Pink Floyd, and Nine Inch Nails as my inspiration to work with electronics. I’ve always
been enamored with textures and soundscapes. I saw a Laser Floyd show when I was ten
at the science museum, and that really blew my mind. I’m fairly certain combining Laser
Floyd and Tesla coils in the same venue are the best way to inspire electronic musicians!
I had a Teddy Ruxpin talking bear tape player, and between him and my Fisher-​Price tape
player, I was able to get some cool effects and harmonies by simply running them on low
batteries. Simple but enjoyable.
My training as a young musician was very conventional. I  entered my under-
graduate program hoping to become a great film composer, the next Danny Elfman.
My composition professor retired after my first year, which was upsetting at first,
but turned out to be the greatest thing possible for my development. The new pro-
fessor, Dr.  Tim Crist, decided to convert the computer lab into a proper electronic
music studio. I helped him install equipment, which was mostly a collection of hard-
ware effects processors he brought from home. I quickly learned the music software
Logic and borrowed a portable DAT recorder and began processing bassoon and vocal
samples and making soundscape compositions. From then on, I  was set on a com-
pletely different musical path.

What are you doing now in computers and music?


I have a live performance practice, performing noise sets under the moniker “n0izmkr.”
I compose for several mediums—​fixed media and interactive work for multichannel per-
formance, installation, dance, and theater. Virtually all of my work now involves elec-
tronics. My current practice is focused on live performance with digital-​analog hybrid
46

Scratch Music Projects

systems. I build systems that include hardware synthesizers and controllers, and custom
software. I started building hardware using low-​tech hardware and began working with
microcontrollers in 2005. This has slowly eclipsed other aspects of my creative practice.
Building hardware instruments and making noise music sparked the evolution of my
live practice. Instead of performing “proper” concerts in traditional music spaces, I was
playing in grungy alternative spaces—​clubs, galleries, coffee houses. I’m not nearly as
46  enamored with high-​fidelity acousmatic work now; I find noisy sources much more in-
teresting. Even when I  work with field recordings for soundscape works I  look for the
noisiest sounds.

Where can we find examples of your recent and current work?


I’ve been building an instrument for real-​time audio-​visual performance, Orrery Arcana,
for the past couple of years as part of my PhD dissertation. The instrument is a kinetic
controller built on a planetary gear train that interfaces with Max. Its software spans
sonification and data-​driven composition, incorporates NASA lunar data, W. B. Yeats’ es-
oteric system found in A Vision (1937), and tarot cards. There are a number of sensors—​
light, touch, magnetic—​that are played with physical control objects (acrylic discs), each
of which corresponds to a tarot card. The sound world is a combination of synthesis,
samples of modular synthesizers and processed field recordings, and emulations of some
of my hardware synthesizers. You can see and hear examples of my work at my website,
http://​nicolecarrollmusic.com.

What role does coding play in your music activities?


Coding is essential to my practice. I’ve been using the Max software since 2000 when
I  started writing instrument + live electronics pieces and is now integrated into
my live improvisation and installation practice. For my live setup, I  code in Max/​
MSP/​Jitter for audio and visual synthesis and processing on the computer, and
code my instrument’s microcontrollers in Arduino. I  send the outputs of my hard-
ware synthesizers into Max, so I have really raw sounds from the digital logic chips
combined with heavily processed sounds. I use a number of amplitude and frequency
trackers on the live inputs from the instruments, which then control processing
parameters. So that gives me lots of flexibility in working with sound materials. I also
use SuperCollider software to process samples to prepare for performance or fixed
media, and work with the Python language to create automated data mapping sys-
tems. For example, in one current work Python scripts pull data from NASA without
my intervention, so the data is refreshed daily for both site-​specific installation and
live performance.

What tips or advice would you give young coders working with music?
There are two key considerations when learning to code: 1) working in the coding envi-
ronment, either graphical or text, that is most appropriate for your learning style, and
2) setting tangible goals. I recommend that you start by going through several tutorials
for different programs to see what feels most comfortable for you. Once you get a sense
of what fits your learning style, focus on building coding skills in that environment.
47

Design Your Own Instrument

When you have a solid foundation, those skills can be transferred to other languages and
environments.
I’ve found it effective to have a project goal in mind or to emulate an existing project
and gather the skills to see that project through. Soon you have the software skills to re-
place an entire orchestra and all the heavy studio gear! For more advanced coding, I recom-
mend deconstructing or reverse engineering code. Examine closely a piece of music that
interests you, or a technique that you want to replicate. Also look at code from multiple 47 
programmers, because everyone has a different approach and style. It can be enlightening
to see how other people work, and from there you will develop your own style.
48

48 
6
Musical Riffs

This project begins with a Scratch code representation of the well-​known bass riff from
Led Zeppelin’s “Kashmir.” If you are unfamiliar with this classic tune, search for it on
YouTube and take a listen.
Let’s start by adding an Electric Guitar sprite that we can click on to play the riff. We
will start by setting the instrument sound and tempo blocks so that Scratch will play back
the riff accurately.

1. Create a new Scratch project, delete the default sprite, and add a new sprite choosing
the Electric Guitar image (called Guitar-​electric2-​a in the set of costumes) as shown in
Figure 6.1. Load the Music extension blocks.

Figure 6.1
The electric guitar
costume.

2. Go to the Code tab and add the script shown in Figure 6.2 to the Electric Guitar
sprite to rotate the image with the point in direction block and attach set
instrument (sound) and set tempo (speed) blocks and set their values respectively
to 135 degrees, Electric Guitar, and 80 bpm.
49

Musical Riffs

Figure 6.2
Script to set up the
Electric Guitar sprite.

49 

Take a look at the Scratch code for the Kashmir riff in Figure 6.3. This script contains
the notes (and rests) for the first half of the riff. Start by adding the script to the Electric
Guitar sprite and click on the image on the Stage to hear the result.

4. Create the script in Figure 6.3 in the Electric Guitar sprite.

Figure 6.3
Script for the first part
of the Kashmir riff.
50

Scratch Music Projects

Rests are silent notes. Music contains sounds and silence; rests are used to indicate
those periods of silence. The duration values of notes and rests are shown in beats.
Next, take a look through the code for the riff and compare it to the traditional mu-
sical (staff) notation in Figure 6.4. What similarities do you notice between the two
representations? What is repeated? What is not?

Figure 6.4
Notated Kashmir riff.
50 

Measures are musical sections of a particular length. The riff in Figure 6.4 is three
measures long. In staff notation measure boundaries are shown with a vertical line.
The  time signature at the beginning of the notation indicates that the measures in
this music are four quarter notes long.

As you can see, it takes a lot of individual blocks to perform this riff. If you built this riff
yourself in Scratch, you experienced how much time it took to code and edit it in this form.
Additionally, this representation of the riff also takes up a lot of space in the Scratch window.
From a computing perspective this note-​by-​note method of coding the riff is very
inflexible and inefficient. What if you wanted to change a note in your riff? What if you
wanted to code a really long tune? You would have to go through each block to find the
notes you wanted to change and then change each note. This would be a tedious process.
The whole point of working with computers is that they can help share in some of the
work for us and, ultimately, make things easier.
We will look next at how to reduce the amount of work required to create this riff by
using the Repeat blocks to replicate the repetition found in the riff.

Repetition
One way we can make our code more manageable is to look for repetitions and patterns, and
then represent these more efficiently with fewer blocks. For example, notice that the first
three play note blocks in Figure 6.3 are the same pitch. Rather than using three blocks to
play each note, we can use a repeat block with a value of 3 to reduce the number of blocks
needed from three to two (see Figure 6.5). One compromise is that all three notes are of the
same duration, but fortunately that turns out to be more similar to how the phrase is played.

Figure 6.5
Comparing note-​
by-​note and repeat
representations the first
three notes.
51

Musical Riffs

This new representation takes up less space and uses fewer play note blocks, but
we can simplify this even further! Notice that the first group of three notes and a rest in
the riff are repeated. Building on what we did before, can you simplify it further using
another repeat block? Your solution may now look like the example in Figure 6.6 that
is equivalent to Figure 6.3. As you can see, we are beginning to code our riff using fewer
and fewer blocks.

5. Rewrite the riff script using nested repeats as shown in Figure 6.6.
51 

Figure 6.6
Using repeat blocks to
condense the script.

Applying this simplification to the code results in a much more visually compact rep-
resentation of the riff. In this version we only use two play note and two rest for
blocks, rather than the twelve play note and four rest for blocks that we started
with earlier in this project!
Repeats are common in traditional music notation also, as shown in Figure 6.7, where
the double dot and double bar lines (repeat signs) indicate the start and end of a repeated
section. In the original recording, some repetitions of the first three notes in this riff are
played on D rather than A.
52

Scratch Music Projects

Figure 6.7
The riff written in staff
notation with repeat
markings at each end.

In staff notation, the repeat sign is a symbol used to tell the performer to play a section
52  of music at least twice, sometimes more. The performer plays the music in between
the repeat signs again after playing it through once.

In order to complete the riff and go even further in reducing the number of blocks, we
need to use variables.

Changing Pitch with Variables


In the prior section, we learned to use the repeat block to optimize our code and re-
duce the number of play note and rest for blocks. The advantage of reducing the
number of blocks in our code is that it saves time constructing our script and makes ed-
iting easier when you want to make changes to your music.
Looking at the riff, can you see a pattern to how the pitch parameters are arranged?
Notice that the sequence of pitches in each measure moves up by a musical half step or
an increment of 1 in MIDI pitch numbers. 57 (A) is the pitch in section 1, 58 (A♯) in sec-
tion 2, 59 (B) in section 3, and 60 (C) in section 4. Figure 6.8 shows these numbers corre-
sponding to notes in the riff. We can use a variable to stand in for the pitch number and
have it change value when required.

Figure 6.8
Note numbers shown
under the notated
riff. The riff is three
measures long, each
separated by a bar line.

Recall that a variable is essentially a symbol or container that can hold different
values at different times. Variables are created, and located, in the Data section of the
Blocks area in the Scratch editor.

6. Create a new variable called “pitch.” We will work with it as a way to further optimize
our code.

Scratch gives you the option of assigning variables as “For all sprites” or “For this sprite
only.” Select “For all sprites” for this and all other variables of projects in this book.
You should now see an oval labeled “pitch” block in the Variables block section.
When you first make your “pitch” variable, Scratch assigns it an initial value of zero.
We need it to start at value 57. To do this we will add a set to block to the start of the
playback script.
53

Musical Riffs

7. Insert a set to block after the when this sprite clicked block, make sure
“pitch” is selected as a variable name, and give it an initial value of “57”—​matching
the MIDI number of the first pitch in our riff.

This implementation is quite efficient, but it can be improved even further to take advan-
tage of the repeating patterns in the music. The nested repeating patterns in the music
are highlighted in Figure 6.9. Notice that there are repeated notes, that are part of re-
peated phrases (notes and rests), that are part of repeated pitches. We will modify our
code to take full advantage of these patterns in the music.
53 

Figure 6.9
Repeated patterns in
the Kashmir theme.

To make best use of our new “pitch” variable, we will add a repeat block around
the section of code that corresponds to the first measure of the riff in staff notation, as
shown in Figure 6.9.

8. Rewrite the riff script using more nested repeats as shown in Figure 6.10. Change the
repeat variable to 4, rather than the default (which is 10).

Figure 6.10
A very condensed
version of the Kashmir
riff script.
54

Scratch Music Projects

In order to make the pitch value change like it does in the riff, we need to increment
or change the value of the “pitch” variable by 1 each time the code gets to the outer re-
peat [4] loop. Insert the change [pitch] by block, in between the end of the re-
peat [2] block and before the end of the repeat [4] block and set it to 1. The value of
the “pitch” variable will increase from 57 to 58 to 59 and finally to 60, each pass through
the outer repeat [4] loop.

54  Adding Bass and Drum Parts


Use nested repeat blocks to add a lower (bass) part for the Kashmir riff as shown in Figure
6.10, which is even simpler because it stays on the same pitch (50) throughout. We will
create Bass and Drum Kit sprites and ensure that parts start together by having the
Guitar sprite broadcast a message to the bass and drums.

9. Insert a broadcast message into the guitar riff script, a new message called play
riff, just prior to the beginning of the repeat blocks, as shown in the partial script in
Figure 6.11.

Figure 6.11
Add a broadcast
message to the guitar
riff script.

Broadcast and receive blocks allow scripts to be divided into smaller pieces and
for one script to be called by several others. The broadcast block sends the mes-
sage and continues on with the script. The broadcast and wait block sends a
message and waits for the when I  receive script to complete before continuing.
These features are similar to those provided by “functions” in other programming
languages.

10. Create a new sprite and title it Bass Guitar. Choose a Bass Guitar costume or
download the bass guitar image by OpenClipart-​Vectors from Pixabay at http://​
explodingart.com/​smp/​images/​bass-​guitar-​180R.png and import it using the Upload a
Costume menu option.
55

Musical Riffs

11. Navigate to the Code tab and add the setup script shown in Figure 6.12 to the Bass
Guitar sprite.

Figure 6.12
Bass guitar initialization
script.

55 

11. Add to this sprite the bass script shown in Figure 6.13 that also uses nested repeats to
play a repeated bass pattern.

Figure 6.13
A condensed version
of the Kashmir
bass part.

An interesting rhythmic feature of the Kashmir riff is how the riff works against the
steady drum pulse. The riff moves at a slightly different rate to the beat and this “phasing”
offset realigns each time the riff repeats. Notice that the drum blocks are each one beat
in duration.
56

Scratch Music Projects

12. Create a new sprite and select the drum kit icon. Add to it the drum script as shown in
Figure 6.14. This code plays a steady repeated pattern of Bass Drum–​Snare Drum.

Figure 6.14
A simple drum pattern
for the Kashmir riff.

56 

Click on the sprite in the stage and take a listen to all the parts together. How does
it sound now? Try placing the change pitch by 1 block in other places in the
code block? What happens to the sound? Can you trace the steps back and explain what
you hear? Explore other changes, such as different numbers in the repeats and listen
to the effect these have. We now have the riff coded in a very flexible way. We can play
back the riff starting on a different MIDI pitch by changing the initial number in the
set [pitch] to block. Musicians refer to this as transposing. We can also transform
the sequence of pitches in the riff by changing the number in the change pitch
block. By setting this value to a negative number, our sequence of pitches goes down,
instead of up!

Transposition, in music, refers to the shifting up or down of the pitch of a note or


whole musical section. The degree of transposition is often counted in half-​steps,
for example up two half-​steps or down two half-​steps. In this project we achieve that
with the change pitch by block then specify the number of half-​steps. Transposing
musical phrases, like in the “Kashmir” example, is used to combine repetition with
an element of variation. Transposition of whole sections of music can be used to
adjust music so that it is within the performable range of a singer or instrumentalist.
Some musical instruments are designed such that their basic pitch register requires
music written for them to be transposed so that they play in the same key as other
instruments.
57

Musical Riffs

Add Visual Interest
Now that the music is playing well, let’s add some more visual elements and movement
to our project.

13. Click the Add Backdrop button and add the stage image as the background.

14. Click the Add Sprite button and add the Devin character to the stage.

We need to add and modify some scripts to realign and animate the graphics to give the 57 
impression of Devin playing the guitar on stage, as shown in Figure 6.15.

Figure 6.15
The final stage
appearance.

14. Position the Drum Kit sprite left of the riser at the back of the stage, the Bass to the
back right of the stage, and move the Devin and Guitar sprites to align their position
at the front of the stage.

Let’s add a script to the Guitar sprite to position it when the program starts.

15. Extend the Guitar sprite script that starts with a when [green flag] clicked
block to make sure it is visible using a go to [front] layer block. The script
should look like that in Figure 6.16.

The stage looks much more interesting with the background and character added. So
now let’s add some animation as the riff plays. We can make the guitar move using the
point in direction and rotate blocks. So that the movements are in time with
the music we will add these blocks into the riff-​playing script. This will ensure that the
animation and notes are synchronized.

16. Insert the point in direction and rotate blocks into the Electric Guitar sprite’s
riff playing script as shown in Figure 6.17.
58

Scratch Music Projects

Figure 6.16
The guitar sprite’s
initialization
script with added
positioning block.

58 

Figure 6.17
The Kashmir riff
script with motion
blocks added.
59

Musical Riffs

Click the Start flag to run the setup scripts and then click on the guitar to hear the
Kashmir riff play and see the guitar will move in time with the music.
Now let’s add animation to the drums as well.

17. Insert set size to blocks in the when I receive [play riff] script of the
Drum Kit sprite so that it visually pulses as the drums play. To make this look right we’ll
need to shorten the drum notes and add rests to keep the overall timing the same.
Modify the script so that it mirrors the one shown in Figure 6.18.
59 
Figure 6.18
The drum playback
script with visual
animation commands.

Finally, we’ll add motion blocks to animate the Bass sprite also. We’ll make the bass
move on the beat, in sync with the drums. A second when I receive [play riff]
script will be added just for animation.

18. Select the Bass sprite and add the script shown in Figure 6.19 to it. This code repeats
a two-​beat-​long processes of turning the Bass sprite clockwise for one beat then
counterclockwise for one beat. This cycle is repeated six times to match the length of
the riff.

Play back the project now and see how the instruments move to reflect the rhythmic
complexities of the music. As an extension exercise you might like to add a script to allow
the p key to trigger playback.
60

Scratch Music Projects

Figure 6.19
The animation script
for the Bass.

60 

Summary
In this project we have seen how to use repeat blocks to code a musical riff. We have
created a compact set of Scratch codes that perform Led Zeppelin’s famous Kashmir riff
using only one play note and one rest block. By using repeat blocks and creating
a “pitch” variable, we now have a version of the code that performs the same riff with only
eight blocks! That’s quite a reduction from the version we started with at the beginning
of this project. Our new version takes up much less space on the Scratch screen, it’s easier
to make changes to, and it opens up new creative possibilities for us. We added some
extra images to the stage and made the guitar move in time with the music to add visual
interest to the project.
As you can see, repeat blocks and variables are quite useful computing tools for us.
Taking the time to optimize and flexibly structure our code with fewer individual blocks
creates new opportunities for being musically expressive with our code.
61

7 61 

Musical Expression

When people play music, they add expression through variations in loudness, speed, du-
ration, and so on. When we program computers to play music, we can add code to control
these performance parameters so that our music playback is more expressive. In this pro-
ject we will explore ways of doing just that. In addition, this project will introduce a new
method of storing note sequences and associated performance information in lists, and it
will show how to use lists to create multi-​part musical structures called canons.

Randomizing Volume and Tempo


A simple way to introduce variety into music playback is to randomly vary attributes of
the performance. When people perform music there are always subtle variations. To ex-
perience this, try tapping a steady beat with your fingers and notice that you can vary the
volume of each tap by changing the force used. Variations in performance are natural,
and it takes quite some training to develop the skills of playing consistently and accu-
rately. By contrast, computers can be too consistent, sounding unnatural, so we need to
add some variation. We can mimic the subtle variations inherent in human performance
in our Scratch music playback using some random variations, the most obvious of which
is changing note volume.

Random values are those that are unpredictable. Computer programming languages,
including Scratch, include a random number generator that produces values within a
specified range. This can be useful for creating games of chance, such as simulating
the roll of dice, or varying the behavior of an automated opponent. In this book we
will use it to add some change, or “noise,” to musical features so as to provide interest
and variety.

1. Start a new Scratch project. Create a new Snare Drum (“Drum–​Snare”) sprite (see
Figure 7.1a).

2. Click on the Code tab. Start a new script as shown in Figure 7.1b with a forever
block. Insert a play sound block and select the tap snare sound.
62

Scratch Music Projects

Figure 7.1 (a)
a) Snare drum icon
b) The script to play a
looped drum sound.

62 

(b)

Consecutive and Concurrent execution of code. The sound blocks in Scratch include
the play sound [ ] until done and start sound blocks. The play block waits
for the sound to finish before moving onto the next block in the script. The start
sound, in contrast, begins sound playback and immediately continues on with the
script. This highlights the difference between consecutive and concurrent operation
within the computer. Most commonly code within a script works consecutively, with
each block running one after another. However, multiple scripts, in one or several
sprites, can operate concurrently (at the same time). Being aware of these differences
can have important effects on the way your project functions.

Click on the script to run it. Notice that playback volume is constant. Click again on the
script to stop it. Now let’s add some volume variation.

3. Introduce random variations in the volume using the set volume to Sound block
and pick random Operators block, as shown in Figure 7.2. Set the minimum random
value to 25 and the maximum to 100. Make sure to set the volume before playing the
drum.


63

Musical Expression

Figure 7.2
Adding random
volume variations.

Click the script to listen back to the results. The pick random block selects a different 63 
volume level for each note between the minimum and maximum values. Experiment with
different values for the minimum (lowest) and maximum (highest).
Let’s add some visual reinforcement to the change in volume by having the snare
drum image change size as the volume changes. To do this we’ll need to put the random
value into a variable that can be accessed by both the set volume to and size
blocks, as described in the next steps and shown in Figure 7.3.

4. Create a new variable called random value. Insert a set [random value] to
block into the loop before the set volume to block. Move the pick random block
into it from the set volume to block.

Remember that Scratch gives you the option of assigning variables as “For all sprites” or
“For this sprite only.” Select “For all sprites” for this and all other variables of projects in
this book.

5. Insert a random value variable into the set volume to block.

6. Insert a set size to block into the script after the set [random value] to
block, and add a random value variable as its value.

Figure 7.3
Changing sound
volume and image size
together.

Start the script by clicking on it. The snare drum volume and image size change to a
shared random value. Click again on the script to stop it playing.
64

Scratch Music Projects

Cycling Through a List of Volumes


As we’ve seen, varying performance parameters randomly introduces variety, sometimes
called “humanizing” the playback. Often, music includes deliberate patterns of variations
that create a feel or groove to the music. For example, in the case of tapping a beat we
can emphasize (accent) every second tap to add a stronger sense of pulse—​try it yourself.
The length of a repeated pulse pattern gives rise to a sense of musical meter, or measure
length. A length of four beats is a very common meter, as is evident when a drummer
counts the band in by shouting “One, two, three, four.” In this section we will examine
64  techniques for creating a sense of pulse and meter in our music.

Meter. The regular emphasis in pulse through accents leads to the musical concept
of meter—​sometimes called the “measure” or a “bar” of music. Repeated patterns
of strong and weak beats create a sense of local structure in music. For example, a
pattern of two beats—​one strong and one weak—​create a meter of length 2. A pat-
tern of three beats—​strong, weak, weak—​creates a meter of length 3. A pattern of
four beats—​strong, weak, medium, weak—​creates a meter of length 4, and so on.
These metric patterns can become more nuanced and complex, as we’ll see in the
examples below.

We are no longer going to use a random value for the volume so delete those parts as
follows.

10. Remove the set [random value] to block from the script.

11. Remove the random value variables from the set volume to and play sound
blocks.

This will return the script to a basic, nonexpressive looped playback. To reintroduce
volume changes we will set up a series of volume values in a list and loop through them.

12. To create a list, navigate to the Variables area of the Code tab and click on the Make a
List button, as shown in Figure 7.4. Name the list dynamics because the loudness of
a musical note is known as its dynamic value.

A new dynamics list object will appear in the Blocks area along with a number of
new blocks that can work with lists, including delete, insert, replace, and so on.
The list is also displayed as a floating window in the Stage. This view can be shown or
hidden from the Stage using the check box next to its name in the Blocks area. Notice that
the new list is empty. Let’s add some items to it.
We will be adding volume (dynamic) values to create a three-​beat-​long meter. The list
values will represent the strength of each eighth note (which is 0.5 of a beat) in the measure
(three beats long in this example), so there will be six items in total. The first beat of a
measure is usually stronger than the other two beats, and the eighth notes between beats
(sometimes called offbeats) will be even less strong, as shown with the values in Figure 7.5.
65

Musical Expression

Figure 7.4
Use the Make a List
button to create a
new list.

65 

Figure 7.5

notes in a  meter (3


Accent levels for eighth

beats in a measure).

Add the accent values to the dynamics list using the list display on the Stage. List
values will be saved with the project.

13. Make sure the checkbox for showing the list in the Stage is selected. Create a new
item in the list by clicking the + button in the bottom left of the dynamics list on the
Stage. Type in the value 100.

14. Use the return key to add a new item. Add item values 40, 60, 40, 60, and 40. The
script will appear like that in Figure 7.6.

Now that we have a list of dynamic (volume or accent) values we need to write code
that reads through the list, adjusting the volume of the drum sound to each value in
turn. The final code we are working toward is shown in Figure 7.7. This code retrieves
items from the list using their position number in the list; item 1, item 2 etc. We need
66

Scratch Music Projects

Figure 7.6
Adding items to the
dynamics list.

66 

to create a variable that will act as a counter to maintain the current list item number
we are up to.

15. Create a new variable and name it dynamics counter.

16. Put a set [dynamics counter] to block before the forever block and change
the number value to 1. This will initiate the counter variable to 1 before the loop
starts.

17. Put a change [dynamics counter] by block inside the forever loop at the
bottom and leave the change number as 1. This block will increase the counter value
each time through the loop.

The counter will increase each time through the loop. In order to reset the counter back to
1 after it has counted to 6 (the number of items in the list) we will use an if [ ] then
[ ] block introduced in Chapter 5.

18. Add an if [ ] then [ ] block inside, and at the end of, the forever loop. Insert
a > (greater than) block in the condition space in the if [ ] then [ ] block. Put
a dynamics counter variable in the left field of the > block and the number 6 in
the right-​hand field. Inside the if [ ] then [ ] block add a set [dynamics
counter] to block with the value set to 1.
67

Musical Expression

19. To change the volume, put an item [ ] of [ ] block, from the Lists blocks area,
into the volume value field. Inside that block insert the dynamics counter variable
into the number field.

20. Let’s add a final touch that resizes the image in line with the volume. Duplicate the
item [counter dynamics] of [dynamics] blocks in volume (right-​click, select
duplicate, and drag) and insert it into the set size to percentage field. This script
may seem a bit complicated when described in words, so check that your script looks
like the one in Figure 7.7.
67 

Figure 7.7
List values control the
volume and size at
each step through the
measure.

Click on the script to start it. The drum pattern and image resizing should be similar
to the earlier randomized script, however this time we have a regular six-​step sequence
that provides a clear and regular sense of pulse and a three-​beat meter. Click the script
again to stop it.

Sequencing with a List of Pitches


In this next section we will extend the list looping technique just explored by also changing
the pitch and duration of notes in a sequence. To make these extensions clear we will use
a pitched instrument, the saxophone.
The pitches we will use come from a piece called a Chaconne, written by the composer
George Frederic Handel in the early 1700s. The first part of the melody is notated in
Figure 7.8. Originally this piece would have been played on a keyboard instrument, such
as the harpsichord, but we will play it on other instruments in Scratch.
68

Scratch Music Projects

Figure 7.8
Fragment from
Handel’s Chaconne,
HWV442, variation 62,
with pitch numbers.

The HWV number is the musicologists’ catalog number. HWV442 means that this
68  was the 442nd composition written by Handel, as catalogued in the Händel-​Werke-​
Verzeichnis—​See this Wikipedia entry for more details: https://​en.wikipedia.org/​wiki/​
H%C3%A4ndel-​Werke-​Verzeichnis

21. Create a new list and name it pitches. Add values to the pitches list on the
Stage, with the pitch values from the melody shown in Figure 7.8; 62, 67, 71, 74,
69, 78, 76, 74, and 79. This list will look similar to Figure 7.6 but longer and with
different values.

The pitch list needs to be read through note by note. We’ll use another forever loop
and another counter variable to manage this. Let’s create the script shown in Figure 7.9
to play the pitch sequence on saxophone.

22. Create a new script in the Saxophone sprite starting with a set instrument to
block, set to (11) Saxophone. Add a forever block.

23. Create a new variable and name it note counter. This will be used to keep track of
the current pitch in the pitches list.

24. Insert a play note Music block into the forever loop. To select the current
pitches list value, insert an item [ ] of [ ] block (one of the list blocks) into
the pitch field of the play note block. Put the variable note counter into the item
field and choose “pitches” from the menu of lists.

25. Insert a change [ ] by [ ] list block below play note. Set the list to note
counter with the drop-​down menu, and leave the value as 1. This will increment (add
1 to) the value of note counter each time through the loop.

26. Insert an if [ ] then [ ] block below change [ ] by [ ]. Add note
counter > 9 as the if condition. Put a set [ ] to [ ] block inside if [ ]
then [ ] and set the list to note counter and its value to 1. This will reset the
pitches counter to 1 (the start) each time before playing.

This script loops through the pitch, list playing each note in turn, and resets back to
the beginning when it reaches the end.
Click on the script, which should now look like Figure 7.9, to hear the melody. The
pitches change for each note, but the durations of all notes are the same. We will fix this
by adding a list of durations for the melody to use.
69

Musical Expression

Figure 7.9
The script to play
the pitches from the
fragment of Handel’s
Chaconne.

69 

Notice that in the current script the play note block has a fixed duration value
of 0.25 beats. In order for our melody to play notes of different lengths we will create an
array of duration values and step through them, just like we have for the note pitches.

27. Create a new list and name it durations. Here we will store the values of different
note lengths for the melody.

28. Add the duration items to the list as shown in Figure 7.10. The duration values from
the melody are shown in Figure 7.10 (ignoring, for now, the first rest): 0.5, 0.5, 0.5, 1.5,
0.5, 1.5, 0.25, 0.25, and 1.0.

Figure 7.10
Fragment from
Handel’s Chaconne,
HWV442, variation 62,
with pitch and duration
numbers.

29. Update the play note block by inserting an item [ ] of [ ] block in the
duration field and ensure that durations is selected as a list from the drop-​down
menu. Add the note counter variable into the item number field.

Start the melody from the beginning when the project starts.

30. Add a set [note counter] to 1 block to the top of the melody script. Add a
when [green flag] clicked block to the top of both scripts in the Saxophone
sprite. The final melody script should look like that shown in Figure 7.11.
70

Scratch Music Projects

Figure 7.11
The melody script
including duration
additions.

70 

Play the script by clicking on it to hear the complete melody with note-​by-​note varia-
tions in pitch and duration. Experiment with changing some of the values in the lists
and hear the result. Make changes either by manually editing numbers in the list view
on the Stage or using the replace item [ ] of [ ] block. Were the changes what
you expected?

Musical Canon Using Two Sequences


The choice of the Chaconne variation as a melody to code was influenced by the fact that
it also can operate in two parts as a musical canon. In this section we’ll build on the script
created so far to produce a two-​part canon. In the process you will see how we can have
more than one script accessing the same lists. The ability for several scripts to access the
same data is a useful coding strategy with applications well beyond the musical example
provided here.
A musical canon is formed when the same musical part is played simultaneously with
a slightly delayed start. It is kind of like a musical echo. But not all melodies can be ef-
fective as a canon and special care is taken by composers to ensure a melody sounds
good played against itself. Canons have been around for a long time, as evidenced by the
one used in this project composed by Handel around three hundred years ago. Popular
canons (often called rounds) you may know include “Row, Row, Row Your Boat” and “Frère
Jacques.”
The canon involves two musical parts playing together. The Saxophone sprite will
be duplicated and then modified to create the second part. The canon is notated in
Figure 7.12, which shows that the second part (trumpet) begins half a beat after the first
(saxophone) part.
Let’s create the second part and make the required changes to it.

31. Duplicate the Saxophone sprite by right-​clicking on it and selecting duplicate from the
menu as shown in Figure 7.13, then change the costume to a trumpet. Rename the
sprite Trumpet.
71

Musical Expression

Figure 7.12
The canon theme in
two parts.

Figure 7.13
Duplicate a sprite
by right-​clicking and 71 
selecting duplicate
from the menu.

32. To account for the rests at the start and the end of the Chaconne, increase the
duration of the last note (index 9) in the durations list to 3.5 beats. This will allow for
correct looping of the canon.

33. Introduce a separate counter variable for the trumpet part. Add a new variable called
note counter 2. In the trumpet script, replace all instances of note counter with
note counter 2.

34. Update the set instrument block to Trombone (this is the closest sound to a
Trumpet that is available in Scratch 3).

35. Insert two new blocks just after the when [green flag] clicked block. Add a
point in direction Motion block with a value of −170 to rotate the trumpet icon.
Add a rest for block set to 1 beat to delay, slightly, the trumpet part, causing the
canon effect.

The final trumpet script should look like that in Figure 7.14.


Click the Start button to run the project and play back the canon. Each of the parts
should play with a slight timing offset between them.

Experiment with different rest times between the two parts; you will probably find the
standard 0.5 beats works best, but you may prefer other intervals. Try extending the
length of the canon by adding extra notes to the pitches and durations lists based on
the more complete Chaconne melody notated in Figure 7.15. Remember to change the
number of repeats in the saxophone and trombone scripts to match the number of items
in the updated lists.
Other alterations could include speeding up or slowing down the playback using a
set tempo block, extend the canon to play the longer version of the Chaconne theme, or
72

Scratch Music Projects

Figure 7.14
The trumpet script to
play the second part
of the canon.

72 

Figure 7.15
A longer version of the
Chaconne theme.

combining the melody playback with the volume accent technique presented earlier in
the chapter.
You may like to add a background image to the project, perhaps a stage image, to
make the instruments look more at home than on a plain white background.

Summary
In this project we have created a musical canon, a musical form where the same melody
is played several times with offset start times such that repetitions overlap. We have
written code to play a melody with some dynamic expression. Melodies are often called
“sequences” in computer music parlance because the notes and rests in a melody occur
one after the other, as a sequence of events. The project used the List feature of Scratch to
store values for melodic parameters. We accessed the items in these lists using “counter”
variables that kept track of where in the list the melody was.
73

8
Building the Band
73 

Rhythm Guitar and Keyboard

This project will simulate a rhythm guitar and keyboard that play chords and arpeggios.
Guitars and keyboards provide harmonic and timbral richness in a band. The final Stage
appearance is shown in Figure 8.1. It includes the Guitar and Keyboard sprites, the letter
name for the current chord.

Figure 8.1
The final stage
appearance for this
project.

We will start by building each instrument first, then add a step sequencer to play
them automatically.

Strumming the Guitar
Let’s start by resizing and positioning the Guitar sprite.

1. Create a new Scratch project. Select the default sprite and change its name to Guitar.
In the Costumes tab select the electric guitar icon from the costume library.

2. Create a script for the sprite the initializes values when the Green Flag button is
clicked. The script is shown in Figure 8.2 and alters the size and rotation of the sprite
and positions it near the top right of the stage.
74

Scratch Music Projects

Figure 8.2
The Guitar sprite’s
initialization script.

74 

When a guitar plays chords, each of the six strings sounds a different pitch. To imitate
this we will use six play note blocks operating in parallel, each triggered by a broadcast
message.

3. Add a two-​block script to the Guitar sprite starting with a when I receive block
and add a new message play guitar. Attach to it a play note music block with
the pitch value of 48 and the duration value of 0.25 beats. This script is shown in
Figure 8.3.

Remember that in Scratch 3, the Music blocks are in the extension library and need to be
activated before use.

Figure 8.3
Play a single guitar
string sound.

4. To test the triggering of this single guitar string, create the script shown in Figure
8.4 that starts with a when this sprite clicked block. It animates the sprite by
expanding and resetting its size and broadcasts the play guitar message to the
script we wrote at Step 3.

Figure 8.4
Scripts to allow playing
of the guitar by clicking
on it or typing the
spacebar.
75

Rhythm Guitar and Keyboard

At this stage try clicking on the Guitar sprite, and pressing the spacebar, to hear the
single note sound.

5. Create the other strings by duplicating the when I receive [play guitar]
script five times and change the pitch values to the following notes of a C major
chord: 55, 60, 64, 67, and 72.

The note numbers in Step 5 correspond to the pitches shown in Figure 8.5 that make up
a C major chord using these MIDI pitches: 48, 54, 60, 64, 67, and 72.

Figure 8.5
The C major guitar 75 
chord notated on a
grand staff.

Now when you click on the guitar you should hear a six-​note chord sound.

This method of broadcasting the play guitar message to six scripts at once is
called “parallel processing.” It allows all six string sounds to play together. Normally
our scripts execute blocks one at a time (step by step), but on certain occasions (like
playing a musical chord) broadcasting can allow multiple blocks to execute together.
In other words, broadcasting can be a technique used to run scripts concurrently.

Playing the Keyboard
Setting up the keyboard to play is quite similar to the guitar. However, to add variety we
will have the keyboard play an arpeggio, that is, play one note after another in series.
Each note will be a note from the chord.

Figure 8.6
The sprite with a
keyboard costume
from Scratch 3.

6. Create a new sprite for the keyboard using the costume in Figure 8.6 and add scripts
by following Steps 1–​4 above but change the following values or reproduce the scripts
in Figure 8.7.

You might find it quicker to duplicate the Guitar sprite and then make the following
changes.
76

Scratch Music Projects

•​ Sprite Name = keyboard
•​ Costume = keyboard-​a
•​ Instrument = Synth  Lead
•​ Size = 200
•​ Point in direction = 85
•​ X and Y locations = 78, −16
•​ Play note 60
•​ Use the k key to trigger playback
•​ Change the broadcast message to play keys
•​ Change the when I receive value to play keys
76 
Figure 8.7
The initial scripts for
the Keyboard sprite.

Clicking on the keyboard now should animate and play a single note number 60.
For now, to play the notes of the chord as an arpeggio, add these notes of the C major
chord to create a short sequence: 60, 67, 72, 76, 79, 84. These notes are similar to those
used by the guitar and shown in Figure 8.5, but one octave higher so as to provide more
differentiation between the guitar and keyboard parts.

7. Add the chord tones as a sequence to the when I receive [play keys] block,
as shown in Figure 8.8.

Try clicking on the Keyboard sprite to play the arpeggio pattern.

Changing Chords
The notes played by the guitar in the scripts above form a C major chord, often simply
called a C chord. Songs usually use several chords that change in a pattern called a “chord
progression.” We will create a Chords sprite that will display the current chord and have
scripts that manage the note selection to allow for different chords.
77

Rhythm Guitar and Keyboard

Figure 8.8
The initial keyboard
arpeggio pattern.

77 

8. Create a new sprite, change its title to “Chords” and select the Block-​C costume that
shows a large letter C. Position the Chords sprite to the left of the Stage.

Remember that note pitches are named after the first seven letters in the alphabet: A–​
G. The Chords sprite will change its costume to reflect the chosen chord, so we need to
add the letter costumes A through G to the Chords sprite.

9. From the Costumes tab of the Chords sprite add the additional costumes Block-​a
through to Block-​g. Rename the sprite Chords. These are shown in Figure 8.9.

Figure 8.9
The Block letter icons
A through to G.

To manage the chord notes we will use a list called chord notes to hold the pitches
and a variable called root pitch to maintain the current chord.

The root pitch is the name given to the base pitch (first note) of a chord; for example,
C is the root pitch of the C chord. In the MIDI note numbering scheme, used by
Scratch for note values, the value 0 (zero) is pitch C, C♯ is 1, D is 2, and so on. This
project will use a root pitch value of 0 (zero) to equal C and other note pitches will be
relative (larger and smaller numbers) to that. The chord notes list (i.e., the chord
notes in Figure 8.5) will remain constant and the root pitch will move up and down
in pitch (transpose) for each chord. For example, to transpose the C chord pitches to
D, each pitch moves up by two half steps.
78

Scratch Music Projects

10. In the Variables area of the Code tab, create a new variable called root pitch and a
new list called chord notes.

11. Create an initializing script in the Chords sprite to fill the chord notes list when the
project starts as shown in Figure 8.10.

Note that the chord notes list is given eight values even though only six of them
are used for the guitar string; the last two are used by the keyboard as part of an eight-​
step arpeggio pattern that is implemented below.

Figure 8.10
78  The Chords sprite
initialization script.

This script starts with a when [start flag] clicked block. Following this is an
if statement that checks to see if the chord notes list is empty (its length is equal to
0). If true, then a repeat block adds eight 0 values to the chord notes list. Finally,
the update chord message is broadcast in order to fill the chord notes list with
appropriate values.
Now it’s time to set the variable root pitch to a value indicating the current chord.

12. To set the variable to a starting value add the set [variable] to block to the end
of the Chords sprite initialization script shown in Figure 8.10. Choose root pitch as
the variable and 0 (zero) as the value.

The script broadcasts update chord, so now we will need to write scripts in the Chords
sprite that receives that message. The first updates the notes in the chord notes list
for the current chord and the second updates the Chords sprite costume to display the
current chord letter name.

13. Add the script shown in Figure 8.11 to the Chords sprite that will update the chord
notes list with values for the chord based upon the root pitch.

The script starts by receiving a when I receive block with the message update
chord. Following this each item in the chord notes list is replaced by an updated value. The
value equals the MIDI pitch number for the C chord plus the root pitch. This will trans-
pose the chord up (or down with a negative number) depending on the root pitch value.
79

Rhythm Guitar and Keyboard

Figure 8.11
A script to update the
chord notes list
values.

79 

14. Add the script shown in Figure 8.12 that also responds to the update chord
message to change the Chords sprite costume to the appropriate letter.

Figure 8.12
Update the Chords
costume according to
the root pitch.
80

Scratch Music Projects

In this script a series of if [ ] then [ ] statements checks the value of the root
pitch and when it gets a match (equals), it uses a switch costume to block to
change the letter displayed by the Chords sprite.

Changing chords
Now that we have the scripts in place to set the note pitches and change the name, let’s
implement a method to trigger the chord change. We’ll do this by typing letters on the
computer keyboard; “a” for an A chord, “b” for a B chord, and so on. As we’ve seen in
earlier chapters the when [ ] key pressed Events block can be used to capture key-
80  board interactions.

15. Add the script shown in Figure 8.13 to the Chords sprite to change to an A major
chord.

Figure 8.13
A script to trigger
chord changes from
key presses.

The script captures the “a” key press, uses a set to block to update the value for
the root pitch, and then broadcasts the update chord message to run the Chord
scripts we recently added, which listen for that message.
A value of −3 for root pitch is used because it is the distance (called the interval),
in half-​steps, from our base note C down to A.
To play chords from A through to G the script needs to be repeated for each other
letter name.

16. Duplicate the script in Figure 8.13 for keys/​chords B, C, D, E, F, and G. Use the table
below for the root pitch offset (transposition) values for each.

Root Pitch Offset Value Root Pitch Offset Value


b −1 e −8
c 0 f −7
d 2 g −5

With these scripts in place try playing different chords by starting the project and clicking
the guitar. The keyboard will change the note that is played too and, soon, we’ll look at how
to introduce a sequence to the keyboard. But first, there is one final step to managing chords.
81

Rhythm Guitar and Keyboard

Minor chords
The chords we have implemented so far are called major chords. Another common type of
chord is the Minor chord. Musically speaking, major and minor are different modes. Both
major and minor chords can be built on each root note, so we can have a C major chord
and a C minor chord. These different chord types have a different tonal quality about
them, even though they are surprisingly similar. In fact, there is only one note difference
between a major and minor chord built on the same root pitch.
We can update the scripts to accommodate both major and minor chords quite easily.

17. First, create a variable, called mode, to indicate if we are currently playing a major or 81 
minor chord. The mode variable will have a value of 0 (zero) for major and 1 (one) for
minor. We will use this later on for shifting between major and minor chords.

18. Next, modify the Chords script that updates the chord notes values to create both
types of chord, as shown in Figure 8.14. We introduce items 7 and 8 in this script,
which will be used later on.

Figure 8.14
Create chord notes
lists for either major or
minor chord types.
82

Scratch Music Projects

The script uses an if [  ] then [  ] else [  ] block to check if the mode is 0


(major) or 1 (minor). If major, then it bases the pitch on 64 (E, the third scale degree in
the C major scale) otherwise it is minor and bases the pitch calculation on 63 (E-​flat,
the flattened third scale degree in the C minor scale). There are two locations this is re-
quired: for item 4 and 7 in the list.

Scale degrees. In musical harmony, a mode (or scale) is a set of pitches used to create
a coherent-​sounding piece with a particular musical character. For example, the scale
of C major is C, D, E, F, G, A, B. The number of each pitch in scale is referred to as the
82 
scale degree; scale degree 1, scale degree 2, scale degree 3, and so on. Scales (modes)
can start on any pitch and follow a similar pattern of intervals from that starting pitch.

A method of changing between major and minor is required, and of displaying the
chord type on the stage. We will create a new sprite with associated costume and scripts
to do this.

19. Create a new sprite and select the Block-​m costume for it. It should look like the
smaller letter displayed in Figure 8.15. Next we will change its size. Name the sprite
Minor.

Figure 8.15
The Minor sprite next
to the Chord sprite
set to D.

It is conventional to indicate a minor chord by putting a small “m” after the chord
letter name, as shown in Figure 8.15. We need to create scripts to show and hide the “m”
as we change between major and minor chords. We also need a method to make that
change, and we’ll use the key press on the “m” key to do that.

20. Select the Minor sprite, choose the Scripts tab, and create the startup script shown in
Figure 8.16.

Figure 8.16
The setup script for the
Minor sprite.
83

Rhythm Guitar and Keyboard

The script sets the size to 50% (half the normal size), sets the mode variable to 0
(major), and because the initial mode setting is not minor, the sprite is hidden. Run the
script to activate the size change. Use a stand-​alone show block to make the sprite reap-
pear on the Stage. You will need to move the Minor sprite around the Stage to position it
next to the Chord sprite as shown in Figure 8.15.

21. To change between major and minor modes, and show and hide the Minor sprite, add
the script shown in Figure 8.17 to the Minor sprite.

Figure 8.17
Use the m key to 83 
change between major
and minor chords.

In this script the when [m]‌key pressed block looks for the m key. When trig-
gered, an if [ ] then [ ] else block checks the current value of the mode variable.
If the value is 0 it is changed to 1 and the sprite is shown, otherwise the mode is set to 0
and the sprite is hidden. This on/​off switching process is often called a “toggle.” In this
project it will change the chord between major and minor.
Now with the ability to select the chord name, A–​G, and its mode, major or minor, we
should be able to play the chords for most songs. Experiment with changing to different
chords using the computer keyboard and clicking on the guitar to hear the result. Next
we’ll get the keyboard part to make use of the chords as well.

Playing Arpeggio Patterns on the


Keyboard Instrument
As mentioned above, an arpeggio is a sequence of tones that are the notes of a particular
chord in succession. For this reason an arpeggio is sometimes called a “broken chord”—​
the notes are played one at a time, one after another, in sequence. Synthesizers often
84

Scratch Music Projects

include an arpeggio function that can play the notes of a chord in differing patterns. The
ability to play arpeggios on a synthesizer allows them to play a harmonic (chordal) role in
the ensemble, even when some synthesizers can only play one note at a time.

22. Create a variable called arp count and replace the existing script in the Keyboard
sprite starting with when I receive [play keys] with the one shown in Figure
8.18.

Figure 8.18
The keyboard
84  synthesizer arpeggio
script.

This script plays the notes held in the chord notes list but transposes them up
one octave (+12 half-​tones) so they sound in a different register to the guitar. It first sets
the arp count variable to start at 1, then a repeat loop set to 8, plays a note, and
increases the value of arp count by 1 on each iteration. The pitch played is the arp
count item of the chord notes list, plus 12.

Try triggering the arpeggio with the k key. Play it repeatedly to restart the pattern pat-
tern. Change the chord with the keys A–​G and hear how the arpeggio changes. Play the
guitar (with the spacebar) and the keyboard parts together. Experiment with arpeggios of
different lengths, by changing the numbers of repeats in the code. For advanced readers
who want to explore further, try changing the code so the arpeggio plays a descending
pattern, then make it play an up-​down pattern—​first ascending then descending through
the chord notes.

23. As a final step for this project, choose a background image for the stage. The one
used in Figure 8.1 is the Stars backdrop image.

Summary
In this project we have explored the playing of chords and arpeggios. These harmonic
elements are typically played by guitar and keyboard instruments in a band. The rhythms
are played by the user by typing keys on the computer keyboard. Visual animations were
used to indicate when an instrument was sounding. We used the inherent patterns of
chordal harmony and list code structures to manage the selection of notes to play.
85

85 

Interview with Eve Klein
Eve Klein is an award-​winning composer, mezzo soprano, sound artist, and academic. She
designs large-​scale, immersive art music experiences for festivals. Eve’s music draws to-
gether traditional and experimental classical music, interactive performance art, and elec-
tronica. She pushes the boundaries of genre to find new ways of immersing contemporary
audiences in art music forms. Her recent artworks explore the intersection of the body,
technology, mass datasets, and social justice. These are developed through collaborations
with festivals, scientists, artists, and researchers to develop cross-​disciplinary perfor-
mance experiences. Eve holds a PhD in music and sound and is a senior lecturer in music
technology and popular music in the School of Music, University of Queensland, Australia.

How did you get into combining computers and music?


My way into music was via computers so they’ve never been separate practices for me. In
1999 I started studying film production with the aim of pursuing foley and sound editing. My
university program didn’t teach film sound effectively, so I looked to the music department
for courses in sound engineering to learn recording and editing skills. I was so enthralled by
Cubase VST24 that I ended up switching programs and graduated with a music degree. At
the same time, I was teaching myself web design using Adobe tools like Dreamweaver and
Flash and quickly ran into limitations that required coding. From there I went on to code
HTML, CSS, PHP, and MySQL mostly because I was hacking together websites. I eventually
became proficient enough to do web projects from scratch. Hacking was also my approach
to learning music skills, and I worked with the first version of EastWest/​Quantum Leap’s
Symphonic Orchestra doing deep-​level MIDI sequencing and learning about orchestration
and harmony. I went on to study opera at Sydney Conservatorium of Music and to com-
plete a PhD in music and sound from Queensland University of Technology to formalize my
skills. I’m still using technology to hack music all these years later.

What are you doing now in computers and music?


I have a dual career as an artist and educator. As an educator I run the popular music
and music technology programs at the University of Queensland. I’m also an Ableton
Certified Trainer, the first woman in Australia with this accreditation. As an educator my
approach is to teach technology through creative music-​making. My students develop
their creative composition and improvisation skills through activities including partic-
ipation in iPad and mobile device ensembles, networked audio jamming, the creation
of original software instruments, group recording and mash-​up projects, and collabora-
tive musical instrument design. I like to work across disciplines on large-​scale real-​world
projects. For example, my music students collaborated with students from architecture
and interaction design to build an interactive virtual reality environment for a festival
86

Scratch Music Projects

audience. I’m also writing a book on the musical affordances of mobile technologies and
apps based upon my work with students in the University of Queensland music program.
As an artist my work explores the intersection of the body, technology, mass datasets
and social justice. My project, Vocal Womb, is a biomedical opera performance and installa-
tion externalizing the hidden, fleshy, and deeply personal workings of the voice from inside
a singer’s body. Participants sit across from an opera singer. She wears a laryngoscope, a thin
viewing tube which passed through her nose providing real-​time video of her vocal chords
projected onto the walls of the chamber. Audio is also captured by stethoscopes placed on her
skin capturing the sounds of lungs inhaling and exhaling, and other internal organs gurgling
with their everyday functions. Participants blend these internal audio signals and amplify
86  them into the chamber using a mixer. By externalizing these intimate, internal mechanisms
in an exaggerated and overwhelming sonic and visual experience, participants are asked to
confront the contradictions of our voices: who gets to wield them and what that means for
our humanity. Vocal Womb is doing a number of novel things with technology. It’s the first per-
formance work to incorporate endoscopy into real world, real-​time performance (rather than
using pre-​recorded video). This means that each time I perform the work I’m capturing data
about how my voice responds to the stress of performance. This is interesting because the
larynx is normally studied in medical environments. As technologies miniaturize we will have
increasing access to our internal bodies as performative spaces. In developing the technology
for performing Vocal Womb I’ve begun experimenting with what’s possible in this space.

Where can we find examples of your recent and current work?


I’m currently developing techniques for using real-​time data from my body to generate
musical sounds and rhythms, or to change musical textures. I’m excited to see the kind of
music that’s generated from and responding to my body in real time! You can follow my
creative projects and writing at: http://​eveklein.com.

What role does coding play in your music activities?


Coding enables you to take full control of the creative projects you are working on. It’s
much easier to make experimental or innovative work if you aren’t running up against
the limitations of commercial software and hardware. Any limitations then become about
what you want to achieve within the scope of the project and how you are imagining the
outcomes and experience.

What tips or advice would you give young coders working with music?
Structuring your learning around creative projects, problems, and challenges will speed
up your progress and make you independent very quickly. If you are going to follow a
structured program of study make sure that it is built around projects. If not, devise a
creative project around the skills you are learning to help reinforce them. If you’re not
sure how to do something—​for example, utilizing field recordings, natural sound, or elec-
tronics to create rich soundscapes—​then start a project and connect with others around
the world who will likely have been working on similar things. If you can’t work it out,
then do some research and see if there are solutions that you could adapt to your project.
Once you’ve hacked a solution, see if you can clean it up or make it more efficient. This
kind of reflection will reinforce your learning and make you a better coder/​creator.
87

9
Building the Band
87 

Drums and Bass

In this project we are going to build a drum and bass sequencer. These instruments are
the backbone of a rock band and frequently work closely together to provide the groove
and drive for a song. We will construct the code so that we ensure tight coordination
between the drum and bass parts and provide a unified interface for creating drum kit
patterns and bass lines. There’s quite a bit to do, so let’s start with an overview of where
we are heading. In Figure 9.1 is an image of the final interface for this project.

Figure 9.1
The drum and bass
sequencer project, as it
will finally appear.

A sequencer has a series of steps from which rhythmic patterns are constructed. In
this project we use an eight-​step sequencer, indicated by the number sprites 1, 2, 3, etc.
laid out across the interface. These numbers act as buttons to turn on or off that step
within a pattern.
There are many sprites in this project, but don’t worry, a lot of them are duplicates
so there are not that many sprites to make. We will build the core elements of the pro-
ject first, then duplicate sprites to amplify the behavior. There are a few central sprites
88

Scratch Music Projects

in the project as depicted in Figure 9.2. These include the Start-​Stop button, the drum
instruments, the sequencer steps, tick/​cross buttons, and the bass instrument.

Figure 9.2
The basic sprite types
in the Drums and Bass
project.

The Start-​Stop Sprite
88  The Start-​Stop button sprite contains the scripts that starts and stops the music. A script
loops through the eight steps in the sequence broadcasting play step messages to
each instrument. First, let’s create the sprite and change its color from green (start) to red
(stop) when clicked. Create a new project and change the costume of the default sprite.

1. Create a new project. Navigate to the costume tab for the default Cat sprite and use
the Choose a Sprite button to select the Button 1 image—​which is a green circular
button. Rename the sprite Start-​Stop and position it in the top left corner of the Stage.
You can delete any other costumes already in this sprite as they won’t be required.

Figure 9.3
The Choose a Costume
option.

Let’s set the tempo and a start-​stop variable for the sequencer on start-​up.

2. Select the Start-​Stop sprite and navigate to the Code tab and add a new script as
shown in Figure 9.4. Start with a when [green flag] clicked block. Add the
Music block extensions to the project using the Add Extension button in the lower
right of the Scratch interface and attached a set tempo to music block and change
the tempo value to 120.
89

Drums and Bass

3. Use the Make a Variable button to make a new variable and name it is playing.
Set it (and most variables in this project) to be available to all sprites. This variable will
be used to indicate if the sequence is running; a value of 1 will indicate that music is
playing, and a value of 0 that it is stopped.

Figure 9.4
Code to set the tempo
when the project starts.

89 

Boolean Variables: Two-​state variables like is playing are sometimes called a


“flag” because it can be used to indicate state by being “raised” (1) or “lowered” ( 0).
A two-​state variable is known in some programming environments as a boolean value;
one that is either true (1) or false ( 0).

4. Create another new variable and name it step. This will maintain the current
sequence step value (1–​8).

5. Create the script shown in Figure 9.5 that begins with a when this sprite
clicked block. An if [ ] then [ ] else [ ] block checks if the variable is
playing equals 0 (or not) and changes the Button color from green to red using the
set [color] effect to block with the values 130 for red and 0 for green. The is
playing variable is changed using the set [is playing] to block and enables
the toggling back and forth between states.

6. Use the broadcast block’s drop-​down menu option, new message, to create the
start message. With the set to block at the top of the if [ ] then [ ] block,
select the step variable, and change the value to 1. This will initialize the step value
to 1 so that the sequence starts playing from the beginning.

Click on the Start-​Stop button on the Stage and its color should change between
green and red, and the is playing value should toggle between 0 and 1.
Next, let’s add another script to the Start-​Stop sprite that begins the sequence loop.
This script, shown in Figure 9.6, starts with a when I  receive [start] block and
features a repeat until block that repeatedly counts through the eight sequence steps
and regularly broadcasts a play step message to the instruments (yet to be created).

7. Drag a when I receive [start] block to the Code area of the Start-​Stop sprite.
This script will be called each time the Play button is clicked.
90

Scratch Music Projects

Figure 9.5
Script to switch the
Start-​Stop button color
and send the start
message when clicked.

90 

8. Connect a repeat until block and put in a conditional to check if is playing is


equal to (=) 0 (zero).

9. Inside the repeat until loop create the remainder of the script as shown in Figure
9.5. This includes these components: broadcast [play step] message (create a
new message); rest for [0.5] beats (to silently control the timing of each step);
change [step] by 1 (to increment the step value) and; use an if [ ] then
[ ] statement to set [step] to 1 (reset the sequence to the start) if the step
variable is greater than (>) 8.

As discussed in Chapter 4, the broadcast feature in Scratch allows a script to send a


message throughout the project. The broadcast block is used to send a message.
You can create your own message names using the drop-​down menu in the broad-
cast block. These messages can be received by the when message received
block. This process is similar to what in other programming languages is referred to as
“calling” a “function,” where a function is another name for a script.

This sprite we’ve created controls the starting, stopping, and timing of the steps in the
sequence. However, our project does not yet make any music, so in the next section we
will introduce the first instrument.
91

Drums and Bass

Figure 9.6
This script loops
through each step
in the sequence and
in time.

91 

The Kick Drum Sprite


The kick drum, sometimes called the bass drum, will be the first drum instrument we
create. We need to make a list of sequence steps for the kick drum, initialize the list when
the project starts, and create a drum player script that is triggered at each step in the
sequence.

Figure 9.7
The drum kit image.

10. Create a new sprite and select the drum kit image for it as shown in Figure 9.7. Name
the sprite Kick Drum. Position the sprite below the Start-​Stop button on the Stage and
navigate to the Code tab ready to write a new script.

We need to set a number to the instrument when its sprite is clicked. Later, when we
set sequences for each instrument it will be necessary to identify each instrument by its
number, so for now we’ll create a variable and set its value to kick drum instrument.
92

Scratch Music Projects

11. With the Kick Drum sprite selected, create a new variable called current
instrument. Select “For all sprites” when prompted in a naming dialog box. This
will contain the number of the currently selected instrument. The kick drum will be
instrument number 1.

Create a script to set the current instrument value when instrument sprite is
clicked.

12. Start a new script for the Kick Drum sprite with a when this sprite clicked
block, as shown in Figure 9.8. Add to it a set [current instrument] to 1
variable block. Add a broadcast block and create a new message named “kick
selected.” This will notify other scripts when instrument selection changes. Finish the
92 
script with a say [Selected] for [2]‌seconds Looks block.

Figure 9.8
Code to select the
instrument when
clicked.

Click on the Kick Drum sprite and it should show the say text bubble. Let’s now
write the Kick Drum script that plays the kick drum sound at each step.

13. Begin a new Kick Drum script with a when I receive Event block. Set the message
name to play step. Recall that this message will be broadcast at each timed step in
the sequence by the Start-​Stop sprite.

14. Add a play drum block from the Music extension blocks. Choose bass drum as a
sound and 0.1 as the duration in beats.

15. To add visual feedback, surround the play drum block with two set [size] to
blocks, the top one set at 110% and the bottom one set to 100%. This will slightly
increase the image size as the drum sounds.

At this stage the script should appear like that in Figure 9.9 and when you click the Start-​
Stop button a repeated kick drum sound should be heard. Click again on the Start-​Stop
button to halt the playback.
It is great to have the sequence playing, however, a constant kick drum pattern is not
very musical, so it’s time to improve it. We need to be able to specify on which steps in the
sequence the kick drum will sound. For example, a simple drum pattern would have the
93

Drums and Bass

Figure 9.9
A simple play script for
the kick drum.

kick drum on steps 1 and 5. To enable this we will create a list with a value in it for each
step (8 in this case). We will use a value of 1 to represent a step where the drum plays and 0 93 
to represent a rest (no playback). Our simple kick drum pattern will be represented in a list
like that shown in Figure 9.10 as an illustration and in Figure 9.11 as it appears in Scratch.

Figure 9.10
A list of play (1) and
rest ( 0) values for the
eight steps in the kick
drum sequence.
Let’s create a list for the kick drum pattern and then a script to initialize it.

16. Click the Make a List button to make a new list and title it “kick steps.”

When a new list is created in Scratch it starts out empty. The named list appears in the
List area of the Blocks sidebar and, when the box next to the name is ticked, the list
appears on the Stage.

17. Enter the step values from Figure 9.10 in the kick steps list. Use the plus (+) button
in the bottom left corner of the list display to add a new item to the list. Click on the
item value to edit it. Create the eight sequence step values, where 1 equals a drum hit
and 0 equals a rest. The final list should appear as in Figure 9.11.

Figure 9.11
The kick steps
list displayed on
the Stage.
94

Scratch Music Projects

When the program is saved these list values will be saved with it. At present the play-
back script is not using the list, so it is time to fix this by enhancing the when I re-
ceive [play step] script in the Kick Drum sprite.

18. Add an if [ ] then [ ] block surrounding the set size to and play drum
blocks.

19. Add a condition to the if [ ] then [ ] block that checks if the current step in
the kick steps list is greater than 0. If so, then it will play and animate. Otherwise it
will not play. The final script should appear like that in Figure 9.12.

94 
Figure 9.12
The complete when
I receive script for
the Kick Drum sprite.

In this script in Figure 9.12 the if [  ] then [  ] block checks the current step
value in the kick steps list. If the value of the item in the list is greater than zero, then
the drum should play for this step. When true the size is increased slightly, the bass drum
sound is played, and the size is reset.
Play the sequence by clicking on the Start-​Stop button to hear the kick drum pattern
and see the Drum Kit sprite animate with each sound. You should hear a slowly repeating
kick drum sound.

The Step Button Sprite


Looking back at Figure 9.1, notice that there is a row of sprites with number icons
representing the steps in the current sequence. There are eight steps in a sequence,
and each instrument has its own sequence pattern. These number sprites represent
the steps in the sequence and act as buttons, and can change state between on and off,
when clicked. Turning some steps on and others off is how musical rhythm patterns
are created by the sequencer. We will start building this functionality by making the
sprite for Button 1, as shown in Figure 9.13, then duplicate it to create the other step
numbers.
95

Drums and Bass

Figure 9.13
The Button 1 sprite.

95 
20. Add a new number button sprite to the project. Use the Choose a Sprite button to
create a new sprite and select the “Glow-​1” image as its costume. Set the name of the
sprite to Button 1.

Position the sprite near the bottom left corner of the Stage, as it appears in Figure 9.1.
It’s time to add some behaviors to the sprite. The first step is to add a script that
changes the appearance and sequence values when clicked.

21. Reproduce the script shown in Figure 9.14 that toggles the button, and the first step in
the kick drum sequence, on or off.

Figure 9.14
Button 1 script to flip
the state between play
to rest when clicked.

In this script the outside if [ ] then [ ] block checks to see if current in-
strument is equal to 1, that is, if the kick drum is active for editing. An if [ ] then
[  ] else [  ] block has a condition that checks whether the first value of the kick
96

Scratch Music Projects

steps list is zero. If so, this step is resting (value 0) and should be set to play (value 1),
so the color effect is adjusted to 100 (which makes it appear red) and the first item of the
kick steps list is set to 1. Otherwise, the color effect value is set to 0 (returning it to
the original aqua color) and item 1 in the kick steps list is set to rest (value 0).
Clicking on the Button 1 sprite should now flip color and also change the value in the
kick steps list. The values in the kick steps list can be shown (or hidden) on the
Stage by selecting the check box next to the kick steps block in the Data section of
the Blocks menu. Figure 9.11 shows the kick steps list values as they appear on the
Stage. In this example, the first and fifth items are set to 1 (to play) and the remaining
are set to 0 (to rest).

96 
Changing the appearance of sprites is a great way of using them as buttons. Each
color can represent a different button state. There are two states for the Step buttons,
on and off. For the Start-​Stop button in this project we changed its appearance by
adjusting the color of the costume. We do a similar process for the sequence step
buttons by changing their color.

We can animate the step numbers when playing so that the current step value is visually
evident.

22. Add the script shown in Figure 9.15 to the Button 1 sprite. In response to a play
step message the script checks the current step is equal to 1 (this button’s value). If
so, then the sprite is animated using a temporary change in size.

Figure 9.15
The step button
animation script.
97

Drums and Bass

We need a script to set the button state on startup.

23. Select the Start-​Stop sprite. Update the script that starts with a when [green flag]
clicked block by adding a set [current instrument] to [1]‌block, and a
broadcast [kick selected] block, as shown in Figure 9.16. This chooses the
kick drum as the initially selected instrument which will, in turn, update the color of the
sequence number buttons to reflect the kick drum pattern.

Figure 9.16
Add to the Start-​Stop
button initialization
script.

97 

Finally, a script is required in the Button 1 sprite to set its colors when the kick
selected message is received.

24. Add the script shown in Figure 9.17 to the Button 1 sprite. It checks the current state
of the first step in the kick steps list and sets the color effect to match the step
status, either off (zero effect) or on (effect level 100).

Figure 9.17
Setting the sprite color
when the kick drum
instrument is selected.
98

Scratch Music Projects

When playing the sequence now, the step button can be used to turn the kick drum on or
off on the first step (beat). Give it a go and experiment with controlling the state of the
kick drum while it plays. This is limited control for now, but the potential as we create
more steps and instruments should be clear.
We will return later to create the remaining sequence number sprites, but before then
the next sections add the other instruments.

Adding More Drums
98  A drum kit has three basic elements: the kick drum, snare drum, and hi-​hat. The icons for
these are shown in Figure 9.18. The snare drum and hi-​hat instruments will be quite sim-
ilar to the kick drum instrument, so we will duplicate the kick drum as a starting point for
these, then make the necessary updates.

Figure 9.18
The icons for the Kick
Drum, Snare Drum and
Hi-​Hat sprites.

Let’s start by creating the Snare Drum sprite and updating its scripts.

25. Duplicate the Kick Drum sprite. Rename it Snare Drum and change the costume to the
Drum-​snare-​a icon.

26. Select the Snare Drum sprite and in the Code tab make a new list called “snare steps.”
Keep the option for this to be used “For all sprites.” The list will be used to store the
rhythm data for the snare drum sequence pattern.

27. With the snare steps list visible on the Stage, add eight new items to the list, each
with the value 0, as shown in Figure 9.19.

28. Update the when sprite clicked script to set [current instrument] to
[2]‌and create a new message named snare selected for the broadcast block.
This will allow the project to update settings when the Snare Drum sprite is clicked on.

29. Update the when I receive [play step] script by changing the item [step]
of [ ] block to snare steps and the play drum block to the (1) Snare Drum
sound.

The scripts in the Snare Drum sprite should appear like those shown in Figure 9.20.
Next, we need to update the Button 1 scripts to account for the snare drum instrument.
99

Drums and Bass

Figure 9.19
The initial snare
steps data.

99 

Figure 9.20
Updated code for the
Snare Drum sprite.

30. Select the Button 1 sprite and navigate the Code tab. Duplicate the when this
sprite clicked script. In the new copy of that script change the if [current
instrument] = to 2. Change all three references to kick steps to be snare
steps. The script should match that shown in Figure 9.21.

Finally, the Button 1 sprite needs to have a script that responds to the snare
selected message that is sent when the user clicks on the Snare Drum sprite.

31. Select the Button 1 sprite and duplicate the script that starts with when I receive
[kick selected]. In the copied script, change the when I receive block to the
snare selected message, and the item 1 of block to the snare steps list, as
shown in Figure 9.22.

Try playing with the project now. Both the kick drum and snare drum should be able to
play, or not, on the first step in the sequence and the Button 1 icon should change, if re-
quired, when the Kick Drum or Snare Drum sprites are clicked on.
Having added the snare drum instrument to our project, we will now repeat the pro-
cess to add the hi-​hat instrument.
100

Scratch Music Projects

Figure 9.21
Updated snare drum
code for the Button 1
sprite.

100 

Figure 9.22
A script for the Button
1 sprite to respond to
the snare selected
message.

32. Repeat steps 26 to 32 to create the Hi-​Hat sprite, but with the following substitutions:

•​ Use the Drum-​highhat-​a costume instead of Drum-​snare-​a


•​ Create a hats steps list and fill it with eight zeros
•​ Use hats steps as the list name instead of snare steps
•​ Use 3 rather than 2 for the current instrument number
•​ Use hats selected as the broadcast message rather than snare selected
•​ Play the sound Closed Hi-​Hat rather than Snare Drum

Having added the hi-​hat instrument the project should play any of the three drum parts
on the first step in the sequence. Button 1 can be used to change whether or not each in-
strument plays on Step 1 of the sequence.
101

Drums and Bass

While it is tempting to want to move onto filling out the sequence step buttons to
create complete drum patterns, we have one more task to complete before doing that—​to
create the bass guitar instrument for our band.

Adding the Bass Part
The rhythm section of a band is made up of the drums and bass. In this section we’ll add
the bass guitar part to the project. Like the drum parts the bass part will involve an eight-​
step sequence pattern that repeats. Unlike the drums, however, the bass part requires a
pitch for notes at each step. This will be achieved by using the step values to represent the
pitches, with a value of zero indicating a rest (silence).
Many aspects of the bass part are similar to those used in the drums, so the Bass 101 
sprite will start as a copy of the drum part.

33. Duplicate the Kick Drum, change the name to Bass and set the costume to guitar-​
electric1-​a. Position it on the stage next to the drum sprites, as shown in
Figure 9.23.

Figure 9.23
All instruments on
the Stage.

Adjust and add to the code for the bass part.

34. In the Bass sprite script that starts with when this sprite is clicked, change
the set [current instrument] to block to 4 and change the broadcast block
message to bass selected.

35. Make a new list called bass steps and fill it with eight zeros. And change the
script starting with when I receive [play step] to include if item [step]
of [bass steps] > 0 then and play note [item {step} of [{bass
steps}] for [0.25] beats as shown in Figure 9.24.

36. Add a new script to set the instrument to Bass. Start with a when [green flag]
clicked Event block and add a set instrument to [Bass] Music block below
it, as shown in Figure 9.25.

Next we need to edit and add to the scripts in the Button 1 sprite to manage the bass
part on that step of the sequence. We will create scripts to choose the bass pitch for Step
1, selecting from a predefined list of suitable pitches. Each time Button 1 is clicked, and
the bass part is selected, the bass sequence at this step will toggle between being silent or
selecting a new pitch at random from the list.
102

Scratch Music Projects

Figure 9.24
The updated bass
script that responds
to the play step
message.

102 

Figure 9.25
Initialize the Bass
sprite to the bass
instrument sound.

37. Make a new list available to all sprites named bass pitches and fill it with these
values: 36, 38, 40, 43, and 45.

38. Select the Button 1 sprite and navigate to the Code tab. Duplicate one of the when
I receive scripts and set it to respond to the bass selected message. Update
the item [ ] of block in this script to be the bass steps list.

39. Duplicate one of the when this sprite clicked scripts and update the following
values. Set current instrument = to 4, update the first replace item block to
replace item [1]‌of [bass steps] with item [pick random {1} to
{5}] of [bass pitches], and the second replace item block to use the bass
steps lists, as shown in Figure 9.26.

This concludes the changes required for the bass part. You should now be able to start
the project and include a bass note on Step 1 of the sequence. By repeatedly clicking the
Button 1 sprite on the Stage you should turn on and off the bass note and each time it is
turned on it will be a newly selected pitch. The remaining step in the project is to com-
plete the other steps in the sequence.

Adding More Sequence Steps


In this section we will add sprites for sequence steps 2 through to 8. We’ve done most of
the hard work in creating a Button 1 sprite for sequence step 1 and can use the duplicate
103

Drums and Bass

Figure 9.26
The additional when
this sprite
clicked script in
Button 1 for the
bass part.

103 

Figure 9.27
The Stage appearance
when all sequence
steps are added.

function to reuse that effort. At this point in the project your Stage should have just one
step number sprite. Buy the end of this section it will look more like Figure 9.27 with a
number button for each step of the sequence.
Create the Button 2 sprite and position on the Stage.

40. Duplicate Button 1 sprite and name the new sprite Button 2. With the Button 2 sprite
selected, navigate to the Costume tab and use the Choose a New Costume button
to select the Glow 2 icon. You can delete the Glow 1 costume from this sprite if you
wish.

41. Move the Button 2 sprite so that it is next to the Button 1 sprite on the Stage. The
numbers will need to be almost touching each other in order to fit all eight across the
Stage.
104

Scratch Music Projects

Update the scripts so that the code relates to Step 2, rather than Step 1.

42. Navigate to the Code tab. In the when I receive [play step] block, have the
if [ ] then [ ] condition check if step = 2. In each of the four when this
sprite clicked scripts and each of the four when I receive scripts change
every reference to item 1 to be item 2.

Repeat the process for each step from 3 to 8.

43. Repeat Steps 41 to 43 to create and modify sprites for Button 3, 4, 5, 6, 7, and
8. Choose the associated Glow icon and change the step and item numbers to match
the button number.


104 

Now it is time to play with the sequencer you have created. Run the project. Click the
Start-​Stop button. Select each of the instruments by clicking on them in turn and, when
selected, turn on or off each step of the sequence with the number buttons to hear what
kinds of patterns you can create.

Tempo Controls
In this section we will create sprites to control the speed of the music. In music the speed
is referred to as a tempo. We will use Plus (+) and Minus (-​) sprites to enable the increase
and decrease of the tempo.
Create icons -​, +, and Metronome to the right of the Start-​Stop button on the Stage.
The three icons, when complete, should look similar to those shown in Figure 9.28.

Figure 9.28
Metronome controls.

44. Download the metronome icon file from this site (http://​explodingart.com/​smp/​
images/​Metronome_​94236_​resized.png) or some other source. The metronome
icon is by Pavel Diatchenko from the Noun Project. Create a new sprite for the
Metronome button using the Upload option in the Choose a Sprite menu and select
the metronome icon file from your computer.

45. Create two new sprites, one named Minus and the other named Plus, using the Paint
option in the Choose a Sprite menu as shown in Figure 9.29.

46. To create the plus and minus costumes, use the Text tool to create a text box, type
the minus (-​) character inside it, and drag the bounding box to expand the font size,
as shown in Figure 9.30. You may need to change the Fill color to black and center
the character in the middle of the editor. Do the same to create a plus (+) sprite. Once
created, position the -​and + sprites next to each other near the top of the Stage.
105

Drums and Bass

Figure 9.29
Choosing a new
painted costume.

105 

Figure 9.30
Using the text tool in
the Costume editor.

47. Select the Minus sprite, navigate to the Code tab, and add a new script starting with
a when this sprite clicked block and adding below it a change tempo by
Music block with a value of −5, as shown in Figure 9.31.

48. Add a similar script to the one in Figure 9.31 to the Plus sprite but change the value to
5. This will increase the tempo by 5 beats per minute each time it is clicked.

Check that the tempo change works by playing the sequence and click on the Plus and
Minus buttons on the Stage and listening for a change in playback speed.
106

Scratch Music Projects

Figure 9.31
A script to decrease
the tempo by 5 beats
per minute.

Add a Background to the Stage


106 
To complete the project, let’s add a backdrop image to the Stage.

49. Use the Choose a Backdrop button in the bottom right of the Scratch interface to
select a background of your choice. The one used in Figure 9.1 at the beginning of
this project is the Rays background.

This has been a lengthy and complex project, but with a great musical result. Take time
now that you have completed it to enjoy playing with the drum and bass sequencer.
Sequence a simple rock beat with the kick on steps 1 and 5, the snare on 3 and 7, and the
hi-​hat on all 8. Then add a bass pattern over that drum beat. Explore the use of minimal
textures at a slow pace, and some contrasting sequences that are fast and furious. Try
to interact with the sequencer live by building and modifying sequence patterns as the
program plays.

Summary
This project involved the creation of a drum and bass machine. Drums and bass form
the rhythmic backbone of many bands and since the 1970s electronic drum and bass
sequencers have been increasingly used in this role. The heart of the sequencer in this
project was a forever loop that counted through steps and broadcast regular messages
to each of the instruments. Sequence patterns were stored in lists, one for each instru-
ment. Sprites on the Stage were employed to create a graphical user interface that allows
the writing of sequence patterns for each instrument, even as the music is played.
107

10 107 

Conducting the Arrangement

A conductor of an orchestra helps ensure that all the musicians play together and with
a common interpretation of the score. In smaller ensembles, like rock bands or chamber
ensembles, musicians listen to each other to stay coordinated. In computer systems,
like Scratch, we can use a common timer to keep parts coordinated—​as we saw in
Chapters 8 and 9.
A musical arrangement is the organization of musical sections. Often music contains
passages that are repeated or reused several times, and an arrangement describes the
structure of such sections into an overall form. For example, a song often has verse and
chorus elements that are played several times. These sections in a particular order make
up the song’s arrangement. The verse or chorus may themselves be made up of smaller
sub-​sections.
In this project we will look at how to segment music into repeating chunks and ar-
range them into a song. One advantage of dividing the song into segments is that we only
need to write the script for each segment once and we can then reuse it as many times as
needed. This saves time and space in creating the project and allows for easy rearrange-
ment of the segments if required.
We will create a version of the famous track “I Got You (I Feel Good)” by James Brown.
Search for this song online and have a listen to familiarize yourself with it before we get
started.
The project will include parts for drums, bass, guitar, horn sections, and vocals. The
final appearance of the Scratch stage for this project is shown in Figure 10.1.
This song uses a classic 12-​bar blues form that lends itself to division into six two-​
measure sections. We will write instrument-​specific scripts for each section and then
scripts that arrange those sections into a song. The final arrangement of the verse and
chorus sections is depicted in Figure 10.2. Let’s get started.

It is a convention that musical sections (or segments) are labeled alphabetically—​A,


B, C, D, and so on. When talking about an arrangement, musicians might refer to
“section A” or “the A section.” When a section has a slight variation then a number
is added while the letter name is maintained. For example, we see this as Section A2
in this example, which is the same as Section A but transposed to work over a dif-
ferent chord. At times larger scale sections (perhaps containing subsections) are given
108

Scratch Music Projects

descriptive names, these names vary on the style of music. In popular music names
include introduction, verse, chorus, bridge, solo, ending, and so on. In jazz music,
section names include the head, solo, coda, and so on. In orchestral music sectional
names include movement, exposition, development, recapitulation, and more.

Figure 10.1
The final stage
for this project
with instruments
representing each
musical part.

108 

Figure 10.2
The arrangement
of verse and chorus
sections in the song “I
Got You (I Feel Good)”
by James Brown.
The Conductor
The first step in our project is to create a Conductor sprite. This sprite will start and stop
the music when clicked, and regularly broadcast “beats” that will keep all parts in time.
Recall that the process of broadcasting messages throughout the project was discussed
in Chapter 9. The script involves a sixteen-​step sequence that will play a phrase of two
measures. The process of incrementing through the steps in a sequence is quite similar to
that used in Chapters 8 and 9, so you can refer back to those chapters for more details if
required. The scripts will also increment through six phrases that make up the verse and
chorus of the song.
Start a new Scratch project, create a Conductor sprite, and add the costume icons
shown in Figure 10.3.

1. Create a new sprite and select the Button1 (green circle) icon. Rename the sprite
Conductor.

2. Move to the Costume tab and rename the costume “Start.”


109

Conducting the Arrangement

Figure 10.3
The start and
stop icons for the
Conductor sprite.

3. Add another costume, select the Button-​5b (red cross) icon, and rename the
costume Stop.

Navigate to the Code tab and create the following scripts to manage the initializa-
tion of variables and continual looping through sequence steps that will drive each of the
instrument parts. 109 
The first script is a short one that runs when the green flag button is pressed. It sets
the costume to the Start button and the tempo to 148 beats per minute.

4. Reproduce the script as shown in Figure 10.4. Remember that the Music blocks need
to be activated using the Extensions button.

Figure 10.4
The startup script for
the Conductor sprite.

The next script is activated when the Start icon (round green button) is clicked. It
sets, or resets, a number of variables that are mostly used to track the current position
within the music and then the script broadcasts a start message. The full script is
shown in Figure 10.5.

5. Start the script with a when this sprite clicked block.

6. Add an if [ ] then [ ] else [ ] block that will take different actions for
starting or stopping the music playback.

7. The if condition is costume [name] = [Start]. This combines an equals


Operators block and the costume name Looks block. Type Start into the right side
of the equals block.

8. Create four new variables called step, phrase, transpose, and playing? and
insert set [ ] to blocks for each of them into the first part of the if statement.
Set all to 1, except transpose, which should be 0.
110

Scratch Music Projects

9. Add a broadcast [ ] and wait and broadcast Event blocks and create new
messages for them: section A and go.

10. Inset a switch costume to [Stop] block at the end of the first part of the if
statement.

11. In the else part of the condition, add blocks for stopping the playback. Inset a set
[playing?] to 0 variables block, and a switch costume to [Start] block.

Figure 10.5
The start and
stop script for the
Conductor sprite.
110 

The third Conductor script shown in Figure 10.6 responds to the go message by re-
peatedly updating the values of the current step (within a sequence) and the current
phrase.

12. Create the script shown in Figure 10.6 within the Code area of the Conductor sprite.

The repeat until block will loop repeatedly until the playing? variable is set to
0 by the user clicking on the stop (red x) icon in the Stage. Both the step and phrase
variables change (increase) by 1 from time to time and are reset to 1 when they reach
their maximum value. This results in continuous looping playback. The script uses if
[  ] then [  ] statements to check when the variables need to be reset back to 1 (the
start). A rest music block is used to control the speed of the repeat until loop and
it is set to pause for half (0.5) of a beat each time around the loop. A play step message
111

Conducting the Arrangement

(you need to create it) is broadcast on each repetition. This message will be received by the
instruments for playback (we will create them later on). Finally, an update section
message (you need to create it) is broadcast each time a sequence of steps ends.

Figure 10.6
This script counts
through the sixteen
steps of each section
and through the six
phrases of the verse
and chorus structure.

111 

This script contains a number of control blocks nested one inside the other. This is
a powerful method of scripted decision-​making but may take some time to fully under-
stand how it works. Take some time to mentally step through the logic of the script to
appreciate how it operates.

With these scripts in place you should be able to use the Conductor sprite to start and
stop scripts. With the variables set to be visible on the Stage, their values should change
as the program runs. Watch that the variable step loops from 1 to 16 and that phrase
loops from 1 to 6. If things don’t work as expected, check for differences in your scripts
from those shown in the book.
The James Brown song features a number of instruments: bass guitar, drum kit, elec-
tric guitar, horn section, and vocal melody. We will create sprites and scripts for each of
these instruments in turn, focusing initially on the sections that comprise the verse and
chorus.
112

Scratch Music Projects

Bass Guitar
The bass guitar is an important foundation instrument for the funk style that James
Brown is famous for. So, it’s a good instrument to start with. Let’s create our instrument
sprite for the bass guitar part.

13. Create a new sprite and assign it the guitar-​electric1-​a icon from the sprite
library. Name the sprite Bass Guitar.

14. Begin an initialization script for this sprite. In the Code tab add a when [green
flag] clicked events block. Attach to it a set instrument to Music block and
select Bass as the instrument.

Let’s now consider the musical sequence to be played by the bass. There are sixteen half-​
112 
beat steps in one section. Each section is two measures long. Figure 10.7 shows the bass
part for section A as both a sequence of note/​rest values and as notated on a musical staff.

Figure 10.7
Notation of the bass
line for Section A of
the song “I Got You (I
Feel Good).”

We need to set up the sixteen-​step sequence for the bass. The heart of this is a list
that will contain the notes to be played by the bass for one section.

15. Click the Make a List button in the Variables area. Name the new list “bass sequence.”

16. To set up the list with sixteen items add the blocks shown in Figure 10.8 to complete
the initialization script.

Figure 10.8
The complete
initializing script for the
Bass Guitar sprite.
113

Conducting the Arrangement

The if [ ] then [ ] block checks to see if the bass sequence list is empty. If
so, then the list has not been set up and the repeat block loops sixteen times, each time
adding a 0 (zero) to the bass sequence list. In this project the value zero will indicate
a musical rest (silence), so initially the bass part has nothing to play, but we will add note
pitches to the list next.
Each section has a unique sequence of pitches to play. Section A  is the first to be
played and the script for filling the bass sequence list with the music shown in Figure
10.7 is displayed in Figure 10.9. Notice how the notated music is translated into a series
of note pitches and rests (zero values).

17. Add the script in Figure 10.9 to the Bass Guitar sprite. Each replace item Variables
block updates a value in the bass sequence list. The script is triggered by the section
A message which will be broadcast by arrangement scripts that we will add later on. 113 
Figure 10.9
The script for filling the
bass sequence list
with Section A data.

18. In order to play the sequence, add the script shown in Figure 10.10 that starts with a
when I receive [play step] block. This script is short but powerful. It plays the
current step in the sequence when the sequence value is greater than zero (i.e., not a
rest).
114

Scratch Music Projects

Figure 10.10
Bass guitar note
playback script.

This script is called each time play step is broadcast by the Conductor sprite. The
play note music block does the hard work here. Notice that the duration is set to 0.5
beats. The pitch to play is selected using a few blocks. The item [ ] of Variables block
selects item step from the bass sequence list. It adds that value to the current
114  transpose amount (which is zero now, but for other sections will change).

Try playing the sequence now by following this process: i) click the green flag to start
the project, ii) click the bass guitar script starting with the when I receive [sec-
tion A] block to enter the note data into the bass sequence list, then iii) click the
Play button (round green) on the Stage to begin playback.
You should notice that variables including the bass sequence list appear on
the Stage. Remember that the ticked boxes next to variables and lists in the Blocks area
shows or hides these on the Stage. The bass sequence list, when initialized, will be
filled with zeros but when you click on the when I receive [section A] script,
the items update to the section A note pitches.

19. To make the project more visually appealing, add another script to the Bass Guitar
sprite so that the icon animates each time a note is played. The script is shown in
Figure 10.11.

Figure 10.11
The bass guitar visual
animation script.

This animation script starts with a when I  receive [play step] block so,
like the note playback, is triggered on each sequence step. First an if [  ] then [  ]
block checks to see if the current step in the bass sequence is not a rest (i.e., it has a value
115

Conducting the Arrangement

greater than zero). If so, then the image size is increased slightly, to 105%, the script
waits (rests) for 0.25 beats, then a second set size to looks block resets the image
size back to 100%.
One more thing remains to complete our bass part—​add the scripts to update the se-
quence data for the remaining sections. This we will do in preparation for the next phrase,
where we will add scripts to control the arrangement.

20. Add the scripts shown in Figure 10.12 that update the bass sequence notes for
sections B and C. It is quickest to duplicate the section A script and modify in the
duplicate the message it responds to and update the pitch values. You will need to
create new messages section B and section C in the when I receive blocks.

115 
Figure 10.12
The bass sequence
scripts for Section
B and Section C as
scores and scripts.

The Arrangement
For our song form to emerge we want playback to transition from one musical section to
another. This arrangement of sections into a whole song is the basis for musical structure
and form. It is now time to add the script that provides this sectional organization. For
this, we return to the Conductor sprite and its scripts.
In Figure 10.6 we included, toward the end, a broadcast message update sec-
tion. Until now we’ve not responded to that message, but we will now in the following
script.
116

Scratch Music Projects

21. Add the script shown in Figure 10.13 to the Code area of the Conductor sprite. This
script responds to the update section message sent at the end of each phrase’s
playback.

Figure 10.13
The script to change
between sections
and create the
musical form.

116 

This arrangement script consists of a series of if [  ] then [  ] blocks. Each


checks the phrase variable and when it finds a match, updates the transposition vari-
able and/​or broadcasts a section update message to the instrument sprites. Notice that
no update is required for phrase 2 because it is identical to phrase 1 (that is a repeat of
section A). Phrase 3 uses the same note data again (section A) but transposes it down
seven half-​steps (semitones). Phrase 4 continues with the section A material but returns
the transposition to 0 (zero). Phrases 5 and 6 change to new note data by broadcasting
the section B and section C messages respectively. The when I  receive
[Start] script resets the phrase number to 1 following phrase 6, and so the pattern
repeats continuously.
Try running the project now to hear the full arrangement of verse and chorus sections.
117

Conducting the Arrangement

Congratulations, we now have a complete structure for creating a musical arrange-


ment from sections and have implemented this for the bass guitar part. In the remainder
of the chapter we will add other musical parts to fill out the song.

Drum Kit
To consolidate the groove of the song we’ll next add the drum part. Normally drum kit
parts have several drums sounding at once, but to keep things a bit simpler here we will
work with a monophonic drum track. In Figure 10.14 is the notated drum part for section
A. It includes a key for aligning numbers to drums that we will use for the sequence list.

Figure 10.14
Drum score for 117 
section A.

The drums, and other instruments, will have a similar logic to the bass part. So we
will save effort by duplicating the Bass Guitar sprite and make modifications rather than
implementing everything from scratch again.

22. Duplicate the Bass Guitar sprite. Rename the sprite Drum Kit. Add a new costume and
select the drum kit icon. Delete the bass guitar icon.

Navigate to the Code tab so you can edit the scripts.

23. Add a new list using the Make a List button in the Variables area, and name it “drum
sequence.”

24. Modify the initialization script that starts with when [green flag] clicked, so it
looks like that in Figure 10.15. Remove the set instrument to block and change
the length of and add to blocks to refer to the drum sequence variable.

Figure 10.15
The drum kit
initialization script.
118

Scratch Music Projects

The sequence data needs to be updated to match the drum score in Figure 10.14.

25. Modify the script starting with when I receive [section A] so that each
replace item [ ] of block refers to the drum sequence variable and the values
are updated to those listed in Figure 10.14 (1, 3, 2, 3, etc.).

The largest update to the Drum Kit sprite will be to the script starting with when I re-
ceive [play step]. Because there are a number of different drum sounds to be trig-
gered this playback script will need to be elaborated.

26. Modify the when I receive [play step] script as shown in Figure 10.16. It
includes a few nested if [ ] then [ ] else blocks that play different drums for
different values in the drum sequence.
118 
Figure 10.16
The drum kit playback
script.
119

Conducting the Arrangement

The script uses four nested if [ ] then [ ] else blocks and one if [ ] then
block to check the current value in item [step] of [drum sequence]. When a
match is found the corresponding drum sound is played. The reason for nesting these
if [ ] then [ ] else blocks, rather than using a series of if [ ] then blocks (as
was done in the script shown in Figure 10.13), is to make the script more efficient. If the
match is found early in the series of the if [ ] then [ ] checks, then remaining checks
are not required. If you are keen, you can modify the script in Figure 10.13 to match this
pattern for greater efficiency also.

27. Modify the second when I receive [play step] script that provides visual
animation using the set size to blocks, so that it references the drum sequence list
rather than the bass sequence.

The final changes are to the note data for Section B and Section C. 119 
28. Just like we changed the Section A data, modify the scripts starting with when
I receive [section B] and when I receive [section C] so that each
replace item block references the drum sequence list. Then change the values to
match those listed in the lists shown in Figure 10.17.

Figure 10.17
The drum kit sequence
data for sections
B and C.


You should be ready to play back the song with both drum kit and bass parts now.
Give it a go. When playing the music back it can help the timing if Scratch is not doing
too much visual processing. Hide all variable and lists from displaying on the Stage by un-​
ticking them in the Variables area of the Code tab.

Electric Guitar
There is a minimal electric guitar role in the song “I Got You (I Feel Good).” It plays infre-
quent short chords that accent the groove. As with other guitar emulations in previous
projects the Guitar sprite will involve triggering several notes at one time to simulate the
multiple strings being struck as a chord.
Start by duplicating the Bass Guitar sprite and reconfiguring it for the guitar.

29. Duplicate the Bass Guitar sprite. Rename it Electric Guitar. Go to the Costumes tab
and add a new costume, choosing the Guitar-​electric2-​a icon. Finally, delete
the other guitar costumes from this sprite.
120

Scratch Music Projects

Navigate to the Code tab so we can change the scripts to reflect the guitar performance.

30. Create a new list (in the Variables area) and call it guitar sequence. Then edit the
when [green flag] clicked initialization script. Change the set instrument
to block to Electric Guitar. Change the length of and add to blocks to use
the guitar sequence list.

The main changes to the guitar part are in the playback scripts that start with the when
I receive [play step] block.

31. Edit the script that animates the sprite by setting the size by changing the item of
block to use the guitar sequence list.

The other play step script, which plays a note, requires modification and duplication.
120  Our sequence data for the guitar part will continue to use 0 (zero) to indicate a rest, but
the other numbers will indicate a pitch offset value to allow the guitar notes to follow the
song’s chord progression. The chords of the song are D, G, and A major. The note pitches will
be written in D major and the offset values 5 and 7 will shift the notes to G and A chords.
The value of 12 will be used for the D chord, simply shifting the notes one octave.

The 12-​bar blues chord progression, as used by James Brown for this song, is very
common. It uses three chords: the root chord, often notated using the roman numeral
I, in this case D major; a chord built on the fourth degree of the scale, notated as IV,
in this case G major; and a chord built on the fifth degree of the scale, notated as V, in
this case A major. Below is a visual depiction of the 12 bar chord progression.

32. Modify the script that includes the play note block as shown in Figure 10.18. The
if [ ] then [ ] control block checks for data values greater than zero that should
be played. The play note block pitch value calculation includes an extra addition
(+) block. This allows the note pitch to be the sum of the current step value from
the guitar sequence, plus the specified pitch (62, or D above middle C, in this
example), plus the transpose variable.

Figure 10.18
A script for playing
one note of the chord
in the Electric Guitar
sprite.
121

Conducting the Arrangement

33. The previous script sounds one note. To fill out the guitar chord, duplicate this script
twice more and change the base pitch values to 54 (F sharp) and 57 (A). The play
step message will then trigger three sounds together.

To complete our modifications for the guitar part, update the sequence data for sections
A, B, and C.

34. For the scripts that start with when I receive section A, section B, and
section C, change each replace item [ ] of block to use the guitar
sequence list. Update the list data values for each script to those shown in Figure
10.19. Notice that the guitar does not play in section C, so the sequence data is all
zeros (rests).

Figure 10.19 121 
The guitar sequence
values for sections A,
B, and C.

Play the project now to hear all three parts together. Notice that the guitar part subtly
enhances the bass line.

Horn Section
Horn sections were a regular feature in bands in the 1960s when James Brown released
this song, and their popularity persisted through the 1970s and 1980s. A horn section
typically includes a variety of brass and woodwind instruments including trumpet, trom-
bone, and saxophone. To keep our project reasonably simple we will include just two sax-
ophone parts, both played by the one sprite.
The horn section, often called “the horns,” follows many of the musical phrases in
the bass guitar, so we will begin the creation of our Horns sprite by duplicating the Bass
Guitar sprite.

35. Duplicate the Bass Guitar sprite by right-​clicking on it and selecting “duplicate”
from the menu. Name the sprite Horns. With the new sprite selected, go to
the Costume tab, add a new costume, and select a saxophone icon. Delete the
previous costume.

As with previous instruments, some modifications of the scripts copied from the Bass
sprite will be necessary for the horns.

36. In the Code tab of the Horns sprite go to the Variables area and make a new list
named horn sequence. Locate the initialization script that begins with the when
[green flag] clicked block. Change the instrument to saxophone in the set
instrument to block. Modify the length of and add [ ] to blocks to use the
horn sequence list.
122

Scratch Music Projects

Playback scripts will need to be slightly modified and duplicated to enable a second horn
part to sound.

37. In the script starting with when I receive [play step] that includes the play
note block change list references from bass sequence to horn sequence. This
will now play the first saxophone part.

A second saxophone part will play in parallel to the first most of the time and drop out in
section C. Let’s create a script to do this.

38. Duplicate the play step script just modified in Step 37. Two modifications need to
be made as shown in Figure 10.20.

122  Figure 10.20
The script for
playing the second
saxophone voice.

The first change is to add a clause to the if [ ] then [ ] block that only plays if
the current segment number is less than 6 (there is no second horn part in Section C). The
and operator is used to indicate that both conditions must be met. The pitch calculation
in the play note block now includes a subtract (-​ ) block to offset the pitch by eight
half-​steps (a major sixth) so that the second part plays in parallel to the first saxophone.

39. Update the sprite animation script that starts with when I receive [play step]
so that the if [ ] then [ ] block uses the horn sequence list.

Next, the note pitches for the horns need to be added for sections A, B, and C.

40. In the scripts starting with when I receive section A, section B, and
section C change each replace item [ ] of block to use the horn sequence
list and update the note pitch values to those shown in Figure 10.21.

Figure 10.21
The pitch data for
section A, B, and C of
the Horns sprite.

Run the project with the horn part added. Hear how the ensemble sounds richer and
fuller with this part added.

Vocal Melody
The final part to add to our song is the vocal melody. This melody provides a counter-
point to the ensemble arrangement created thus far as it weaves in, out, and across the
123

Conducting the Arrangement

underlying structure. This contrast between the band and melody contributes to the
magic of the song, but means that to script it we are going to have to deviate from the
established patterns. Let’s add the melody part now.

41. Duplicate the Bass Guitar sprite and rename it Melody. Select it and navigate to
the Costumes tab. Add a new costume and select the microphone icon. Delete the
previous costume.

42. Go to the Code tab and then to the Variables area. Click the Make a List button to
create a new list and name it melody sequence.

43. Update the initialization script that starts with the when [green flag] clicked
block. Change the instrument to Choir. Change the list used in the length of and
add [ ] to blocks to melody sequence. 123 
Some small adjustments are required to the playback and animation scripts.

44. Locate the two scripts starting with when I receive [play step]. Change all
references to sequence lists to melody sequence. The melody does not need to
follow the transposition changes, so in the play note block remove the + (plus)
block from the note pitch selection calculation and put back just the item [step]
of [melody sequence] blocks, as shown in Figure 10.22.

Figure 10.22
The updated playback
script for the Melody
sprite.

We are now ready for the biggest change to this sprite, the addition of the note pitch
data for sections A, B, and C. The melody that needs to be scripted is notated in Figure
10.23, as staff notation and as MIDI pitch sequences in Figure 10.24.
Figure 10.23
The notated vocal
melody.

In previous parts, section A  was repeated a number of times. The melody is more
varied, however, and does not follow the same structure. To account for this, when section
A is repeated the melody needs to change to different melody data. As is suggested in the
list data in Figure 10.24, this will be done by creating several different section A scripts
that each use an if [ ] then [ ] block to check for a unique phrase number.
124

Scratch Music Projects

45. Starting with the script that begins when I receive [section A], add an if
[ ] then [ ] block around the replace item [ ] of blocks. Then add the
condition to it, [phrase] = [1]‌as shown in Figure 10.25. Update the note pitch
values for each of the sixteen steps in the sequence to reflect the values of Section A1
in Figure 10.24.

Figure 10.24
The melody as note
pitch lists.

124 

Figure 10.25
The Section A1 script
for the Melody sprite.
125

Conducting the Arrangement

46. Duplicate the when I receive [section A] script three times and update them
for Section A phrase 2, Section A phrase 3, and Section A phrase 4, by changing the
phrase number and note pitch values.

Sections B and C are still only used once, so they need less modification.

47. Update the note pitch values for scripts starting with when I receive [section
B] and [section C] to reflect the values shown in Figure 10.24.

The melody includes a short introduction that precedes the band. To accommodate this, a
couple of changes are required. We need to add the intro section to the Melody sprite and
update the conductor scripts to trigger the intro before the rest of the song.

125 
48. Add the intro script to the melody sprite. Start with a when I receive block and
create a new message intro. Append two play note blocks, one to play 67 for
0.5 beats and the second to play 69 for 1.5 beats. The complete script is shown in
Figure 10.26.

Figure 10.26
The intro script for the
Melody sprite.

The conductor needs a small change to include the intro section. We need to ensure
that the intro notes are played first and, when complete, the rest of the song will begin.
Select the Conductor sprite and navigate to the Code tab.

49. In the script that begins when this sprite clicked, insert a broadcast
[intro] and wait block just prior to the broadcast [Start] block.

The song arrangement is now complete! Run the project and listen to how the melody
interacts with the band and how all the sections fit together. Remember to re-​click the
project’s Stop and Start buttons so the melody lists are initialized before playing the
music from the Start button on the Stage. Try to count the twelve four-​beat measures in
the verse-​chorus pattern. Sing along if you like.
As a final visual flourish, you can add a backdrop image to the Stage from the back-
drop library. Your finished project may look similar to that in Figure 10.27.
126

Scratch Music Projects

Figure 10.27
The Stage from the
completed project.

126 

Remaining Sections
We have created the verse and chorus sections of “I Got You (I Feel Good)” in Scratch.
These are the heart of the song. However, like many songs there are additional sections.
This song includes sections for the solo and bridge. These sections provide contrasting
material that adds richness and variety to the song.
Having successfully implemented the verse and chorus you may like to try adding the
4-​measure solo and 8-​measure bridge sections. This song is quite well known and so you
can search online for transcriptions of the parts to assist your implementation of these
remaining sections. Once you have created the individual sections, you can consider the
overall form of the song. Listen to a recording of the original song and follow the sec-
tional outline below.

Intro
Verse and Chorus 1
Verse and Chorus 2
Solo 1
Bridge 1
Verse and Chorus 3
Solo 2
Bridge 2
Verse and Chorus 4
Verse and Chorus 5
Chorus 6
Chorus 7
Coda
127

Conducting the Arrangement

Summary
In this project we have explored how to divide a song into sections and to recreate and
conduct those sections in Scratch through code. This approach combines the ability of
computers to iterate over and over (almost endlessly) with the repeated patterns often
found in music. By dividing the song into sections and instrument parts we have made
the task of scripting each musical phrase more manageable. From these elements the
song emerges as they are organized into an arrangement.

127 
128

128 
11
Making Games with Sound Effects

In this book so far you have created some interactive music applications. This project will
show how to use these techniques inside a game. We will build the basics of the Witches
and Dragons game and add some sound design elements to the game.
This game is set in the night sky with an objective to fly a witch through the sky gath-
ering magic wands. However, the witch needs to watch out for dragons that also lurk in
the sky at night. Dragons will chase the witch and try to turn her into a toad with their
fiery breath. Try to help the witch catch all the wands while avoiding the dragons. The
screenshot in Figure 11.1 shows the game in action.

Figure 11.1
The Witch and
Dragons game in
action.

There are four sprites in the game: the Witch (the player’s avatar), Wand, Dragon, and
a Home button. We need to create each of them and program their behaviors. But first,
we’ll add some features to the stage.
129

Making Games with Sound Effects

Set the Stage
The Stage will be the location for general game variables and behaviors that are either not
related to any particular sprite or shared by all sprites. When the Start button is pressed
the stage sets up some variables that control the game state and then a “go” message is
broadcast to the sprites.

1. Let’s create some global variables, as shown in Figure 11.2. Select the Stage for
editing, and in the Variables section of the Code tab, use the Make a Variable
button to create variables for counting items named number of wands, number
of dragons, and wands collected. Also create a dragon speed variable for
controlling game difficulty and an is going variable which will be used to indicate
when the game is either running or has ended. Make all these variables global, that is,
usable by all, by selecting the “For all sprites” option when creating the variables.
129 

Figure 11.2
Game variables
displayed with an
initial zero value on
the Stage.

We can now set the value of each of these variables at start-​up using a script starting
with the when [green flag] clicked block.

2. Select the Stage by clicking in the Stage area of the Scratch interface, navigate to the
Code tab, and create the script as shown in Figure 11.3.

Figure 11.3
The Stage’s startup
script.
130

Scratch Music Projects

The script shown in Figure 11.3 uses the set [  ] to block to define values for
each of the variables to their starting values. Notice that it uses pick random for the
number of wands to provide some variety in the game play. The Witch sprite will move
automatically so the witch speed is 6. The is going variable is set to 1 to indicate
the game is running. Finally, the script sends a go message via the broadcast block.

The Witch Character
There are three main characters in our game: the witch, wand, and dragon. Each will have
its own sprite and associated costumes, sounds, and code.
The first character sprite we’ll create is the witch. This is the sprite that the user will
move around the Stage, collecting wands and avoiding dragons. We’ll add scripts that
allow it to respond to motion instructions triggered by key presses.
130 
3. Select the default Cat sprite and navigate to the Costume tab. Click the Choose a
Costume button and select the witch (on a broomstick) costume, as shown in Figure
11.4. Change the name of the sprite to Witch.

Figure 11.4
The witch on a
broomstick costume.

4. Select the Witch sprite, go to the Costume tab, and add a second costume to the
Witch sprite, choose the Wizard-​toad-​a icon.

The Witch now has two costumes, the main witch costume and a toad costume. The cos-
tume will be switched to a toad if the witch gets caught by a dragon. Next create a script
for the witch that sets up its initial conditions.

5. Select the Witch sprite and navigate to the Code tab and reproduce the script shown
in Figure 11.5.

Figure 11.5
The witch startup script
(part 1).
131

Making Games with Sound Effects

This script starts with a when I  receive block that receives the go message
from the Stage at startup. The script sets the size of the sprite to 40% of its original
dimensions. In case it was hidden, the sprite is made visible by the show block. The go
to block positions the sprite in the lower left corner of the screen.
The witch character needs to move around the screen. We will assign arrow keys that,
when pressed, change the direction of movement so the witch can pick up wands and
avoid dragons. Figure 11.6 shows the script that manages this character movement.

6. Add a forever block to the previous script to move the Witch forward and two when
[ ] key pressed blocks to rotate the witch direction when the up arrow or down
arrow keys are pressed, as shown in Figure 11.6.

Figure 11.6
The witch movement
scripts. 131 

You should now be able to start the project and use the up and down arrow keys to
move the witch around the Stage. Make sure to connect the forever loop to the bottom
of the when I receive [go] script. Notice that set rotation style [left-​
right] and the if on edge, bounce block flips the Witch’s direction when it
collides with the edge of the Stage.

The Wand Sprite
The goal of the game is to collect all the wands without being burned by the fire-​breathing
dragon. Wands need to be placed at random locations in each new game to provide variety
and interest. First, let’s create one Wand sprite. Next, we will clone (copy) this sprite to
create many more.

7. Create a new sprite, as shown in Figure 11.7, and name it Wand.

Figure 11.7
The Wand sprite.
132

Scratch Music Projects

For visual variety, create two more wand costumes with different colors.

8. Select the Wand sprite and navigate to the Costume tab. Right-​click on the existing
Costume and use the “Duplicate” option to create the second and third costumes,
then edit their colors.

9. In the costume editor, change the fill color and use the Fill tool to re-​color the star
on the tip of each of the duplicated costumes, as shown in Figure 11.8. Zoom in to
increase the accuracy of your edits. You can retain the shadowed effect of the original
gold star by adjusting the Brightness level of the fill color for different sections of the
star.

Figure 11.8
Editing the wand
132  costume star color.

Add scripts to control the wand’s behavior.

10. At the start of the game create several wands using the script shown in Figure 11.9.
Create this script in the Wand sprite now.

Figure 11.9
The wand startup
script.
133

Making Games with Sound Effects

The script starts with a when I receive Events block to detect the go message
sent by the Stage when the game starts. Then uses a repeat Control block to loop for
the number of wands times. Inside this repeat loop is a create clone of [my-
self] Control block that makes a duplicate of the sprite. We will use the wand clones
in the game, therefore we don’t need the original wand and so the hide block removes
it from the Stage.

Cloning, or automatic duplication, is an example of the power of computers to au-


tomate processes, saving us time and effort. The create clone of Control block
provides a way to dynamically duplicate a sprite while the project is running and can
be repeated a variable number of times, as required. Other related blocks recognize
when a clone is started and can delete a clone. Cloning can be very useful in games
when there are many instances of the one object, like wands or dragons in this game. 133 
It can be useful in other cases too, for example, creating many cars on a road simula-
tion or many violins in a virtual orchestra.

Now let’s position the wand clones around the Stage. We position them at random so that
each time we play the game there is variation, surprise, and a different challenge.

11. Add the script in Figure 11.10 to the Wand sprite. It will affect the clones produced in
the previous script.

Figure 11.10
Script to run when a
wand is cloned.

Each time the create clone of block in the script in Figure 11.9 is run it will
call the when I start as a clone block which runs the script in Figure 11.10. It
is here that we customize each wand. First, the set size to Looks block resizes the
flower to better match the size of the witch. We then choose a random costume. The next
costume block is called by the repeat Control block between 1 and 3 times, using a
pick random Operators block. We move each wand to a random location within the
134

Scratch Music Projects

Stage using the go to [random position] Motion block. The show block makes
the wand visible.
Having created the wand, we next need to add scripts so that they appear and dis-
appear when captured by the witch. A new script checks for collisions between the wand
and the witch. It uses a forever loop to run as long as the game is being played. The
key to this is to detect when the witch character collides with a wand using the Sensing
block touching. The game needs to know when wands have been collected by the witch,
so when a collision is detected the wand disappears and the wands collected vari-
able is incremented. The script also checks if all the wands have been collected and, if so,
broadcasts a win message.

12. Add the script shown in Figure 11.11 as an extension to the when I start as a
clone script in the Wand sprite, below the existing show block. When adding the
broadcast [ ] and wait block, create a new message to broadcast named win.
134 
We will write a script to respond to that message later in the chapter.

Figure 11.11
A script to check for
collisions between
the Wand and Witch
sprites.

The script in Figure 11.11 detects a collision between the Witch and Wand sprites
and makes itself (the Wand clone) disappear. Within the forever block is an if
[ ] then [ ] Control block. The touching [Witch]? condition checks if the
Witch sprite and the Wand sprite have collided. When true, the wand is hidden with
the hide block. The change [wands collected] by Data block increments
(increases) the wands collected variable by 1.  It then checks if all the wands
have been collected using another if [ ] then [ ] Control block that compares
the wands collected with the number of wands. If equal, then the game
has ended, and three remaining blocks are executed. The is going variable is set
to 0 to indicate that the game is over, the witch speed variable is set to 0 so the
witch stops moving, and a win message is sent by the broadcast [win] and
wait block.
135

Making Games with Sound Effects

With this much of the game created you should be able to test it out. Start the game and
move the witch around with the arrow keys to collect all the wands. Make sure every-
thing works as expected; if not, check that the scripts are correct. The witch movements
should halt when you have collected all the wands. You may like to view the variables
number of wands and wands collected to keep track of progress; if so, then tick
their check boxes in the Variables area of the Code tab so they appear on the Stage. At
this stage the game is not much of a challenge, so we’ll add some dragons that need to be
avoided to make the game more interesting and fun.

The Dragon Character
In this game, dragons are to be avoided by the witch as they have the power to turn the
witch into a toad, and to lose the game. Like the wand, we need to create a Dragon sprite 135 
and add some scripts to provide behavior. The scripts are similar, so start by duplicating
the wand as the basis for the dragon.

13. Create the Dragon sprite by duplicating the Wand sprite. Make sure it is named Dragon.
There are several dragon costumes available in Scratch. The default one is a light green
dragon and there is another darker green fire breathing dragon titled Dragon1-​b,
shown in Figure 11.12, that can be accessed from the Costume tab via the Choose a
Costume button, which looks more threatening and so is preferable for this project.
Choose the Dragon1-​b costume for the Dragon sprite as shown in Figure 11.12.

Figure 11.12
Fire-​breathing dragon
costume.

Use a similar cloning technique used for wands to create multiple dragons. However,
all dragons will look pretty much the same, so we won’t bother creating alternate costumes
for them.
Let’s create the script to set up the dragons at the start of the game.

14. Select the Dragon sprite and choose the Code tab. Edit the when I receive [go]
script shown in Figure 11.13 to replace the variable number of wands with number
of dragons.

The script in Figure 11.13 starts with a when I receive [go] Event block that
will respond to the go message from the Stage when the game starts. The repeat
Control block loops over for a random number of times between 1 and the number of
dragons. In each loop it calls the create clone of [myself] Control block which
136

Scratch Music Projects

Figure 11.13
The dragon startup
script.

is set to make a new Dragon sprite. As with the wands, we use only the cloned dragons in
the game and so we can hide the original Dragon sprite with the hide block.
136  Next let’s edit the script to position the dragon clones randomly around the Stage.

15. Edit the when I start as a clone script of the Dragon sprite as shown in Figure
11.14 to randomly position the clones as they are created. Change the set size to
value. Remove the repeat loop that changes costumes.

When the witch collides with any dragon the game ends. Update the code to detect these
collisions.

16. Edit the remainder of the script that checks collisions between the witch and the
dragons, as shown in Figure 11.14. Remove the code related to the wand count,
checking the number collected. Create a new message, lose, for the broadcast
[ ] and wait block.

Figure 11.14
Code for the dragon
clones.

The when I  start as a clone Control block starts the next script. It first
positions the Dragon clone at random within the Stage. The location is determined by
the go to x: [  ] y: [  ] Motion block with values randomly selected to avoid the
bottom left corner of the screen where the witch starts from. The turn degrees block
137

Making Games with Sound Effects

chooses a random angle from 1 to 360 degrees so that the Dragons are moving in dif-
ferent directions. Next, the show block makes the Dragon appear on the stage. The if
[touching {Witch}?] condition is checked regularly inside a forever loop. When
true, the game is stopped and the lose message is broadcast.
Now it’s time to enable the dragons to fly around the space, making the avoidance of
them somewhat more challenging.

17. Extend the previous script to add an additional if [ ] then [ ] block within the
forever loop to provide a continuous random flight path for each dragon around the
Stage. The relevant section of the script is shown in Figure 11.15.

Figure 11.15
A script portion that
adds movement to the
dragons. 137 

In Figure 11.15, the previous script is extended to add independent behavior to


each cloned Dragon. An existing forever Control block ensures that the Dragons keep
moving until the game ends. Inside this is added an if [ ] then [ ] Control block
that checks the is going variable to see that it equals 1, which indicates that the
game is still in play. Inside the if [ ] then [ ] block are three Motion blocks that
move the dragon around the stage. First is the turn [  ] degrees block, which uses
a random number between −2 and 2 to turn the dragon a small amount left or right.
Second is the move [ ] steps block that shifts the dragon a number of steps forward
by using the dragon speed variable. If you want to make the game harder, you can
increase the value of the dragon speed variable in the Stage code. Third, the set ro-
tation style [left-​right] and if on edge, bounce blocks make sure the
dragon does not go off the Stage.

Winning and Losing
Once all the Wands have been collected or the witch has touched a dragon, then the
game is over. As we saw earlier the Wand sprite counts the wands collected and uses the
broadcast block to send the win message. The Dragon sprite has a script that detects
collisions between it and the Witch sprite and, if so, then sends the lose message. At
these times the game should stop and the player should be notified.
We can detect these win and lose messages using the when I receive Events block.

18. Add the scripts shown in Figure 11.16 to the Witch sprite. One is for the win and one
for the lose message. They allow the project to handle these conditions and provide
feedback to the user.
138

Scratch Music Projects

Figure 11.16
Scripts for winning and
losing.

These scripts, shown in Figure 11.16, notify the user of these outcomes using a say
[  ] for [5] seconds block. This command pops up a “voice” bubble for the sprite
with the desired message. In the case of a win the character says “Yay! I collected all the
wands” and when losing it says “Ahh! I got turned into a toad.” Of course, you can change
these messages to say whatever you like.

Adding a Background
138  To locate our game in the night sky let’s add an appropriate background to the Stage.

19. Click on the Choose a Background button in the Stage edit area on the right side of
the Scratch interface. Choose the Stars background. The final game will look similar
to that shown in Figure 11.17.

Figure 11.17
The game with the
Stars background.

Playing the Game
We should now have all the sprites and scripts in place to play the game, so give it a try.
There were many scripts in this game and so it may be that the game does not work cor-
rectly the first time because of a scripting mistake. If so, simply work through all of the
scripts and make sure everything is correctly entered.
139

Making Games with Sound Effects

Click the green flag to start the project. Use the arrow keys to guide the witch around the
Stage collecting wands and avoiding dragons. Experiment with changing the speed of the
witch and the number of wands and dragons to vary the difficulty of the game.
Games need sound. So, in the next few sections, we will add sound and music to the game.

Adding Sounds
To help your game come to life we’ll add sound effects to various actions and some music
in the background. Each of the sprites will require some action sounds: wands disappear,
dragons and the witch fly, collisions happen, and so on. The task of creating sound effects
for actions in movies, TV, or games is traditionally called “foleying.” Therefore, our task
in creating these sound effects is to make decisions like a foley artist. We will use sounds
built into Scratch for this project, but you can also record your own sounds into Scratch if 139 
you prefer, as we explained in Chapter 4.
In addition to creating sound effects, scripts will be written or modified to make the
sounds play at appropriate times in the game. So let’s get started.
As we work through the sounds for each of the sprites in this game, we will discuss
the scripts required to make the sound play when needed. We encourage you to elaborate
on the scripts suggested here to make the game audio even more interesting.

Witch Sounds
As the Witch character moves around the garden we should hear the sounds of flying; but
what are the sounds of flying? We can use a whoosh sound typical of an object speeding
past. You may be able to think of other suitable sounds; perhaps the flapping of the
witch’s clothing in the wind. We will apply the whooshing sounds when the witch changes
directions.

20. Select the Witch sprite, navigate to the Sounds tab and use the Choose a Sound
button to load the sounds, Low Whoosh and High Whoosh. To make the sounds a
bit slower, adjust the playback speed by clicking the << (slower) button in the sound
editing interface. Notice that other effects can be added, such as echo, if you desire.

21. Set the pan value for the sound, that is where it will appear from left to right in the
stereo field, with the set [pan left/​
right] effect to Sound block. Add this
block at the end of each of the when key pressed scripts in the Witch sprite, as
shown in Figure 11.18.

Panning is the term used for positioning a sound between loudspeakers. Typically, this
is between the left and right speakers is the stereo field. In Scratch, a pan position of
0 is in the middle of the stereo field, with −100 being the far left and 100 being the
far right.
140

Scratch Music Projects

The pan position calculation in the scripts in Figure 11.18 assumes that the far right po-
sition of the sprite on the Stage is location −200 and the far right location is 200. These
figures are double the range of the panning values (−100 to 100) so we divide the witch’s
x position by 2 to get the panning value.
We next need to add blocks to our script to play the sounds each time one of the
arrow keys is pressed.

22. Add the start sound block to each when [ ] key pressed script as shown in
the script in Figure 11.18. Select the appropriate Whoosh sound for each script; the
High Whoosh for the up-​arrow key and the Low Whoosh for the down arrow key.

Figure 11.18
Sound playback blocks
added to the witch
140  arrow key scripts.

When we used the start sound block, then if the arrow keys were held down you
would notice that the whooshing sound was played in very quick succession. Using the
play sound until done block forces the script to wait for the sound to conclude
before continuing, thus slowing down the rate of sound repetition; however, it would also
restrict the user’s control over the witch direction.

Wand Sounds
When a Wand is collected, it should make a sound. Typically, in cartoons and movies,
sounds like wind chimes are used for wands, so let’s use a similar sound from the Scratch
library. You may like, instead, to use a “pop” sound that highlights the disappearance of
the wand when collected.

23. Select the Wand sprite, go to the Sound tab, and see if it includes the Magic Spell
sound. If not, then load the Magic Spell sound.

When the game is played, Wands should make a sound when they are collected by the
player. To achieve this, we will add a start sound block to the Wand sprite script,
starting with when I start as a clone.

24. Insert a start sound block into the Wand script after the change [wands
collected] by [1]‌block, as shown in Figure 11.19. Select the Magic Spell
sound for playback from the drop-​down list in the block.

Figure 11.19
The wand script with
sound block added.
141

Making Games with Sound Effects

The location of the start sound block in the script shown in Figure 11.19 is
positioned along with other blocks that are executed when the Wand clone is collected.

Dragon Sounds
Dragons roar while they are flying around so the first sound design task is to create the
threatening roar sound. Because there are several dragons, and each of them may be
roaring, so we will create some variations in the sound playback to make them sound
more like a collective.

25. Select the Dragon sprite, navigate to the Sounds tab, and use the Choose a Sound
button to load the Scream 2 sound.

A human scream may not seem like an obvious dragon sound, but we will use an audio
technique of slowing down the sound to make it sound more like a dragon roar. 141 
26. Go to the Code tab of the Dragon sprite and edit the when I start as a clone
script in two ways. First, insert a set volume to Sound block with a value of 35%
just prior to the forever loop, as shown in Figure 11.20. Second, insert the code
fragment shown in Figure 11.21 into the same script just after the if on edge,
bounce block.

Figure 11.20
Set the volume of the
dragon sounds.

Figure 11.21
A script to randomize
the playback of Dragon
sounds.

The if [ ] then [ ] statement shown in Figure 11.21 is, itself, nested inside the
if [is going] loop, inside the forever loop—​quite a sophisticated decision pro-
cess! The condition for entering the if statement, and playing the dragon roar sound, is
when a random number from 1 to 200 is less than 2 (i.e., it is 1). This is quite unlikely, but
the loop repeats very quickly so it happens quite often when the game is running.
To provide variety in the dragon sound the pitch (higher or lower playback) and pan-
ning location (left-​right stereo position) are randomized. The value for the set [pitch]
effect to block is a random number from −150 to −250. The numbers are negative
because we want to sound to be played back slower, and lower, than the original scream
142

Scratch Music Projects

recording. The value for the set [pan left/​right] effect to block is also ran-
domly chosen, this time between the values of −100 (far left) to 100 (far right). With
these variations set, the start sound [Scream2] block plays the sound.

Stage Music
It is common for computer games to have a music track playing in the background. Scratch
allows us to loop a short musical fragment to provide this continuous background music.
First load the music clip into the Stage.

27. Select the Stage, and navigate to the Sounds tab. Using the Choose a Sound button,
load the Cave music loop that is included with Scratch.

Now let’s write the code to loop the music and turn its volume up and down when the
142  game starts and stops.

28. Go to the Stage’s Code tab and extend the when [green flag] clicked script
with the code shown in Figure 11.22.

Figure 11.22
Code to start and loop
the background music.

The code extension in Figure 11.22 uses a set volume to [30]% block to make
sure the background music is not too loud. Next, a play sound [Cave] until done
block, within a forever loop, plays the music clip over and over. Because the length
of the Cave music clip is not exactly equal to a multiple of its tempo, a wait [0.5]
seconds block is used to correct the timing of the music loop.
We want the music to end when the game ends and so two similar scripts make sure
the sound fades out when the player wins or loses.

29. Add the scripts shown in Figure 11.23 to the Stage.

Figure 11.23
Scripts to fade the
music when the
game ends.
143

Making Games with Sound Effects

The scripts in Figure 11.23 respond to either the win or lose broadcast messages
in the same way. A repeat until loop checks whether the volume level is less than
zero. If not, then the change volume by [−2] block turns down the volume a little,
and the process is repeated until the volume is zero (silent). Note that the music is still
playing, we just can’t hear it.
The when [green flag] clicked script in the Stage includes a set volume
block, which will turn the music level back up again when the game restarts.

Win and Lose Sounds


The final sounds to add are those that help signify the end of the game. We’ll make the
witch laugh when she wins and croak like a toad when she loses.

30. Select the Witch sprite, go to the Sounds tab, and use the Choose a Sound button to
143 
load both the Crazy Laugh and Croak sounds.

31. Move to the Code tab and edit the Witch’s scripts starting with when I receive
blocks as shown in Figure 11.24. To the one that responds to the win message, add a
start sound block and select the Crazy Laugh sound. To the script that responds
to the lose message, add a start sound block and select the Croak sound.

Figure 11.24
The modified win and
lose scripts for the
Witch sprite.

Play the Game with Sounds


Try playing the game now and test out all the sounds. Notice how the addition of sounds
brings the game to life and makes actions seems more realistic. You may find some al-
ternate sounds that improve that game experience, or make it quite funny, and you can
always record new sounds of your own to add and make changes to a script to change the
volume balance between sounds or to incorporate new sounds.

Summary
In this project we have built an interactive game, with logic about game interaction and
checking for end-​of-​game states. The project introduced the concept of sprite clones to
create multiple instances of the one sprite from code. It also included a range of sprite
behaviors including user-​control of the Witch sprite, random placement and collision de-
tection in the Wand sprite, and autonomous movement in the Dragon sprite. Of course,
we also added foley sound to each of the sprites to enhance their character and added
looped background music to the Stage.
144

144 

Interview with Rebecca Fiebrink
Rebecca Fiebrink is a reader at the Creative Computing Institute at University of the Arts
London. As both a computer scientist and a musician, she designs new ways for people to
use computers in creative practice and creates open-​source software tools that make it easy
for anyone—​from children to professional musicians—​to use machine learning in making
music and art. Much of her work is driven by a belief in the importance of inclusion, par-
ticipation, and accessibility. She frequently collaborates with many different people to
create new software, and she is currently working on creating new accessible technologies
for people with disabilities. Rebecca received undergraduate degrees in computer science
and music from The Ohio State University, a masters in music technology from McGill
University, and a PhD in computer science from Princeton University. She has performed
with a variety of musical ensembles, including as a laptopist in Sideband and Squirrel in the
Mirror, as the principal flutist in the Timmins Symphony Orchestra, and as the keyboardist
in the University of Washington computer science rock band the Parody Bits.

How did you get into combining computers and music?


I was always interested in both computers and music. I began playing the piano as a kid,
and for most of my childhood I  thought I  would become a professional musician. Yet
I  always loved computers, as well. My dad brought home a PC in the early 1980s and
I learned a little bit about programming. I also played a lot of video games with my dad
and sister. Computers were always a fun, creative thing for me. I learned more and more
about programming as a teenager, using my graphing calculator to make silly visual art,
then eventually learning the language C++. When it came time for me to go to univer-
sity, it was really hard to choose whether I wanted to study music or computer science,
so I did both. Luckily, while I was at university, I met a professor of musicology who was
writing computer programs to help him analyze and understand music. I was astonished
to learn about all the ways that computers can help people engage with music in new
ways, whether it’s helping us learn new things about music, or making music we wouldn’t
be able to make with acoustic instruments. I immediately knew that I wanted to do work
combining music and computers for my career.

What are you doing now in computers and music?


I combine computers and music-​making when I teach university students and do research.
In my research, I develop new ways for people to use computers in music composition and
performance, as well as other creative practices. A lot of my work explores how we can use
machine learning techniques to make new types of music. For instance, machine learning
can allow us to build new digital musical instruments that use sensors to detect how a per-
former is moving, and that use this information to drive changes in sound produced by a
145

Making Games with Sound Effects

computer. We can program machines that can learn from examples of how we might want
a performer to move, along with examples of the sounds we might want the instrument to
produce in response. The computer then uses these examples to learn the intended rela-
tionship between the performer’s movements and the resulting sounds.
In my teaching, I work with students who—​like me—​don’t want to choose between
doing creative work and using computers. Some of them want to become digital artists or
electronic musicians, while others want to become software developers making the next
generation of technologies used by artists and musicians. I  teach topics including pro-
gramming, creative machine learning, and human perception (that is, the ways that people
perceive the world through hearing and vision). Understanding perception is important if
we want to build machines capable of producing music and art that people will appreciate.

Where can we find examples your recent and current work?


I release a lot of my research work as free and open-​source software. One example is 145 
Wekinator, which is machine learning software I’ve made to help people make new mu-
sical instruments and other interactive systems using machine learning. It’s online at
http://​www.wekinator.org, where people can also see examples of creative projects that
people have made with my software.
There are a lot of examples of me giving talks about my work on YouTube: (https://​
www.youtube.com/​results?search_​query=rebecca+fiebrink).
More information about my research and academic papers is available at my
homepage: https://​www.doc.gold.ac.uk/​~mas01rf/​

What role does coding play in your music activities?


I write a lot of software! Some of this software, like Wekinator, is meant for musicians
and composers to use, to make new kinds of music with computers whether they’re
programmers or not. And I use this software myself, too. Using my own software gives me
a better idea about what it’s good for, and what I might improve about it. I also write com-
pletely new software any time I’m creating a new electronic music piece. I use programming
environments for music and art (especially ChucK, Max/​MSP, and openFrameworks) to
write software that specifies the kinds of sounds I want in a new piece, as well as how the
actions of a performer (which could be me, or someone else) will influence the sound in a
live performance. So for me, making music often means being a composer, a performer, a
software developer, and an interaction designer at the same time.

What tips or advice would you give young coders working with music?
Learning to code is like discovering a magical gateway into a world of new possibilities.
Explore these possibilities! Have you figured out how to recreate your favorite pop song
with code? Great! Now try composing a new song that’s all your own. Or see if you can
create new types of music that are easier to make with code than without it—​or even types
of music that are impossible to create without a computer! What do you come up with? Can
you make new sounds that nobody in the world has ever heard before? Can you make mu-
sical “instruments” that you can interact with to perform music that you could never make
with a piano or a violin? What else could you create, that nobody before you has ever made?
146

146 
12
Generative Music

In many cases when creating music with Scratch we have a musical outcome in mind and
then create the sprite and scripts to realize that music. A good example of that was the
recreation of the song “I Got You (I Feel Good)” in Chapter 10.
In this project we are going to focus, instead, on writing scripts that generate new
music that has never been heard before. This does not mean the music will be radical or
crazy, just that the computer will make choices as the program runs based upon rules
and constraints we provide. As a result, we will have a good idea about the style of music
it will create, but not the precise notes that it will generate. In a sense the computer is
improvising. And, as with human improvisation, this can lead to both exciting and un-
predictable outcomes.
To reinforce the idea of Scratch generating a musical improvisation, this project will
make music in a jazz style where improvisation is common. This project will see you build
a program that imitates a jazz trio of vibraphone, double bass, and drums. The Stage for
the final project is shown in Figure 12.1. We will start by working on scripts that generate
a melodic solo.

Figure 12.1
The project Stage
featuring the jazz trio.
147

Generative Music

Random Selection
One of the simplest ways to generate music with some variety is to include random (un-
predictable) choices in the notes that it plays. This is not too hard to achieve because
coding environments, including Scratch, usually include a random command (block). We
will start developing our solo improvisation generator using randomness and develop it
further in the following sections of this chapter. Let’s get started.
The first instrument for this project is a vibraphone. Start a new project and setup the
default sprite for this instrument.

1. Rename the sprite “Vibraphone.”

2. Select the Costumes tab for that sprite and add an image of a vibraphone as a new
costume. The scratch image library may not contain a vibraphone image; if it doesn’t,
then download one by Chrom72 from Pixabay at http://​explodingart.com/​smp/​
images/​vibes-​colored-​400.png. Delete the previous costumes. 147 
Let’s write a script to play random notes over the two octaves above middle C.

3. Add the Music blocks extension. Click the Add Extension button in the bottom-​left
corner of the Code tab and activate the Music extension.

4. Reproduce the script shown in Figure 12.2 to generate notes at random.

Figure 12.2
Playing random
notes with pitches
selected from over a
two-​octave  range.

The script in Figure 12.2 starts with a when [green flag] clicked block.
Next it selects the vibraphone instrument with the set instrument to block.
Following that a forever Control block enables the script to loop indefinitely. Inside
the loop the play note block uses a pick random block to select a pitch number
between 60 and 84. These are the MIDI pitch numbers from middle C (60) to the C two
octaves higher (84). The duration of each note is set to 0.25 beats.
Click this script to start it and listen to the scattered patterns of notes that result.
Click again to stop the script.
This is a good start for our solo and is simple to achieve. The first step to improve the
generated music is to limit the pitches that are used. It is common to limit the pitches
used in music to a particular scale or mode. Our current script can use any pitch in the
two-​octave range. To constrain the notes used to a particular scale our script will make a
random selection from a list of allowable pitches.
148

Scratch Music Projects

A scale (or sometimes called a mode) is a selection of pitches within the octave.
Typically, in Western music scales use eight of the twelve pitches in the octave. For
example, the C major scale uses C, D, E, F, G, A, and B and ignores C♯, D♯, F♯, G♯, and
A♯. These scale pitches can be used across the range of the instrument; that is, any
note (high or low) whose pitch is part of the scale is allowed. In C major, any C, D, E,
and so on, is a scale tone.
A pentatonic scale is a scale with just five notes in the octave. This provides an
even more constrained selection of notes that avoid possibly inharmonic sounding
notes. A C major pentatonic scale includes the pitches C, D, E, G, A.

First, make the list in which we can store allowable pitches.

148  5. In the Variables section of the Blocks area use the Make a List button to create a new
list called pitches.

6. Reproduce the script in Figure 12.3 in the Vibraphone sprite to add the desired
pitches (those from the C major pentatonic scale) across the same two-​octave range
into the pitches list.

Figure 12.3
Initializing the list with
desired pitches for the
solo to select from.
149

Generative Music

The script in Figure 12.3 is run when the project is started, as indicated by the when
[green flag] clicked block. An if [ ] then [ ] block checks to see if the list
is currently empty: if length of equals zero. If true (the list is empty), then a series of
add to blocks inserts the desired pitch values into the list.

7. Click the Start button to run the script. If the pitches variable is displayed on the
Stage then the values should appear in the list. Click the checkbox next to the variable
to set/​unset its appearance on the Stage.

Now that we have a list of pitches, we can modify the music generation script to select
randomly from it.

8. Update the music generation script to only choose notes from the pitches list, as
shown in Figure 12.4.

Figure 12.4
Randomly selecting
149 
notes from the
pitches list.

Within play note, the random block provides variety in our generated solo by
choosing an item of pitches between 1 and the length of the pitches list.
The list of pitches constrains the note choice to just those from a particular scale.
Start the playback by clicking the play note script. Hear that the solo sounds
more harmonious with the restricted pitch choice. The notes in the pitches list com-
prise a pentatonic (five-​ note) scale, which ensures that notes in any combination
sound okay.
One problem with the music so far is that the solo is not very melodic because it
jumps all over the pitch range in an unnatural way—​literally at random. In the next sec-
tion we’ll look at a technique to provide a smoother melodic contour to our solo.

A Random Walk
The flow of a melodic line, as used in jazz solos, is made up a series of small pitch steps
between notes with the occasional larger jump. To restrict our generated solo to smaller
pitch steps, but still maintain variety, we will use a process called a random walk.
A random walk applies a random value to the size of the pitch step (or interval) be-
tween each note rather than selecting the pitch from anywhere within the pitch range.
The difference in result looks like that shown in Figure 12.5. The random selection, on
the left, causes small or large jumps from note to note, producing an angular contour
(shape) for the melody, whereas the random walk only allows smaller steps and produces
a smoother overall contour. The random walk process gets its name from the fact that it
focuses on the step size from one note to the next (like we do when we walk) rather than
jumping anywhere within the pitch range.
150

Scratch Music Projects

Figure 12.5
A visual comparison of
random versus random
walk outcomes.

To calculate the pitch step size between notes, a random walk process needs to keep
track of the current pitch value in order to calculate the next value. We will use a variable
so that our script can remember which pitch from the list was last used; this variable will
be called the pitches index because it keeps track of a location (an index) within the
pitches list.

9. Create a new variable and name it pitches index.

150  10. Modify the play note script, as shown in Figure 12.6, to use this variable when
calculating the random choice for the next note.

Figure 12.6
The random walk
version of the note
playback script.

The script in Figure 12.6 adds a set [ ] to block before the loop to give an initial
value to pitches index. In this case we set it to 6, which is in the middle of the list.
The play note block now plays with the pitch at that index. A new change [ ] by
block is added within the loop to choose a new value for pitches index that will be
used for the next note. The new value is changing the current value by a random number
between −2 and 2. Negative numbers will mean the melody moves down the pitch list;
positive values means it moves up the pitch list. Restricting the values to 2 or −2 means
that the distance between notes will be no more than two items in the list.

Click the play note script to hear it. Listen to the musical result and hear how the
contour of the solo is much smoother. Experiment with different values of the random
number, for example change it to −5 and 5 to allow a bigger range of step sizes and thus
more variety.
You may hear that after a time the music plays unexpected pitches, often very low.
This is because the random walk “wanders” off the ends of the pitch range and attempts
to play unknown pitch values. Let’s add some code to prevent that.
151

Generative Music

We can add code to set up boundaries that prevent the pitches index value from
“walking” off either end of the pitches list.

11. Update the play note script to match that shown in Figure 12.7 which includes an
additional if [ ] then [ ] statement within the forever loop.

Figure 12.7
The random walk
script with boundary
conditions.

151 

After generating an updated value for the pitches index, the script in Figure
12.7 adds a new rule to the selection of the next note. It uses if [  ] then [  ]
statements to check if the new value exceeds the boundaries of the list. If so, then set
to blocks reset the pitches index back to the center of the range (list index number
6). The heart of these boundary checks are the conditional statements in the if [  ]
then [ ] blocks. Less than (<) and greater than (>) blocks are conditions within the
if [ ] then [ ] block. The greater than (>) block check is the pitches index
value exceeds the top of the list, calculated using a length of pitches block. The
less than (<) block checks if the pitches index is off the bottom (below index 1) of
the list.
Play back the play note script with these additions by clicking on it and hear that the
smooth melodic flow continues. Click on the script again to stop it.
We have so far created a flowing melodic solo. However, as previously stated, an in-
teresting melody has both smoothly flowing passages and occasional larger jumps. We will
add one more rule to our play note script to add some larger jumps to the melody. We
will do this in a way that reinforces the tonality of our scale by jumping to the root note
(C in this case) on strong beats in the meter.
Thus far we have not been concerned with locating meter or downbeats in our melody,
so to account for this now we need to add another variable that will count the pulses in
the music. By resetting the pitch to the root note (C) of our scale every two measures
(eight beats) this will help provide some sense of phrasing to the melody.

12. Add an additional if [ ] then [ ] statement to the forever loop to insert a rule
for setting the pitch index variable to index 6 (middle C) as shown in Figure 12.8.

13. Finally, create a variable called pulse count and add a change [pulse count]
by 1 block to the bottom the forever loop so that the pulse count is incremented
each time a note is played.
152

Scratch Music Projects

Figure 12.8
The play note script
with an added rule for
setting the pitch index
to a root note of the
scale on each eight
pulse steps.

152 

The new if [ ] then [ ] block in Figure 12.8 looks to see if the pulse count
is a multiple of 8 using the mod (modulo or remainder) block. If this is true, then musi-
cally we are at the beginning of a two-​bar phrase and will jump to the root note (index 6).
Play this updated script and hear how there are occasional larger leaps to the root
note that interrupt the otherwise smooth flow of the melody.
We have created an interesting melodic contour with the generative rules so far. But
the constant and regular rhythm does get tedious after a while. It’s now time to add some
more rhythmic variety to the vibraphone solo.

Rhythmic Interest
One of the rhythmic hallmarks of jazz performance is the use of rhythmic “swing” feel.
When a rhythm is played with a swing technique pulses alternate between longer and
shorter durations, rather than the standard or “straight” technique where each pulse is the
same duration. The differences between a straight and swung performance is depicted vis-
ually in Figure 12.9, in which each block is a note and its length equates with the duration.

Figure 12.9
A visual comparison
of straight and swing
performance timings.

Our method of implementing the swing feel will be to alternate between two play
note blocks with different durations. An if [ ] then [ ] else [ ] block is used
to check if the pulse count is either odd (a downbeat) or even (an upbeat) and play
the appropriate note duration.

14. Replace the play note block in the play note script with the if [ ] then [ ]
else [ ] code segment shown in Figure 12.10.
153

Generative Music

Figure 12.10
A code segment to
alternate between
notes with different
durations to create a
swing feel.

In the script in Figure 12.10 a mod block is used to check if the pulse count is an
even number (divisible by 2). If the pulse count is even, then a longer note is played
(0.35 beats); otherwise a shorter note is played (0.15 beats). Notice that the two duration
values (0.35 and 0.15) add up to 0.5 so that the general tempo is maintained from the
previously straight rhythm where every note was 0.25 beats long and two of them added
up to 0.5 as well. 153 
Having inserted the replacement code for the swing feel, play back the music and hear
the difference in the music.
The difference in duration between notes is vital to the swing feel. The alternating of
dynamics (i.e., loudness) between each note, however, is also crucial to developing a less
“mechanical” swing. We can implement this by changing the volume of each note.

15. Add set volume blocks prior to each play note block as shown in Figure 12.11
and add pick random blocks with different ranges of values so that the longer block
tends to be louder and the shorter block tends to be softer.

Figure 12.11
Swing feel accented
by alternating changes
in volume as well as
changes in duration.

The volume changes occurring in Figure 12.11 only apply to the current sprite. This
allows us to control the volume balance between sprites (instruments) and we will utilize
this function later in the chapter to independently control the volume of other instruments.
Even though we have added swing feel and dynamic expression to the rhythm, the
music is still very constant and unchanging. The next adjustment to the script will intro-
duce small gaps (rests) into the rhythm so that the rhythmic patterns vary in more in-
teresting ways. The changes introduced will randomly replace the short (downbeat) notes
with a rest to provide this variety.
154

Scratch Music Projects

16. Surround the shorter note with an if [ ] then [ ] else [ ] block that plays
either a note or a rest at random. The code for this is shown in Figure 12.12 and is a
further modification of the play note script.

Figure 12.12
A script that adds
occasional rests into
the rhythm.

154 

The method of choosing a note or rest in Figure 12.12 involves an if [ ] then [ ]
else [ ] block with a < (less than) condition block which compares a random number
between 1 and 10 to see if it is below a threshold, in this case a value of 8. If true then the
note is played, otherwise the rest is played. So, in this case there is a 80% chance of a note
being played. By changing the threshold value, the rhythm can be made more or less busy.
Try experimenting with this value to hear the difference.
The full vibraphone solo script is shown in Figure 12.13. Compare your code with this
to ensure all is correct.
We now have a vibraphone solo that generates some interesting solo phrases with
some swing-​style expression. Now seems like a good moment to introduce some new
instruments into our ensemble, which we’ll do in the next section.

Filling Out the Trio


Jazz ensembles are often underpinned by a steady drum pattern, so in this section we will
start by adding a drum kit part before adding a double bass instrument.
As in previous projects, one consideration is to have multiple instruments play to-
gether in time. The vibraphone is already establishing the beat, so we’ll use it as the
basis for other parts to coordinate with by broadcasting the down beat and up beat
messages at the right times.
155

Generative Music

Figure 12.13
The full vibraphone
solo generation script.

155 

18. Insert broadcast blocks into the vibraphone play note script that will message
the beat to other instruments. Add a broadcast block immediately after the if and
else lines of the main if [ ] then [ ] else [ ] block in the play note script.
Create new messages for each, down beat and up beat. The modified section of
the script is shown in Figure 12.14.

Let’s add a Drum Kit sprite and add scripts to pick up those broadcast messages.

19. Add a new sprite. Select the drum kit icon.

Add two small scripts to the Drum Kit sprite to accept the beat messages and play appro-
priate sounds.

20. Select the Drum Kit sprite and navigate to the Code tab. Add two scripts that start
with a when I receive block and follow each with a play drum block as shown
in Figure 12.15. The first script should look for the down beat message and play the
Closed Hi-​Hat for 0.35 beats. The second script should respond to the up beat
message and play the Open Hi-​Hat for 0.15 beats.
156

Scratch Music Projects

Figure 12.14
Adding broadcast
messages to the
vibraphone play
note script.

156 

Figure 12.15
Drum kit scripts to
play open and closed
hi-​hats.

Start the project to hear the vibraphone solo with simple drum kit backing.
With this hi-​hat backbone in place, the drum kit can be filled out with a regular bass
drum part and randomly varied side stick parts. This combination of steady and varied
drum parts helps to generate a lively drum kit performance. Add similar scripts to those
for the hi-​hats for the kick drum and side stick.

21. Add the scripts shown in Figure 12.16 to the Drum Kit sprite. These scripts respond to
the down beat and up beat messages but use if [ ] then [ ] blocks to add a
condition (rule) about when they should play.

The scripts in Figure 12.16 show that the bass drum responds to the down beat
message, but only plays each eight pulses; when the pulse count is divided by 8 and has a
157

Generative Music

Figure 12.16
Drum kit scripts to
play bass drum and
side stick.

157 

remainder of 1 (every two beats). The side stick responds to the up beat message and
plays at random 40% of the time.
Having generated a drum kit part it is now time to turn our attention to the last
member of the trio, the double bass.

22. Add a new sprite. Select a double bass icon if there is one and name the sprite Bass.
The scratch image library may not contain a double bass image; if it does not then
download one by Luc Mahler from Pixabay at http://​explodingart.com/​smp/​images/​
double-​bass-​200.png. (See Figure 12.17.)

Figure 12.17
The Bass sprite icon.
158

Scratch Music Projects

Go to the Code tab for the Bass sprite and add the scripts for generating the bass part.
As with the other parts we will use a combination of fixed and randomized notes to create
a bass line that has stability but contains constant variation within fixed boundaries.
First, let’s set up the initialization script for the bass.

23. Start a new script with a when [green flag] clicked block. Add to it a
set instrument to Music block and choose the Bass instrument. Then add
a set volume block and specify a value of 60 percent. The full script is shown in
Figure 12.18.

Figure 12.18
The Bass sprite
initialize script.

158 

Next, we can write the music generation script for the bass. It will respond to down
beat messages, and not the up beat messages, because the movement of a bass part
is usually more steady than the vibraphone.

24. Create the script shown in Figure 12.19 in the Bass sprite.

Figure 12.19
The bass line script.

The bass script in Figure 12.19 plays a root note at the beginning of every eight-​
pulse sequence. This rule is implemented by the first if [ ] then [ ] condition.
The modulo (remainder of the division) of the pulse count and 8 is checked and if
it is equal to 1 (at the start of a cycle) then the play note block selects the first item
in the pitches list and plays it two octaves (−24) lower, in a range more suitable for
the bass.
The else clause looks after the other down beat pulses (i.e., on the remaining seven
steps of the down beat sequence). To provide some space in the bass line an if [  ]
then [ ] condition randomly lets 80% of the pulses trigger the play note block. To
achieve this the pick random block selects a number between 1 and 10 and the less
than (<) block checks if this number is less than 8; if so, the note plays. When these notes
159

Generative Music

play, they randomly select a pitch among the first five in the pitches list (i.e., C, D, E, G,
or A) to play and, again, transpose the pitch down two octaves into the bass range.
These offbeat notes are sounded for just 0.25 beats compared to the 0.5 beats of the
note on count 1 of the cycle. This provides more emphasis on the first note so that it is
more clearly heard as musically significant.

So now we have created the whole trio: the vibraphone solo, the drum kit, and the double
bass. Take some time to play back the parts and have a listen to how they fit together. You
can adjust some of the values in the rules to see what difference it makes to the music.
In the remaining sections we will look at elaborating the program by adding some
visual effects and a backdrop, changing chords so the music is more interesting over time,
having the ability to mute (silence) some parts, allowing for an interactive arrangement
to be performed.
159 
Animating the Instruments
There are three instruments playing in our jazz trio project. However, the instruments
are static on the stage, so now it’s time to provide some animation as they play to make
the project more interesting to look at. For each instrument we’ll make the icon move
each time a note is played, and we’ll add slightly different methods of animation to each.
To animate the vibraphone, the image will bounce slightly on each note by tempo-
rarily changing its position on the Y axis.

25. Select the vibraphone sprite and navigate to the Code tab. For each play note
block in the script insert change y by blocks before and after, as shown in Figure
12.20. Set the values to 2 and −2, so that the icon shifts up slightly as the note is
played and then returns back to its original position when the note stops.

Figure 12.20
Vibraphone script
section showing
position move blocks.

To animate the drum kit we will slightly increase the image size on the down beats
and restore the size on the upbeats.

26. Select the Drum Kit sprite and navigate to the Code tab. Insert a set size to
block before the play drum [Closed Hi-​
Hat] block in the when I receive
[down beat] script and set the value to 102 percent, as shown in Figure 12.21.
Make a similar change to the script starting with the when I receive [up beat]
script that includes the play drum [Open Hi-​
Hat] block, but set the size value to
100 percent.
160

Scratch Music Projects

Figure 12.21
The downbeat hi-​hat
script with resize block
inserted.

To animate the Bass sprite, we will randomly rotate the icon back and forward slightly.

27. Select the Bass sprite and navigate to the Code tab. Insert a point in direction
block just prior to each play note block in the scripts. Add a pick random block
to each point in direction block and set the values to 85 and 95, as shown in

160  Figure 12.22.

To complete the visual update of this generative Jazz Trio project, add a concert back-
ground backdrop to the Stage.

Figure 12.22
Randomly changing
the direction of the
Bass sprite.

28. Click on the Choose a Backdrop button of the Stage area of the Scratch interface.
Select a background appropriate for a live performance, such as Theater 2. The
project should look similar to that shown in Figure 12.1 at the top of this chapter.

Having added some visual interest to the project, we will next turn our attention back to
updating the music by adding scripts for making chord changes.

Chord Progression
Jazz music is made more harmonically interesting by regularly changing the underlying
chords, and therefore the notes the melody will play on top of those notes. In keeping
with the project’s focus on generative music we will implement a randomized chord pro-
gression that chooses chords used in the 12-​bar blues progression. In the key of C major
these chords are C, F, and G.
The chord progression behavior will require a variable to keep track of the current
chord via its fundamental tone, known as the “root” of the chord.

29. Create a new variable and name it root.

Because the bass provides the chord foundation within the ensemble, we will add the
chord change script to that sprite.

30. Select the Bass sprite and navigate to the Code tab. Add a new script that responds to
the down beat message, as shown in Figure 12.23.
161

Generative Music

Figure 12.23
Chord change script.

The chord change script in Figure 12.23 receives the down beat message and uses
an if [ ] then [ ] block to check if it’s time to change chord. The mod block is used
to make the condition true only once every thirty-​two pulses. At these times the set to
block changes the root variable to a random value between −1 and 1; that is either −1, 0
or 1. The value is then multiplied by 5 to produce −5, 0, or 5. This root value will be used
to transpose the pitches to match the chords G (−5 half-​steps below C), C (0 distance away 161 
from C) or F (5 half-​steps above C).
The final step in implementing the chord progression is to adjust the play note
blocks so they take account of the current root value. The play note blocks that
need updating are in the scripts found in the Bass and Vibraphone sprites. The Drum Kit
sounds are unpitched and so don’t need to change to follow the chord progression.

31. Locate all play note blocks in the project (don’t change any play drum blocks) and
add  . . .  + root to the pitch selection code. An example of one updated play
note block is shown in Figure 12.24.

Figure 12.24
Adding the root value
to the pitch selection in
a play note block.

Start the project with these changes made and listen to the changing harmonic context
and how this adds more variety to the music.

Muting Parts
When an ensemble, like a jazz trio, play together, they all play but at certain times one or
more members drop out for a while, giving the remaining musicians more musical space
and more time in the limelight. In this section we’ll add scripts to each of the sprites to
allow them to switch between playing or being muted when they are clicked.

32. We need to create variables for each instrument that can indicate if an instrument
should be muted. Create three new variables and name them vibes mute, drums
mute, and bass mute.

33. Add a script to each sprite that sets each mute variable to either 1 (muted) or 0
(unmuted). The script in Figure 12.25 shows how this is done for the vibraphone. Add
this script to the Vibraphone sprite and add similar ones to the Drum Kit and Bass
sprites but change the mute variable to match the instrument.
162

Scratch Music Projects

Figure 12.25
A script to set the
mute variable for the
vibraphone.

We have set the mute variable, but to silence an instrument we need to prevent it playing
notes. To achieve this, we will add an if [ ] then [ ] else [ ] Control block
162  around each play note block with a condition that checks the state of the instrument’s
mute variable and plays either the note or a rest of equivalent duration.

34. For each play note block in the Vibraphone and Bass sprites and for each play
drum block in the Drum Kit sprite, surround the blocks related to playing the note
(including visual animation and volume blocks) with an if [ ] then [ ] else
[ ] control block that has a condition checking that the mute variable equals 0. If true
then the note is played, otherwise a rest is played. A code section demonstrating this
from the Vibraphone sprite is shown in Figure 12.26.

Figure 12.26
A code fragment
showing the addition
of an if [ ] then
[ ] control to mute/​
unmute an instrument.

The condition in the script in Figure 12.26 works by checking the instrument’s mute
variable state and if it equals 1 the note playback and associated actions are evaluated.
Otherwise a rest is played and so the instrument is muted (makes no sound). Technically,
the drums and bass don’t need the else clause—​it is the vibes that control the timing—​
so you can use an if [ ] then [ ] block for them if you prefer.
With these changes in place the instruments should mute and unmute as you click on
the sprite icons in the Stage. Make sure that works before moving on.
Finally, let’s add a visual cue about the mute status of the instrument. We will make
the icons appear dim when the playback is muted.
163

Generative Music

35. In the when this sprite clicked script for each instrument, Insert a set
effect to Looks block after the set [instrument mute] to [1]‌block where
[instrument mute] is the appropriate variable vibes mute, drums mute, or
bass mute. Set the effect to brightness and the value to −30. Then, insert a
clear graphics effects block after the set [instrument mute] to 0
block. An example of this for the Vibraphone sprite is shown in Figure 12.27.

36. Insert a set [variable] to [number] block near the top of the when [green
flag] clicked script for each instrument with the variable set to the [instrument
mute] for that instrument and the value of 0. Add a new script to the Drum Kit sprite
that currently does not have a when [green flag] clicked script.

Figure 12.27
An updated mute
script for the
Vibraphone sprite. 163 

With the mute script changes in place, experiment with running the project and
muting various instruments and hearing the effect of different combinations within
the trio.
One final change that can be useful is to vary the tempo. A single set tempo block
can be placed in any sprite and clicked to specify a new tempo. The default tempo around
which this project is designed is 60 bpm so other values not too different from that are
most likely to be effective for this project.

Summary
In this project on generative music we have developed a jazz trio featuring a me-
lodic solo performed by the vibraphone with a rhythm section of drums and bass
164

Scratch Music Projects

with their own, more modest, generative music scripts. A significant use of the ran-
domness has formed the basis of novelty and variation in the parts. In particular we
explored the random walk process in the solo part, but these have been balanced by
elements of predictability, stability, and constraints to produce a reliable but always
varying outcome.

164 
165

13 165 

Video Interaction

There are a number of ways to interact with your music scripts in Scratch. So far, we’ve fo-
cused on keyboard and mouse interaction, because these controllers are very common. It
is possible to also utilize external hardware controllers that send key or mouse messages
such as the Makey Makey.1 In this project we focus on using your device’s camera as a
controller.

Video Sensing
Scratch includes an extension library to detect motion in the video stream from your
computer’s camera. The Video Motion extension in Scratch includes blocks that do algo-
rithmic video analysis to recognize movement in a scene. Even if the camera is stationary,
then the image it sees will have still and moving elements. For example, the moving ele-
ment might be a person dancing, and the background scene will be mostly still. As we’ll
see in detail below, the sensitivity of the motion detection can be set so only deliberate
movements will trigger events and small changes, such as the swaying of a tree branch in
the background, will be ignored.
In Scratch, the video image can be shown on the Stage and motion can interact with
sprites that are also on the Stage. Another feature of the video motion blocks is the ability
to detect overlap between the areas of video motion and the sprite locations. We will
use these collisions as a trigger to start and stop music playback. This will allow motion
gestures, such as waving your hand, to control the music.

Screen Layout for Interaction


An important consideration for video motion projects is to design the location of sprites
so they coordinate with moving elements in the video image. In this project we plan for
a person’s head and shoulders to be in the middle of the screen, with sprites in the upper
right and left of the screen as depicted in Figure 13.1. When the user’s image is overlaid
these sprites can be “touched” by the user’s hands as they raise them beside their head.
There are four musical elements in this project: drum kit, snare drum fill, bass guitar,
and electric guitar. Each can be triggered separately as the user waves their hand over
the sprites. They are arranged so that as hands are raised, the drum and bass parts are

1 http://​makeymakey.com
166

Scratch Music Projects

Figure 13.1
The Stage layout for
this project.

166 
triggered first, then as hands are raised higher the snare and guitar parts are added for
further musical intensity.

Turning Video On and Off


To start the project, activate the video motion Extension blocks and add scripts to the
Stage to turn the video camera on and off.

1. Select the Stage editor (on the right of screen) and navigate to the Code tab. Click the
Add Extension button in the bottom left corner of the Scratch interface and select the
Video Sensing extension. Be sure to add the Music extension block, using the same
process.

2. Add three scripts to the Stage that each use the turn video block as shown in
Figure 13.2. One script turns the video camera on when the project is started. The
other two use the up and down arrows to turn the video on and off, which can be
useful to avoid unnecessary triggering during development and performance.

Figure 13.2
Stage scripts to turn
the video camera on
and off.

Try these scripts to make sure your video setup works as expected. The video image
from your computer’s camera should appear in the Stage. Your computer may ask per-
mission to access the video camera as a precaution; answer Yes to use this feature. The
following sections will discuss the scripts for each musical sprite in turn.
167

Video Interaction

Drum Kit
The drums will form the backbone of our music and will need to be triggered for other
parts to play. The critical block that detects motion in the video is video [motion]
on [sprite]. This block returns a value, zero or greater, and represents the amount of
movement under the area of the sprite. Our scripts will use this block inside an if [ ]
then [ ] block to check for movement and play music when movement is detected.

3. Create a new sprite and select the drum kit icon. Position it on stage in the location
shown in Figure 13.1. Navigate to the Code tab for this sprite.

4. Add a new script to the Drum Kit sprite as shown in Figure 13.3.

Figure 13.3
The initial drum kit
script.

167 

The script in Figure 13.3 starts with the project and runs forever, checking if the
video motion below the Drum Kit sprite is at a level greater than 5. If so, then the beat
message is broadcast (this does not do anything yet but will be a signal to the other music
sprites) and it plays a simple pattern with the kick and snare drums. Repeated video
movement over the drums icon on the Stage should make it play continuously. Make sure
to give it a try before moving on.
Now let’s use the beat message to trigger another part of the drum kit, the hi-​hat.
To add interest to the hi-​hat part the script randomly chooses between a one-​quarter-​
note or a two-​eighth-​note rhythm. Using the beat message to trigger this script
ensures that it stays in time with and can play in parallel to the kick-​snare script. We’ll
also use this coding technique to synchronize the other instruments in this project with
the drums.

5. Start a new script in the Drum Kit sprite with a when I receive [beat] block and
complete it as shown in Figure 13.4.

The hi-​hat script in Figure 13.4 uses a random selection to give the single eighth-​
note pattern a 80% chance of occurring, and the two sixteenth-​note patterns occurring
168

Scratch Music Projects

Figure 13.4
The hi-​hat script.

168 

with a 20% likelihood. This creates variety and unpredictability that is in contrast to
the stable kick-​snare pattern and adds interest to the drum kit part. These patterns are
one-​quarter of a beat long, so the repeat [4] block is used to loop the process to fill
the beat.

Snare Drum Fill
To enhance the drums we will add a second sprite that, when touched by the video image,
will play a more elaborate snare drum pattern, commonly called a “drum fill.” This involves
creating a snare drum sprite, write a script to check when there is motion behind it, and
trigger a drum fill. We will also modify the drum kit script to add the drum fill music
blocks. Let’s go.

6. Create a new sprite, name it “Snare Drum,” and select the drum-​snare-​a icon.
Position the sprite above the drum kit on the stage, as shown in Figure 13.1.

7. Select the snare drum and navigate to the Code tab. Create a new variable named
drum fill. Untick the variable so it is not visible on the Stage. This variable
will be set to 1 or 0 to indicate if a drum fill should be played or not. By default,
variables start out as equal to zero, but it is always best practice to include code that
deliberately sets an initial value.

8. Build the script shown in Figure 13.5, starting with a when [green flag] clicked
block.

This script continually checks for motion on the Snare Drum sprite. When the mo-
tion level is above 5 it sets the drum fill variable to 1. We will next modify the drum
kit script so that it responds musically to this change in drum fill variable.
169

Video Interaction

Figure 13.5
The script for the drum
fill trigger.

In drumming, a “fill” is defined as a brief change in the groove that’s kind of like a
mini-​solo. The changed pattern provides variety in the groove and may “fill in the
gaps” of the music. A fill often signals the end of a phrase or acts as an announcement 169 
of a new section.

Select the Drum Kit sprite and show its Code tab. Locate the script starting with when
[green flag] clicked that was created as in Figure 13.3. We will modify this script
to play the drum fill when triggered.

9. Modify the script to be the same as Figure 13.6.

The script in Figure 13.6 can alternate between playing the standard kick-​snare
rhythm or the drum fill pattern. If the drum fill variable is greater than zero (i.e.,
set to 1 by the snare drum sprite) then it plays the drum fill, otherwise the simpler kick-​
snare pattern is played. The drum fill is a rapid series of snare drum play drum blocks
following a bass drum on the beat. A set to block is used to reset the drum fill
variable to 0, ready to be triggered again.

Give the drums a try. When the video is on, waving your hand behind the Drum Kit sprite
should play the drum pattern. Raising your hand so that it also touches the Snare Drum
sprite should trigger the drum fill. Music is triggered by movement so if you stay still, or
away from the sprites, the music will stop. Try bringing the drum fill in from time to time
as you play a steady drum kit groove.

Bass Guitar
Just like in a band, the drums in this project provide the beat that other parts lock into.
The bass guitar part listens for the beat and looks for motion from the video. When both
170

Scratch Music Projects

Figure 13.6
The updated drum
script including the
snare drum fill pattern.

170 

are present (the drums are playing, and a hand is waving over the Bass sprite) then the
bass guitar plays along. In this section we will create the Bass sprite and write code to en-
able it to play music along with the drum kit.

10. Create a new sprite, name it Bass and select the guitar-​electric1-​a guitar icon.
Position the bass guitar on the right side of the stage, level with the drum kit, as
shown in Figure 13.1.

11. Select the Bass sprite and navigate to the Code tab. Write an initialization script, as
shown in Figure 13.7, that sets the icon position and instrument for this sprite.

Figure 13.7
The bass guitar setup
script.
171

Video Interaction

We want the bass guitar to play a stable groove pattern. Like the other sprites in this
project it will detect video motion and play when it is above a threshold.

12. Create a new script starting with a when I receive [beat] block and complete it
as shown in Figure 13.8.

Figure 13.8
Bass guitar playback
script.

171 

The bass will only play when there is motion detected on both the drum kit—​to send
a beat message—​and on the bass guitar itself. The long-​short rhythm of the two bass
notes sets up a lilting groove against the more regular drum pattern. Try playing the pro-
ject at this point to hear how the parts work together musically and to get used to the
two-​handed interactive control.
While bass parts can be as simple as this, it’s more common that they change pitch
to follow chord progression or just to add harmonic interest. So, the next step will be to
elaborate on the bass script to add some harmonic variation by allowing the bass part to
select from a list of pitches.

13. Add a new list using the Make a List button in the Variables block area and name the
list bass pitches. The list should appear on the stage; if it does not, check the box
next to the bass pitches list in the Code tab. Add four numbers to the list as shown
in Figure 13.9. Click the + button in the bottom left corner of the list display and type
in the value each time; 45, 45, 47, and 48. Hide the list from appearing on the Stage
by unchecking the box next to the list name in the Code tab.

14. Create a new variable called bass pitch (singular this time) and uncheck the box so
it is not shown on the Stage.

15. Make the bass guitar pitch change randomly to one of the values in the bass
pitches list. We will expand on the script made in Figure 13.8, while the script for
this exercise is in Figure 13.10.

This completed bass guitar script selects one new pitch at random from the four
in the bass pitches list on each beat. The value 45 (pitch A) is the root pitch and
so is in the list twice, to give it double the chance of being selected over 37 or 38. This
ensures that the harmonic “center” trends toward the root pitch, providing more har-
monic stability.
172

Scratch Music Projects

Figure 13.9
The bass pitches
list shown on
the Stage.

172 

Figure 13.10
The complete bass
guitar playback script.

Run the project at this stage and explore ways of interacting with it to control various
combinations of the drum kit, drum fill, and bass guitar.

Electric Guitar
The final instrument for this project is an electric guitar. It will play some guitar riffs that
weave their way between the rhythm section parts, adding both rhythmic and harmonic
variety to the musical texture. The guitar will choose at random between two different
patterns to add further unpredictability and interest to the music.

16. Create a new sprite, call it “Electric Guitar” and select the guitar-​electric2-​a
icon. Position the guitar on the right side of the stage above the bass guitar, as shown
in Figure 13.1.
173

Video Interaction

17. Select the Electric Guitar sprite and navigate to the Code tab. Like the bass part, the
guitar has an initialization script as shown in Figure 13.11.

Figure 13.11
The guitar setup script.

The guitar part receives the beat message, like other parts, from the drums. It then
uses nested if statements to see if there is video motion on the sprite and whether to
play a “riff” or “chop” pattern. The decision-​making script is shown in Figure 13.12 and
we’ll implement it first. 173 
18. Start a new script with a when I receive [beat] block and complete the script
shown in Figure 13.12.

Figure 13.12
Selecting between the
riff and chord guitar
patterns.

Now it’s time to create the guitar patterns. The riff is a five-​note sequence, and the
chord includes two notes. Chords require, as we’ve seen in other projects, several scripts
that run in parallel, one for each note of the chord.

19. Start the riff script in the Electric Guitar sprite with a when I receive block set
to the message guitar riff. Add five play note blocks and set the pitch and
duration values as shown in Figure 13.13.
174

Scratch Music Projects

Figure 13.13
The script for the guitar
riff pattern.

174 

20. Start two new scripts also in the Electric Guitar sprite with a when I receive block
set to guitar chord. To each add a rest for 0.25 beats block, and a play
note block. Set the pitch values to 69 and 64, respectively, and the duration for both
to 0.125 beats, as shown in Figure 13.14.

Figure 13.14
The scripts for the
guitar chord playback.

These guitar chord scripts respond together to the same message, and so sound a
chord of two notes. The rest block delays these notes slightly so that they act in contrast
to most other parts that start at the beginning of the beat.
This concludes the scripts for the project, so run the project to make sure everything
is working as it should. Then play with it to explore the possibilities of controlling how
parts come in and out by using your hand gestures captured by the video.

Summary
In this project, the focus has been on using video motion detection as a novel method
of interacting with music in Scratch. As well we have seen some useful techniques for
generating music through beat-​based looping and the use of random selection to create
alternative patterns within parts. Parts have been synchronized by the broadcasting of a
beat message. However, the main new feature introduced was controlling the arrange-
ment of musical parts via video capture of gestural motion.
175

14 175 

Live Coding

Coding in Scratch is usually approached as a production process with a view to building


a project or to creating a composition. In such situations coding is seen as a “back room”
task that is typically behind the scenes of the finished product. The practice of live coding
brings the coding activity into the public sphere, as a creative performance. To code “live”
is to build the project in real time, as it runs, and in public—​perhaps even on stage as a
performance. The practice of live coding music has become more popular over the past ten
to fifteen years and there are now regular live coding concerts, live coding dance parties
called Algoraves, and a yearly international live coding conference. Check out these live
coding videos to get a sense of the practice.

• Andrew Sorensen—​The Concert Programmer (2014)1


• Eric Rosenbaum—​Scratch Live Coding (2011)2

In this project we’re going to introduce you to live coding in Scratch. A  technical re-
quirement for live coding music is to have a programming environment that does two
things:  Firstly it must support music and sound production, and secondly it needs to
allow editing of a running program. The fact that Scratch includes music blocks and that
it functions as a live interpreter/​compiler makes it interesting for the purposes of live
coding.
Even though in live coding practice you create the program as you go, it is not un-
common to develop some basic code structures to build upon before you start a perfor-
mance. In the next two sections we’ll look at building some live coding infrastructure in
Scratch.

Create Your Own Block


Scratch allows you to define your own code blocks. This is an advanced feature of Scratch
and this is the first time in this book that it has been introduced.
One of the keys to successful live coding is to have blocks and code structures that are
quick to implement and edit. We don’t want to have the audience waiting around as we
fuss over complicated code assembly during a performance. We will create a new play
block that plays from a list of predefined pitches and references them as scale degree

1 https://​youtu.be/​yY1FSsUV-​8c
2 https://​youtu.be/​rDyo4p1qLuE
176

Scratch Music Projects

numbers rather than absolute pitch values. This will hide some code complexity and help
keep the music “in key” automatically. Let’s see how we create a new block.

1. Create a new Scratch project. Select the default sprite and rename it “Player.” The
costume for the sprite doesn’t matter but if the screen will be projected for people to
see, you might want to choose an appropriate costume. Go to the Code tab. Select
My Blocks and click on Make a Block.

2. The Make a Block editor will appear as shown in Figure 14.1. Type play as the block
name. Click on Add an input(number or text) and type list offset. Click
on Add a label and type for. Click Add an input (number or text) and
type duration. The new block template should read play list offset for
duration. Click OK to create it.

Figure 14.1
The Make a Block
editor.

176 

The Run without screen refresh option in the Make a Block editor enables more
complex code within the block to run more efficiently. Normally, after Scratch has
finished executing one iteration of a script, it refreshes the screen. For scripts with
repeat blocks that do not change the screen appearance it can be more efficient to
avoid these screen redraws. The “Run without screen refresh” option causes a custom
block to run all at once and not refresh the screen until it has finished executing. In
our example, with only a single line of code in the custom block, this option has little
effect.
177

Live Coding

The new play block will appear in the MyBlocks list and in the code editor an associated
define block will appear. The play block has two input areas (where parameter values
can be entered); to fill the second of these we need to create a list of pitches to select from.

3. In the Variables area, create a new list by clicking on the Make a List button. Name
the list pentatonic.

4. Add the following values (twenty-​one in total) to the pentatonic list: 36, 38, 40, 43, 45,
48, 50, 52, 55, 57, 60, 62, 64, 67, 69, 84, 86, 88, 91, 93, 96.

This implements a Scratch list that contains a pentatonic (five-​note) scale over several
octaves. These are the pitches (C, D, E, G, and A) that we will use for our live coding
performances and they will be selected using numbers in the first input in the play
block. It will be useful to change the key of our pentatonic pitch set by transposing it, so
we will next create a variable to act as that offset.

5. Click on Make a Variable and create a new variable called root.

Now it’s time to define our new play block.


177 
6. Add the Music extension blocks to the project and then add a play note block
below the define block. Complete the inputs as shown in Figure 14.2. The list
offset and duration variables can be dragged from the define block when
required.

Figure 14.2
Defining the new
play block.

In Figure 14.2 the play note pitch is an item selected from the pentatonic
list. The item number equals the list offset variable (the number typed into the
play block) plus 11. This means that a typed value of 0 (zero) will become item 11,
that is pitch 60 (middle C), and other pitches are selected relative to that with positive
and negative numbers from −10 to 15. The root variable is added to the returned list
value to provide the final pitch to play. The duration input for play note is much
less complicated; it directly corresponds to the duration input typed into the new
play block.
We prepare this new play block beforehand, as we don’t want to construct this
code every time we want to sound a note while live coding. Now we can play a note by
just referring to it by a single number from −10 to 15 and this new block automatically
converts it to the correct pitch and plays it.

Try using the play block now. Drag it to the Code area and input values for the pitch
(between −10 and 15) and duration (e.g., 0.25, 0.5, 1, or 2). Click on the block to make it
run. Try connecting several play blocks together to create a melody.
178

Scratch Music Projects

Live Coding with Loops


One of the challenges with live coding music is to get some music playing so that you
can move onto coding the next element. An effective technique is to use repeating mu-
sical loops that continue to play for as long as is required to get the next section of music
going. Let’s use the new play block in a forever loop to do this.

7. Start a new script with a forever loop and insert three of the new play blocks in it.
Set the pitch values to −5, −3, and −1 and the duration values to 1, 0.5, and 0.5. Add a
when [space] key is pressed block above the loop to start it playing.

Start the looped phrase by pressing the spacebar. Let the music play while you code the
next script.

8. Create a second script by duplicating the previous one and change the play block
pitch values to 2, 1, 0 and the duration values to 0.5, 0.5, and 2.

Press the spacebar in time with the first loop to start the second as well. At any time, you
can pause playback with the Stop button and restart by pressing the spacebar. Notice
178  that the two sequences are of different lengths. The first one is two beats long (1 + 0.5 +
0.5) and the second is 3 beats long (0.5 + 0.5 + 2). This creates a polymeter where the two
loops repeat at different rates, yet they still follow the same underlying beat, to create an
evolving interaction that maintains the listener’s attention.

9. Add a third loop that is four beats long to increase the complexity of the music even
further. The third loop values are shown along with the previous two in Figure 14.3.

Figure 14.3
Three independent
musical loops.

Polymeter. Music is often organized into regular beat groupings which we call meas-
ures. The length of the measure is the meter of the music. The most common meters
are three or four beats long. Polymeter is when more than one musical meter is playing
at the same time. The beat duration remains constant, but the length of the metric
cycle differs between parts. Because the beat is the same, the parts stay in time, but
the measure cycles go out of synchronization, eventually realigning again every so
often. For example, a meter of 3 against a meter of 4 will realign every 12 beats.
179

Live Coding

Experiment with various combinations of the loops by clicking on them in the Code tab
to start and stop individual loops. Change the pitch and/​or rhythmic values of the play
blocks while the music plays to explore how you can update the code as it runs. Modifying
code on the fly is an important part of live coding. Remember that the pitch numbers are
degrees of the pentatonic scale from −10 (lowest) to 15 (highest), with 0 being middle
C. Use the Stop button and spacebar to halt and restart all parts as required to create an
interesting arrangement.
Congratulations, you have just done your first live coding! If you are really keen, then
find an audience and do a live coding performance with forever loops in Scratch.

Set Up a Downbeat Clock
It can be important to keep all the musical parts in time when live coding, and if we are
to use multiple forever loops it would make sense to create a master forever loop
that all parts can reference. This is what we’ll do next. We will create a master downbeat
clock as the final piece of live coding infrastructure that will, along with the play block, 179 
be the basis for all performances.

10. In the same sprite, start a new script with a forever loop. This script can be seen in
the second half of Figure 14.4. Click on the script to check that it works.

We will want this script to start running when we start the project. As well there are some
other initialization settings to include for startup.

11. Add blocks above the forever script, as shown in Figure 14.4, that set the root
variable to zero, set the tempo to 130 bpm and set the instrument to the Guitar
sound. All of this is actioned by a when [green flag] clicked block.

The down beat message broadcasted by this script will be sent every four beats.
Other scripts can listen to this and use play blocks (and others) to specify various re-
peating musical phrases.
Inside the forever block a broadcast block sends the change params message,
which is short for “change parameters” and is used to update variables prior to the next beat.
The rest for [0.1] beats music block adds a short pause allowing the parameters
to be updated. Inside the loop a broadcast block sends the down beat message; this
triggers musical sequences we will write later. Assuming our music will be in a 4-​beat meter,
a rest for 3.9 beats music block makes up the full time when added to the 0.1 rest.
With the play block defined and the broadcast down beat script written, all of the
infrastructure for live coding is now in place—​we’re ready to perform. In the remaining
sections of this chapter we’ll look at various strategies for creating efficient and modifi-
able code to use as we build up our live coded music.

Triggering Play Blocks
It’s now time to create musical scripts that respond to the down beat messages. With
the guitar as our instrument it seems natural to start with strummed chords. The basis
for this will be three short scripts that each play a note of the chord.
180

Scratch Music Projects

Figure 14.4
The initialization and
downbeat clock script.

180 

12. Create three new scripts in the sprite as shown in Figure 14.5. Each starts with a when
I receive down beat block and includes a play block. Pitches are set to −5, −2,
and 0 and all durations are for 2 beats. Two of the scripts have a rest for block prior
to the play block that introduces a short delay to provide the strumming effect. One
rest for block is set to 0.05 beats, the other to 0.1 beats.

Figure 14.5
The scripts for chord
strumming.

Start the project and hear the regular chord playing with a strumming effect. If you
were live coding you could start the project (which runs the down beat clock), and build
each script one at a time, sounding each script as they are built so that the chord becomes
progressively more obvious. You could also create all three scripts simultaneously and
then start the project for a more dramatic entry. These are the kinds of performance
considerations that affect live coding, but may not seem to matter when coding alone to
produce a completed project.
The repeated single chord, however, quickly becomes uninteresting so the next script
will add a change of chord by randomizing the root note variable.
181

Live Coding

13. Create the script shown in Figure 14.6 that, from time to time, sets the root variable to
one of three values.

Figure 14.6
Randomizing the root
variable to change
the chord.

By changing the root variable all notes generated by the play block are transposed.
This results in a chord change. The script responds to the change param message
which is sent just prior to the down beat message that synchronizes the notes. An if
[ ] then [ ] block checks the value of a pick random number between 1 and 4. If 181 
it is equal to 1 (25% chance) then the set [root] to block is evaluated. Another pick
random block selects a number between −1 and 1 (i.e., −1, 0, 1) and multiplies it by 5,
resulting in a transposition of either −5, 0 or 5; half-​tones to G, C, or F.
Next, we’ll add an arpeggio pattern over the top of the chords.

14. Create a new sequence starting with a when I receive [down beat] block.
Follow this with a series of 5 play blocks. Set the pitch values to 0, 1, 2, 4, and 3 and
the durations to 1, 1, 0.5, 0.5, and 1. The full script is shown in Figure 14.7.

Figure 14.7
An arpeggio sequence
over four beats.

As this script plays, notice that it automatically follows the chord changes. With that
sequence playing add some more interest by modifying the pitch selection of the fourth
play block with a pick random block that chooses values between 0 and 4. Notice that
182

Scratch Music Projects

because of our choice of a pentatonic scale for the play block then such random choices
of notes almost never sound bad. This means you can safely improvise modifications to
scripts as you live code.

Using Repeat Blocks and Rests


Even though the down beat message is sent at the start of each measure, not all parts
need to start on the downbeat. The next script uses a rest to delay the phrase it plays. It
also uses a repeat block to play more than one note following the rest and uses random
pitch selection to vary those notes to provide more musical interest.

15. Create a new sequence starting with a when I receive [down beat] block. The
full script is shown in Figure 14.8.

Figure 14.8
A phrase with an offset
start and repeated
random selection.

182 

Listen back to all parts and how they intertwine. The balance of fixed and random
pitch selection hopefully provides both stability and intrigue.

Adding Drums
We can add some drive to our music by adding a drum kit. We’ll use two scripts for the
drums. One for the hi-​hat, that need to be constant, and the other for the remainder of
the kit. First, we’ll build the hi-​hat script as shown in Figure 14.9.

16. As usual, start a new script in the same sprite with a when I receive [down
beat] block. The closed hi-​hat can play on every half a beat (i.e., every eighth-​note),
so add a repeat [8]‌block with a play drum music block inside it. Select the
Closed Hi-​Hat sound and set the duration to 0.5 beats.

Figure 14.9
The hi-​hat script.
183

Live Coding

Now let’s add the code for the remainder of the drum kit, as shown in Figure 14.10.

17. Create a new script as shown in Figure 14.10.

Figure 14.10
The script for main
the drum kit and
percussion part.

183 
Starting with a when I  receive [down beat] block this script includes five
play drum blocks, set the durations to 1, 1.5, 0.5, 0.5 and 0.5. Sounds for the first two
play drum blocks are Bass Drum and Snare Drum. The remaining three play
drum blocks use a pick random block to choose between drum sounds. Blocks 3 and
4 pick between core drum kit sounds 1 and 3 and the final block adds some percussive
interest by picking between sounds 7 and 12.
The drum kit script continues the theme of providing a balance of stability through
fixed events and constrained random variation.

Adding a Melodic Line
As a final touch to our live coding techniques, we’ll add a quickly flowing melody into the
mix. Starting with a simple, but not so musical, random selection of pitches we’ll then revisit
the more effective but more complex random walk melody we’ve seen in previous projects.

18. Start a new script with a when I receive [down beat] block. Add a repeat [16]
block and insert into it a single play block. Assign a pick random block to the pitch
input slot that chooses between 0 and 11, covering the higher range of the pitches in the
pentatonic list. Set the play duration to 0.25 beats. This script is shown in Figure 14.11.

Figure 14.11
A simple flowing
melody algorithm.
184

Scratch Music Projects

The faster pace and higher pitch range of this melodic part provides a contrasting tex-
ture to the overall music. It is also quick to code and easy to edit. However, it is not so con-
vincing musically because the melodic contour is scattered as a result of the wide random
range. A random walk algorithm will provide a smoother contour by limiting the size of
pitch leaps between notes. The updated melody algorithm is shown in Figure 14.12.

19. Create a new variable called pitch index, which will be used to keep track of
the pitch for the random walk. Add blocks to the repeat loop as shown in Figure
14.12, first to change the pitch index to a random value between 3 steps up or down
from the current index. Then check if the pitch index exceeds the bounds of the
pentatonic pitch set. Finally, use the resulting value in the play block.

Figure 14.12
A random walk melody
script.

184 

Using both the random and random walk approaches can allow the live coder to start
with a “quick and easy” version of the part then, while it is playing, take more time to
write a more sophisticated version and swap it in when completed. This is an important
strategy for effective live coding.

Structuring a Performance
Having outlined some techniques for coding musical parts in this section, we’ll discuss
approaches to structuring these into a live coding performance. The challenge for live
coding performers is to efficiently establish interesting music and to concurrently evolve
musical ideas across all sounding parts to maintain the interest.
185

Live Coding

There are various approaches to the amount of prepared material taken into the
performance. Blank-​screen live coding is a practice that seeks to start with a minimal
amount of prepared resources, acknowledging that there are always coded functions
“under the hood” in any programming environment. In this project we have developed
the play block and the down beat forever loop as prepared material. To emphasize
the way code is built up and modified during live coding it is typical that performers pro-
ject their computer screens for the audience to see them coding. A well-​known mantra for
live coders is “Show us your screens.” Audiences find this revealing and intriguing and it
makes the creative process more transparent and accessible.
The diagram in Figure 14.13 outlines a typical live coding process that starts with
some code preparation followed by iterations of coding new scripts and revising them. As
the number of scripts grow so does the challenge of managing them. It is a good strategy
to have a few interesting scripts rather than a lot of musically simple ones. It is also impor-
tant to design scripts that can be easily modified to make interesting musical variations.

Figure 14.13
The process of live
coding. 185 

One of the simplest ways of modifying the music is to change block input values. For
example, the play block has inputs for pitch index and duration. As well as manually
changing these, especially pitch, you can insert a random choice so that the pitches are
continually varying. When changing rhythmic values be aware of the total duration of all
play blocks in the script and remember that the script will be re-​triggered at the start of
each measure so the total length cannot exceed one measure.
Changes can be made by disconnecting and reconnecting script sections, as shown
in Figure 14.14. Parts can be stopped and started by disconnecting the body of the
script from the when I receive [down beat] block. Phrases can be truncated by
disconnecting parts of the scripts.

Figure 14.14
A connected and
disconnected body of
the script.

Finally, you can make manual changes by dragging in and clicking single blocks. This
is especially useful for global changes like set [tempo] to, change [volume] by
(e.g., for a fade-​out), or a temporary halt with the stop all block.
186

Scratch Music Projects

There are likely many more tricks and techniques you can develop. Watch some live
coding videos online to see more approaches to performance that you can incorporate into
your live coding. Most importantly give it a go, experiment, and get better by practicing.

Now it’s time to try your own live coding performance. Disconnect all the Event blocks
at the top of scripts so they don’t trigger existing code. Assume that only the play block
is prepared beforehand and live code other scripts using the techniques described in this
chapter. As you practice, your fluency and confidence will develop. When you feel ready,
invite some friends or family to listen to a performance. You could even screen capture a
performance and share it on the Internet.

Summary
Live coding is an emerging practice made possible by real time, media-​rich coding
environments, like Scratch. In this project we’ve introduced a specific live musical coding
performance practice in the Scratch environment, whereby music is created in real time.
186  Along the way we have seen how to make your own blocks and have explored techniques
for creating multiple musical parts with only a few short scripts. Have fun turning your
production coding skills into live music performances.
187

187 

Interview with Andrew Sorensen
Andrew Sorensen is an artist, a programmer, and a computer scientist. His interests are
diverse but unify under a common theme—​the programming language as an interface
for real-​time dialogue between human, machine, and environment. In exploring these
ideas Andrew is as likely to be found hacking code in nightclubs as steering code on dis-
tributed high-​performance computing clusters. Andrew is well known for creating the
programming languages that he uses in live performance to generate audiovisual the-
ater. He is regularly invited to perform these contemporary improvisations all around
the world and is a frequent speaker at international conferences. Andrew is the author of
the Extempore programming language—​a systems programming language designed for
high-​performance “live” programming. He holds bachelor and master’s degrees in music
from the Queensland University of Technology, and a PhD in computer science from the
Australian National University.

How did you get into combining computers and music?


My very first experience making music with a computer was writing small Basic programs
to play little melodies. My parents had just brought our first computer, a Spectravideo,
and I was about twelve at the time and we didn’t have many fun games so I had to learn
to write little programs to amuse myself. I had also just started learning the trumpet, so
making music with the computer was natural, although very primitive at the time.
I kept on writing Basic programs and also practicing the trumpet until I eventually
went on to study trumpet performance at university. I kept up my interest in computer
programming while studying music and began mixing programming with music more
seriously. By the time I had finished my music degree I had built a computer music pro-
gramming system and had decided that I enjoyed computer programming so much that
I wanted to pursue it as a career, a career that I still love twenty years later!

What are you doing now in computers and music?


My main interest in computers and music today is in developing programming languages
that can be used to program music and sound in real time. The neat thing about real-​time
programming languages is that they let you write music as a performance. We often call
this type of musical performance live coding. A live coding performance might be for a
small group of friends and family or even for a huge concert at a large festival! I have been
lucky enough to perform live coding concerts for some very large audiences.
In my performances the audience can watch me writing the code on a large screen.
When the audience is able to follow the performer’s code, an interesting thing happens.
Suddenly the performance is no longer just about the music, the performance is about
the combination of the code and the music. It is no longer just about how the music
188

Scratch Music Projects

sounds, but also about how the music was made. The process becomes a part of the per-
formance. I find this fascinating!

Where can we find examples your recent and current work?


The best place to find examples of my work is on YouTube where there are
recordings of many of my performances. https://​
www.youtube.com/​
results?search_​
query=Andrew+Sorensen

What role does coding play in your music activities?


I’m afraid that I  gave up the trumpet many years ago, so these days computer pro-
gramming is central to all of my music-​making. I code all of my rhythms, melodies, and
chords for my compositions along with all of the sounds and instruments that play these
rhythms, melodies, and chords. Code is really quite central to all of my music-​making.
My passion for making music with code also influences my work as a software devel-
oper and computer scientist. I ended up going back to university to study for a PhD in
computer science. I worked on developing programming languages during my PhD and
188  my experience working with music and computers made these new programming lan-
guages much more interesting and useful.
I use these programming languages every day for making music, but also for other
exciting things like controlling robot telescopes, studying subatomic particles, and even
writing computer games. So, as you can see, I’m still programming computers to amuse
myself thirty years later!

What tips or advice would you give young coders working with music?
Music can be beautiful, and code can be beautiful. There is an extra special joy to be dis-
covered when beautiful code makes beautiful music. Search for that beauty, even in the
simplest programs you write.
189

GLOSSARY
12-​bar blues  a pattern of chords used often in blues, jazz, and rock music, chord pattern
being I–​I–​I–​I–​IV–​I–​V–​I–​I–​V–​IV–​I–​I
accent  an emphasis or stress on a particular beat
algorave  a dance music event featuring algorithmic music and live coding
amplitude  the volume of a sound over time
arpeggio  notes of a chord played one after the other rather than simultaneously
arrangement  the organizing structure of a piece of music: its form, instrumentation, and
expressive use of elements
array  a computer data structure to store a list of values
audio  a sound, or a representation of it, as a signal or as data
bass drum  a large drum, typically played with a foot pedal as part of the drum kit
beats  units, or counts, of musical pulse
blocks  elemental code segments in Scratch
boolean  a symbol with two possible values, typically 1 or 0, true or false 189 
BPM  beats per minute; often displayed as quarter note=120, meaning a speed of 120 quarter
notes per minute
bridge  a passage of music that connects two other musical sections
broadcast  a method of sending a message between scripts within the Scratch environment
canon  a musical canon is formed when the same musical part is played simultaneously with
a slightly delayed start
chord  notes sounded together, usually three or more of different pitches
chord progression  a series of chords, often that repeat
chorus  a refrain in a song. In rock music, the “hook” or most memorable section of the form
clone  an automatic duplication. In Scratch, cloning provides a way to dynamically duplicate
a sprite while the project is running
coda  a tailpiece or short section at the end of a piece
code  the text or blocks that make up a computer program
conductor  a person or mechanism that controls an ensemble during performance
costume  images used for the visual appearance of sprites in Scratch
data  values, usually numbers, that are processed by a computer program
downbeat  rhythmic step that falls on a strong accent, typically at the beginning of a measure;
in Scratch, labeled “down beat”
drum fill  in drumming, a fill is defined as a short change in the groove that’s like a mini-​solo
dynamics  degrees of loudness or softness of musical performance
forever  the forever block creates a continuous loop that repeatedly calls the blocks inside it
form  the structure and design of a piece of music
global variable  a symbol in a computer program whose value is accessible to all of the program
groove  the rhythmic qualities of music and the phrasing against a pulse that affect the body’s
response to music
harmony/​harmonic  the combining together of notes of different pitches
190

Glossary

hi-​hat  matched small cymbals held together or apart by a foot pedal, typically included as
part of a drum kit
horn section  general name for a group of brass instruments
if-​then  the “if [ ] then [ ]” block is a conditional statement that allows things to happen when
particular conditions are true or false
increment  increase in value, typically by 1. Often related to counting through a list
index  a position within a list or array
interpreter  a process that assists understanding by translating between languages or symbol
systems
interval  the distance between the pitches of two notes
iterate  to repeat over and over
key  in music, the use of a scale or mode centered on a particular note which gives the key
its name
key signature  an indicator of a key
kick drum  see bass drum
library  in coding, a set of predefined code scripts that are typically focused on assisting with
a particular task
lists  lists in Scratch hold a sequence of values—​usually numbers or words
190  live coding  a performance featuring the writing of code to produce music and/​or visuals
local variable  a symbol is a program whose value is only visible to a sub-​section of the
program
major chord  a chord made from the first, third, and fifth degrees of a major scale
major scale  a particular set of pitches that define a major key or mode
maximum  the highest value in a set
meter  the regular emphasis in pulse through accents that leads to a “measure” or a “bar” of
music being a particular length
metronome  a virtual or physical machine that provides a steady pulse
MIDI  an acronym for Musical Instrument Digital Interface. It is a standard language that lets
instruments, devices, software, and hardware from different manufacturers communicate
fluently
minimum  the lowest value in a set
minor chord  a chord made from the first, third, and fifth degrees of a minor scale
minor scale  a particular set of pitches that define a key or mode. Can be harmonic, melodic,
or natural minor
negative number  numbers with a value less than zero
notation  (staff) symbols representing music or sound, usually graphically. Common music
notation is written on a five-​line staff
note  individual musical building blocks that, on the piano and in general, have pitch names,
like C, D, E, F♯ (♯means sharp), and so on
note number  each note has a number associated with it specified by the MIDI standard
note length  the duration that a notes sounds
octave  the musical distance from one note to the next note of the same name. A distance of
twelve half-​steps
offbeats  musical accents that fall between the main (or down-​) beats of the music
panning  placement of a sound in space between the left and right speakers
191

Glossary

parameters  values for variables within an algorithm or computer program


pentatonic scale  a scale of five notes
percentage  a proportion out of a total of 100
performing  playing music live
phrase  short segment of a melody
pitch  the relative frequency of a musical note, described as sounding “high” or “low”
polymeter  when more than one musical meter is playing at the same time
probability  the likelihood that events will occur
pulse  regular accent in music
random  a value chosen in an unpredictable way (unknown in advance), like the roll of dice
for a board game
repeat  do something several times; at least twice, sometimes more
rests  silent notes, used to indicate periods of silence in music
rhythm  the positioning of notes in time
riff  a musical fragment, often repeated
root pitch  the name given to the base pitch (first note) of a chord or scale
scale  a selection of pitches within the octave. Typically, in Western music scales use eight of
the twelve pitches in the octave 191 
scale degree  a note’s position within a scale. 1,̂ 2,̂ and so on
Scratch  a programming language and environment
script  a segment of code
section  a musical section is a large chunk of music, such as a verse or chorus
sequence  a term for an ordered list of items; in music it refers to a series of notes that make
up a musical pattern or phrase
snare drum  a drum with a vibrating wire “snare” attached against one drum head. Typically
part of a drum kit
sound  audible vibrations in the air
sprite  in Scratch, a sprite is an object that appears on the Stage
staccato  short sharp notes, indicated in notation as a dot on a note
stage  (music) a platform for performing on
Stage  (Scratch) an area of the computer screen on which sprites and other visual elements
are positioned
tempo  the musical term for the speed of a piece of music, often described as the number of
beats per minute (BPM)
transpose  in music, shifting up or down from the original pitch
upbeat  rhythmic steps between downbeats; in Scratch, labeled “up beat”
variable  in computer programming, variables hold a value that can be changed
waveform  a pattern of sound pressure variations over time, often represented as a series of
digital sample values (numbers) in a computer and visualized as a graph
X axis  the horizontal plane of a 2D or 3D space
Y axis  the vertical plane of a 2D or 3D space
192
193

INDEX
algorithms, xi, 165, 183–​184 echo, 70, 139
arpeggios, 77, 83–​84, 181–​182 extensions
arrangement, 107–​108, 115–​127. See also music blocks, 2, 9–​10
musical forms video motion blocks, 165–​166
arrays. See lists
Fiebrink, Rebecca, 144–​145
beats. See rhythms flag. See boolean variables
beats per minute. See tempo foley sounds, 139
blocks, creating your own, 175–​177 forever loop. See loops
boolean variables frequency, 19
definition, 89 functions, 54, 62, 87, 185
using as a flag, 89 fundamental. See root pitch
using for sprite buttons, 96–​98
using to play rhythms from a generative music, 146, 152, 160, 163-​164
list,  95–​98 global variable. See variables
BPM. See tempo
broadcasts. See messages Handel, George Friedrich, 67–​68, 70
HWV number, 68
canons. See musical form 193 
Carroll, Nicole, 45–​47 if -​else statements
chaconne. See musical form conditional loops, 68–​70, 110, 162
chords, 43–​44, 73–​84, 86, 119–​120, 159–​161, 173, 179, definition, 34, 38
181, 188 for setting values, 83, 89–​90, 94–​95,
chord progression, 76, 120, 160–​161, 171 110–​111
cloning, 131, 133–​136 nesting, 40–​41, 111, 118, 158
code execution within forever loops, 66
clicking on blocks, 2–3 improvisation, 146
concurrent, 62 index. See lists
consecutive, 62 initialization scripts, 78, 129–​130
live editing blocks and values, 185 instruments
parallel audio & video scripts, 12–​13 bass drum, 157
parallel processing, 75 bass guitar, 88, 101–​104, 112–​116,
conditional operators 169–​172
greater than (>), 39–​40, 66–​67, 101, 122–​124, 151–​ changing instrument sounds, 48–​49
152, 167–​173 drum kit, 3–​7, 98, 117–​118, 154–​156, 167–​168
equal to (=), 23, 40, 90, 99, 104, electric guitar, 73–​74, 119–​122, 172–​174
109, 124, 134 hi-​hat, 4, 6, 98–​100, 106, 155–​156, 159–​160,
less than (<), 39–​40, 151–​152, 154, 158 167–​168,  182
conducting arrangements, 107–​111 kick drum, 88, 91–​98
coordinates piano & keyboard, 9–​14, 75–​77
following the mouse, 22 rhythm guitar, 73–​75
setting sprite positions, 20–​21, 39 saxophone, 68–​72, 121–​122
counters. See variables snare drum, 61–​67, 98–​100, 168–​169
synth lead, 76–​81, 83–​84
delay. See wait theremin,  19–​24
deleting blocks, 36 trumpet,  70–​72
down beats, 152–​153, 160, 179–​180 ukulele,  34–​44
downbeat clock, 179 vibraphone,  150–​54
drum fills, 165, 168–​172 vocal melodies, 122–​125
duplicating scripts, 13–​14 intervals, 80, 82, 149
durations. See rhythms iteration
dynamics, 64. See also volume through lists with conditionals, 93–​103
194

Index

Klein, Eve, 85–​86 looping,  22–​24


setting pitch values, 10–​12
lists. See also sequences transposing with offsets, 56, 84
adding values to lists, 42, 148
creating lists, 42 octaves, 84, 147, 158–​159, 177
index to, 114–​115, 149–​155, 177 offset variable, 177
replacing values in lists, 43–​44, 78–​79, 113, optimizing code, 50–​56
115, 124
selecting list item, 43, 114, 149 panning, 139–​141
live coding, 175–​186 parallel processing. See code execution
loops patterns. See repetition
forever loops, 21–​24, 68–​72 pentatonic scale, 148, 177
repeat loops, 21 performing scripts, 184–​186
repeat until loops, 110–​111 phasing, 55
phrases, 50–​51, 53, 56, 108, 111, 116, 121, 154,
major chord, 44, 75–​76, 80 179, 185
major scale, 82, 148 piano. See instruments
mapping pitches. See notes
X-​axis to volume, 23–​24 polymeter, 178
Y-​axis to pitch, 24 project,
measures creating, 2
definition, 50 saving, 8
messages
broadcast, 90 random walk, 149–​151, 164, 183–​184
broadcast and wait, 110
194  receiving, 54, 91
randomizing
bass lines, 170–​172
sending, 54, 88–​91 definition, 29, 61
synchronizing down beats, 179–​185 drum fills & rhythms, 168–​169, 183
win and lose, 137–​138 drum sounds, 167–​168, 170
meter pitches from a list, 148, 171–​172
creating through changing volume levels, 64–​66 pitches within a range of values, 29, 147, 149–​151,
definition, 64 181,  183–​4
MIDI, 10, 13, 23, 52–​53, 56, 75, 77–​78, 85, 123, 147 positions of sprites, 133
minor chord, 81–​82 rests, 182
minor scale, 82 riff playback, 172–​174
modes,  82–​83 sound effects, 141–​142
modulo, 152, 158, 161 volume,  62–​63
music blocks. See extensions within a range of values, 29, 63
music for games, 128–​143 receiving. See messages
musical forms repetition,  50–​56
canon,  70–​72 of note & rest patterns, 50–​52, 54–​56
chaconne,  67–​72 of phrases, 53–​56
rounds, 70 of sections, 110–​111
song forms (verse–​chorus), 107–​108 rhythmic patterns, 87
song sections for I Got You (I Feel Good) by rests, 49–​53, 59, 71, 113, 115, 121, 153–​154
James Brown, 108, 126 riffs, musical, 48–​56
12-​bar blues, 107, 120, 160 root pitch, 77–​81, 120, 151–​152, 158, 160–​161, 172,
muting parts, 161–​163 177, 179–​181
rhythms
notation, 42, 50–​53, 112, 123 setting duration values, 4
notes Rosenbaum, Eric, 16–​18
changing with variables, 52–​54
for I Got You (I Feel Good) by James Brown, 112, scale degrees, 82
122–​124 score, 107, 115, 117–​118
for Kashmir by Led Zeppelin, 49–​50, 52–​53 scripts
for pentatonic scale, 148, 177 creating in a sprite, 38, 57, 101, 140
for ukulele strings, 42–​43 creating on the stage, 166
intervals, 80 definition, 22
195

Index

semitones. See transposition strumming effect, 35–​43, 73–​75, 180–​181


sensing swing feel, 152–​154
computer key press, 3–​5, 10–​13, 74–​75, 80,
83, 178 tempo
mouse click on sprite, 27, 35–​37, 74–​75, 90, 92, setting tempo, 4–​5, 49, 89
103, 106 speeding up/​slowing down tempo, 71–​72,
mouse position on screen, 19–​24, 37–​40 104–​106
sprites touching, 134–​136 time signatures, 50
video motion, 165–​174 transposition
sequencing, See also lists definition, 56
arpeggios, 77, 83–​84 by semitones, 116
chords, 771, 120–​121 using root offsets, 80
drum sounds, 5–​7, 117–​119 triggering. See sensing
pitches with lists, 67–​72, 102, 112, 148, 171–​172
pitches with repeats, 42 up beats, 154–​159
rhythms & durations, 69–​70
sequence steps, 93, 95–​98 variables,
volume levels & dynamics, 64–​66 changing values, 53–​54
Sorensen, Andrew, 187–​188 counters, 66–​67, 71, 151–​158
sounds definition, 39
adding to games, 139 global vs. local, 39, 129
changing volume, 141–​142 making,  38–​39
editing,  30–​31 startup variables, 129
play until done, 33 video interaction. See sensing
recording,  29–​30
start playing, 27
volume
creating accents, 64–​65, 153
195 
selecting, 2, 26, 139–​141 fading out, 142–​143
uploading,  32–​33 setting volume values, 65, 141–​142
sound effects
changing panning, 139–​141 wait, 28, 54
changing pitch, 29–​30
speed. See tempo X-​axis. See coordinates
startup scripts, 129–​130
step sequencer, 87–​97 Y-​axis. See coordinates
196
197
198
199
200
201
202

You might also like