Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / HelpSource / Classes / Pdfsm.schelp
blob1f1ba087d5f1c2c1688397e339a97eb64e34760d
1 class:: Pdfsm
2 summary:: deterministic finite state machine
3 related:: Classes/Pfsm
4 categories:: Streams-Patterns-Events>Patterns>List>Indexing
6 description::
8 Pdfsm is a deterministic finite state machine with signal input (written by by ccos).
10 ClassMethods::
12 method::new
14 argument::list
15 a list consisting of the stream which gives input signals to determine state transitions, and then dictionary entries, one for each state, mapping the destinattion state and yield streams to those input signals.
17 argument::startState
18 an integer index for the state to start with.
20 argument::repeats
21 an integer giving the number of times the pattern should cycle. A cycle ends when the strong::signal stream:: ends or nil is given for the destination state to a signal value, see below.
23 Examples::
25 definitionList::
26 ## list structure:
27 || [
28 definitionList::
29 ## signal stream || can be a stream of anything which can serve as a key for an associative collection. integers, symbols, etc... asStream is called on this for each repeat.
30 ## states || states should be an instance of link::Classes/IdentityDictionary::, link::Classes/Event:: or some other associative collection.
32 ## ] ||
33 ## list syntax:
34 || [
35 definitionList::
36 ## signal stream, ||
37 ## ( // state 0, ||
38 definitionList::
39 ## signal value : [destination state, return stream or pattern], ||
40 ## signal value : [destination state, return stream or pattern] ||
42 ## ), ||
43 ## ... // state 1 ... N ||
45 ## ] ||
48 Any number of states can be given, and are indexed by the order in which they are given.
50 If the fsm is in state x and it receives a strong::signal value:: y it looks up y in the state dictionary supplied for x, if there is no y entry, it looks for a \default entry and uses that.
52 The next state is then set to strong::destination state::, and the stream yielded is given by strong::return stream or pattern::. That is unless the strong::destination state:: is given as nil, or if a strong::destination state:: is given for which you have not supplied a dictionary - in both cases the current cycle ends and any remaining repeats are executed. If there is no strong::signal value:: given for a particular signal, and no \default is supplied then one will get a runtime error.
54 code::
56         p = Pdfsm(
57                 [
58                         Pseq( [\foo,\bar], 2 ), // foobar signals
59                         ( // state 0
60                                 \foo : [ 1, Pseq([ 0, 1 ], 2 ) ]
61                         ),
62                         ( // state 1
63                                 \bar : [ 0, 3 ]
64                         )
65                 ],
66                 0,
67                 2
68         ).asStream;
70         11.do({ p.next.postln });
74 SynthDef(\help_Pdfsm1,
75         { arg out=0, freq=440, sustain=0.05;
76                 var env;
77                 env = EnvGen.kr(Env.perc(0.01, sustain, 0.2), doneAction:2);
78                 Out.ar(out, SinOsc.ar([freq, freq + 0.1.rand2], 0, env))
79         }).add;
83 var p;
84 p = Pdfsm(
85         [
86                 Prand([0,1,2],inf),     // signalStream
88                 IdentityDictionary[     // state 0
89                         0 -> [ 2, Pseq([67,68,69], 2) ],
90                         1 -> [ 0, 66 ],
91                         2 -> [ 1, 65 ]
92                 ],
93                 IdentityDictionary[     // state 1
94                         1 -> [ 1, Pseq([69,68,67],2) ],
95                         \default -> [ 0, 70 ]
96                 ],
97                 IdentityDictionary[
98                         0 -> [ 0, 71 ],
99                         1 -> [ 0, 72 ],
100                         2 -> [ nil ]    // signalStream is infinitely long,
101                                         // so the fsm only ends because of this nil
102                                         // 2 -> [nil, nil] is also fine
103                 ]
104         ],
105         1,                              // startState
106         1                               // repeats
107 ).asStream;
109 Routine({
110         var freq;
111         while({ (freq = p.next.postln).notNil },{
112                 Synth(\help_Pdfsm1, [ \freq, freq.midicps ]);
113                 0.1.wait;
114         })
115 }).play;
119 SynthDef(\help_Pdfsm2,
120         { arg freq, gate=1;
121                 var n=8, env, osc;
122                 env = Linen.kr( gate, 0.01, 1, 0.03, 2 );
123                 osc = {Mix.fill( n, { arg i;
124                         FSinOsc.ar(freq + Rand(-2.0,2.0), Rand(0, 0.05pi)) ring4:
125                         FSinOsc.ar(freq * (i+1));
126                 })}.dup * FSinOsc.kr(Rand(1.5,4.5),{Rand(-0.1pi,0.1pi)}.dup,0.6,env*0.4);
127                 Out.ar(0, env * osc / (n*4) )
128         }).add;
132 var n=3, base, penult;
134 base = [3,4,4,0];
136 for( 1, n, { arg i;
137         penult = Pbind( \degree, Pshuf(base - (i*5), 2), \dur, Pseq([0.2],2) );
138         Pset(
139                 \instrument, \help_Pdfsm2,
140                 Pdfsm(
141                         [
142                                 Pseq([  // signalStream
143                                         Pn(1,22 + i),
144                                         Pn(0,4),
145                                         Pn(1,8),
146                                         Pseq([
147                                                 Pn(0,3),
148                                                 Prand([0,1],8),
149                                                 Pn(1,8)
150                                         ], 3 ),
151                                         Pn(2,2)
152                                 ], 1 ),
153                                 (       // state 0
154                                         0 : [ 0, Pbind( \degree, Pseq(base - i, 1), \dur, Pxrand([0.2,0.3],4) ) ],
155                                         1 : [ 1, Pbind(
156                                                         \degree, Pseq(base.reverse - (i*2), 2),
157                                                         \dur, Pseq([0.2,0.21],1)
158                                                         )
159                                                 ],
160                                         2 :     [ 2, penult ]
161                                 ),
162                                 (       // state 1
163                                         0 : [ 0, Pbind( \degree, Pshuf(base * i.neg, 8), \dur, Pseq([0.08],8) ) ],
164                                         1 : [ 0, Pbind( \degree, Pseq(base - (i*3),3+i), \dur, Pseq([0.11],3+i) ) ],
165                                         2 : [ 2, penult ]
166                                 ),
167                                 (       // state 2
168                                         \default : [ 2, Pbind(
169                                                                         \degree, Prand(base - (i*7), 5),
170                                                                         \dur, Prand([0.6,0.8],5)
171                                                                 )
172                                                         ]
173                                 )
174                         ],
175                         i % 2           // startState
176                 )
177         ).play;