Triggering envelope with phasor

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

(\dline, {
	arg buf, trans=0;
	var dl, env, phasor;
	phasor =,*trans.midiratio, 0,;
	env =, phasor, timeScale:;
	dl =
	dl = dl * env;, dl);

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 =,, timeScale:;

Untested but I think it will work.


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 =, 440 *, 0, 1, 0);
	var derivative =;
	var negated = derivative.neg;
	var envs =, 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.