2 summary:: deterministic finite state machine
4 categories:: Streams-Patterns-Events>Patterns>List>Indexing
8 Pdfsm is a deterministic finite state machine with signal input (written by by ccos).
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.
18 an integer index for the state to start with.
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.
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.
39 ## signal value : [destination state, return stream or pattern], ||
40 ## signal value : [destination state, return stream or pattern] ||
43 ## ... // state 1 ... N ||
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.
58 Pseq( [\foo,\bar], 2 ), // foobar signals
60 \foo : [ 1, Pseq([ 0, 1 ], 2 ) ]
70 11.do({ p.next.postln });
74 SynthDef(\help_Pdfsm1,
75 { arg out=0, freq=440, sustain=0.05;
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))
86 Prand([0,1,2],inf), // signalStream
88 IdentityDictionary[ // state 0
89 0 -> [ 2, Pseq([67,68,69], 2) ],
93 IdentityDictionary[ // state 1
94 1 -> [ 1, Pseq([69,68,67],2) ],
100 2 -> [ nil ] // signalStream is infinitely long,
101 // so the fsm only ends because of this nil
102 // 2 -> [nil, nil] is also fine
111 while({ (freq = p.next.postln).notNil },{
112 Synth(\help_Pdfsm1, [ \freq, freq.midicps ]);
119 SynthDef(\help_Pdfsm2,
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) )
132 var n=3, base, penult;
137 penult = Pbind( \degree, Pshuf(base - (i*5), 2), \dur, Pseq([0.2],2) );
139 \instrument, \help_Pdfsm2,
142 Pseq([ // signalStream
154 0 : [ 0, Pbind( \degree, Pseq(base - i, 1), \dur, Pxrand([0.2,0.3],4) ) ],
156 \degree, Pseq(base.reverse - (i*2), 2),
157 \dur, Pseq([0.2,0.21],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) ) ],
168 \default : [ 2, Pbind(
169 \degree, Prand(base - (i*7), 5),
170 \dur, Prand([0.6,0.8],5)