Evaluating Code

Do you mean the following way using a unique variable for each synth?

(
x = {
SinOsc.ar(100, 0, 0.5)
+ SinOsc.ar(400, 0, 0.4)
+ SinOsc.ar(900, 0, 0.3)
* SinOsc.ar(3, 0, 1)
}.play
)

(
y = {
SinOsc.ar(200, 0, 0.5)
+ SinOsc.ar(800, 0, 0.4)
+ SinOsc.ar(1800, 0, 0.3)
* SinOsc.ar(4, 0, 1)
}.play
)

(
z = {
SinOsc.ar(300, 0, 0.5)
+ SinOsc.ar(1200, 0, 0.4)
+ SinOsc.ar(2100, 0, 0.3)
* SinOsc.ar(5, 0, 1)
}.play
)

x.free // stop
y.release // fade-out
z.release(10) // fade-out in 10 sec.

s.freeAll // stops all synths. You can do it by pressing cmd + . in macOS and Ctrl + . in Linux and Windows.

You could simplify this processes as follows:
(Please refer to regarding the syntaxes in the codes above:
< NamedControl: a better way to write SynthDef arguments >)

// 1. defining a function or defining a SynthDef.

// 1.1. defining a function
(
~chordTrm = {
var freq = \freq.kr(100), amp = \amp.kr(0.2); // I modified the value for amp because I think your value could be too loud.
var trmFrq = \trmFrq.kr(3), trmAmp = \trmAmp.kr(1);
var freqOsc = SinOsc.ar(freq * [1, 4, 9], 0, amp * [0.5, 0.4, 0.3]).sum;
var trmOsc = SinOsc.ar(trmFrq, 0, trmAmp);
freqOsc * trmOsc
}
)

// 1.2. defining a Synth
(
SynthDef(\chordTrm, {
var freq = \freq.kr(100), amp = \amp.kr(0.2);
var trmFrq = \trmFrq.kr(3), trmAmp = \trmAmp.kr(1);
var freqOsc = SinOsc.ar(freq * [1, 4, 9], 0, amp * [0.5, 0.4, 0.3]).sum;
var env = Env.asr.kr(gate: \gate.kr(1)); // to use release in the SynthDef
var trmOsc = SinOsc.ar(trmFrq, 0, trmAmp) * env;
var sig = LinPan2.ar(freqOsc * trmOsc, \pos.kr(0)); // stereo panning is added. Often we use Pan2.ar.
Out.ar(0, sig) // In SynthDef, we must always use Out.ar or OffesetOut.ar. Otherwise, in specific cases ReplaceOut.ar or XOut.ar
}
).add
)

// 2. execute various synths

x = ~chordTrm.play // default values are used
y = ~chordTrm.play(args:[\freq, 200])
z = Synth(\chordTrm, [\freq, 300])

// 3. change the arguments of synths

x.set(\freq, 150);
y.set(\freq, 250, \trmFrq, 5)
z.set(\freq, 350, \trmFrq, 1)

// 4. algorithmic changes of

fork{ 100.do{ arg nthExecution; z.set(\freq, 325, \trmFrq, 2, \pos, sin(nthExecution/10)); 0.05.wait} }

// 5. fade-out
x.release(2); y.release(4); z.release(8)

Have I correctly understood your question in the previous post?

1 Like