Make a Duck Piano

What You'll Do

In this project you'll make keys on your computer play "quack" sounds at different pitches, like a piano that sounds like a duck. You can use your duck piano to play simple tunes. Later, you can try other sounds, such as dog barks or even sounds you record yourself.

This project works best on a computer with a keyboard, but it could be adapted to work on a tablet.

What You Should Know

If you've never done any coding, this project is a fun way to get started. If you're experienced, this project is still great fun, and there are many ways to extend and customize it.

If you've never used GP Blocks, you may want to watch this one-minute video before you get started.

As you work, periodically click the button to download and save a copy of your project. To resume work on a saved project, launch GP, then drag and drop the saved .gpp file onto the GP browser window.

Step 1: Playing a Sound

Click here to start this project in a new browser tab or window. You'll be going back and forth between these instructions and the project, so set up your windows or tabs to make that easy.

When you open the project, you'll see a single block:

Click this block to hear what it does. If you don't hear anything, check that your sound is not muted and the volume is turned up.

Click on the black extension arrow on the right side of the block to reveal the rate parameter:

Change the rate to 50 and listen to the result:

You'll notice that the pitch of the quack is lower. In fact, it is half the frequency of the original or, in musical terms, an octave lower. Try some other numbers to get a feel for how the rate changes the pitch.

How it Works: Sound from Numbers
How does the "play samples" block work? To understand this drag out (or duplicate) the "samples for 'Duck'" block and click on it:

What you see are the first 100 of the 5,792 numbers that represent the duck sound. Each number in that list is the sound level of the duck sound at one moment in time. When those numbers are fed to your computer speaker in rapid succession, the speaker moves, recreating the sound waves picked up by the microphone when the duck sound was recorded.

Step 2: Changing the Pitch

Make two copies of the "play samples" block by clicking on that block with the right mouse button and selecting "duplicate" from the menu:

Stick them together and set their rates like this:

Click on this stack to run it. You may recognize these notes as "do, re, mi", the first three notes of the musical scale.

Step 3: Making a Musical Scale

To make it easier to build our piano, we're going to define our own block. Select the "My Blocks" category and click the "Make a shared block" button. You'll be asked to name the block:

A purple "hat" block will appear in the scripting area:

Add a parameter by clicking the right arrow to the right of "play note". A new parameter will be added with the default name "foo":

Since the parameter will be used to say what Note to play, click on the "foo" block and change the parameter name from "foo" to "n":

The block definition hat should now look like this:

Attach a "play samples" block, then use blocks from the "Operators" category to compute the rate like this:

We'll explain how this works later. For now, finish your script by dragging a copy of the "n" parameter and dropping it into the equation like this:

Now we're ready to try the "play note" block. You can get a "play note" block from the "My Blocks" category of the palette, but there's a shortcut. You can just drag a copy of the block out from the purple block definition hat. Either way, you'll have a "play note" block like this:

Try some different numbers to see what they do. Be sure to try zero and negative numbers, too. Then stick three blocks together with 0, 2, and 4 as parameters like this:

When you play this you should recognize the same "do, re, mi" notes you heard before. Congratulations -- you've finished the hardest part of this project!

If your "play note" block isn't working, check that it looks like the picture. If you get really stuck, click here to open a project with a pre-built "play note" block.

How it Works: Computing Musical Intervals
We hear pitch differences as musical intervals based on the ratio between two frequencies, not their difference. For example, we hear the change from 27.5 Hz (the very bottom "A" on a piano keyboard) to 55 Hz as an octave because 55 is two times 27.5. We also hear the change from 1760 Hz to 3520 Hz (the second highest "A" to the highest "A" on the piano) as an octave, again because the higher pitch is double the frequency of the first.

In the first case, the difference in frequencies is 27.5 Hz, in the second case, it is 1760 Hz. So, if we want to go up an octave from some frequency we can't just add some fixed number such as 27.5 Hz. That number works for the low A, but increasing the pitch of the high A by 27.5 Hz results in a pitch of 3547.5 Hz, which is still the same A, just slightly out of tune!

To make a given music interval starting on any pitch, we have to multiply by some amount. As we've seen, we can go up an octave by multiplying by 2.

To go up by one half step -- for example from E to F -- we multiple by the twelfth root of 2, which is approximately 1.059. To check that this produces octave after 12 half steps, we can multiple it by itself 12 times:

The result is pretty close to 2. It's not exact because 1.059 is only an approximation to the twelfth root of two.

The "raised to" block is just a way of automatically multiplying numbers together. So in our "play note" definition, this block:

is a shortcut for multiplying by the twelfth root of two n times, raising the original pitch by n half steps. Happily, it turns out that the "raised to" block also works with negative numbers (it divides by the twelfth root of two n times). Thus, this block also creates musical intervals that go down from the original pitch by n half steps. Sometimes math is actually your friend!

Step 4: Using Keys to Play Notes

With this new block, it's easy to program the number keys on your computer keyboard to play different notes. Get a "when key pressed" block from the "Control" category, use the menu to select "1" from the menu, then attach a copy of your "play note" block and set the parameter to -5:

You'll need nine more scripts similar to this one. Instead of building them all from scratch, you can use the "duplicate all" command:

Be sure to right-click on the top block of the script; that block and everything under it are duplicated.

Change the key numbers and fill in the parameters like this:

Step 5: Playing Music

Here's how the number keys on your keyboard map to piano keys:

Note that the "4" key is like "middle-C" on the piano, so the keys 4, 5, 6 play "do, re, mi". Here are the keys for the first part of "Twinkle twinkle little star":

Twin-kle twin-kle lit-tle star
  4   4    8   8   9   9   8
How I won-der what you are?
 7  7  6   6   5    5   4

And "Jingle Bells":

Jin-gle bells jin-gle bells jin-gle all the way
 6   6   6     6   6   6     6   8   4   5   6
Oh what fun it is to ride in a one horse o-pen sleigh
 7  7    7  7  7  6   6   6  6  8    8   7  5    4

Can you figure out how to play other tunes?

Where did the keyboard picture come from?
The keyboard picture was part of the project you started with. Part of the picture comes from an image that was imported into GP and appears in the "Images" tab. But the numbers on the keys were added by a script that is included in this project.
To see that script, select the "My Blocks" category. You'll see a "draw keyboard" block. You can make the definition of that block appear in the scripting area by right-clicking on the block and choosing "show definition..." from the menu.

Remix 1: Try Other Sounds

There are several other sounds, such as dog barks, already installed. Use the drop-down menu in the "samples for" block in your "play note" block definition to try those sounds.

You can import your own sounds by dragging and dropping an uncompressed WAV file onto the GP browser window. The sound will be added to the "Sounds" tab and will appear in the drop-down menu of the "samples for" block. Short sounds with steady pitch work best. You can use the sound recorder in Scratch to record your own sound, then export it from Scratch and add it to your GP project. You might try a wind chime, xylophone, plucked violin string, yourself singing a short "la" -- or even an actual piano!

Remix 2: Extending the Range

It would be nice to have a few more notes... What if holding down the shift key made each number key play an octave higher? Adding 12 to "n" in the "play note" definition will raise the pitch by an octave. (Why 12? One clue is that there are 12 keys, including both white and black keys, in one octave of the piano keyboard.)

Here's one way you might do that:

Remix 3: Changing to a Minor Scale

If you've had some music lessons, you may notice that the scale starting with the "4" key is a major scale, as if you played the white keys on the piano starting with C. You could change this to a minor scale by changing the parameters in some of the "play note" blocks like this:

Try some other scales.

Going Further

If you enjoyed this activity, you might be interested in these other GP Blocks Hour of Code activities:

You may also want to check out: