I’m trying to find a reliable way to detect when a Phasor.ar wraps from 1 back to 0. Essentially, I want to track the moment when it resets after reaching its end value.
Ideally, I’d get a 1 or a trigger-like signal exactly at that wrap-around moment, and 0 otherwise.
With this trigger i want to set a variable to a defined value.
What would be the best approach here?
hey, to derive a trigger from a linear ramp between 0 and 1 you can calculate the absolute Delta (current sample - last sample).abs and compare that with a threshold of 0.5. This will work for upward and downward ramps. But with this setup you wont get an initial trigger and when you manually reset your phasor in the first half of the Duty cycle you also wouldnt get a trigger, because the Delta isnt big enough. A more reliable trigger detection is based on calculating the Delta and the Sum and get the proportional Change, If the Change was big you get a trigger. Check out the rampToTrig function in my Collection of functions for sub-sample accurate Granulation here: A collection of functions for sub-sample accurate granulation
If you are just interested in when the phase is a certain value you can also directly query it and derive a trigger from it:
(
(
{
var phaseDur = 1 * s.sampleRate;
var phase = Phasor.ar(Impulse.ar(0), 1, 0, phaseDur);
var trig = phase |==| 0;
// var trig = phase |==| (phaseDur - 1);
var env = Env.perc().ar(2, trig);
var sig = LFSaw.ar(110) * 0.1;
sig = sig * env
}.play
)
Now, if you need to derive intersample triggers, that’s where it get’s more complex and this is the stuff which @dietcv has been working on mainly (to my understanding).