Live Coding in Ableton Live: Surprisingly Creative
Published on Tuesday 4 February 2025
Live coding: type programming code in and spit music out. It might sound a bit much, but if you’re an Ableton Live user looking for a new way to get creative, you’ve just found it. If you want to impress your hoards of fans on stage or at home, it’s time to set that inner nerd free and invite Sonic Pi into your Ableton software. Here, Guestblogger Hens Zimmerman shows you the way.
Image 1: Ableton Link. More on this later.
Sonic Pi: 100% Free & 100% Complete
Sonic Pi is one of the most popular live music coding programmes going. It’s also completely free and is continuously improved and updated. The current 4.5.0 version is available for MacOS (including versions that support ARM as well as Intel micro processors), Windows, Linux and Raspberry Pi OS.
Who’s Typing?
Programming music can, in the first place, meet with some resistance – simply because, who wants to be typing when they’re making music? But it’s actually a surprisingly expressive music-making method. Just a tiny change in a piece of code can lead to something completely different and, by building up lines of code and removing others, you can create something really beautiful. The documentation and community that surrounds Sonic Pi is also great. A tutorial even comes as part of the software so, before you know it, you’ll be making music.
Time for a little taste of what you can expect. The short piece of code included below can be copied and pasted into Sonic Pi and, when you hit the Run button, Sonic Pi will simply play it back for you:
live_loop :bax do # random melody. use_random_seed 101 use_synth :prophet notes = (scale :d2, :phrygian, num_octaves: 3) 16.times do play notes.choose, release: 0.3, cutoff: rrand(70, 120) sleep 0.25 end end
Ruby, is that You?
While I won’t be laying out and explaining the entire syntax of Sonic Pi in this article, it’s worth saying a little bit about the coding language. Anyone with any experience with coding might already have noticed that it looks a lot like Ruby (which also goes under the name Ruby on Rails, among others). And you’d be right. The coding language of Sonic Pi is actually based on many of the concepts from Ruby so it can also be used in Sonic Pi.
Experiments in Music
The piece of code above simply generates a random melody and, by speaking to Sonic Pi using the right language and instructions, you can really experiment with music. The line ‘use_synth :prophet’ instructs Sonic Pi to use the built-in synthesizers. That’s right, Sonic Pi comes with a big library of virtual synthesizers, but you can use the samples that also come included. The code below, for example, plays back and loops the iconic ‘Amen Break’:
use_bpm 134 live_loop :bax2 do sample :loop_amen, beat_stretch: 4 sleep 4 end
But there’s nothing standing in the way of you using your own samples and loops:
use_bpm 70 live_loop :bax3 do sample "/Users/henszimmerman/Loop_003.wav", beat_stretch: 8 sleep 8 end
Something For You
Sonic Pi is an extraordinarily complete programme. In the second image below, you can see what it looks like when it executes a piece of code. In the main screen, you can see both the programming code as well as the results of what you do. In the top left, you can see the interface where the code is typed in. This is really useful, because it means you can edit your code and immediately test it out by hitting the Run button (or pressing Command-R). It also means that you can even DJ with Sonic Pi and change and evolve your music over time. Multiple loops can also be coded, synchronised and played back and, in the world of Sonic Pi, a loop is a really broad concept, so it could be a one or two bar long sample, or it could be a more complex piece of code fed through various synthesizers.
Play Around with Sonic Pi Before You Read On?
If live coding is an entirely new idea, then it maybe makes sense to try a few things out with Sonic Pi before going any further. In the rest of this blog, we’re going to take a look at the different ways you can exchange information between Sonic Pi and Ableton Live, so if you don’t have any experience with either programme, then things are likely to get a bit abstract. Don’t worry though, even kids can learn to work with Sonic Pi, so you’ll have no problem getting to grips with it. Remember: you need to walk before you can run.
Open the Door For Sonic Pi
How do you share the magic of Sonic Pi with your favourite Live software? To be honest, it just takes a few mouse clicks.
Sending Audio From Sonic Pi to Ableton Live
Maybe you want to be able to play with both Ableton Live and Sonic Pi on stage, or maybe you want to record your Sonic Pi algorithm to Ableton Live, in any case it can be useful to set up Sonic Pi as an audio source in Ableton Live. While Sonic Pi does support multi-channel audio interfaces, there is a little trick you can use to make it easier to connect the audio output from Sonic Pi to Ableton Live. The first thing you need to do is install the MacOS Blackhole driver, which serves as a sort of virtual cable that can connect different programmes. Basically, if you send the sound of your Mac to Blackhole, you can record the sound of anything you have running on your Mac to Ableton Live. In the 3rd image below, you can see in the top right of the window how to send the audio output of your Mac through Blackhole.
Image 3: Setting up Blackhole as the audio output of your Mac.
Now, when you start Sonic Pi, the programme will automatically use Blackhole as the audio output. You can also see this in the Sonic Pi preferences (see Image 4).
Image 4: Sonic Pi is now using Blackhole
Now, all we need to do is tell Ableton Live that Blackhole is our audio input. Alternatively, you can go into the Audio Midi Setup menu (by clicking on Command>Shift>U) and create an aggregate device made up of Blackhole and your standard audio hardware. This will mean that, besides using Blackhole as your audio input, you can still select your hardware as an audio input within Ableton Live. For this blog, we’ll just use the blunt-axe method and make Blackhole our only Ableton Live audio input.
Image 5: Blackhole set up as the input in Ableton Live (Live – Settings… – Audio).
Now, when you make a new audio track in Ableton Live and Ext and select ‘as input’, then Sonic Pi will be the audio source for the track. Sometimes, it can be useful to set the Monitor mode to ‘In’ on the audio track. Just bear in mind that effects like Beat Repeat only work in Ableton Live when the transport is running.
MIDI From Ableton Live to Sonic Pi
Of course, you could just see Sonic Pi as another virtual instrument. So if typing out notes and chords sounds a little bit awkward and cumbersome, you’ll be glad to know that Sonic Pi also accepts MIDI commands. How does it work?
Before we can get started, we need to build a ‘bridge’ so that Sonic Pi can read any MIDI messages coming from Ableton Live. The easiest way to do this is to use the MacOS IAC Driver. Open Audio MIDI Setup (just search for it in Finder under Applications > Utilities) and, under MIDI Studio you’ll see all of the MIDI devices you have available. In Image 6, you can see what this currently looks like on my Mac.
Image 6: MIDI Studio in MacOS Audio MIDI Setup.
Double click on the IAC Driver to open a new window. Tick the box next to ‘Device is online’ (see Image 7 below).
Image 7: IAC Driver is online.
Now, we need to go into Sonic Pi and, under the ‘IO’ tab in the Preferences menu, you can see that Sonic Pi is now listening to both the IAC Driver and MIDI input. In Image 8, you can see what this looks like on my computer, but you might see a slightly different configuration. As long as you can see the IAC Driver there, you’re all good.
Image 8: In Sonic Pi you can see the IAC MIDI driver. Yay!
Now, we need to configure Ableton Live to send MIDI notes to Sonic Pi, which also happens via the IAC Driver. Go into the Ableton Live Preferences menu and, in the Track column under Link Tempo MIDI, tick the box next to ‘MIDI Ports – Out: IAC Driver’. In Image 9, you can see what that looks like.
Image 9: Ableton Live Preferences and IAC MIDI out activated.
Now, we can make a MIDI track in Ableton Live and select the IAC Driver as the MIDI out. The MIDI channel can just be set to default 1. In Image 10, you can see that I’ve also made a clip that continuously loops a few notes.
Image 10: Ableton Live now sends MIDI-notes out to the IAC Driver.
Hopefully, you already have Sonic Pi open and ready to go, so you can see in the Cues window on the right of the Sonic Pi interface that MIDI information is coming in. For as long as Ableton Live continues to loop the clip, the MIDI events will continue to scroll through the Cues window. You can see what this looks like in Image 11. The Log and Cues windows of Sonic Pi are really handy for seeing what’s happening and for solving any problems. It goes a bit too far for this particular blog post, but Sonic Pi also supports OSC (Open Sound Control), so if that’s your kind of thing, you can have a whole load of fun with it.
Image 11: Incoming MIDI events in Sonic Pi in the Cues window.
The MIDI events that Sonic Pi receives will roughly look like the following:
/midi:iac_driver_bus_1:1/note_on [53, 127]
The relevant MIDI note number (53) and the note velocity (127) is displayed between the square brackets. The communication between the two programmes is sorted, so now all we need to do is write some code that will listen to the correct MIDI input and watch it translate everything into beautiful music. Here’s an example of what that might look like:
live_loop :bax6 do # Less latency! use_real_time note, velocity = sync "/midi:iac_driver_bus_1:1/note_on" synth :rhodey, note: note, amp: velocity / 127.0 end
The ‘use_real_time’ command allows our Sonic Pi synthesizer to playback at minimum latency. In the next line, we can see the respective note and velocity information from the IAC driver:
note, velocity = sync "/midi:iac_driver_bus_1:1/note_on"
From here, we can use the variable note and velocity in any way we want. In this case, we’re creating a Fender Rhodes style synthesizer where the note-information can have a value between 0 and 127, inclusive. For the note velocity, we still need to divide the read value by 127.0, because Sonic Pi expects a velocity value with a 0 to 1 range. As you probably know already, MIDI velocity (just like MIDI CC) has a value of 0 to 127, inclusive.
synth :rhodey, note: note, amp: velocity / 127.0
When we click on Run, Sonic Pi plays our MIDI information via Ableton Live using the sound of the built-in ‘:rhodey-synthesizer’. In Image 12, you can see where the Run button is in the Sonic Pi interface. Here, it’s absolutely essential that the name of the MIDI event (/midi:iac_driver_bus_1:1/note_on) is written flawlessly into your code. Luckily, you can easily copy any events from the Cues window and paste them into your code editor.
Image 12: We run our code in Sonic Pi.
Send MIDI From Sonic Pi to Ableton Live?
And what if we want to flip things around? That means sending MIDI information from Sonic Pi to Ableton Live – which is totally possible! There’s also a pretty interesting reason for doing it, because Sonic Pi is capable of generating a lot of controlled yet ‘random’ melodies in both familiar and less familiar scales. In Image 13, you can see Sonic Pi helping me to figure out one of many available scales using code completion. How great would it be if we recorded all of that as MIDI notes that can be used in Ableton Live?
Image 13: Code completion and scales in Sonic Pi.
We’ll start off with a simple loop that plays a melody using just Sonic Pi:
live_loop :bax7 do use_random_seed 1 use_synth :dsaw notes = (scale :d2, :phrygian, num_octaves: 2) 16.times do play notes.choose, release: 0.1 sleep 0.25 end end
If we do a little tweaking (try inputting a different number after ‘use_random_seed’) until we’re satisfied, we can send our melody as a set of MIDI events to Ableton Live. Because we want to be able to hear each note in Sonic Pi while sending it as a MIDI event, we’ll save it as variable ‘n’ in the meantime.
live_loop :bax8 do use_random_seed 1 use_synth :dsaw notes = (scale :d2, :phrygian, num_octaves: 2) 16.times do # Save the following note as a variable. n = notes.choose play n, release: 0.1 midi_note_on n, 100 sleep 0.25 midi_note_off n end end
Ableton Live only needs to listen to the IAC Driver, so we need to open Preferences in Ableton Live and set the IAC Driver as the only MIDI input. You can see what this looks like in Image 14. You can also just leave the IAC Driver selected as the MIDI input and MIDI output.
Image 14: Ableton Live listens to the MIDI input from the IAC Driver.
You might be wondering where in the code it states that our MIDI notes are being sent to the IAC Driver. That’s not something we have to specify in the code since Sonic Pi will automatically send out MIDI notes to all available MIDI devices. Since we’re dealing with a fairly simple example here, it doesn’t matter too much, but in the real world you might want to be able to very specifically control multiple MIDI devices. Luckily, that’s not a problem either. In the code below, a looping bass line will play back on MIDI channel 1 (bax9) and a higher melody will loop on MIDI channel 2 (bax10). Because both loops take different random seeds as their starting point, they each play the same notes but do it differently and, because both loops are fishing their notes out of the D phrygian scale, they sound, by definition, beautiful with each other. In the first line of code, we’ve also specified the BPM. We’ll talk more about that in a minute. What you’ll also notice if you feed Sonic Pi this bit of code, is that it won’t generate any sound itself, because we’re just sending the MIDI note info to the IAC Driver.
use_bpm 110 live_loop :bax9 do use_random_seed 1 notes = (scale :d2, :phrygian, num_octaves: 2) 16.times do # Save the following note as a variable. n = notes.choose midi_note_on n, port: "iac_driver_bus_1", channel: 1 sleep 0.25 midi_note_off n, port: "iac_driver_bus_1", channel: 1 sleep 0.25 end end live_loop :bax10 do use_random_seed 42 notes = (scale :d3, :phrygian, num_octaves: 3) 16.times do # Save the following note as a variable. n = notes.choose midi_note_on n, port: "iac_driver_bus_1", channel: 2 sleep 0.25 midi_note_off n, port: "iac_driver_bus_1", channel: 2 sleep 0.25 end end
Now, we can head back over to Ableton Live and create two MIDI tracks, each assigned with an instrument. So, for our bass line on MIDI channel 1, you can use an Analog Bass preset and for the melody on MIDI channel 2, you can use, say, an Analog Guitar & Plucked instrument. But, if you’d prefer to use Serum or Massive X… don’t let me hold you back. In Image 15, you can see that the track on the left is getting its MIDI info from IAC Driver channel 1 and the track on the right is getting its MIDI info from IAC Driver channel 2. We also need to make sure to set the BPM of our Ableton Live project to 110 so any effects we use will also be synchronised with our coded loops.
Image 15: Two MIDI tracks with virtual instruments assigned to them playing back notes sent from Sonic Pi.
There is another very obvious but very good reason for making sure that the BPM is set to the same value in Sonic Pi and Ableton Live. At the same BPM, when you record a coded loop or piece, the MIDI notes immediately come in at the right tempo in your Arrangement View. In Image 16, you can see what the MIDI note info looks like when recorded from Sonic Pi to Ableton. The notes are perfectly synchronised because both programmes have been set to 110 BPM. However, the notes aren’t snapped perfectly into the piano roll grid. Of course, you can neaten everything up quickly with the Quantize command, but it’s not a very stylish fix, is it?
Image 16: Recorded MIDI notes displayed in the Arrangement View. The notes are synchronised but not snapped to the grid.
No Fuss with Ableton Link
A solution for this is Ableton Link because, yep – Sonic Pi fully supports Ableton Link, which is open source tech that keeps devices and programmes in sync. A lot of equipment and software developers now support Ableton Link and, as long as your programmes or ‘peers’ are in the same network, they can be perfectly synchronised. A lot of Ableton Link programmes also support start and stop commands from other Ableton Link peers within the same network. At the moment, Sonic Pi supports the start command but not the stop command. By including the program line ‘link_sync’ in our code, we can make Sonic Pi wait for Ableton Live (or your preferred network programme) so that both programmes start in perfect synchronicity.
Using Ableton Link we can make sure that our little melody is perfectly synchronised with Ableton Live. But, to activate the Ableton Link function, we first need to adjust a couple of settings in both programmes, starting with Ableton Live. Go to the Preferences and, next to the Show Link Toggle, switch Start Stop Sync on. You can see what this looks like in Image 17 below.
Image 17: The Link function is now switched on in Ableton Live.
From now on, a Link button will be displayed in the top left corner of the Ableton Live window, so you can activate Ableton Link by simply clicking on the Link button so that it’s lit. You can see what that looks like in Image 18.
Image 18: Link is now activated in Ableton Live.
In Sonic Pi, go to the Link Metronome & Global Time Warp window. If you can’t see it, then you can enable it in the Preferences menu (found under the Editor tab). In Image 19, you can see that Sonic Pi is happily aware that Link has been activated and has found one peer within the network, and that peer is Ableton Live. If a few Ableton Link devices are involved in the session, then the correct number of peers will be displayed.
Image 19: Link is already activated in Sonic Pi.
Now, the blue Link button in Ableton Live that we activated earlier also displays ‘1 Link’. Yay! Ableton Live and Sonic Pi are now synchronised via Ableton Link. If you were to stick the code below into Sonic Pi and hit Run, Sonic Pi will now wait for a start signal from Ableton Live.
use_bpm 110 # Sonic Pi is waiting for Ableton Live to start. link_sync # 4 beats in one bar. quantum = 4 # experiment determined value # to tighten up sync. phase = -0.034 # This loop plays a melody in D Phrygian. live_loop :bax11 do # The following line synchronises this loop # with Ableton Live. link quantum, phase use_random_seed 1 notes = (scale :d2, :phrygian, num_octaves: 2) 16.times do # Save the following note as a variable. n = notes.choose midi_note_on n, port: "iac_driver_bus_1", channel: 1 sleep 0.25 midi_note_off n, port: "iac_driver_bus_1", channel: 1 sleep 0.25 end end # This loop plays the Amen break. live_loop :bax12 do # The following line synchronises this loop # with Ableton Live. link quantum, phase sample :loop_amen, beat_stretch: 4 end
Now, when we hit record in Ableton Live, Sonic Pi will start to play on the first count of the bar. The MIDI notes come in and are recorded into Live, snapping perfectly into the grid. You can see how good this looks in Image 20 below. All going well, you’ll also hear the Amen break playing back in perfect synchronisation. It’s Sonic Pi and not Ableton Live that’s playing the loop back, so make sure that Sonic Pi is using your sound card and isn’t routed through Blackhole yet. Of course, you could use Blackhole as an input for an audio track in Ableton Live in this situation.
Image 20: Ableton Live is now in step with Sonic Pi.
Can Ableton Link Do More?
Some specific coding languages like C++, Max, Lua and Python, include modules or github repositories that can handle Ableton Link. If you really get into Ableton Link, you can also take a look at all of the apps and programmes that support it. The brilliant Patterning 2 app for the iPad, for example, fully supports Link. For modular synth heads, it’s also worth knowing that you can use the ML:2 Eurorack module, which is just 2 HP wide. Titles like Avid Pro Tools, Bitwig and Logic Pro all support Ableton Link as well and, if you’re a DJ, you can also work with Link. The Denon SC6000 Prime, for example, can be synchronised via Ableton Link. If you want to know more, then check out the full list of Ableton Link enabled gear and software here.
Happy live coding!
See also
» Ableton Note: A Musical Notepad for Fresh Tracks
» Ableton Live Arrangement Templates: Copy & Paste Inspiration
» A 7-Step Guide to Live-Editing in Ableton Live
» Tools for Discovering Chords in Ableton Live
» How to Use MIDI Polyphonic Expression (MPE) in Ableton Live
No comments yet...