Pbind + Randomise PLayBuf array of Buffers

Sorry it’s really simple but I don’t find the good syntax, I m feeding 100 buffers with a folder containing 100 wav files and then I would simply like to randomly play between thoses but my method doesn’t work as it’s not possible to seq the idx param… Thanks

(
~bells = Array.new;

~folder = PathName("mypath");

~folder.entries.do({ | path |
	
	~bells = ~bells.add(Buffer.read(s, path.fullPath));
	
});

)

~bells.size.postln;


(
SynthDef(\RDM, { | idx |
	
	var sig = PlayBuf.ar(2, ~bells[ idx ]);  // not right
	
	Out.ar(0, sig);	
		
}).add;
)


(

Pbindef(\ZZZ,
	
	\instrument, \RDM,
	
	\idx, Prand( " all the buffers" , inf),
	
	\dur, 1
	
).play;

)



Like this…

~bells = PathName("mypath")
.enteries
.collect{ |p| Buffer.read(s, p.fullPath) };

SynthDef(\RDM, { 
	var sig = PlayBuf.ar(2, \buf.ir);
	Out.ar(0, sig);	
}).add;

Pbindef(\ZZZ,
	\instrument, \RDM,
	\buf, Prand(~bells, inf),
	\dur, 1	
).play;

The issue is indexing into a language side array on the server. You need to send the buffer number when the synth is made. This is done by keeping the array of buffers on the language side, i.e., in the pattern.

… I’m on mobile and haven’t tested this code, should work though. Also, that array thing is just a collect function.

1 Like

Thank you Jordan it’s clear and now it works. Also, I would like to match the Pbind \dur arg to the duration of the wavefile which is currently playing to avoid overlap between them, any idea ? Thanks

\dur, Pkey(\buf).duration 

Does that work? It should look up the value of the \buf key and then get it’s duration…

I thought also about it but no

ERROR: Message 'duration' not understood.
RECEIVER:
Instance of Pkey {    (0x11f6652b8, gc=80, fmt=00, flg=00, set=02)
  instance variables [2]
    key : Symbol 'buf'
    repeats : nil
}
ARGS:

PROTECTED CALL STACK:
	Meta_MethodError:new	0x117af2100
		arg this = DoesNotUnderstandError
		arg what = nil
		arg receiver = a Pkey
	Meta_DoesNotUnderstandError:new	0x117af4440
		arg this = DoesNotUnderstandError
		arg receiver = a Pkey
		arg selector = duration
		arg args = [  ]
	Object:doesNotUnderstand	0x116a92f40
		arg this = a Pkey
		arg selector = duration
		arg args = nil
	a FunctionDef	0x11f5e85c8
		sourceCode = "(

Pbindef(\\ZZZ,
	
	\\instrument, \\RDM,
	
	
	
	\\buf, Pstutter(4, Prand(~bells , inf)),
	
	\\dur, Pkey(\\buf).duration,
	
).play;

) "
	Interpreter:interpretPrintCmdLine	0x117185200
		arg this = an Interpreter
		var res = nil
		var func = a Function
		var code = (

Pbindef(\ZZZ,
	
	\instrument, \RDM,
	
	
	
	\buf, Pstutter(4, Prand(~bells , inf)),
	
	\dur, Pkey(\buf).duration,
	
).play;

)
		var doc = nil
		var ideClass = ScIDE
	Process:interpretPrintCmdLine	0x1170c9c00
		arg this = a Main

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>
	Object:throw
		arg this = <instance of DoesNotUnderstandError>
	Object:doesNotUnderstand
		arg this = <instance of Pkey>
		arg selector = 'duration'
		arg args = [*0]
	< closed FunctionDef >  (no arguments or variables)
	Interpreter:interpretPrintCmdLine
		arg this = <instance of Interpreter>
		var res = nil
		var func = <instance of Function>
		var code = "(

Pbindef(\ZZZ,
	
	\instrum..."
		var doc = nil
		var ideClass = <instance of Meta_ScIDE>
	Process:interpretPrintCmdLine
		arg this = <instance of Main>
^^ ERROR: Message 'duration' not understood.
RECEIVER: a Pkey

Pbindef(\zzz,
	\instrument, \RDM,
	\index, Prand( (0..~bells.size-1), inf ),
	\dur, Pfunc{|ev| ~bells[ev.index].duration },
	\buf, Pfunc{|ev| ~bells[ev.index] }
).play

Oft! Its nastier than it should be! If any one else has a nicer way, please chime in!

Essentially, you store the index in the event, then access inside each Pfunc, this will unwrap the index to its actual value. Pfunc, takes (implicitly) the pattern’s event as its first argument.

That’s great, thanks. And to finish, what is the way to allow the repetition of the buf index (with Prand or equivalent) only if all the other buffers of ~bells played before ?

Pxrand

(I’m telling you I’m on my phone now to explain why this msg is so harshly short and also to get enough characters in so this is accepted as an answer, cheers!)