Find /u_cmd synthIndex automatically?

Hi!

I’m working on a UGen for interpreting bytebeat expressions on the fly. I’m using a UnitCmd to send strings containing expressions to the UGen for parsing.

On the sclang side, the interface looks like this:

(
SynthDef.new(\bytebeat, {
    var t = PulseCount.ar(Impulse.ar(8000));
    Out.ar(0, ByteBeat.ar(t).dup)
}).add;
)

b = ByteBeatController(Synth.new(\bytebeat), 2);
b.eval("((t<<1)^((t<<1)+(t>>7)&t>>12))|t>>(4-(1^7&(t>>19)))|t>>7");

The second parameter of ByteBeatController is the index of the ByteBeat UGen in the Synth. The controller uses this to send the UnitCmd to the UGen: synth.server.sendMsg('/u_cmd', synth.nodeID, synthIndex, cmd, *args).

I’d like to avoid having to manually provide the index. Is it possible to determine it automatically from the Synth instance?

Thanks!

Have a look at VSTPlugin.sc, specifically the optimiozeGraph and synthIndex_ method. sc/classes/VSTPlugin.sc · develop · Pure Data libraries / vstplugin · GitLab

The basic idea is to store the index in the SynthDef metadata, so it can also survive serialization from/to disk (needed for SynthDef.store!). Also, VSTPlugin.ar has an (optional) id parameter, in case you want to have more than one instance in the same SynthDef. VSTPluginController can use this ID to look up the index of a specific VSTPlugin instance. If no ID is given, it simple takes the first (and only) entry.

Hope that helps. Feel free to ask if you have any more questions.

1 Like