Gui to control the synth

Hi everyone! Back with another (but the last) question. I’ve created a knob that changs the bacground colour when you move it, but i wanted to do a knob that changes something in my synthdef, maybe the amp or the rate, but I don’t know how to do it! Here is the knob: `w = Window.new ;
w.front ;
b = Rect(400, 500, 500, 400) ;
w = Window(“AuVis”, b).front ;
r = Color.hsv(0.7, 0.4, 1) ;
w.background = r ;

k = Knob(w, Rect(5, 5, 200, 200)) ;

k.action_({
}) ;

and here is my synth `(
SynthDef(\playbuf, { |sndBuf|
var sig;
sig = PlayBuf.ar(1, sndBuf, \rate.kr(1), loop: 1);
sig = sig * \amp.kr(-10.dbamp);
sig = sig * Env.asr(0.001, 1, 0.001).ar(Done.freeSelf, \gate.kr(1)); // I’ve created an envelope with a Done.freeSelf to stop the Synth
sig = Pan2.ar(sig, \pan.kr(0));
Out.ar(\out.kr(0), sig);
}).add;
)``

thnaks in advance for the response!

The knob can change a value in a Synth (not SynthDef) using the method .set.
Here’s an example:

// first run this
(
SynthDef.new("test1", { arg freq = 60, out = 0;
    Out.ar(out, Saw.ar(freq, 0.1));
}).add;
)
// then  run this
(
// add a Synth using SynthDef
x = Synth.new("test1");
// gui
b = Rect(400, 500, 500, 400);
w = Window.new.front;
w.onClose_({x.free;});  // free synth when window closes
r = Color.hsv(0.7, 0.4, 1) ;
w.background = r ;

k = Knob.new(w, Rect(5, 5, 200, 200)) ;

k.action_({arg knob;
	var newFreq;
	// use .linexp to map knob value to frequency range
	newFreq = knob.value.linexp(0, 1, 60, 6000);
	x.set("freq", newFreq);
});
)
// to free synth
x.free;

Best,
Paul

But your SynthDef doesn’t have a control called “freq”, so you should use either “rate” or “amp”.
For example, you could use a knob action like this to set your rate:

k.action_({arg knob;
	var newRate;
	newRate = knob.value.linexp(0, 1, 0.25, 4);
	x.set("rate", newRate);
});

Why not use a different audio file.

Can you post your full code?
(Please use 3 backticks before and after the code so it is formatted correctly.)
Best,
Paul

My rule #1 of GUI is: everything you want to do in a GUI should be possible first by a code statement. This helps you figure out what the GUI needs to do.

So the first question is, what code statement will set the control as you wish?

Currently there is none, because you haven’t saved the synths in a variable. Your collect does create synths, and they continue to run on the server, but in the language, the reference gets thrown away, so the language has no idea how to find them.

~mySynths = ~myBufs.collect { ... };

Next question, you have an array of synths, but one knob. Should the knob set them all, or only one at a time?

If all at once, you need a ~mySynths.do { |synth| synth.set(\rate, ...) } inside the knob action function. (Up to you to calculate the rate and substitute for ....) There are a couple other ways, but this is easiest to explain right now.

If one at a time, you’ll need some logic to decide which one.

hjh