Oct 072013

I watched one of Carrie-Anne Philbin’s Geek Gurl Diaries videos yesterday about the newly released Sonic Pi application. Sonic Pi is a clever way of introducing programming concepts through making music. It looked like fun, so I had to have a play straight away.

I have a ‘musical past’, so this was rather irresistable. For some time I’ve been harbouring an urge to make a solenoid-powered glockenspiel play Rimsky-Korsakoff’s “Flight of the Bumblebee”, which is an amazing piece because it’s so fast.

Solenoids are costly, software isn’t

But that’s a really expensive project (solenoids + a decent glockenspiel) and I haven’t got there yet. Sonic Pi offered me the chance to do it without the costly hardware part. So I got hold of a score arranged for Piano, and started programming it in.

The first thing I did was set some variables for different rhythms, which meant the tempo could be varied by changing just the “speed” variable.

I forgot that, apart from the last three bars, and one quaver each in bars 83 & 95, the entire piece (treble part) is semi-quavers with ALMOST NO RESTS.

# flight of the bumblebee RasPi.TV style (or lack thereof)
# http://raspi.tv/?p=5216

# first some rythms
speed = 0.45  #set overall speed
breve = 8 * speed
semibreve = 4 * speed
minim = 2 * speed
crotchet = 1 * speed
quaver = 0.5 * speed
semi = 0.25 * speed
demi = 0.125 * speed
hemi = 0.0625 * speed

One Pi I have can handle speed = 0.4 without mangling the sound. Another needs to be 0.45. Ideally it would be slightly faster. I’m not sure exactly what the limiting factor is, but it’s nice to be able to push the limits and see what you can get away with.

4-5 hours & 1268 lines

It’s a 5 page score and took about 4-5 hours to program in 1268 lines of code. It’s quite a good example too, because there are quite a lot of repeated segments, which gives you a chance to practice loops. This is the first time I’ve ever written anything in Ruby.

Three quarters of the way through, I also realised it would have been more elegant and efficient to make functions for some of the repetitive parts which start at different pitch. That might happen in a future revision.

Repeats are easy

I can remember the feeling, in the school orchestra, when you see those two little dots at the end of a passage. “Oh no, we’ve got to do it all again!” Well in Sonic Pi, you just add a 2.times do before your repeated passage and an end at the end and you’re good to go (you should indent too, for readability).

But for now, sit back and enjoy the Sonic Pi rendition of The Flight of the Bumblebee.

Listen to it here

Download Bumble-Sonic-Pi MP3 file

The penultimate note sounds flat, but it is coded correctly. :)

Sonic Pi code download

Download bee.txt Sonic Pi code

Or get it straight on your pi with…
wget http://raspi.tv/wp-content/uploads/2013/10/bee.txt

No video from me today as my video PC has died. :( But here’s the GeekGurlDiaries video I mentioned that shows you how to do all this.

  8 Responses to “Sonic Pi – flight of the Bumblebee”

  1. Now that’s cool…


  2. Thank you once again Alex for this new and nice add-on to my list of applications for
    the RasPi.
    I’ve got it bumble_ing after reading 3x your instructions.


  3. Nice :-) That’s quite an impressive amount of typing there Alex!

    • Quite a lot of it was cutting and pasting blocks of 8 as I did a bar at a time.

      Hardest part was keeping track, which is why, nearer the end, I started adding bar numbers in comments.

      With hindsight, it could be done much more efficiently with functions for each of the main repeating themes, feeding the function the note number to start the sequence at. Although I don’t know how to do functions in Ruby yet, I can’t imagine it’s massively different/harder than Python.

      That’s for V2 :)

  4. This is great. I had thought of doing the bee before but was put off by the length. well done on your stamina! After an initial play through I made a couple of changes. The speed factor (0.45) is about as low as it will go without the output breaking up with clicks etc. However, if you change the synth to saw_beep by inserting the line
    with_synth “saw_beep
    after the variable declarations but before the first play command, then this synth is more amenable to playing faster. I was then able to reduce the speed factor to as low as 0.25 with acceptable output, and this gets nearer to the 1 minute duration for the piece, taking 1 min 23 seconds.

    If you are interested I have written quite a few sonic=pi pieces from beatles to bach which are available on https://gist.github.com/rbnpi

    • Excellent. I hadn’t played about with different synth sounds.

      I’m off abroad in a bit. When I get back, I’ll have a look at your Sonic Pi work Robin.

      If I did it again, I’d definitely try to use a function for the repeating themes, just passing it the value of the starting note.

  5. […] Sonic Pi flight of bumblebee video from RasPi.TV: Using Sonic Pi to play flight of the bumblebee on a Raspberry Pi also using an HDMIPi portable Hi-Def screen. This is using the V1 HDMIPi prototype which has HDMI audio broken out to 3.5mm stereo jack. It’s a bit crackly, but that’s Sonic Pi, not the hardware. […]

  6. […] Rimsky Korsakoff’s Flight of the Bumble Bee for Sonic Pi 2. I know that there is at least one version out there already for Sonic Pi 1, written by Alex Eames of Raspi-TV, which  used a single synth, and numerical […]

Leave a Reply