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.
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.
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.
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.
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.
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:
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?
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!
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:
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.
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: