Sonic Pi: first loops and timing
Learning objectives
- learner can sequence pitch and rhythm with play/sleep and use_bpm
- learner can use rings, iteration and synth opts to shape a repeating phrase
- learner can turn a phrase into an editable live_loop
Capstone — one whole task that evidences the objectives
Build an 8-bar Sonic Pi groove from play/sleep primitives, drive it from rings with tick, set tempo with use_bpm, and convert it into a live_loop you edit while it plays.
Prerequisite modules
This module is where Sonic Pi stops being a beep machine and becomes an instrument: a repeating groove you can lean on at a jam, a school demo, or the opening minutes of an algorave set, where a single laptop and the built-in synth engine are the whole rig. The target is one authentic act — write an 8-bar phrase, get it cycling, and keep editing it while the audience hears it never stop.
The arc starts fully supported. First you hard-code a melody line-by-line, leaning on “play and sleep are the two primitives for pitch and timing” and the surprise that consecutive plays without sleep sound simultaneously — the strongly-timed model that trips up every procedural programmer. Then you scale it: use_bpm to pick a tempo so sleep values become musical beats, N.times to stamp out bars, and amp:/pan: opts to give notes dynamics and width. The middle exercises replace hard-coded notes with data: rings that wrap forever, scale() and chord() to fill them musically, tick to walk them one step per iteration, and immutable chain methods (.reverse, .shuffle, .mirror) to mutate the phrase without rewriting it. The final, unsupported step is the conversion — seeing why a bare infinite loop is a black hole you cannot edit, then wrapping the phrase in a named live_loop and re-evaluating changes mid-playback.
Every required atom is load-bearing for that capstone: remove any one and the groove either won’t sequence, won’t cycle, or won’t survive a live edit. Supporting atoms enrich the same groove afterwards — knit, line and octs for fancier rings, deterministic randomness for shareable variation, envelopes and use_synth for timbre — worth reaching for once the loop is already breathing. Drill play/sleep phrasing and ring-tick cycling until they are reflexes; everything else can stay look-up knowledge.
Runnable examples
Generated from the context/ instrument corpus by concept (redistributable idioms only). Do not edit — regenerate with gen-module-examples.mjs.
adsr-envelope
note("c3").s("sawtooth").attack(0.01).decay(0.1).sustain(0.6).release(0.3)
strudel-0205 · CC0
{ Saw.ar(220) * EnvGen.kr(Env.perc(0.001, 0.2), Impulse.kr(2)) * 0.3 }.play
supercollider-0013 · CC0
strongly-timed-sequencing
1::samp => now;
chuck-0008 · MIT
seeded-randomness
use_random_seed 42; 8.times { play (scale :e3, :minor).choose; sleep 0.25 }
sonicpi-0051 · CC0
Atoms in this module
Required — these gate the capstone
Supporting — enrichment, not gating
Part of curricula
- Live Coder — zero to performing live-coded music — Patterns, Grooves & Voices optional
Unlocks — modules that require this one