Iteration in a OSCdef

Hi,
newb here,

i’m building a synth with the DynKlang Ugen and i want to control each amplitude with a slider , using an ihm built in open stage control

.

The code below works well but i think it deserves factorization

(
//-----create the synth
SynthDef(\klang2, {
	arg out, freqs=#[88, 293, 210, 287, 296, 157, 112, 65 ], amps=#[0.35, 0.23, 0.12, 0.05, 0.27, 0.14, 0.09, 0.1], phases=#[1, 1.5, 2, 2.5, 0, 0.5, 0.3, 1.9], gate = 0;
	var sig, amp, env;
	env = EnvGen.kr(Env.adsr(0.05,0.1,0.9,3),gate, doneAction:2);
	sig = (DynKlang.ar(`[freqs, amps, phases]));
	sig = Splay.ar(sig) * 0.25 * env;
	Out.ar(out, sig);
}).add;

//-----instantiate
~klang2 = Synth(\klang2);


//-----Osc controls

//gate
OSCdef.new(
	\klang2,
	{
		arg msg, time, addr, port;
		msg.postln;
		~klang2.set(\gate, msg[1]);
	},
	'/klang2'
);

//amplitudes
OSCdef.new(
	\klang2_amp,
	{
		arg msg;
		8.do{
			var amp1 = msg[1], amp2 = msg[2], amp3 = msg[3], amp4 = msg[4], amp5 = msg[5], amp6 = msg[6], amp7 = msg[7], amp8 = msg[8];
			msg.postln;
			~klang2.setn(\amps, [ amp1.linexp(0,1,0.001,1), amp2.linexp(0,1,0.001,1), amp3.linexp(0,1,0.001,1), amp4.linexp(0,1,0.001,1), amp5.linexp(0,1,0.001,1), amp6.linexp(0,1,0.001,1), amp7.linexp(0,1,0.001,1), amp8.linexp(0,1,0.001,1)]);
		}
	},
	'/canvas_amp'
);

i’ve tried to iterate with do but i’m making a mistake. This must be obvious but i can’t figure it out.

here is the code

OSCdef.new(
	\klang2_amp,
	{
		arg msg;
		8.do({arg i;
			msg[i].postln;
			~klang2.setn(\amps, msg[i]);
		});
	},
	'/canvas_amp'
);

thank you for your help

1 Like

I think you want something of the form:

~klang2.setn(\amps, [some, array, of, values]);

Your first example is functionally correct, but with one mistake and one cleanup thing…

  1. You don’t need 8.do - you’re just setting the same values 8 times.
  2. It looks like your linexp’s are all the same: you can run linexp on an array and get the same result - this could look something like:
msg[1..8].linexp(0, 1, 0.001, 1)
2 Likes