Buffer: random start position

Hey everyone, I’m trying to create a SynthDef that’s played through Pbind to randomise the starting position of a buffer; however, when each Pbind event occurs I’ve noticed it’s not random at all, with buffers being played at the same position every few events. To debug, I polled the value of Pwhite being passed into the SynthDef via poll and I can see there are repeating values which confirms the behaviour. Yet when I view the output of Pwhite using asStream I can see that the values are entirely different, which leads me to believe there’s something wrong in how I implemented the SynthDef. Here’s my code:

(
SynthDef(\randomBufferPattern, {
	arg buf = 0, density = 1, out = 0, startPos = 0.0, duration = 1, rate = 1, atk = 0.1, rel = 1.0, amp = 0.5;
	var sig, env, bufFrames;
	
	// Normalise start position float
	bufFrames = BufFrames.ir(buf);
	startPos = startPos * bufFrames;

	startPos.poll(label: "startPos");

	env = EnvGen.kr(
		Env.linen(atk, duration - 0.01, rel, 1, -4), doneAction: 2);

	// Play the buffer with random start position and duration
	sig = PlayBuf.ar(2, buf, BufRateScale.kr(buf) * rate, startPos, loop: 0);

	// Apply high-pass filter
	sig = HPF.ar(sig, 350);

	// Apply envelope
	sig = sig * env;

	Out.ar(out, sig);
}).add;
)

(
var myClock = TempoClock.new;
myClock.tempo = 90/60;  // Set tempo to 90 BPM

z = ();
z.buffer = Buffer.read(s, b.paths[2]);
z.bufferPlayer = Pbind(
	\instrument, \randomBufferPattern,
	\buf, z.buffer,
	\density, Pwhite(0.5, 2, inf),
	\startPos, Pwhite(0.0, 0.9, inf),
	\duration, Pexprand(4, 8, inf),
	\rate, exprand(0.3, 0.6, inf),
	\atk, exprand(0.1, 0.5,inf),
	\rel, exprand(0.1, 3, inf),
	\dur, 1,
	\out, 0,
).play;
)

Thank you!

That’s a simple typo :smiley: !

You didn’t specify the startPos flag argument, so, currently, your startPos variable is passed as trigger:

This should be:
sig = PlayBuf.ar(2, buf, BufRateScale.kr(buf) * rate, startPos: startPos, loop: 0);
or
sig = PlayBuf.ar(2, buf, BufRateScale.kr(buf) * rate, 0, startPos, loop: 0);


As a side note, even if it does not impact your algorithm, I’d advocate using z[\buffer] and z[\bufferPlayer] instead of z.buffer and z.bufferplayer (see this post about this syntax distinction).

I don’t believe it, I feel I looked at everything but that! Thanks very much, you’ve alleviated a constant headache.