Phasor with controllable looping? [Solved]

Hey all! I’m writing a SynthDef to act as a “global transport” phasor for reading into buffers and I’m looking to achieve the following behavior: the ability to manually trigger it to jump back to 0 and start playing, as well as to choose whether it loops when it reaches the end or not. So far I have this:

SynthDef(\transportPhasor, { |dur = 1, loop = 1, t_manualTrig = 0|
	var loopingTrig = Impulse.kr(1/dur) * loop;
	Out.ar(~transport, Sweep.ar(loopingTrig + t_manualTrig, 1/dur).clip(0, 1));
}).add;

The problem with this is that I can’t control the phase difference between the Impulse UGen and my manual triggers. For example, say that loop is set to 0 and the phasor is stopped at 1, if I reset it by using the manual trigger and then set loop to 1 so that it keeps on looping, I can’t guarantee that the next trigger from Impulse won’t happen before the phasor finishes its cycle, thus prematurely resetting it. Is there a way to reset the looping trigger’s “counter” with every manual trigger? Thanks!

I think what you want is the Phasor Ugen? Here’s an example using it to drive frequency instead of buffer position, but it should make it clear what it’s doing. When you’re driving a buffer w/ a phasor, make sure you set your rate argument to BufRateScale.kr(bufnum), to ensure the buffer is playing back at the right rate.

Ndef(\phasorTest, {
	var sig, phase, resetTrig, resetPos;
	
	resetTrig = MouseButton.kr(lag:0);
	resetPos = TRand.kr(60, 600, resetTrig);
	
	phase = Phasor.ar(
		trig: resetTrig, 
		rate: 1/1000, 
		start: 60, 
		end: 600, 
		resetPos: resetPos
	);
	
	phase.poll(1, "phase");
	resetPos.poll(resetTrig, "resetPos");
	
	sig = SinOsc.ar(phase);
	
}).play

You can prevent looping by setting end: inf or a very large number - and enable looping again by setting it back to a reasonable value.

Also - you can do code syntax highlighting on multiple lines if you start and end them with three backticks (```)

Thanks, that’s exactly what I was looking for; I didn’t know you could set the end point to infinity!

I’m clipping the phasor between 0 and 1 and multiplying it with each buffer’s BufFrames.kr, as some buffers have different sampling rates yet same duration. Would using BufFrames.ir instead of .kr have any noticeable performance benefits if I’m using a lot of buffers (say >50 or >100)? Does the .ir version only calculate its value at the instantiation of the synth, therefore not updating when a new bufnum argument is supplied?

The performance impact of BufFrames.kr is not worth worrying about if you’re using it for e.g. controlling a Phasor. The .ir variation is set only once at synth creation time, and doesn’t update if you change your buffer. Even if you’re not planning on e.g. swapping buffers at the moment, using .kr prevents bugs from sneaking up on you in the future.