Kids, Code, and Computer Science Magazine

A Sonic Pi Christmas

Tara Schmidt on Flickr

To celebrate this wonderful time of the year, let’s create some holiday music using Sonic Pi on our Raspberry Pi.

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


Also In The December 2016 Issue

Hour of Code and EU Code Week are events designed to introduce kids, young adults, and others to programming and computer science.

Real life treasure hunts are a way to get outdoors, learn map skills, and have fun finding hidden caches near you.

A trainable puppy plus treats plus technology equals a dog that can send selfies. Here's how.

An app to help kids remember important stuff like feed your pets, brush your teeth, and smile.

These books include lots of great projects to work on by yourself or with others, from Scratch and Minecraft to fun maker space projects.

The mBot robotics kit is an excellent comparatively low-cost way to begin working with robots.

There are maybe a bazillion Raspberry Pi projects online. Here are really fun projects plus links to find more.

The Wayback Machine lets you travel back in time to see old websites. Plus the Internet Archive has thousands of vintage games, software, books, and more.

Eating dog food doesn't sound like much fun but it's an important part of creating software.

The ability to identify patterns, decompose large problems into small parts, develop algorithms to solve problems, and generalize to find solutions.

To celebrate this wonderful time of the year, let’s create some holiday music using Sonic Pi on our Raspberry Pi.

This project shows how to use the pygame code library to move simple animations with the Python programming language.

This project, shows you how to create your own random password generator in the C# programming language.

These projects mix science and technology in interesting ways. Sewing and electronics, for example, is a different way to learn about electronics.

Links from the bottom of all the December 2016 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about computer science, software programming, and technology for December 2016.

What sounds like a country western dance actually is an efficient way to sort large sets of data randomly.