I want to synchronize the phase of the BufRd with a hanning window envelope:

```
(
SynthDef.new(\dline, {
arg buf, trans=0;
var dl, env, phasor;
phasor = Phasor.ar(0, BufRateScale.ir(buf)*trans.midiratio, 0, BufFrames.ir(buf));
env = EnvGen.ar(Env.sine, phasor, timeScale: BufDur.ir(buf)).poll;
dl = BufRd.ar(
2,
buf,
phasor,
0,
4);
dl = dl * env;
Out.ar(0, dl);
}).add;
)
```

The Phasor is supposed to jump to 0 and then increase, which should trigger the envelope, right? Why is this not the case? Is there a better way to implement this synchronization?

It isn’t guaranteed to go back exactly to 0, I guess.

A safer way is based on the fact that the Phasor’s slope is positive except for the one sample where it jumps back down. HPZ1 will give you a value proportional to the momentary slope – this will be mostly positive by a small value, then have a large negative spike at the reset point.

Negate this and you’ll have a mostly slightly negative signal with an occasional large positive spike = good trigger.

```
env = EnvGen.ar(Env.sine, HPZ1.ar(phasor).neg, timeScale: BufDur.ir(buf)).poll;
```

Untested but I think it will work.

hjh

1 Like

Thanks, it works! Although without the neg method.

Without `neg`

, yes, you do get a trigger, because HPZ1 is negative for one sample and then goes back to a positive value.

But this is late by one sample.

```
(
{
var phase = Phasor.ar(0, 440 * SampleDur.ir, 0, 1, 0);
var derivative = HPZ1.ar(phase);
var negated = derivative.neg;
var envs = EnvGen.ar(Env.perc(0.0001, 0.001), [derivative, negated]);
[phase, derivative, envs[0], negated, envs[1]]
}.plot(duration: 0.005);
)
```

I’m zooming way, way in on this plot, and I added a green line to show the moment when Phasor resets.

The second and third rows are based on HPZ1 as a trigger. The envelope gets triggered, but slightly after the green line.

The fourth and fifth negate the HPZ1 trigger. Now the positive trigger occurs exactly at the moment of reset, and so does the envelope.

One sample delay might not make that much of a difference – you’re free to ignore it if it’s OK for your use case.

hjh