# Programming with sound

The Scratch Music extension allows you to use sound effects, as well as samples from musical instruments. Learn how to use these blocks here.

In this step, you are going to look at the Sound blocks and create a program that plays sound. When creating your project, you will use sound by using two different sets of blocks:

• Blocks from the Sound category control sound effects. Each individual sprite comes with its own effect. For example, the Scratch cat sprite has a “meow” sound effect, but you can add more.
• The Music extension blocks use sound recordings from different musical instruments, known as samples. For example, you can use the blocks to play notes from a piano and use these to create a piece of music for a game or an animation.

### Music terminology

In order to understand some of the Sound blocks, it’s helpful to understand the music terminology that they use.

• Volume: This is how quiet or load a sound is — the higher the volume, the louder the sound.
• Pitch: This describes the tone of the sound — high sounds like a whistle have a higher pitch, while lower sounds like those from a bass drum have a lower pitch.
• Beat: Music is often split into equally spaced beats, the duration of notes is measured in terms of these beats. The beats are generally the parts of the music that a listener would tap along to.
• Tempo: This is how fast the music is, measured as the number of beats per minute. This means that a song with a higher tempo will have shorter beats. The default tempo in Scratch is 60 beats per minute.
• Pan left/right: This describes changing the volume played by the left and right speakers (or headphones) to make a sound appear to come from a different position; if the sound is panned to the right, the volume of the sound played by the right speaker will increase, while the volume played by the left speaker will decrease.

### Sound blocks

When you want to add a sound effect to your Scratch project you will use the blocks from the Sound category. There are nine blocks you can use:

You have two options for starting a sound — which one you pick depends on if you want the set of instructions to pause while Scratch plays the sound. The play sound () until done block will pause the instructions, while the start sound () block will not.

### Volume

By default, Scratch will play the sound effects at full volume: 100%. If you want to adjust this, you can use the set volume to () % and change volume by () blocks to change the volume the sprite will play sounds at.

### Effects

When you set the pitch effect on the set effect () to () block to a positive number, later sounds will have a higher pitch than their default, while a negative number will cause sounds to play at a lower pitch.

To experience the pan left/right effect, you need either stereo speakers or headphones; these have two outputs for sound. In Scratch, positive values will pan the sound towards the right, and negative values to the left. -100 (left) and +100 (right) will set the sound to only play through one speaker. You can use this effect in animations when you want to associate a sound with a sprite that moves across the screen.

### Music extension

You can use samples of musical instruments in your Scratch projects by adding the Music extension blocks.

#### The play drum block

The play drum () for () beats allows you to play a percussion sound for a given number of beats — you can choose which sound from the dropdown in the block.

#### The play note block

The play note () for () beats will play a note from the sprite’s current instrument — by default, this starts as a piano. As with the play drum block, you have to specify the number of beats you want, but you also have to pick a note. When you click on the play note value in the play note () for () beats block, a graphic of a keyboard will appear.

The keyboard graphic gives you the option of clicking on the keys of the keyboard to hear different notes. When you click on a key to hear the note, it also changes the note value in the block.

#### Selecting the instrument

To change the instrument that a sprite uses, you will use the set instrument to () block. There are 21 instruments to choose from; to play multiple instruments at once you will need multiple sprites because one sprite can only play one instrument at a time.

#### Adding a rest

To add a period of silence to your music, you should use the rest for () beats block. Like the wait block, this will pause the set of instructions, but it measures time in beats rather than seconds, making it more useful when you are working with music.

#### Changing the tempo

Use the set tempo () block to speed up or slow down your music, giving the tempo you want in beats per minute.

### A musical program

To see these Music extension blocks in action, follow the instructions below to create a program to play four different notes one after another:

1. Create a new Scratch project
2. Add the Music extension
3. Drag-and-drop the blocks below into the code area

4. Click on the green flag to run the program

Experiment with the Music extension blocks to produce your own music. You might want to try:

• Adding more notes, and changing the pitch and length of the notes
• Changing the instrument using the set instrument to () block
• Using the set tempo () block to speed up or slow down the music
• Adding a rest for () beats block
• Using the play drum () for () beats block to add some percussion

Share your project on the Scratch website, and paste the share link in the comment section below. Look at the projects shared by your fellow learners, and reply to them with your comments.