Pfunc event order in Pbindef

How to set Pfunc order in Pbindef ?

I set \pan ans random switch Chanel , and want to send OSC after \pan changed.
It seems sometimes sending osc before \pan value changed .

s.boot;
s.meter;

~oscOut = NetAddr.new(“127.0.0.1”, 1234 );

//1 synth definition____________________________________________________/////////////////////////////////////////////

//sin pi
(
SynthDef(“sin_atac”, { | amp=0.5 , freq=400 , pan=1 |
var sig, env , ch ,val;

// パーカッション的な音の減衰を作る
env = EnvGen.kr(Env.perc(0, 0.11), doneAction: 2);

sig = SinOsc.ar(freq);

sig = sig*env;

sig = sig * amp ;
sig = Pan2.ar(sig, pan); // 左右の音の位置を決める

Out.ar(0, sig );

}).add;
);

Synth(“sin_atac”, [amp:0.5 , freq:1000 , pan: 1 ] );

//2 pattern____________________________________________________/////////////////////////////////////////////
(
Pbindef(\pdef_test1,
\instrument, “sin_atac”,
\dur, 1.0 ,
\freq, 1000 ,
\amp, 0.5,
\pan, { rand(2)*2-1 } ,
\bla , Pfunc( {|e| ~oscOut.sendMsg("/triger1", e.pan , e.freq ); } )

    );

)

//play
Pdef(\pdef_test1).play;
Pdef(\pdef_test1).stop;

Same question as https://scsynth.org/t/sendosc-with-pbind, right?

I had given you a suggestion here but there was no response.

(One side note: please use backticks to enclose the entire code example – I had to reformat some of your code in order to run any of it.)

```
the correct way to embed code into a post
```

Anyway – checking the pairs as I had suggested:

(
Pbindef(\pdef_test1,
	\instrument, "sin_atac",
	\dur, 1.0,
	\freq, 1000,
	\amp, 0.5,
	\pan, { rand(2)*2-1 },
	\bla, Pfunc( {|e| ~oscOut.sendMsg("/triger1", e.pan, e.freq ); } )
);

Pbindef(\pdef_test1).source.pairs
)

-> [ instrument, a PatternProxy, dur, a PatternProxy, freq, a PatternProxy, amp, a PatternProxy, pan, a PatternProxy, bla, a PatternProxy ]

So the order of the keys is, in fact, instrument, dur, freq, amp, pan, blabla definitely comes after pan – so, key order is not the problem.

There’s a new element in this code example, that wasn’t there in the old thread: \pan, { rand(2)*2-1 },.

TL;DR if you correct this to read \pan, Pfunc { rand(2)*2-1 }, then it should work.

  • \pan, Pfunc { rand(2)*2-1 }, means: While evaluating Pbind, run the function and set \pan to the value returned by the function.

  • \pan, { rand(2)*2-1 }, means: set \pan to the function object itself, not a concrete value. Then \bla evaluates, based on \pan = a Function. sendMsg tries to send the objects as given – but there’s no OSC representation of a Function, so nothing goes across:

    OSCdef(\x, { |msg| msg.postln }, '/test');
    
    NetAddr.localAddr.sendMsg(\test, \pan, { 1.0.rand2 });
    
    // result
    [ /test, pan ] -- **no value for pan!!**
    

    After that, the event-play logic does include instructions to resolve functions to their numeric values – but it’s too late, \bla has already run, and the other side already got the malformed OSC message.

So the take-away here is to be careful that all of the Pbind pairs are really patterns (or hardcoded values). Pfunc {} is a pattern and is valid. {} is not a pattern, and is not valid in this context.

hjh

Thank you detail .
Ok , enclose the entire code example!! , sorry .
Now i understood what is “pairs”

\pan, Pfunc { rand(2)*2-1 }
I wanted to change pan by random.
{} is function , that why data is seemed not correct.

\pan, Prand([-1,1] , inf)
worked well.

Thank you for detail, and how to debug .
Thanks again.

Yes, that’s a valid solution too (and even more idiomatic to patterns).

hjh