I’m still trying to get a grasp on sclang, especially when it comes to conditionals. My SynthDef takes a ctlbus and deviceId as args. It reads a bunch of digital IOs from a Bela mini and uses most of the bits for velocity and 1 bit to determine which device sent it. I’m able to send the velocity part on the control bus, but I don’t know how to integrate the device ID. The goal is that if the device digital IO matches the deviceID argument, then it should set the bus signal.
You can see I’m trying to use if statements, but I’m pretty sure these are only getting used during the initialization of the SynthDef. I tried using |==| as I saw in another post, but that fails with an error. This is sclang 3.12.1, though it’s running on the Bela. I’m guessing there’s a uGen for this purpose, but I’m not sure how to find it. Help?
var digitalPins = [ 0, 1, 2, 3, 4, 5, 6, 7];
var triggerCount = 1;
SynthDef(\trigDetector, {
arg ctlBus, deviceId;
var all = Array.fill(digitalPins.size, { |i|
DigitalIn.ar(digitalPins[i]);
});
// Convert all the bit array to a binary value
var allId = all.reverse.reduce({ arg total, bit; total * 2 + bit }, 0);
// Make sure they are set by delaying a little bit
var delay_allId = TDelay.ar(in: allId, dur: 0.003);
// Create bitmasks.
// velMask is how many bit used for velocity
// devMask used for devices, of which there are only 2
var velMask = pow(2, digitalPins.size - triggerCount) -1;
var devMask = pow(2, triggerCount) - 1;
// Use the masks to get the velocity and device that was activated
var velocity = allId.bitAnd(velMask);
var device = (allId >> (digitalPins.size - triggerCount)) & devMask;
// Various attempts to understand how this works. But the goal is to Out.ar(ctrlbus, velocity)
// if the deviceId matches the device that is derived from the digtial in.
// Determine which device is set, left=0 or right foot=1
var devtest = (device == deviceId);
// scale button between 0 and 1 based on velMask
var button = velocity / velMask;
var signal = Trig.ar( delay_allId * button, 0.4);
// if(device == deviceId,
// { SendTrig.ar(HPZ1.ar(delay_allId), 23, 123); }
// , {SendTrig.ar(HPZ1.ar(delay_allId), 23, 321); }
// );
if(device == deviceId,
{ ("device="+device+" id="+deviceId).postln; }
, { ("f device="+device+" id="+deviceId).postln; }
);
if(device == deviceId,
{ Out.ar(ctlBus, signal); Out.kr(ctlBus, signal); }
,{ Out.ar(ctlBus, signal); Out.kr(ctlBus, signal); }
);
}).send(s);