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) # https://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
Or get it straight on your pi with…
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.