Hi, I am trying to learn SC, just getting my toes wet with the very first steps how to use SynthDefs. I struggle to understand different random behaviors when using SynthDefs. I will try to explain what I don’t get, and I hope to use the correct terms in my descriptions, if I fail, my apologies, please bear with me.
To me it looks like Rand()
is evaluated anew each time an instance with Rand()
is created from a SynthDef; whereas Array.rand()
seems to be only evaluated when adding the SynthDef with Array.rand()
to the server, so that all instances of the SynthDef have identical values in Array.rand()
.
- Is my assumption correct?
- If it isn’t correct, where is my mistake?
- If it is correct, how can I achieve that also each instance of
Array.rand()
has different random values? And what is the reason (or benefit) whyRand()
andArray.rand()
behave differently?
I’ll try to explain with code examples what I mean with the above.
In the following example, I get different random notes each time I execute x=Synth(\zufall);
which uses Rand()
.
(
SynthDef(\zufall,{
arg freq = 400;
var sig, env;
sig = SinOsc.ar(Rand(300,400));
env = EnvGen.ar(Env.perc(0.02, 2), doneAction: 2);
Out.ar(0,sig!2*env*0.1)
}).add
)
x=Synth(\zufall);
But in this example, the pitch does not change whenever I execute x=Synth(\kein_zufall);
, which uses Array.rand()
.
(
SynthDef(\kein_zufall,{
arg freq = 400;
var sig, env;
sig = SinOsc.ar(Array.rand(2, 300, 400));
env = EnvGen.ar(Env.perc(0.02, 2), doneAction: 2);
Out.ar(0,sig*env*0.1)
}).add
)
x=Synth(\kein_zufall);
But if I execute the following repeatedly, I get different notes each time, although Array.rand() is being used:
(
SynthDef(\kein_zufall,{
arg freq = 400;
var sig, env;
sig = SinOsc.ar(Array.rand(2, 300, 400));
env = EnvGen.ar(Env.perc(0.02, 2), doneAction: 2);
Out.ar(0,sig*env*0.1)
}).play
)
Thanks much in advance!