it’s much more common to just change the amplitude, especially for a synth as simple as the one in your code so far. if the synth was significantly more complicated (CPU / RAM hungry), then it may make sense, but as-is it’s consuming very little.
to spell out how you would do it if you really wanted:
Here's how you'd free and re-alloc a synth
SynthDef(\mySynth, {
arg out, hz=220, amp=0.5, amplag=0.02, hzlag=0.01;
var amp_, hz_;
amp_ = Lag.ar(K2A.ar(amp), amplag);
hz_ = Lag.ar(K2A.ar(hz), hzlag);
Out.ar(out, (SinOsc.ar(hz_) * amp_).dup);
}).add;
context.server.sync;
synth = Synth.new(\mySynth, [\out, context.out_b], context.xg);
this.addCommand("hz", "f", { arg msg;
synth.set(\hz, msg[1]);
});
this.addCommand("amp", "f", { arg msg;
synth.set(\amp, msg[1]);
});
this.addCommand("startStop", "f", { arg msg;
if (msg[1] == 0, {
synth.free;
}, {
if (synth.notNil, {
synth = Synth.new(\mySynth, [\out, context.out_b], context.xg);
});
});
});
basically, .play is a shortcut for defining and allocating a synth in one line. instead, you separate the definition and allocation into their own lines (SynthDef(...) and Synth.new(...)) so you can call Synth.new wherever you’d like