## Pswitch and Pswitch1

```                Wed, 07 Feb 2001 14:40:40 +0100

Alberto de Campo

Hi Mike,

"Michael P. Whyte" wrote:

> I'm having some difficulty understanding Pswitch.
> Could someone who understands these objects forward me some sample code?
> I do understand the first arg is an array of patterns, and the second is the index.
> What i am confused about is:  (for example)
>
> Pswitch([pattern1, pattern2, pattern3], Pseq([2,1,3],inf));
>
> Why would you do this when you could use a Pseq and then order
> the patterns as you see fit?

Right. That would do same thing.
Except it would maybe be easier to generate a more complex
sequence of indices than Pseq([2,1,3],inf) algorithmically.

> The only reason to use Pswitch is to switch to a different pattern on cue, as
> opposed to Pseq, which will wait until the first pattern is done before moving on to the
> next one....right?

Sort of. PSwitch still finishes each pattern, but it could switch whenever
a new pattern starts. This patch shows both options:

(
var melody1, melody2, melody3, indexPattern, indexByMouse, melodyPatterns, mousex;

melody1 = Pseq([48, 53, 55], 2);
melody2 = Pseq([60, 61, 63, 64, 65, 66, 67], 1);
melody3 = Pseq([75, 76, 77, 78, 79], 1);

melodyPatterns = [ melody1, melody2, melody3 ];

// repeat complete runs of [ melody1, melody3, melody2, melody3 ] 3 times.

indexPattern = Pseq([0, 2, 1, 2], 3);

// Or make an ExternalControlSource from mouse with 3 zones;
// in Pfunc, i.e. whenever a new pattern starts:
// poll the mouse position, and truncate it to integer (to make a legal index).

mousex = MouseX.new(0, 2.99);
indexByMouse = Pfunc({ mousex.value.asInteger });

// try changing indexPattern1 to indexByMouse.
Pbind(
\midinote, Pswitch(melodyPatterns, indexPattern1),
\dur, 0.13
).scope;
)

>
> So I guess what I'm asking is how to send the index to get it to switch to the next
> pattern in the array when I tell it to, regardless of the state of the currently playing
> pattern.

That's what Pswitch1 is for.
It generates a new index for _each_ event, not just
when a pattern ends. So for each event it jumps to the stream
at that index, and gets only one next value from there;
for the event after that, the index may be different again.

>
> Also, what's the difference between Pswitch and Pswitch1?  I've read the source code, but
> lots of good that does me..;-)  (i understand ~10% of it..somethin' about streams...)

In other words, Pswitch1 can criss-cross between several streams,
and these streams are left in their state between index changes;
they just wait for the next time they are asked "next".

(If one of them ends early, the whole Pswitch1 stream stops.)

Three options here:
(
var melody1, melody2, melody3, simpleIndexPattern, longIndexPattern, indexByMouse, mousex;

// all streams are infinite, and different lengths.
melody1 = Pseq([48, 53, 55], inf);
melody2 = Pseq([60, 61, 63, 64, 65, 66, 67], inf);
melody3 = Pseq([75, 76, 77, 78, 79], inf);

// simple: One event each from [ melody1, melody3, melody2, melody3 ],
//repeat 10x.
simpleIndexPattern = Pseq([0, 2, 1, 2], 10);

// longer index dance:
// get one event from each pattern, then two, three, and four.

longIndexPattern = Pseq(
[ 0, 1, 2,
0, 0, 1, 1, 2, 2,
0, 0, 0, 1, 1, 1, 2, 2, 2,
0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
],
3
);
// interactive: With Pswitch1, indexByMouse runs for each new event,
// i.e. even in mid-pattern.
mousex = MouseX.new(0, 2.99);
indexByMouse = Pfunc({ mousex.value.asInteger });

// try all three.
Pbind(
\midinote, Pswitch1([ melody1, melody2, melody3 ], simpleIndexPattern),
\dur, 0.13
).scope;
)

Hope this helps,
best,
adc
```

### Links to this Page

• using Patterns and Streams last edited on 20 February 2001 at 12:29 pm by cw05.DD1.srv.t-online.de
• enter. last edited on 29 January 2003 at 10:46 pm by 203.14.169.19