How to get computed Event values in a Pbind?

Hi !

I’m sure I knew how to do it in the past but I forgot and I couldn’t find it on the documentation, even though it must be somewhere :slight_smile:

I am building a multisampler with sample zones and I want to play the sample which rate is the closest to the played frequency.

I tried that :

// let's say I have 8 mono buffers
~buffers = 8.collect { Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav") };

// a mono buffer player synthdef
(
SynthDef(\monoPlayer, { |out = 0, bufnum = 0, rate = 1.0, amp = 1, pan = 0|
	var sig;
	var playRate;
	playRate = BufRateScale.kr(bufnum) * rate;
	sig = PlayBuf.ar(1, bufnum, playRate, doneAction: done.freeSelf);
	sig = Pan2.ar(sig, pan);
	sig = sig * amp;
	Out.ar(out, sig);
}).add;
)

// we'll return a random buffer for now
~getTheRightBuffer = { |freq|
	freq.value.debug("freq");
	~buffers.choose;
};

// test
~getTheRightBuffer.value(440); // prints 440 and returns a buffer, good

Pbind(*[
	instrument: \monoPlayer,
	degree: Pwhite(0, 8, inf),
	bufnum: Pfunc({ |e| ~getTheRightBuffer.value(e[\freq]) }),
	dur: 1
]).play;

It fails

-> an EventStreamPlayer
ERROR: Message '+' not understood.
RECEIVER:
   nil
ARGS:
   nil

PROTECTED CALL STACK:
	Meta_MethodError:new	000001AD8C1EE280
		arg this = DoesNotUnderstandError
		arg what = nil
		arg receiver = nil
	Meta_DoesNotUnderstandError:new	000001AD8C1F0240
		arg this = DoesNotUnderstandError
		arg receiver = nil
		arg selector = +
		arg args = [ nil ]
	Object:doesNotUnderstand	000001AD8BCA0000
		arg this = nil
		arg selector = +
		arg args = nil
	a FunctionDef	000001AD8D270480
		sourceCode = "#{
					(~midinote.value + ~ctranspose).midicps * ~harmonic;
				}"
	a FunctionDef	000001AD8FEF68F8
		sourceCode = "{ |freq|
	freq.value.debug(\"freq\");
	~buffers.choose;
}"
		arg freq = a Function
	a FunctionDef	000001AD8D231F40
		sourceCode = "<an open Function>"
	Function:prTry	000001AD8C4A0800
		arg this = a Function
		var result = nil
		var thread = a Routine
		var next = nil
		var wasInProtectedFunc = false
	
CALL STACK:
	DoesNotUnderstandError:reportError
		arg this = <instance of DoesNotUnderstandError>
	Nil:handleError
		arg this = nil
		arg error = <instance of DoesNotUnderstandError>
	Thread:handleError
		arg this = <instance of Thread>
		arg error = <instance of DoesNotUnderstandError>
	Thread:handleError
		arg this = <instance of Routine>
		arg error = <instance of DoesNotUnderstandError>
	Thread:handleError
		arg this = <instance of Routine>
		arg error = <instance of DoesNotUnderstandError>
	Object:throw
		arg this = <instance of DoesNotUnderstandError>
	Function:protect
		arg this = <instance of Function>
		arg handler = <instance of Function>
		var result = <instance of DoesNotUnderstandError>
	Environment:use
		arg this = <instance of Environment>
		arg function = <instance of Function>
		var result = nil
		var saveEnvir = <instance of Environment>
	< FunctionDef in Method Pbind:embedInStream >
		arg i = 4
		var name = 'bufnum'
		var stream = <instance of FuncStream>
		var streamout = nil
	Integer:forBy
		arg this = 0
		arg endval = 7
		arg stepval = 2
		arg function = <instance of Function>
		var i = 4
		var j = 2
	Pbind:embedInStream
		arg this = <instance of Pbind>
		arg inevent = <instance of Event>
		var event = <instance of Event>
		var sawNil = false
		var streampairs = [*8]
		var endval = 7
	Routine:prStart
		arg this = <instance of Routine>
		arg inval = <instance of Event>
^^ The preceding error dump is for ERROR: Message '+' not understood.
RECEIVER: nil

Thanks for your help !

Geoffroy

E.g.

  Pbind(*[
    	degree: Pwhite(0, 8, inf),
    	bufnum: Pfunc { |e| e.use { ~freq.value.postln } },
    	dur: 1
    ]).play;
1 Like

Thank you so much Daniel ! I always forget an Event is also an Environment !