@jamshark70 I finally had a chance to test this out, and it’s not working as expected. Actually, I’m getting better results with the real application, but I don’t know why. To explain the difference, bela.io has a UI in a web browser where I can copy in the code below. But my app uses OSC and only defines the synths in sclang. So maybe there’s a difference there, but in any case it doesn’t work in the web UI, so I want to understand why.
I did make some changes, so I’ll start there. I still didn’t understand the /trig stuff so if my alternate version is ok, then perhaps we can start with that, since I do understand that method. If your version is critical, then I need to ask more questions, so let me know.
My version turns sends a bus as an input called trigBus, which replaces. So again, if that’s the issue, we can discuss that part. But also I want the value of the trigger to be change the volume based on it’s 0-1 value. I didn’t see how your code took that into account. But I’m obviously no expert.
The other change I made was to the startPos, which you had a TRand. I could not understand why this would be important so I made it start at zero each time, which is what I want.
Also for testing I created an autoTrig synthDef which sends random values to simulate what the digital inputs do. And that does reproduce the issue as well, but I think it’s not working the same. The first image below is from autoTrig, and the second using the digital ins, which also send a 0.003 pulse.
Also, the bela scope seems to show some strangeness when I added the output signal but. I’m hoping it’s accurate for this test. Or perhaps you have some explanation.
Here’s the code I’m using now, hoping you are somebody can help as I do feel I’m getting closer. Meanwhile I will try the supercollider UI directly, as I just thought about that tshooting step.
s = Server.default;
s.options.numAnalogInChannels = 8;
s.options.numAnalogOutChannels = 2;
s.options.numDigitalChannels = 16;
s.options.belaMaxScopeChannels = 3;
s.options.numMultiplexChannels = 0; // do not enable multiplexer channels
s.options.blockSize = 128;
s.waitForBoot({
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.001);
// 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)).bitAnd(devMask);
// scale button between 0 and 1 based on velMask
var button = velocity / velMask;
var signal = Trig.ar( Select.ar(abs(device - deviceId) < 0.1, [K2A.ar(0), delay_allId * button]), 0.05);
Out.ar(ctlBus, signal);
Out.kr(ctlBus, signal);
button.belaScope(2);
SendTrig.ar(HPZ1.ar(delay_allId), 20, allId);
// SendTrig.ar(HPZ1.ar(delay_allId), 21, deviceId);
}).send(s);
SynthDef("sh1", {
});
SynthDef("sh",{
arg buf=0, gate = 1, scale = 1.0, trigBus = -1, wetBus, vol = 0.6, loop = 0, t_reset = 0;
var main_eg = EnvGen.kr(Env.asr(0.01, 1, 0.1), gate, doneAction: 2);
var trigVal = In.ar(trigBus);
// var trigVariableToProvideAccessToTheSynthControlInput = NamedControl.tr(\trigNameOfTheSynthControlInput, 0);
// var toggle = ToggleFF.kr(trigVariableToProvideAccessToTheSynthControlInput);
var toggle = ToggleFF.kr(trigVal > 0.01);
var latch = SetResetFF.ar(trigVal, t_reset) ;
var gates = [toggle, toggle <= 0];
var egs = EnvGen.kr(Env.asr(0.01, 1, 0.01), gates);
var players = PlayBuf.ar(1, buf, BufRateScale.kr(buf),
gates,
startPos: 0 /*TRand.kr(
0,
BufFrames.kr(buf) * 0.5,
gates
)*/
,
loop: loop * latch
);
var signal = ((players * egs).sum * (main_eg * vol * ((1.0 - scale) + (trigVal*scale)) )).dup;
Out.ar(wetBus, signal);
//signal.belaScope(1);
trigVal.belaScope(0);
}).send(s);
SynthDef("autoTrig",
{
arg trigBus;
var trigger = Impulse.ar(1);
var randTrigger = Trig.ar(TRand.ar(0.3, 0.8, trigger) * trigger, 0.003);
randTrigger.belaScope(0);
Out.ar(trigBus, randTrigger);
}).send(s);
SynthDef("scope2", {
arg ctlBus;
In.ar(ctlBus).belaScope(2);
}).send(s);
s.sync;
b = Bus.audio(s, 1);
c = Bus.audio(s, 1);
Synth.new("scope2", [\ctlBus, b]);
//k = Buffer.read(s, "/home/paul/src/HarmProject/data/samples/Ride.wav");
//j = Buffer.read(s, "/home/paul/src/HarmProject/data/samples/Kick2.wav");
k = Buffer.read(s, "/root/src/HarmProject/data/samples/Ride.wav");
j = Buffer.read(s, "/root/src/HarmProject/data/samples/Kick2.wav");
//k = Buffer.read(s, "/root/src/HarmProject/data/samples/Snare.wav");
//Synth.new("simpleTrig", [\buf: k]);
Synth.new("sh", [\buf: k, \trigBus: b]);
Synth.new("sh", [\buf: j, \trigBus: c]);
Synth.new("trigDetector", [\deviceId, 0.0, \ctlBus, b]);
Synth.new("trigDetector", [\deviceId, 1.0, \ctlBus, c]);
Synth.new("autoTrig", [\trigBus: b]);
// o = OSCFunc({
// arg msg, time;
// //[time, msg].postln;
// if(msg[2] == -1 && msg[3] == 10, { ~sendFootTrigger.value(msg[5], msg[4] ) });
// },'/trg');
o = OSCFunc({
arg msg, time;
[time, msg].postln;
},'/tr');
k.postln;
j.postln;
"Ready ".postln;
s.sync;
});
"Waiting for quit ".postln;
ServerQuit.add({ 0.exit }); // quit if the button is pressed