Skip to content

Instantly share code, notes, and snippets.

@AlexDev404
Created October 23, 2026 18:44
Show Gist options
  • Select an option

  • Save AlexDev404/92cfa33b0e0ca7e7ae8aaee03d7490a3 to your computer and use it in GitHub Desktop.

Select an option

Save AlexDev404/92cfa33b0e0ca7e7ae8aaee03d7490a3 to your computer and use it in GitHub Desktop.
Sonic Pi (Full test)
# ============================================================================
# SONIC PI ENGINE DIAGNOSTIC — "Self-Test in D Minor"
# ============================================================================
# A full track (~3 min, 120 BPM, D minor) that doubles as an engine test.
#
# PART 1 Preflight: ~60 silent checks of the language & engine API
# (notes, scales, chords, rings, randomness, time, tuning, nodes,
# FX buffers, mixer, MIDI out, OSC loopback). Results in the log.
# PART 2 The track. Every section exercises a subsystem:
# BOOT basic waveforms (sine/tri/square/saw) as a test-tone arpeggio
# GROOVE samples, swing via `at`, density fills, tb303 + control/slides
# SYNTHS every synth in `synth_names` plays one note of the melody
# FX every FX in `fx_names` processes half a bar of arpeggio
# SAMPLES every built-in sample, as a 16th-note kaleidoscope
# FINALE beat_stretch, slicing, rpitch, reverse playback, perlin
# randomness, time_warp echoes
# OUTRO waveforms in reverse, play_pattern_timed, final report
#
# Lists are read from the engine at runtime, so this adapts to your Sonic Pi
# version. Skipped on purpose: :sound_in/:sound_in_stereo (need an audio input),
# :sound_out/:sound_out_stereo (route to other output channels), :record
# (tested separately in preflight).
#
# Watch the log: every synth/FX/sample is named as it plays, and a full
# PASS/FAIL report prints at the end. Press Stop after "DIAGNOSTIC COMPLETE".
# Needs a reasonably strong machine.
# ============================================================================
use_bpm 120
use_debug false
use_sched_ahead_time 2
use_random_seed 2026
diag = {
pass: 0, fail: 0, failed: [],
synth_ok: 0, synth_fail: [],
fx_ok: 0, fx_fail: [],
sample_ok: 0, sample_fail: [],
loaded: 0, bars: 0, osc: false
}
# ============================================================================
# PART 1 — PREFLIGHT CHECKS (silent)
# ============================================================================
define :check do |name, test|
begin
test.call
diag[:pass] += 1
puts " PASS #{name}"
rescue Exception => e
diag[:fail] += 1
diag[:failed] << "#{name}: #{e.message.to_s.lines.first.to_s.strip}"
puts " FAIL #{name}"
end
end
define :near do |a, b|
assert (a - b).abs < 0.01
end
puts "=== PREFLIGHT: #{version} ==="
# --- Notes, pitch, conversion
check "note(:a4)", -> { assert_equal note(:a4), 69 }
check "midi_to_hz", -> { near midi_to_hz(69), 440 }
check "hz_to_midi", -> { near hz_to_midi(440), 69 }
check "ratio_to_pitch", -> { near ratio_to_pitch(2), 12 }
check "pitch_to_ratio", -> { near pitch_to_ratio(12), 2 }
check "octs", -> { assert_equal octs(60, 3).to_a, [60, 72, 84] }
check "note_info", -> { assert note_info(60) != nil }
check "quantise", -> { near quantise(11, 4), 12 }
# --- Harmony
check "chord", -> { assert_equal chord(:c4, :major).to_a, [60, 64, 67] }
check "chord_invert", -> { assert_equal chord_invert(chord(:c4, :major), 1).to_a, [64, 67, 72] }
check "scale", -> { assert_equal scale(:c4, :major).size, 8 }
check "degree", -> { assert_equal degree(:iii, :c4, :major), 64 }
check "chord_degree", -> { assert_equal chord_degree(:i, :c4, :major, 3).to_a, [60, 64, 67] }
check "note_range", -> { assert_equal note_range(:c4, :c5, pitches: chord(:c, :major).to_a).to_a, [60, 64, 67, 72] }
check "scale_names", -> { assert scale_names.to_a.include?(:dorian) }
check "chord_names", -> { assert chord_names.size > 20 }
# --- Rings & sequencing
check "ring wrap", -> { assert_equal ring(1, 2, 3)[4], 2 }
check "knit", -> { assert_equal knit(:a, 2, :b, 1).to_a, [:a, :a, :b] }
check "spread (euclidean)", -> { s = spread(3, 8); assert (s.size == 8 && s.to_a.count(true) == 3) }
check "range", -> { assert_equal range(0, 10, step: 2).to_a, [0, 2, 4, 6, 8] }
check "line", -> { assert_equal line(0, 4, steps: 4).to_a, [0, 1, 2, 3] }
check "bools", -> { assert_equal bools(1, 0).to_a, [true, false] }
check "doubles", -> { assert_equal doubles(1, 4).to_a, [1, 2, 4, 8] }
check "halves", -> { assert_equal halves(8, 4).to_a, [8, 4, 2, 1] }
check "ring.reverse", -> { assert_equal ring(1, 2, 3).reverse.to_a, [3, 2, 1] }
check "ring.rotate", -> { assert_equal ring(1, 2, 3).rotate.to_a, [2, 3, 1] }
check "ring.mirror", -> { assert_equal ring(1, 2, 3).mirror.size, 6 }
check "ring.reflect", -> { assert_equal ring(1, 2, 3).reflect.size, 5 }
check "ring.stretch", -> { assert_equal ring(1, 2, 3).stretch(2).size, 6 }
check "ring.repeat", -> { assert_equal ring(1, 2, 3).repeat(2).size, 6 }
check "ring.take/drop", -> { assert_equal ring(1, 2, 3).take(2).drop(1).to_a, [2] }
check "ring.butlast", -> { assert_equal ring(1, 2, 3).butlast.size, 2 }
check "ring.pick", -> { assert_equal ring(1, 2, 3).pick(5).size, 5 }
check "ring.shuffle", -> { assert_equal ring(1, 2, 3).shuffle.sort.to_a, [1, 2, 3] }
check "tick/look", -> { t = tick(:diag); assert_equal look(:diag), t }
# --- Randomness
check "random seed repeat", -> { use_random_seed 42; a = rand; use_random_seed 42; assert_equal rand, a }
check "rand_back", -> { a = rand; rand_back; assert_equal rand, a }
check "rand_skip/reset", -> { rand_skip; rand_reset; assert rand.between?(0, 1) }
check "rrand_i / dice", -> { assert (rrand_i(1, 3).between?(1, 3) && dice(6).between?(1, 6)) }
check "one_in / choose", -> { assert (one_in(1) && [1, 2].include?([1, 2].choose)) }
check "current_random_seed",-> { use_random_seed 7; assert_equal current_random_seed, 7 }
check "random source perlin", -> { use_random_source :perlin; rand; use_random_source :white }
use_random_seed 2026
# --- Time & tempo
check "current_bpm", -> { assert_equal current_bpm, 120 }
check "with_bpm", -> { with_bpm(60) { assert_equal current_bpm, 60 } }
check "bt", -> { near bt(1), 0.5 }
check "rt", -> { near rt(1), 2 }
check "vt", -> { assert vt >= 0 }
check "sched ahead", -> { assert current_sched_ahead_time > 0 }
check "density", -> { density(2) { } }
check "time_warp", -> { time_warp(0.5) { play 60, amp: 0 } }
check "at", -> { at([0.1]) { play 60, amp: 0 } }
check "with_swing", -> { with_swing(0.1, pulse: 4) { play 60, amp: 0 } }
# --- Synth state & tuning
check "with_synth", -> { with_synth(:tb303) { assert_equal current_synth, :tb303 } }
check "transpose", -> { with_transpose(2) { assert_equal current_transpose, 2 } }
check "octave", -> { with_octave(1) { assert_equal current_octave, 1 } }
check "cent tuning", -> { with_cent_tuning(10) { assert_equal current_cent_tuning, 10 } }
check "just tuning", -> { with_tuning(:just) { play 62, amp: 0 } }
check "synth defaults", -> { with_synth_defaults(amp: 0) { with_merged_synth_defaults(release: 0.1) { play 60 } } }
# with_synth_defaults restores the *previous* value on exit; here that was nil, which
# poisons the thread-local every later live_loop inherits (nil.merge in synth). Reseed it.
use_synth_defaults({})
# --- Nodes
check "play + kill", -> { n = play 60, amp: 0, sustain: 4; kill n }
check "control + slide", -> { n = play 60, amp: 0, note_slide: 1; control n, note: 72 }
check "play chord", -> { play chord(:d4, :m7), amp: 0 }
# --- Samples
check "sample_groups", -> { assert sample_groups.to_a.include?(:bd) }
check "all_sample_names", -> { assert all_sample_names.size > 100 }
check "load_sample", -> { load_sample :loop_amen; assert sample_loaded?(:loop_amen) }
check "sample_duration", -> { with_bpm(60) { near sample_duration(:loop_amen), 1.753 } }
check "beat_stretch length",-> { near sample_duration(:loop_amen, beat_stretch: 4), 4 }
# --- Engine features
check "synth_names", -> { assert synth_names.to_a.include?(:beep) }
check "fx_names", -> { assert fx_names.to_a.include?(:reverb) }
check "buffer + :record", -> { with_fx(:record, buffer: buffer(:diag_buf, 1)) { play 62, amp: 0 } }
check "mixer control", -> { set_mixer_control! hpf: 21; reset_mixer! }
check "set_volume!", -> { set_volume! current_volume }
check "time-state set/get", -> { set :diag_x, 7; assert_equal get[:diag_x], 7 }
check "spark", -> { spark range(1, 9) }
check "MIDI out", -> { midi :d3, sustain: 0.1, vel_f: 0.01; midi_cc 7, 100 }
# OSC loopback: send to Sonic Pi's own OSC port; a thread waits for it to come back
in_thread name: :osc_listener do
sync "/osc*/diag/ping"
diag[:osc] = true
puts " PASS OSC loopback (received)"
end
check "OSC send", -> { use_osc "localhost", 4560; osc "/diag/ping", 1 }
puts "=== PREFLIGHT DONE: #{diag[:pass]} passed, #{diag[:fail]} failed ==="
# ============================================================================
# PART 2 — THE TRACK
# ============================================================================
synths = synth_names.to_a.reject { |s| [:sound_in, :sound_in_stereo].include?(s) }
fxs = fx_names.to_a.reject { |f| [:sound_out, :sound_out_stereo, :record].include?(f) }
samples = all_sample_names.to_a
plan = [
[:boot, 8],
[:groove, 16],
[:synths, (synths.size / 4.0).ceil], # 4 synths per bar
[:fx, (fxs.size / 2.0).ceil], # 2 FX per bar
[:samples, (samples.size / 16.0).ceil], # 16 samples per bar
[:finale, 16],
[:outro, 4]
]
# Harmony: i – VI – III – VII in D minor (Dm7, Bbmaj7, Fmaj7, C7), one chord per bar
prog = ring(:i, :vi, :iii, :vii)
octfix = ring(0, -12, 0, -12)
define :chord_at do |g|
chord_degree(prog[g], :d3, :minor, 4).to_a.map { |n| n + octfix[g] }
end
# Preload every sample in the background so the sample section stays in time
in_thread name: :preload do
samples.each_slice(6) do |group|
group.each do |s|
begin
load_sample s
diag[:loaded] += 1
rescue Exception
end
end
sleep 2
end
end
# ---------------------------------------------------------------- DRUMS
kick_a = bools(1,0,0,0, 0,0,0,0, 1,0,1,0, 0,0,0,0)
kick_b = bools(1,0,0,0, 0,0,1,0, 0,0,1,0, 0,0,0,1)
hats = spread(11, 16)
swing = 0.035
with_fx :compressor, threshold: 0.4 do
live_loop :drums do
sync :bar
sec = get[:section]
stop if sec == :end
sb, sbs = get[:sbar], get[:sbars]
diag[:bars] += 1
mode = { groove: :full, synths: :light, fx: :light, samples: :kick, finale: :full }[sec]
if mode
kick = sb.even? ? kick_a : kick_b
times = (0..15).map { |i| i * 0.25 + (i.odd? ? swing : 0) } # 16ths with swing
at times, (0..15).to_a do |i|
sample :bd_haus, amp: 1.5, lpf: 110 if kick[i]
if mode != :kick
sample :drum_cymbal_closed, amp: (i % 4 == 2 ? 0.6 : 0.3), rate: 1.1 if hats[i]
if mode == :full
sample :sn_dolf, amp: 1.0 if i == 4 || i == 12
sample :drum_cymbal_open, amp: 0.25, sustain: 0.3, release: 0.1 if i == 14
end
end
end
# Crash on the downbeat of big sections
sample :drum_splash_hard, amp: 0.7 if sb == 0 && mode == :full
# Snare fill on the last beat of a section (density compresses time 2x)
if sb == sbs - 1 && mode != :kick
in_thread do
sleep 3
density 2 do
4.times do |k|
sample :sn_dolf, amp: 0.4 + k * 0.15, rate: 1.2
sleep 0.25
end
end
end
end
end
# Reverse cymbal swelling into every section change (negative rate)
if sb == sbs - 1 && sec != :outro
in_thread do
sleep [4 - sample_duration(:drum_cymbal_open), 0].max
sample :drum_cymbal_open, rate: -1, amp: 0.5
end
end
end
end
# ---------------------------------------------------------------- ACID BASS
with_fx :compressor, threshold: 0.3 do
live_loop :bass do
sync :bar
sec = get[:section]
stop if sec == :end
if [:groove, :synths, :finale].include?(sec)
root = chord_at(get[:gbar])[0] - 12
offs = ring(0, 0, 12, 0, 7, 0, 10, 12)
gates = spread(9, 16)
in_thread do
use_synth :tb303
b = play root, sustain: 3.8, release: 0.2, amp: 0.6, wave: 0, res: 0.8,
cutoff: 55, cutoff_slide: 3.8, cutoff_slide_shape: 3,
note_slide: 0.05, amp_slide: 0.02
control b, cutoff: (sec == :finale ? 105 : 90) # filter sweep over the bar
16.times do |i|
control b, note: root + offs[i], amp: (gates[i] ? 0.6 : 0.12) # glides + gating
sleep 0.25
end
end
end
end
end
# ---------------------------------------------------------------- PADS
with_fx :reverb, room: 0.85, mix: 0.6 do
live_loop :pads do
sync :bar
sec = get[:section]
stop if sec == :end
sb, sbs = get[:sbar], get[:sbars]
a = case sec
when :boot then sb >= 2 ? 0.5 * (sb - 1) / 6.0 : 0
when :groove, :finale then 0.5
when :fx then 0.2
when :outro then 0.5 * (sbs - sb) / sbs.to_f
else 0
end
if a > 0
c = chord_at(get[:gbar])
synth :hollow, notes: c, attack: 1, sustain: 2.5, release: 2, amp: a, cutoff: 95
synth :blade, note: c[-1] + 12, attack: 1, sustain: 2, release: 2, amp: a * 0.4,
vibrato_rate: 5, vibrato_depth: 0.08, cutoff: 90
end
end
end
# ---------------------------------------------------------------- BOOT / OUTRO TEST TONES
with_fx :echo, phase: 0.75, decay: 2, mix: 0.25 do
live_loop :test_tones do
sync :bar
sec = get[:section]
stop if sec == :end
if sec == :boot || sec == :outro
sb = get[:sbar]
wave = sec == :boot ? ring(:sine, :tri, :square, :saw)[sb] : ring(:saw, :square, :tri, :sine)[sb]
puts "WAVEFORM TEST: #{wave}"
c = chord_at(get[:gbar]).map { |n| n + 12 }
notes = c + c.map { |n| n + 12 }
notes = notes.reverse if sec == :outro
in_thread do
use_synth wave
notes.each do |n|
opts = { amp: 0.25, attack: 0.02, release: 0.4, pan: rrand(-0.4, 0.4) }
opts[:cutoff] = 75 + sb * 4 unless wave == :sine
play n, opts
sleep 0.5
end
end
# Last bar: a closing bell motif, testing play_pattern_timed
if sec == :outro && sb == get[:sbars] - 1
in_thread do
use_synth :pretty_bell
play_pattern_timed [:d5, :a4, :f4, :d4], [0.5, 0.5, 0.5, 2], amp: 0.3
end
end
end
end
end
# ---------------------------------------------------------------- SYNTH PARADE
with_fx :compressor, threshold: 0.25 do
with_fx :reverb, room: 0.5, mix: 0.3 do
live_loop :synth_parade do
sync :bar
sec = get[:section]
stop if sec == :end
if sec == :synths
sb = get[:sbar]
c = chord_at(get[:gbar]).map { |n| n + 12 }
at [0, 1, 2, 3], [0, 1, 2, 3] do |i|
idx = sb * 4 + i
s = synths[idx]
if s
begin
synth s, note: c[ring(0, 2, 1, 3)[i]], amp: 0.45, release: 0.8, pan: ring(-0.5, 0.5)[i]
diag[:synth_ok] += 1
puts "SYNTH #{idx + 1}/#{synths.size}: #{s}"
rescue Exception
diag[:synth_fail] << s
puts "SYNTH FAIL: #{s}"
end
end
end
end
end
end
end
# ---------------------------------------------------------------- FX PARADE
with_fx :compressor, threshold: 0.3 do
live_loop :fx_parade do
sync :bar
sec = get[:section]
stop if sec == :end
if sec == :fx
sb = get[:sbar]
c = chord_at(get[:gbar]).map { |n| n + 12 }
at [0, 2], [0, 1] do |h|
idx = sb * 2 + h
f = fxs[idx]
if f
begin
puts "FX #{idx + 1}/#{fxs.size}: #{f}"
with_fx f do
use_synth :pluck
4.times do |k|
play c[(k + h) % 4], amp: 0.8
sleep 0.5
end
end
diag[:fx_ok] += 1
rescue Exception
diag[:fx_fail] << f
puts "FX FAIL: #{f}"
end
end
end
end
end
end
# ---------------------------------------------------------------- SAMPLE KALEIDOSCOPE
with_fx :compressor, threshold: 0.3 do
with_fx :reverb, room: 0.4, mix: 0.25 do
live_loop :sample_parade do
sync :bar
sec = get[:section]
stop if sec == :end
if sec == :samples
sb = get[:sbar]
at (0..15).map { |i| i * 0.25 }, (0..15).to_a do |i|
idx = sb * 16 + i
s = samples[idx]
if s
begin
sample s, amp: 0.7, attack: 0.005, sustain: 0.15, release: 0.08,
pan: ring(-0.6, 0.6)[i], lpf: 120
diag[:sample_ok] += 1
puts "SAMPLE #{idx + 1}/#{samples.size}: #{s}"
rescue Exception
diag[:sample_fail] << s
end
end
end
end
end
end
end
# ---------------------------------------------------------------- FINALE LAYERS
with_fx :reverb, room: 0.6, mix: 0.3 do
live_loop :finale do
sync :bar
sec = get[:section]
stop if sec == :end
if sec == :finale
sb = get[:sbar]
g = get[:gbar]
c = chord_at(g)
# Amen break: whole (beat_stretch) on even bars, sliced and rearranged on odd bars
in_thread do
if sb.even?
sample :loop_amen, beat_stretch: 4, amp: 0.7, lpf: 115
else
order = ring(0, 1, 2, 3, 0, 1, 6, 7)
8.times do |k|
begin
sample :loop_amen, beat_stretch: 4, num_slices: 8, slice: order[k], amp: 0.7, lpf: 115
rescue Exception
sample :loop_amen, beat_stretch: 4, start: order[k] / 8.0, finish: (order[k] + 1) / 8.0, amp: 0.7
end
sleep 0.5
end
end
end
# Em9 guitar sample re-pitched to Dm9 (rpitch) on the tonic bars
sample :guit_em9, rpitch: -2, amp: 0.6, lpf: 100 if prog[g] == :i
# Lead: notes chosen by smooth perlin randomness, with time_warp echoes
in_thread do
use_synth :prophet
begin
use_random_source :perlin
rescue Exception
end
sc = scale(:d4, :minor_pentatonic, num_octaves: 2)
8.times do |k|
if spread(5, 8)[k]
n = k == 0 ? c[0] + 24 : sc.choose
play n, amp: 0.3, release: 0.4, cutoff: 95
time_warp 0.75 do
play n + 12, amp: 0.1, release: 0.3, cutoff: 80
end
end
sleep 0.5
end
end
end
end
end
# ============================================================================
# CONDUCTOR — drives every loop with one cue per bar
# ============================================================================
in_thread name: :conductor do
sleep 2 # let all live_loops reach their first `sync`
gbar = 0
plan.each do |sec, bars|
puts "======== SECTION: #{sec.upcase} (#{bars} bars) ========"
bars.times do |b|
set :section, sec
set :sbar, b
set :sbars, bars
set :gbar, gbar
cue :bar
gbar += 1
sleep 4
end
end
set :section, :end
cue :bar
sleep 4
total_pass = diag[:pass] + diag[:synth_ok] + diag[:fx_ok] + diag[:sample_ok]
total_fail = diag[:fail] + diag[:synth_fail].size + diag[:fx_fail].size + diag[:sample_fail].size
puts "=" * 48
puts " SONIC PI ENGINE DIAGNOSTIC — REPORT"
puts " #{version}"
puts "=" * 48
puts " Preflight checks : #{diag[:pass]} passed, #{diag[:fail]} failed"
diag[:failed].each { |f| puts " x #{f}" }
puts " Synths : #{diag[:synth_ok]}/#{synths.size} failed: #{diag[:synth_fail]}"
puts " FX : #{diag[:fx_ok]}/#{fxs.size} failed: #{diag[:fx_fail]}"
puts " Samples : #{diag[:sample_ok]}/#{samples.size} failed: #{diag[:sample_fail]}"
puts " Samples preloaded: #{diag[:loaded]}/#{samples.size}"
puts " Bar clock : #{diag[:bars]}/#{gbar} bars delivered via cue/sync"
puts " OSC loopback : #{diag[:osc] ? 'received' : 'NOT received (check OSC settings)'}"
puts " TOTAL : #{total_pass} passed, #{total_fail} failed"
puts "=" * 48
puts " DIAGNOSTIC COMPLETE — press Stop"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment