home/ modules/ sonic-pi-first-loops-and-timing

Sonic Pi: first loops and timing

  • 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

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.

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

play and sleep are the two primitives for pitch and timing in Sonic Pi
Procedure L1 Foundations F
All of Western music can be expressed with two Sonic Pi commands: play (pitch) and sleep (timing)
Fact L1 Foundations F
Sonic Pi synth opts (amp:, pan:) control per-note volume and stereo position
Concept L1 Foundations F
use_bpm sets the tempo so sleep, envelopes, and FX phases all scale accordingly
Procedure L1 Foundations F
Rings in Sonic Pi wrap around on any index access, enabling infinite cycling of sequences
Concept L1 Foundations F
Sonic Pi ring chain methods transform rings immutably, returning a new ring without modifying the original
Concept L2 First instrument F
tick advances a beat counter per live_loop and returns the current ring element; look reads without advancing
Procedure L2 First instrument F
N.times do ... end repeats a code block exactly N times then continues execution
Procedure L1 Foundations F
loop do ... end in Sonic Pi runs forever and prevents any code after it from executing
Concept L1 Foundations F
chord() and scale() return rings of MIDI notes, making music-theory patterns directly executable
Procedure L1 Foundations FA
Sonic Pi is a strongly-timed language where sleep controls when events occur rather than pausing execution
Concept L2 First instrument F
Sonic Pi's live_loop runs each named loop as a concurrent thread you can edit while it plays
Concept L2 First instrument FN

Supporting — enrichment, not gating

Sonic Pi was built to teach programming to school children through music-making
Fact L0 Orientation FPO
use_synth switches the active synthesiser for subsequent play calls in the current thread
Procedure L2 First instrument F
knit creates a ring by repeating each value a specified number of times
Procedure L2 First instrument F
line generates a ring that linearly interpolates from start to finish across N steps
Procedure L2 First instrument F
octs generates a ring of the same note across multiple consecutive octaves
Procedure L2 First instrument F
Sonic Pi's random functions are deterministic: the same run always produces the same sequence
Concept L1 Foundations F
ADSR envelopes in Sonic Pi shape both amplitude and duration of each triggered note
Concept L2 First instrument FB
In the imperative sequencing paradigm time only advances when you explicitly move it forward — nothing sounds until you advance time
Concept L2 First instrument FA