dark mode light mode Search Menu
Search

A Sonic Pi Christmas

Tara Schmidt on Flickr

Christmas is coming and to celebrate this wonderful time of the year, let’s create some holiday music using Sonic Pi software on our Raspberry Pi.

What will you need for this project?

  • A Raspberry Pi
  • The latest Raspbian operating system
  • Keyboard, Mouse

All of the code for this project can be downloaded from here online:

https://github.com/lesp/KidsCodeCS-SonicXmas/archive/master.zip

Getting Started

Sonic Pi can be found in the main menu on your Raspberry Pi, just look for the Programming menu at the top of the computer screen.

projects-sonic-pi-holidays-image1
Sonic Pi can be found in the main menu, just look for the Programming menu

Converting Music

Sonic Pi is a musical instrument that uses code rather than keys, frets or drums to make sound. So it obeys the same rules that music follows, for example, notes, chords, pitch, tempo etc. We did a Google search and found a Youtube video that shows the notes that we shall play in this tutorial.

You can use any music site for reference. For best results look for piano music arrangements or guitar tablature.

In Sonic Pi, notes can be referenced either by their pitch, for example :c or they can be referenced via a number, in this case 60 for a middle c. These numbers are MIDI, Musical Instrument Digital Interface, and are used by computers to control sequences of music. In this tutorial we shall refer to the pitches of each note via their name.

Coding the Music

projects-sonic-pi-holidays-image2
The Sonic Pi interface has been designed with easy access to everything that you will need to create a masterpiece

We start our project in Sonic Pi, available via the Programming menu. In Buffer 0 in the interface, we shall write the code, starting with the creation of three variables. Variables are containers that can be used to store data, in this case we store the tempo of the music in beats and how long our code should delay between musical phrases in delay_bell and delay:

beats = 55
delay_bell = 0.21
delay = 0.2

Our next section of code is called a live_loop and Sonic Pi uses these to enable lots of instruments to play at once, just like a band playing together. Every live_loop needs a name, ours is called choir as it provides a choral background sound. In choir loop we set the BPM, Beats Per Minute, to the value stored in the variable beats and we then tell Sonic Pi to load an instrument, a synth, called pretty bell.

Using this instrument we play the notes g3 and c4, one after the other. But each note is played at only 15% (0.15) volume, the note is sustained for 1.5 seconds and then released.

live_loop :choir do
    use_bpm beats
    use_synth :pretty_bell
    play :g3, amp: 0.15,sustain: 1.5, release: 1.5
    sleep 2
    play :c4, amp: 0.15,sustain: 1.5, release: 1.5
    sleep 2
end

Now we create another live_loop, this time called melody_bell. It also uses the same BPM as our choir, and the same instrument. But we also introduce audio effects, in the form of reverb. Reverb is used to give audio a warmth and spacey sound. We use reverb in this loop for all notes.

live_loop :melody_bell do
    use_bpm beats
    use_synth :pretty_bell
    with_fx :reverb do
projects-sonic-pi-holidays-image3
All of our composition has been created in 47 lines of code, a tiny amount of code for a full Christmas song

So now we shall play the notes for the first part of the tune. We use a function called play_pattern_timed, this function uses two arrays, values stored in an indexable list.

The first array are the notes that we play, separated by colons. For this array we use the names of the notes, rather than the numerical value, as it is easier for converting sheet music. The second array contains the time in seconds that we pause between each note. Our entire song uses six phrases of notes that are played in this manner. Between each phrase we introduce a sleep, a pause, that will pause the music for the value stored in the variable delay_bell.

live_loop :melody_bell do
    use_bpm beats
    use_synth :pretty_bell
    with_fx :reverb do
        play_pattern_timed [:g3,:c4,:c4,:d4,:c4,:b3,:a3,:a3], [0.4,0.3,0.2,0.3,0.3,0.4,0.4]
        sleep delay_bell
        play_pattern_timed [:a3,:d4,:d4,:e4,:d4,:c4,:b3,:g3], [0.4,0.3,0.2,0.3,0.3,0.4,0.4]
        sleep delay_bell
        play_pattern_timed [:g3,:e4,:e4,:f4,:e4,:c4,:a3,:g3,:g3,:a3,:d4,:b3,:c4], [0.4,0.3,0.2,0.3,0.2,0.4,0.4,0.3,0.3,0.3,0.3,0.2]
        sleep delay_bell
        play_pattern_timed [:g3,:c4,:c4,:c4,:b3], [0.4,0.3,0.3,0.3]
        sleep delay_bell
        play_pattern_timed [:b3,:c4,:b3,:a3,:g3], [0.4,0.3,0.3,0.3]
        sleep delay_bell
        play_pattern_timed [:d4,:e4,:d4,:c4,:g4,:g4,:g4,:a4,:d4,:b3,:c4], [0.4,0.3,0.3,0.2,0.2,0.2,0.3,0.3,0.3,0.3]
    end
end

All of our composition has been created in 47 lines of code, a tiny amount of code for a full Christmas song.
So now we can play the music and hear our backing track play a sustained note, and our melody play We Wish You A Merry Christmas using a bell instrument.

Now lets create another live_loop, this time it will be called melody and it will use a piano instrument to play the same notes as the previous loop, but slightly out of time, giving us a richer sound. We cause the delay by using the delay variable which contains a slightly different duration for our musician to pause for. The rest of the code is identical to before.

live_loop :melody_piano do
    use_bpm beats
    use_synth :piano
    with_fx :reverb do
        play_pattern_timed [:g3,:c4,:c4,:d4,:c4,:b3,:a3,:a3], [0.4,0.3,0.2,0.3,0.3,0.4,0.4]
        sleep delay
        play_pattern_timed [:a3,:d4,:d4,:e4,:d4,:c4,:b3,:g3], [0.4,0.3,0.2,0.3,0.3,0.4,0.4]
        sleep delay
        play_pattern_timed [:g3,:e4,:e4,:f4,:e4,:c4,:a3,:g3,:g3,:a3,:d4,:b3,:c4], [0.4,0.3,0.2,0.3,0.2,0.4,0.4,0.3,0.3,0.3,0.3,0.2]
        sleep delay
        play_pattern_timed [:g3,:c4,:c4,:c4,:b3], [0.4,0.3,0.3,0.3]
        sleep delay
        play_pattern_timed [:b3,:c4,:b3,:a3,:g3], [0.4,0.3,0.3,0.3]
        sleep delay
        play_pattern_timed [:d4,:e4,:d4,:c4,:g3,:g3,:g3,:a4,:d4,:b3,:c4], [0.4,0.3,0.3,0.2,0.2,0.2,0.3,0.3,0.3,0.3]
        sleep delay
    end
end

With the code complete, click on Run to hear your masterpiece! You can also click on the Rec button in Sonic Pi while your music is playing to record an audio file for use in presentations or other musical projects.

Learn More

Sonic Pi

http://sonic-pi.net

YouTube Videos

https://www.youtube.com/embed/SnGNXL2fg6o
https://youtu.be/YnOycrTooyo

Raspberry Pi

https://www.raspberrypi.org/

Code for this Project

https://github.com/lesp/KidsCodeCS-SonicXmas/archive/master.zip