Introduction:

In this project you will create a musical round where different instruments play the same tune but start at different times.

Click the play button below to hear how your musical round will sound:

Do you recongnise the tune?

Step 1: The tune

In the last project you used Sonic Pi to program music using letter names. This time we’ve provided the music for you.

Activity Checklist

  • Choose a buffer in Sonic Pi and Load frerejacques.txt. Check with your Club Leader if you don’t know where the file is.

    screenshot
    screenshot

    Run it. Do you recognise the tune?

  • Let’s give our tune a name so that we can play it when we want to.

    screenshot
    screenshot
  • Now nothing will happen if you run your code. You need to tell Sonic Pi to play ‘fj’.

    Add the following line to end bottom of your code:

    screenshot
    screenshot
  • Try playing the tune with two different instruments:

    screenshot
    screenshot

The instruments play one after the other.

Step 2: Concurrency

Now let’s get two instruments working together to play the tune.

Activity Checklist

  • We don’t want the second version to wait until the first has finished so we’ll need to tell Sonic Pi that it doesn’t need to wait. We do this by running each version inside a ‘thread’.

    screenshot
    screenshot

    In computing we call things happening at the same time ‘concurrency’.

  • Run your code and see if you can hear two instruments.

  • Look at the output and you will see the same notes being played by both instruments at the same time:

    screenshot
    screenshot

    Each time is highlighted in a different colour.

  • Let’s look at the music for this piece.

    Here are the first four bars:

    screenshot
    screenshot

    And the final four bars:

    screenshot
    screenshot

    Run your Sonic Pi project again and follow along.

  • Frere Jacques is a musical round. It’s designed to sound good when multiple versions of it start at different times. You might have been involved in singing or playing a round in music lessons at school.

    Let’s add a sleep before the piano starts playing:

    screenshot
    screenshot

    How does it sound?

  • Look at the output from Sonic Pi, can you see when the piano starts playing? And when the first instrument stops playing?

    screenshot
    screenshot

    This is just an excerpt, look at your Sonic Pi output to see the whole piece.

Save your project

Challenge: More instruments

Can you add two more instruments (synths) playing Frere Jacques so that each waits another 4 beats?

Challenge: More concurrency

What else can you do by playing multiple pieces of music at the same time using in_thread do?

You could find the music for another round (such as London’s Burning) and program that.

Or you could program a tune and then add rhythm in another thread.