Accessing an Array of Arrays in a Pbindef

If I had an array of arrays, something like:

~arrays  = Array.fill(4, {Array.fill(10, {[0,4, 6, 8, 4, 5, 7, 2].choose})});

And I wanted to randomly play from it in a Pbind, every time an array repeated 3 times… How would one do that?
The following code does not seem to do it…

		
Pbindef(\fm,
\dur, 1/8,
\scale, Scale.mixolydian,
\degree, Pfunc({Pseq(~arrays[4.rand], inf)})).play;

I personally find that Pthings (patterns) usage takes some time to understand and feel… First, patterns can be nested. Recently, I figured out that most of the time, you do not create your arrays yourself, but instead take advantage of patterns types to do what you need.

Simple example

var array = [ 1, 1, 1, 1 ];
Pseq( array, inf );

is equivalent to

Pseq(
    Pseq( 1, 4 ),
    inf
);

Most of the time, it ends up being easier to use patterns and nesting, than “array construction”, at least for me. I feel like patterns were designed to be used with nesting, not with “classical programming”.

In your case, I think that the problem’s equivalent in pattern/nesting style is :

\degree, Prand( [
    Pseq( ~arrays[0], 1 ),
    Pseq( ~arrays[1], 1 ),
    ...
    Pseq( ~arrays[n], 1 )
    ], inf )
).play;

EDIT : to accommodate to the 3 times repeat, you’ll change the Pseq repeat :

\degree, Prand( [
    Pseq( ~arrays[0], 3 ),
    Pseq( ~arrays[1], 3 ),
    ...
    Pseq( ~arrays[n], 3 )
    ], inf )
).play;
2 Likes

If I understand your problem correctly, I think you could do it this way:

~arrays  = Array.fill(4, {Array.fill(10, {[0,4, 6, 8, 4, 5, 7, 2].choose})});

(
Pbindef(\fm,
	\dur, 1/2,
	\scale, Scale.mixolydian,
	\degree, Pn(Plazy({Pseq(~arrays[4.rand], 3)}), inf)
).play;
)
2 Likes

This is slightly more efficient than the Plazy approach (by reusing, rather than rebuilding, Pseq objects) :+1: – but could be written more compactly:

\degree, Prand(~arrays.collect { |row| Pseq(row, 3) }, inf )

That’s assuming that the contents of ~arrays won’t change. If it does change, then, back to Plazy.

hjh

1 Like