Scratch
Scratch is a visual programing language and coding platform developed by the MIT Media Lab. This language and platform can be used to create games, animations, and music. Scratch can easily be paired with Makey Makey (discussed below) to create interactive music instruments/controllers. Below, we share some common music-related codes.
To navigate this page, click on the topic to be taken to the corresponding location:
- Platform
Click on Scratch (left) to be taken to the web-based platform or click on Scratch 2.0 (right) to download the desktop Scratch editor,
Play a Recorded Sound
Platform
Play a Pitch
This code allows you to set a specific key on the keyboard to play a sound. Here, “when 1 key pressed” code selects the key to act as a trigger to play the selected sound "pop."
This code allows you to set a specific sprite (image on the screen) to play a sound. Here, “when this sprite click” code selects "this sprite" as a trigger to play the selected sound "meow."
For both the code combinations above, you can decide to use "play sound" (which will restart the sound for each new click or keypress) or "play until done" (which will play the entire recorded sound).
You can select prerecorded sounds by clicking "Sounds" (blue circle), and clicking the speaker icon (red circle). You can also upload your own prerecorded sounds using the upload icon (folder with upward arrow) or record new sounds directly into Scratch using the microphone icon.
This code allows you to set a specific key on the keyboard to play a specific pitch. Here, “when 2 key pressed” code selects the key to act as a trigger to play "note 60" (Middle C) for "0.5" beats. You can change the pitch by clicking the downward arrow and edit the length by clicking on the number and typing a new value
This code allows you to set a specific sprite to play a specific pitch. Here, “when this sprite” code selects the key to act as a trigger to play "note 60" (Middle C) for "0.5" beats. You can change the pitch by clicking the downward arrow and edit the length by clicking on the number and typing a new value
You can change the instrument for each pitch if you'd like by inserting the "set instrument" code between the orange event code and the "play note" code. This can be done with "when key pressed" (shown) and "when sprite clicked"
Play a Drum
This code allows you to set a specific key on the keyboard to play a specific percussion sound for a specific length of time. Here, “when 3 key pressed” code selects the key to act as a trigger to play "drum 1" (snare drum) for "0.25" beats. You can change the pitch by clicking the downward arrow and edit the length by clicking on the number and typing a new value
This code allows you to set a specific sprite to play a specific pitch. Here, “when sprite is clicked” code selects the image to act as a trigger to play "drume 1" (snare drum) for "0.25" beats. You can change the sound by clicking the downward arrow and edit the length by clicking on the number and typing a new value
Sustain Pitch(es)
This set of code allows you to create a sustaining pitch for as long as a given key (or sprite) is pressed (or clicked). The first set decides what key will be pressed (space). When "space" is pressed, it will broadcast a message (message1). "Message1" will be broadcast until "space" is not pressed." When "space" is no longer pressed, all code will stop.
​
The next set of code below the first set decides what pitch will be played when the given broadcast message is received. Notice that the beat length is set to "9999" in order to sustain the sound.
This set of code uses the same logic as the code before. The first set of code sets "4" to be the key that broadcasts the new messages "play c chord." The message will be broadcasted until "4" is no longer pressed.
​
This set of codes below allows you to play a chord that is sustained until the key is no longer pressed
Play a Drum
This code allows you to set a specific key on the keyboard to play a specific percussion sound for a specific length of time. Here, “when 3 key pressed” code selects the key to act as a trigger to play "drum 1" (snare drum) for "0.25" beats. You can change the pitch by clicking the downward arrow and edit the length by clicking on the number and typing a new value
Play a Melody
You can code sequences of pitches to create melodies. Here you see "Hot Cross Buns" coded out. Here you can see the use of both the "repeat" code and "rest" code.
Other Controls
These codes allow you to use "video motion." With this, you can use your computer's camera to allow you make interactive visuals on the screen. This makes your computer a simplified Xbox Kinect.
These codes allow you to use "loudness." With this, you can use your computer's microphone to trigger events.
These code and those below allow you to create randomized lists of sounds. These can be used with "when key pressed" and "when sprite clicked" event codes. The first randomize code was created by Dr. Jesse Heines at the University of Massachusetts, Lowell. Consult his user page at DrJay and check out Performamatics.
These other randomize codes were created by Eric Rosenbaum. Both of Eric's codes make use of playing only sound files. You would upload numerous sound files and then this would scroll through them either in order (counter) or at random. For counter, you use the Data section of the scripts to create a new variable (Data).