Replacing a long-running synth from an Event

It’s not hard to discover how to replace a running Synth using the object interface, e.g.

y = Synth(\default);

(SynthDef(\beep2, {
	var out = \, amp = \, freq = \;, amp *;

y = Synth.replace(y, \beep2, [\amp, 0.1], sameID: true);

If you’re using Event type set in combo with long-running synths (which is bit more flexible than Pmono, when it comes to combining with other events, e.g. using the new type \composite), you may sometime want to replace a long-running synth definition entirely.

It turns out it’s possible to do this using event type on with addAction:4, which does node replacement (it’s basically how Synth.replace is implemented, i.e. calls into with that addAction: \addReplace), albeit it’s a little tricky in terms of passing the arguments in events:

(type: \on, instrument: \default, id: y.nodeID, addAction: 4).play
// FAILURE IN SERVER /s_new duplicate node ID

In an Event \group is the target-equivalent argument of Synth.replace, even when you are doing synth replacements.

// here ~nid gets the new auto-generated id,
// i.e. this is like Synth.replace(sameID: false)
~nid = (type: \on, instrument: \default, group: y.nodeID, addAction: 4)\id)

// could use ~nid to instantiate a new Node with basicNew
// if also need use the object interface
y = Node.basicNew(s, ~nid)

More simply though, keeping the same node id, one needs to specify both group and id and making them the same:

(type: \on, instrument: \beep2, group: y.nodeID, id: y.nodeID, addAction: 4).play

A bit of an aside, \instrument can be function in Events, which gets evaluated asDefName, i.e. sent to server as a synth, so you even can do something like:

((type: \on, instrument: {, 0.1 *!2)) },
 group: y.nodeID, id: y.nodeID, addAction: 4).play

A word of warning with this approach: if you accidentally leave ~group and ~id nil in your event (and still use addAction: 4), you’ll replace your default group… because in Event.default ~group is a function that by default returns 1…

Luckily, Cmd+. [Ctrl+. on Windows] recreates group 1, no need to reboot the server.