I’m working with Tdefs a bit and trying to get flexible using them in live coding. I haven’t been able to get my head around using arguments inside a Tdef in order to control it from outside. In the following example I’ve got a Synth making Grains, and a “control” Tdef controlling these grains. I’d then like to generate a number of these control Tdefs and feed them different values. But at the moment I can’t even get the Tdef I’m working with to accept arguments. Not sure if this is a bug or if I’m the problem. Any help appreciated!
Cheers,
Jordan
(
SynthDef(\grain, {
var env, sig;
env = Env.sine(\dur.kr(1) * \overlap.kr(2)).ar(Done.freeSelf);
sig = SinOsc.ar(\freq.kr(440) * 2) * env * Rand(200, 2000);
sig = SinOsc.ar(\freq.kr(440) + sig + [0, 1]);
sig = sig.tanh;
sig = sig * env * \amp.kr(-10.dbamp);
//sig = Pan2.ar(sig, \pan.kr(0));
Out.ar(\out.kr(0), sig);
}).add;
)
// this works
(
Tdef(\a, {
loop {
rrand(10, 80).do {
Synth(\grain, [freq: (1..15).choose * 50, amp: -20.dbamp, overlap: 2]);
0.1.wait;
};
rrand(5, 7).wait;
};
}).play;
)
// this doesn't work
(
Tdef(\a, {
|freq = 50|
loop {
rrand(10, 80).do {
Synth(\grain, [freq: (1..15).choose * freq, amp: -20.dbamp, overlap: 2]);
0.1.wait;
};
rrand(5, 7).wait;
};
}).play;
)
I don’t use Tdef in my own workflow, so I guessed it was parallel to Task, but it isn’t.
I found, however, that it isn’t supported to replace the Tdef environment using envir_while the Tdef is playing. This might be a bug, or it might be an unavoidable limitation (perhaps Tdef should throw an error if you call envir_ while it’s playing).
^^ The problem here is that event is populated once, at the beginning of the function, and never updated again. Even though you set the object’s environment, the new information doesn’t make it into the function.
Thanks everyone for the help. This question sent me down quite a rabbit hole, I ended up (finally) using a custom event function instead of arguments directly inside the Tdef to give me more control. Here’s the finished code if anyone’s interested. Would also be keen for feedback if anyone thinks there are better ways of doing this!