2 years ago

Sonic Pi: Coded Beats

Sonic Pi creator Sam Aaron continues his essential new series with one of the most influential drum breaks in music history...

This tutorial can be found in The MagPi 37

One of the most exciting and revolutionary technical developments in modern music was the invention of computer-based samplers in the late 1970s. These electronic boxes of tricks allowed you to record any sound into them and then manipulate and play back those sounds in many interesting ways. For example, you could take an old record, find a drum solo (or break), record it into your sampler, and then play it back on repeat at half-speed to provide the foundation for your latest beats. This is how early hip-hop music was born, and today it’s almost impossible to find electronic music that doesn’t incorporate samples of some kind. Using samples is a really great way of easily introducing new and interesting elements into your live-coded performances.So where can you get a sampler? Well, you already have one: it’s your Raspberry Pi! The built-in live-coding app Sonic Pi has an extremely powerful sampler built into its core. Let’s play with it!

The Amen Break

One classic and immediately recognisable drum break sample is called the Amen Break. It was first performed in 1969 in the song Amen Brother by The Winstons, as part of a drum break. However, it was when it was discovered and sampled by early hip-hop musicians in the 1980s that it started being heavily used in a wide variety of other musical styles such as drum and bass, breakbeat, hardcore techno, and breakcore.

I’m sure you’re excited to hear that it’s also built
right into Sonic Pi. Clear up a buffer and throw in the
following code:

Hit Run and boom! You’re listening to one of the most influential drum breaks in the history of dance music. However, this sample wasn’t famous for being played as a one-shot: it was built for being looped.

Beat stretching

Let’s loop the Amen Break by using our old friend the live_loop, introduced in issue 36’s tutorial:

OK, so it is looping, but there’s an annoying pause every time round. That is because we asked it to sleep for 2 beats; however, with the default BPM of 60, the :loop_amen sample only lasts for 1.753 beats. We therefore have a silence of 2 – 1.753 = 0.247 beats. Even though it’s short, it’s still noticeable.

To fix this issue, we can use the beat_stretch: opt to ask Sonic Pi to stretch (or shrink) the sample to match the specified number of beats.

Now we’re dancing! Although, perhaps we want speed it up or slow it down to suit the mood. Playing with time OK, so what if we want to change styles to old-school hip-hop or breakcore? One simple way of doing this is to play with time or, in other words, to mess with the tempo. This is super-easy in Sonic Pi: just throw a use_bpm into your live loop…

Whilst you’re rapping over those slow beats, notice that we’re still sleeping for 2 and our BPM is 30, yet everything is in time. The beat_stretch opt works with the current BPM to make sure everything just works. Now, here’s the fun part. Whilst the loop is still live, change the 30 in the use_bpm 30 line to 50 . Whoa, everything just got faster yet kept in time! Try going faster: up to 80…to 120…now go crazy and punch in 200!


Now we can live-loop samples, let’s look at some of the most fun opts provided by the sample synth. First up is cutoff: , which controls the cutoff filter of the sampler. This is disabled by default, but you can easily turn it on:

Go ahead and change the cutoff: opt. For example, increase it to 100, hit Run, and wait for the loop to cycle round to hear the change in the sound. Notice that low values like 50 sound mellow and bassy, and high values like 100 and 120 are more full-sounding and raspy. This is because the cutoff: opt will chop out the high-frequency parts of the sound, just like a lawnmower chops off the top of the grass. The cutoff: opt is like the length setting, determining how much grass is left over.


Another great tool to play with is the slicer FX. This ill chop (slice) the sound up. Wrap the sample line ith the FX code like this:

Notice how the sound bounces up and down a little more. (You can hear the original sound without the FX by changing the mix: opt to 0 ). Now, try playing around with the phase: opt. This is the rate (in beats) of the slicing effect. A smaller value like 0.125 will slicer faster and larger values like 0.5 will slice more slowly. Notice that successively halving or doubling the phase: opt value tends to always sound good. Finally, change the wave: opt to one of 0, 1, or 2 and hear how it changes the sound. These are the various wave shapes. 0 is a saw wave, (hard in, fade out), 1 is a square wave (hard in, hard out), and 2 is a triangle wave (fade in, fade out).

Bringing it all together

Finally, let’s revisit the early Bristol drum and bass scene. Don’t worry too much about what all this code means; just type it in, hit Run, then start live-coding it by changing opt numbers and see where you can take it. Please do share what you create! See you next time…

Code listing