Exercise: Create a Simple Melody


Exercise instructions

Suggested time limit: 10 minutes

  • Use at least 8 notes
  • Use play command to play the notes
  • Use sleep command to time the melody
  • You can use either numbers (70) or note names (:D5) with play command
  • Bigger values (sleep 4) = longer sleep time, smaller values (sleep 0.25) = shorter sleep time
  • Remember you can repeat parts (2.times do - end)
  • Save your melody code. 
  • Make sure you know which folder the code was saved in!

An online keyboard like this one is a great help when composing!

Educator notes

Observe your students and ask them what kind of melodies they are planning to create. Sometimes students want to make very complex melodies, like a complete pop tune. Sonic Pi - especially at this point of the learning curve - is not the easiest tool for replicating actual songs and the students may get frustrated if they get stuck with such an idea. 

Tip: Instruct the students to make really short and simple tunes at first and play the tune after each change they’ve written! This is a real programmers’ technique - test often to spot bugs in the code early on.

This is also a great time to start listening together to what others have made. If you have a portable speaker, it’s practical to walk around the classroom and plug the speaker into the students’ computers to play the melodies for others to hear.

Sonic Pi remembers the contents of each buffer - when you open Sonic Pi, it will show the code that was last edited in the buffers. However, this is not a fail-safe way to save the code! To make sure the students' programs are safe, it is better to save them as files.

Record Your Sounds

Exercise instructions
  1. Click the Rec button
  2. Start your song by clicking Run
  3. Once your song is finished (or you’ve played enough of your live_loops) click Stop
  4. Click Rec again to stop the recording
  5. A Save dialog window opens
 - choose the same folder where you saved your Sonic Pi code files
  6. Name your audio file in the dialog window (for example, MyFirstMelody) and click Save

Note: Sonic Pi records audio files in .wav format.

Educator notes

If the students want to use their songs as ringtones for their mobile phones, this is definitely possible! We can’t give exact instructions on how to do this because mobile operating systems are updated often and such instructions will not stay up-to-date very long. For Android the file should be in .mp3 format and for iPhone in .m4r-format. When you record sounds from Sonic Pi they will be in .wav format. You can use an online converter such as this free one here to get the file in the right format. Then you just have to find a way of transferring the file from your computer to your phone. One simple way is email or cloud storage services.

It may be necessary to edit the recordings to make them louder or to cut to a certain length. There are many free programs available for simple audio editing, for example Audacity (all operating systems) and Garageband (Mac OSX). Audacity can also be used to convert a file to MP3.