home/ atoms/ sc-tempo-clock-scheduling

TempoClock schedules functions in beats, re-running them whenever they return a number

SuperCollider offers several clocks: SystemClock and AppClock both run in seconds (AppClock at lower priority, for GUI work), while TempoClock is beat-based and is the default for musical scheduling. Many TempoClocks can run at once at different speeds. Scheduling is driven by return values: if a scheduled function returns a number, the clock treats that number as the delay before running the function again, so returning a number repeats and returning nil stops. The classic gotcha is a function that should run once but accidentally returns a number, producing an unintended loop.

Examples

t = TempoClock.new(2); // 2 beats/sec = 120 BPM t.sched(0, { |beat| (“beat: ” ++ beat).postln; 1 }); // repeats every beat t.sched(4, { “once, 4 beats in”.postln; nil }); // runs once

Assessment

Why does TempoClock.default.sched(0, { 1 }) loop forever, and how do you make a scheduled function run exactly four times then stop?

“If you schedule function that returns a number, the clock will treat that number as the amount of time before running the function again.”