Binary Bizet - Sonic Pi

By Russell Barnes. Posted

In this month’s guide we’re going to bring a classical operatic dance piece straight into the 21st century, using the awesome power of code…

Let’s jump into a time machine and head back to the year 1875. A composer called Bizet had just finished his latest opera, Carmen. Unfortunately, like many exciting and disruptive new pieces of music, people initially didn’t like it at all because it was too outrageous and different. Sadly, Bizet died ten years before the opera gained huge international success and became one of the most famous and frequently performed operas of all time. In sympathy with this tragedy, let’s take one of the main themes from Carmen and convert it to a modern format of music that is also too outrageous and different for most people in our time: live-coded music!

The full article can be found in The MagPi 41 and was written by Sam Aaron

You'll need

Sonic Pi v2.7+ (update using: sudo apt-get update && sudo apt-get install sonic-pi)

Speakers or headphones with a 3.5mm jack

Decoding the Habanera

Trying to live-code the whole opera would be a bit of a challenge for this tutorial, so let’s focus on one of the most famous parts: the bassline to the Habanera.

 You may recognise Habanera from the film Up

This may look extremely unreadable to you if you haven’t yet studied music notation. However, as programmers we see music notation as just another form of code, only it represents instructions to a musician instead of a computer. We therefore need to figure out a way of decoding it.

Notes

The notes are arranged from left to right, like the words in this magazine, but also have different heights. The height on the score represents the pitch of the note. The higher the note on the score, the higher the pitch of the note.

In Sonic Pi, we already know how to change the pitch of a note: we either use high or low numbers such as play 75 and play 80, or we use the note names such as play :E and play :F. Luckily, each of the vertical positions of the musical score represents a specific note name, as shown here…

 Reading music is an excellent skill to have

Rests

Music scores are an extremely rich and expressive kind of code, capable of communicating many things. It therefore shouldn’t come as much of a surprise that musical scores can not only tell you what notes to play, but also when not to play notes. In programming, this is pretty much equivalent to the idea of ‘nil’ or ‘null’ – the absence of a value. In other words, not playing a note is like the absence of a note.

If you look closely at the score, you’ll see that it’s actually a combination of black dots with lines which represent notes to play, and squiggly things which represent the rests. Luckily, Sonic Pi (v2.7+) has a very handy representation for a rest – :r. So if we run play :r, it actually plays silence! We could also write play :rest, play nil, or play false, which are all equivalent ways of representing rests.

Rhythm

Finally, there’s one last thing to learn how to decode in the notation: the timings of the notes. In the original notation, you’ll see that the notes are connected with thick lines called beams. The second note has two of these beams, which means it lasts for a 16th of a beat. The other notes have a single beam, which means they last for an 8th of a beat. The rest have two squiggly beams, which means they also represent a 16th of the beat.

When we decode and try to understand new things, a handy trick is to try to make everything as similar as possible to attempt to spot any relationships or patterns. When we rewrite our notation purely in 16ths, you can see that our notation just turns into a nice sequence of notes and rests…

 Maybe next time we can do La donna è mobile

Recoding the Habanera

We’re now in a position to start translating this bassline to Sonic Pi. Let’s encode these notes and rests in a ring:

(ring :d, :r, :r, :a, :f5, :r, :a, :r)

Let’s see what this sounds like. Throw it in a live loop and tick through it:

live_loop :habanera do
    play (ring :d, :r, :r, :a, :f5, :r, :a, :r).tick
    sleep 0.25
end

Fabulous: that instantly recognisable riff springs to life through your speakers. It took a lot of effort to get here, but it was worth it. High-five!

Moody synths

Now we have the bassline, let’s recreate some of the ambience of the operatic scene. One synth to try out is :blade, which is a moody 1980s-style synth lead. Let’s try it with the starting note :d, passed through a slicer and reverb:

live_loop :habanera do
    use_synth :fm
    use_transpose -12
    play (ring :d, :r, :r, :a, :f5, :r, :a, :r).tick
    sleep 0.25
end

with_fx :reverb do
    live_loop :space_light do
        with_fx :slicer, phase: 0.25 do
            synth :blade, note: :d, release: 8, cutoff: 100, amp: 2
        end
        sleep 8
    end
end

Now, try the other notes in the bassline: :a and :f5. Remember, you don’t need to hit Stop; just modify the code while the music is playing and hit Run again. Also, try different values for the slicer’s phase:, such as 0.5, 0.75, and 1.

Bringing it all together

Finally, let’s combine all the ideas so far into a new remix of the Habanera. You might notice that I’ve included another part of the bassline as a comment. Once you’ve typed it all into a fresh buffer, hit Run to hear the composition. Now, without hitting Stop, uncomment the second line by removing the # and hit Run again; how marvellous is that? Now, start mashing it around yourself and have fun!

use_debug false
bizet_bass = (ring :d, :r, :r, :a, :f5, :r, :a, :r)
#bizet_bass = (ring :d, :r, :r, :Bb, :g5, :r, :Bb, :r)

with_fx :reverb, room: 1, mix: 0.3 do
    live_loop :bizet do
        with_fx :slicer, phase: 0.125 do
            synth :blade, note: :d4, release: 8,
                cutoff: 100, amp: 1.5
        end
        16.times do
            tick
            play bizet_bass.look, release: 0.1
            play bizet_bass.look - 12, release: 0.3
            sleep 0.125
        end
    end
end

live_loop :ind do
    sample :loop_industrial, beat_stretch: 1,
        cutoff: 100, rate: 1
    sleep 1
end

live_loop :drums do
    sample :bd_haus, cutoff: 110
    synth :beep, note: 49, attack: 0,
        release: 0.1
    sleep 0.5
end

BBC Ten Pieces

If you’ve enjoyed this creative mashup of a classical piece, you should definitely get yourself and your school involved with the BBC Ten Pieces project. Check out the website to see all the other fantastically creative responses people are making to classical music. Last year, one of the schools coded their way to the finals with Sonic Pi; let’s see what you can do this year!

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.