How to change parameters dynamically within a Tdefgui


I found this snippet of code (can’t remember where):

SynthDef(\sinegrain, {
	arg pan, freq, amp;
	var grain;

	grain =, 0, amp)*(,0.001,0.1,doneAction:2)-0.001);,,pan))}).add;

{{arg i;

		Synth(\sinegrain, [\freq,rrand(100,10000),\amp, exprand(0.05,0.1), \pan, 1.0.rand2]);

I would like to change parameters like “frequency” or “amp” in a GUI while this Synth is running. I tried to figure out how I could put this Routine ( in a Tdef and apply a GUI to it, but I don’t seem to be able to find out how to do this or whether what I wish to do should be done in a different way.

Thanks for your help!

NdefGui is a good choice for that in general but here you don’t have one Synth running but many, each fired every 0.1 seconds. So gui-ing that is going to be a challenge. Turn the anonymous routine into a Pdef and use PdefGui to change the common arguments of the events.

But even then it’s unclear what do you want to vary from a gui here since all the Synth arguments are set to random values… maybe the endpoints of the random ranges? Assuming you want something like that, just showing how to do it for freq(s) and dur:

Pdef(\zingy).envir = (dur: 0.1, freqlo: 100, freqhi: 10000)

Spec.add(\freqlo, \freq); Spec.add(\freqhi, \freq); 

(Pdef(\zingy).source = Pbind(
	\instrument, \sinegrain,
	\freq, Pwhite(Pkey(\freqlo), Pkey(\freqhi)),
	\amp, Pexprand(0.05, 0.1),
	\pan, Pwhite(-1, 1),


Thanks a lot!

Yes, that does what I was looking for.
Sorry, forgot to change the part with the random ranges.
That wouldn’t make much sense.