home/ atoms/ sc-iteration-do-synthesis

Integer.do inside a SynthDef creates that many UGen instances and accumulates them into a sum

A common synthesis technique in SuperCollider is to use n.do { ... } inside a SynthDef to create n UGen instances and sum them. Initialize a sum variable to 0, then on each iteration create a UGen (with unique random parameters per channel via {ExpRand(...)}!2), add it to the running total, and output the scaled sum. The iteration count must be a literal integer — not a SynthDef argument — because SynthDef arguments become Control UGens at runtime, which can’t be used as collection receivers for .do. Attempting to use an argument as the iteration count produces a static tone instead of the expected iteration.

Examples

SynthDef(\iter, { var sum = 0; 10.do { sum = sum + VarSaw.ar(40 * {Rand(0.99,1.02)}!2) }; Out.ar(0, sum * 0.05); }).add;

Assessment

A student writes arg num=10; num.do { ... } in a SynthDef expecting to control the number of voices. What actually happens? How do you correctly vary the number of oscillator voices?

“but it's not. It's actually an instance of a class called Control, which is a type of UGen that SuperCollider creates automatically whenever you declare an argument in a SynthDef.”
corpus · supercollider-tutorials-full-transcripts-and-code-eli-fields · chunk 15