Choose a random item on each Synth-call

I have a synthdef and somewhere in it want to have a random item chosen from an array of items, so that every time the Synth is triggered a new item from the array is being polled. Can some one give me a help, how I can do this?

You can use a combination of TRand and Select:

( 
x = { 
	var index = TRand.kr(0, 5, \trig.tr(1)).floor.asInteger.poll(5, \index);
	Select.kr(index, [10, 20, 30, 40, 50]).poll(5, \val)
}.play 
)

x.set(\trig, 1)
1 Like

I would do this in the language, when creating the synth (out of the view that the language is for control and logic, and the server is for mindless heavy lifting of audio data).

Select and TRand (or, alternately, TChoose which does those for you) are valid. The array in Select will have to encode all its constant values into the SynthDef, which is ok for small arrays but I’d be skeptical of doing that for large arrays. From that perspective, keeping array work in the language as much as possible has the advantage of consistency (the language handles small or large arrays equally well). And, if you hardcode the array into the SynthDef, it’s harder to change it later, where it’s easy to swap out an array reference in the language.

Opinions of course, ymmv, but IMO your use case (“every time the Synth is triggered”) is a perfect case for SynthDef arguments.

hjh

1 Like