Post

Introduction to Live Coding with Sonic Pi

Welcome to our introduction to live coding with Sonic Pi! Today we’ll explore how to create music in real-time using code, from basic concepts to the cultural movements that champion this art form.


What We’ll Cover Today

  • 🎵 What is Live Coding? A Philosophical Dive
  • 🏛️ The TOPLAP Movement & Its Manifesto
  • 🎉 The Algorave Movement: Hacking the Dance Floor
  • 🎹 Why Sonic Pi?
  • ⚡ Basic Concepts & Your First Loop
  • 🔥 Breaking Down Real Code
  • 🎛️ Advanced Techniques
  • 🚀 Performance Tips & Next Steps

What is Live Coding? A Philosophical Dive

💡 Core Concept

Live coding is a performing art form where the artist creates music, visuals, or other media by writing and modifying code live in front of an audience. It’s an act of improvisation where the creative process itself becomes the performance.

🎯 Key Principles

At its heart, the philosophy of live coding is about transparency and immediacy. It’s a conversation between the human performer and the machine, where every thought, mistake, and breakthrough is laid bare for the audience to see.

  • Transparency & Liveness: The process is made visible, often by projecting the coder’s screen. This demystifies the act of creation and invites the audience into the performer’s mind. As the TOPLAP manifesto states, “Obscurantism is dangerous. Show us your screens.
  • Improvisation: Live coding embraces the unexpected. Errors aren’t just bugs; they’re opportunities for “happy accidents” that can lead the performance in new and exciting directions.
  • Thinking in Action: The code is not just a tool but a direct representation of the performer’s thoughts. Algorithms are thoughts, and the act of writing them is a display of mental and creative dexterity.

The TOPLAP Movement & Its Manifesto

TOPLAP (The Temporary Transnational Terrestrial Transdimensional Organisation for the Promotion of Live Algorithm Programming) is the grassroots organization that formalized the live coding movement around 2004. It’s a global community built on a shared set of ideals.

✨ The Manifesto

The TOPLAP manifesto provides the philosophical backbone for the movement. Its core demands include:

  • “Give us access to the performer’s mind, to the whole human instrument.” This emphasizes the human element and the creative thought process.
  • “Code should be seen as well as heard…“ This reinforces the principle of transparency, making the underlying logic of the art visible.
  • “Algorithms are thoughts. Chainsaws are tools.” This powerful statement distinguishes live coding from simply using pre-made software. The act of creating the algorithm is the art form.

TOPLAP prefers the “skillful extemporisation of algorithm” and encourages performances with no backups, celebrating the risk and liveness of the moment.


The Algorave Movement: Hacking the Dance Floor

Born from the live coding scene, an Algorave is an event where people dance to music generated from algorithms. Coined in 2011, the term fuses “algorithm” and “rave” to describe a unique club culture.

🕺 Cultural Background

An Algorave is “algorithmic dance music where the algorithms are exposed and the music is for dancing.” It takes the experimental, academic roots of live coding and injects them with the energy of rave culture. It’s a meeting point for hackers, artists, and club-goers.

🌍 Global Community

  • It’s a worldwide movement that celebrates diversity and inclusivity, consciously working against the “tech bro” stereotype often associated with programming.
  • The focus is on music-as-activity and music-as-culture, rather than a commercial product. Most Algorave software is free and open-source, aligning with a hacker ethic of sharing and transparency.
  • The music itself is often complex, generative, and defies the repetitive nature of traditional electronic dance music, sometimes as a direct response to historical laws that tried to regulate rave music.

Why Sonic Pi?

Educational ✏️

  • Created at Cambridge
  • Designed for learning
  • Built-in samples & synths
  • Friendly error messages

Professional 🎤

  • Real-time modification
  • Precise timing control
  • Advanced synthesis
  • Live performance ready

🎼 Ruby-Based Syntax

Readable, expressive, and powerful enough for complex compositions.


Basic Concepts

Your First Sound

play :C

Adding Time

play :C sleep 1 play :E sleep 1 play :G

🔄 The Foundation: live_loop

live_loop :simple do play :C sleep 1 end

Your First Live Loop

Simple Drum Pattern

live_loop :drums do sample :bd_haus sleep 1 sample :sn_dolf sleep 1 end

🎛️ Adding Variety

live_loop :drums do sample :bd_haus, amp: 0.8 sleep 0.5 play :C4, synth: :fm, release: 0.3 sleep 0.5 end

Try it! Modify the amp, change the sleep values, or swap out samples!


Adding Complexity

⏰ Precise Timing with at

live_loop :precise do at [0, 0.5, 1, 1.5] do sample :bd_haus end at [0.25, 1.25] do sample :sn_dolf end sleep 2 end

🎲 Adding Randomness

live_loop :random do sample :bd_haus, amp: rrand(0.5, 1.0) sleep [0.5, 1, 0.25].choose sample :sn_dolf if one_in(3) sleep 0.5 end

🎭 Live Coding Reality Check

Drake meme: Writing music with traditional notation (no), Live coding beats in real time (yes)

When your code runs the first time without errors vs. debugging in front of an audience 😅


Breaking Down Real Code: Breakbeat

🥁 Complex Breakbeat Pattern

live_loop :breakbeat3 do # Scheduled hihat-like sounds at (range 0.25, 7, 0.5) do sample :loop_breakbeat, beat_stretch: 2, onset: 11, attack_level: 1, release: 0.1, hpf: 50, amp: 1 end # Main breakbeat loop - 3 full cycles 3.times do sample :loop_breakbeat, beat_stretch: 2 sleep 2 end end

Key Techniques:

  • range - Creates rhythmic patterns: (0.25, 0.75, 1.25…)
  • onset - Extracts specific parts of a sample
  • beat_stretch - Matches sample to tempo

Advanced Sample Manipulation

8.times do sample :loop_breakbeat, beat_stretch: 2, num_slices: 16, slice: pick, amp: (ring 0,1).choose, rate: (ring 1,-1).choose sleep 0.125 end

🎛️ What’s Happening?

  • num_slices: 16 - Chops sample into 16 pieces
  • slice: pick - Randomly selects pieces
  • (ring 0,1).choose - Randomly mute/unmute
  • rate: (ring 1,-1).choose - Forward/backward playback

💡 Result: Glitchy, granular breakbeat with random variation!


Advanced Techniques: LOFI Chords

🎹 Chord Progression Setup

use_bpm 88 rhy = (ring 2, 1, 0.5, 2, 1, 0.5) # Triplet rhythm chrd = (ring :g, :d, :c, :d) # Root notes modd = (ring :minor7, :minor7, :minor, :major)

🎵 Dynamic Chord Player

live_loop :voxy do use_synth :tri c = chrd.tick # Get next chord root m = modd.look # Get chord quality play_chord (chord c+12, m), attack: 1, sustain: 0.7 sleep 2.25 play_chord (chord c, m, num_octaves: 2), attack: 0.75 sleep 1.75 end

Effects and Sound Design

🌊 Layering Effects

with_fx :distortion, distort: 0.55 do with_fx :reverb, room: 0.75, mix: 0.4 do with_fx :wobble, res: 0.6, phase: 4, cutoff_min: 50 do live_loop :bass do play :C2, synth: :fm, release: 0.8 sleep 1 end end end end

🎛️ Effect Chain

Signal Flow: Bass → Wobble Filter → Reverb → Distortion → Output

💡 Pro Tip: Effects process from inside out—the innermost effects are applied first!


Live Performance Tips

🎯 Preparation

  • Modular Code - Independent live_loops you can enable/disable.
  • Clear Variables - Easy to modify parameters.
  • Backup Plans - Have working code ready.

⚡ Real-time Strategies

  • Comment/Uncomment - Use # to toggle code sections.
  • Parameter Tweaking - Change numbers for immediate effect.
  • Stop/Start Loops - Add stop inside live_loops.

🎪 Performance Mindset

Embrace mistakes—they often lead to the best musical moments!


Common Challenges & Solutions

❌ Syntax Errors

Solution: Keep backups, use simple syntax first.

⏰ Timing Issues

Solution: Always include sleep in live_loops, check BPM settings.

🔊 Audio Overload

Solution: Use stop to halt loops, manage amp levels.

🧠 Mental Overload

Solution: Start simple, build gradually, practice transitions.

💡 Remember: Ctrl+. (or Cmd+.) stops all sound immediately!


Next Steps & Resources

📚 Learning Path

  1. Practice Daily - 15 minutes of experimentation.
  2. Study Examples - Analyze existing live coded music.
  3. Join Community - Online forums and local algorave events.
  4. Perform Early - Start with friends, build confidence.

🌐 Community Resources

  • in-thread.sonic-pi.net - Official Sonic Pi forum
  • toplap.org - The home of the live coding community
  • algorave.com - Find events worldwide
  • GitHub - Share and discover code
  • YouTube/Twitch - Watch live coding streams

Let’s Code Together! 🎵

Engage This Step by Step:

live_loop :our_song do # Your code here! sample :bd_haus sleep 1 sample :sn_dolf sleep 1 end live_loop :melody do play (scale :c, :major).choose, synth: :fm sleep 0.5 end

🎯 Challenge: Add effects, change the scale, or modify the timing! Remember: Ctrl+R (or Cmd+R) runs your code!


Thank You!

Questions & Live Coding?

🎵 “Code is the new instrument”

Start simple, think algorithmically, and embrace the glitches!

Keep Experimenting! 🚀

This post is licensed under CC BY 4.0 by the author.