In this project you will program a short tune. It could be used as a doorbell chime, a mobile phone ringtone, an alert notification on your computer or included in a web page.
Click the play button below to hear how the doorbell chime will sound:
Step 1: Play your first notes
Sonic Pi allows you to program music. Let’s start by playing some simple musical notes.
Run Sonic Pi. Your Club Leader will be able to tell you where to find it. If you’re using a Raspberry Pi then it’s under Programming on the menu.
See where it says ‘#Welcome to Sonic Pi’? Below there type:
Click ‘Run’. Did you hear a musical note? If not, make sure that the sound isn’t muted on your computer and that the volume is high enough. If the sound is too loud turn it down.
If you’re using a Raspberry Pi then make sure you are using either an HDMI monitor with speakers or have speakers or headphones connected to the audio jack.
There’s also a volume setting under ‘Prefs’ that you can adjust.
Now add another line below your first one:
Click ‘Run’. Did you hear what you were expecting? In Sonic Pi,
playmeans start playing, so it starts playing the first note and then immediately starts playing the second note so you hear both notes at the same time.
To get the second note to play after the first note add a
sleep 1line in between so that your code looks like this:
Now run your code and it should sound like a doorbell chime.
Listen and you should hear a higher note and then a lower one. Higher notes have higher numbers.
Save your code by clicking on ‘Save’ and name your file ‘doorbell.txt’.
If you’re not sure, check with your Club Leader where you should save your file.
You can load files back into Sonic Pi by clicking on ‘Load’.
Challenge: Improve the chime
Try choosing different numbers for the notes. Numbers 21 to 108 correspond to the notes on the piano. Experiment to find out which notes you like.
Is the first note in the doorbell too long? Try changing sleep to 0.75 or 0.5.
Step 2: Repeat the chime
Doorbells often repeat the chime.
Let’s put the doorbell chime into a loop:
Sonic Pi will helpfully indent the doorbell chime code inside the
Listen to the chime. Is it what you expected?
You’ll need to add a
sleepline at the end so that there’s a pause before repeating:
Listen to the chime again and it should sound better.
Save your project
Challenge: Repeat 4 times
Can you make the doorbell repeat 4 times to be really annoying!
Step 3: Recording your chime
Now let’s record the doorbell chime so that you can play it outside of Sonic Pi. Sonic Pi can create
wav files which are commonly used sound files.
Click on the ‘Rec’ button to start recording.
Then click ‘Run’ to play your doorbell chime.
When it finishes click ‘Rec’ again to stop recording and you will be prompted for a filename. Call your file ‘doorbell.wav’.
If you have a program that can play ‘.wav’ files then you’ll be able to play your doorbell chime outside of Sonic Pi.
Tip: If you don’t have another program that can play ‘.wav’ files then you could import the sound into Scratch and play it.
Tip: You may need to close Sonic Pi to be able to play sound using another app.
Challenge: Compose a new tune
Now take what you’ve learnt and compose your own tune. It could be a doorbell chime, mobile ringtone or notification.
times to create your own more interesting tune.
Remember, higher numbers play higher notes and numbers 21 to 108 correspond to the notes on the piano.
You can also record your tune as a ‘.wav’ file.