| name | sonic-pi |
| description | Sonic Pi Ruby API reference and patterns. Use when: writing Sonic Pi code, creating synths, samples, live loops, effects, ADSR envelopes, OSC communication, MIDI, scales, chords, rings, randomisation, or real-time audio coding. |
Sonic Pi Reference
When to Use
- Writing or modifying Sonic Pi Ruby code
- Creating synths, samples, live loops, or effects
- Working with scales, chords, rings, or randomisation
- Setting up OSC or MIDI communication
- Debugging audio timing or envelope issues
Core Audio Functions
Playing Notes
play 60
play :e3
play 60, amp: 0.5, pan: -1
play chord(:E3, :minor)
play_pattern_timed scale(:c3, :major), 0.125
Synths
use_synth :prophet
synth :tb303, note: :e1, cutoff: 100
s = synth :prophet, note: :e1, release: 8
control s, note: 65, cutoff: 130
Popular synths: :prophet, :dsaw, :fm, :tb303, :pulse, :blade, :beep, :tri, :saw, :square, :dpulse, :dtri, :piano
Samples
sample :loop_amen
sample :loop_amen, rate: 0.5
sample :loop_amen, rate: -1
sample :loop_amen, beat_stretch: 2
sample :bd_haus, start: 0.25, finish: 0.5
sample "/path/to/file.wav"
Sample prefixes: :ambi_, :bass_, :elec_, :perc_, :guit_, :drum_, :misc_, :bd_, :loop_
ADSR Envelopes
play 60, attack: 0.5, decay: 0.2, sustain: 1, release: 2
play 60, attack: 0.1, attack_level: 1, decay: 0.2, decay_level: 0.5,
sustain: 1, sustain_level: 0.5, release: 0.5
Timing & Structure
Sleep and BPM
sleep 1
sleep 0.5
use_bpm 120
use_bpm 60
Live Loops
live_loop :drums do
sample :bd_haus
sleep 0.5
end
live_loop :bass do
sync :drums
use_synth :tb303
play :e1, release: 0.3
sleep 1
end
Threads and Cue/Sync
in_thread do
loop do
cue :tick
sleep 1
end
end
in_thread do
loop do
sync :tick
sample :drum_heavy_kick
end
end
Functions
define :my_riff do |note, dur|
use_synth :dsaw
play note, release: dur
end
my_riff :e3, 0.5
Effects (FX)
with_fx :reverb, room: 0.8 do
play 50
end
with_fx :echo, phase: 0.25, decay: 4 do
sample :loop_amen
end
with_fx :reverb do
with_fx :distortion do
play 50
end
end
with_fx :reverb do |r|
play 50
sleep 1
control r, mix: 0.9
end
Common FX: :reverb, :echo, :distortion, :slicer, :wobble, :flanger, :lpf, :hpf, :rhpf, :rlpf, :compressor, :bitcrusher, :eq, :tremolo, :whammy, :pan, :ring_mod, :vowel, :ixi_techno, :gverb
Data Structures
Rings
r = (ring 60, 62, 64)
r[0]
r[3]
r.tick
r.look
r.choose
r.shuffle
r.reverse
Scales and Chords
scale(:e3, :minor_pentatonic)
scale(:c3, :major, num_octaves: 2)
chord(:E3, :minor)
chord(:E3, :m7)
chord(:E3, :dom7)
Tick System
live_loop :arp do
play (scale :e3, :minor_pentatonic).tick, release: 0.1
sleep 0.125
end
play notes.tick(:melody)
sleep durations.tick(:rhythm)
Randomisation
rand
rrand(50, 100)
rrand_i(50, 100)
dice
one_in(3)
choose([1, 2, 3])
use_random_seed 42
State (Thread-Safe)
set :my_val, 100
get[:my_val]
OSC Communication
live_loop :osc_listener do
use_real_time
a, b, c = sync "/osc*/trigger/prophet"
synth :prophet, note: a, cutoff: b, sustain: c
end
use_osc "localhost", 4560
osc "/hello/world", 1, 2, 3
MIDI
live_loop :midi_piano do
use_real_time
note, velocity = sync "/midi*/note_on"
synth :piano, note: note, amp: velocity / 127.0
end
midi_note_on :e3, 100
midi_note_off :e3
midi :e3, sustain: 0.1
midi_cc 1, 64
Sliding (Smooth Transitions)
s = play 60, release: 8, note_slide: 1
sleep 1
control s, note: 72
Useful Functions
sample_duration :loop_amen
use_synth_defaults release: 0.2, cutoff: 80
density 2 do ... end
spread(3, 8)
knit(:e3, 3, :g3, 1)
octs(:e3, 3)
line(0, 1, steps: 10)