home/ atoms/ sonic-pi-live-coding-define-redefine

Redefining a named function while a named thread loops is the foundational Sonic Pi live-coding pattern

The foundational live-coding approach: define a function :my_sound, start a named thread that calls it in a loop, then redefine the function on the fly. The thread keeps running and picks up the new definition each iteration. Named threads prevent layering on repeated Run presses. This pattern is more explicit than live_loop but illustrates the same principle: decouple sound definition from looping infrastructure and hot-swap the definition while the loop continues.

Examples

define :my_sound do use_synth :tb303 play 50, release: 0.2 sleep 0.25 end

in_thread(name: :looper) do loop do my_sound end end

Assessment

Explain why the thread does not need to be restarted when you redefine :my_sound. Contrast this with live_loop and describe one advantage of each approach.

“`:looper` thread looped around it simply called the new definition”
corpus · sonic-pi-built-in-tutorial · chunk 19