Ptpar and Pdefn

Hello all!
I have some Pbinds, that are arranged within a Ptpar:
some of the Pbinds have a Pdefn for e.g. \dur.
When I run the Pbind outside the Ptpar, everything works as expected: I can change the related key pair by calling the related Pdefn with a different setting than the original from outside.
When I have e.g. a Pbind used 4 times in a Ptpar, and only for the last time I want to change the Pdefn, the Pbind is changed from its first call, no matter if I explicitly set the Pdefn in the Ptpar 3 times with the original setting and only for the last time with the altered one.
What do I have to do, to have the Pdefn behave the same in the Ptpar as with only the Pbind playing solo?

Thanks for any suggestions!
best
Rainer

If I understand your meaning, then… Pdefn doesn’t work that way, I’m afraid. It’s not analogous to a variable, but rather like a lazy reference.

TL;DR If you need the Pdefn values to be unique, then they need to have different names.

That’s a bit abstract… if you write like this:

(
var a;

p = Ppar([
	a = 440; Pbind(\freq, a, \dur, Pwhite(0.2, 1.2, inf)),
	a = 550; Pbind(\freq, a, \dur, Pwhite(0.2, 1.2, inf)),
	a = 660; Pbind(\freq, a, \dur, Pwhite(0.2, 1.2, inf))
]).play;
)

p.stop;

… you get three pitches. (This is valid syntax – each line in the array is an expression sequence, whose result is the result of the last expression, i.e., the Pbind.) All three Pbinds refer to \freq, a, and – here’s the important point – the variable a is resolved immediately when constructing each Pbind object. For the first Pbind, a holds 440; when the variable is evaluated, only its value goes through, so the first Pbind gets \freq, 440, the second \freq, 550 etc.

But if you write it like this:

(
p = Ppar([
	Pbind(\freq, Pdefn(\a, 440), \dur, Pwhite(0.2, 1.2, inf)),
	Pbind(\freq, Pdefn(\a, 550), \dur, Pwhite(0.2, 1.2, inf)),
	Pbind(\freq, Pdefn(\a, 660), \dur, Pwhite(0.2, 1.2, inf))
]).play;
)

p.stop;

The meaning is different. Pdefn(\a) is not a variable to resolve to its value right now. It’s a reference to a stream of values. All 3 Pbinds refer to the same reference to the same stream. There is no way to lock the first Pbind to Pdefn’s stream at the moment of building the Pbind object.

Shorter:

Pdefn(\z, 1);
z = Pdefn(\z).asStream;
Pdefn(\z, 2);
y = Pdefn(\z).asStream;
[z.next, y.next]

-> [2, 2]  // and NOT [1, 2]

hjh