| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 
 | class:: Pdfsm
summary:: deterministic finite state machine
related:: Classes/Pfsm
categories:: Streams-Patterns-Events>Patterns>List>Indexing
description::
Pdfsm is a deterministic finite state machine with signal input (written by by ccos).
ClassMethods::
method::new
argument::list
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.
argument::startState
an integer index for the state to start with.
argument::repeats
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.
Examples::
definitionList::
## list structure:
|| [
definitionList::
## 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.
## states || states should be an instance of link::Classes/IdentityDictionary::, link::Classes/Event:: or some other associative collection.
::
## ] ||
## list syntax:
|| [
definitionList::
## signal stream, ||
## ( // state 0, ||
definitionList::
## signal value : [destination state, return stream or pattern], ||
## signal value : [destination state, return stream or pattern] ||
::
## ), ||
## ... // state 1 ... N ||
::
## ] ||
::
Any number of states can be given, and are indexed by the order in which they are given.
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.
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.
code::
(
	p = Pdfsm(
		[
			Pseq( [\foo,\bar], 2 ), // foobar signals
			( // state 0
				\foo : [ 1, Pseq([ 0, 1 ], 2 ) ]
			),
			( // state 1
				\bar : [ 0, 3 ]
			)
		],
		0,
		2
	).asStream;
	11.do({ p.next.postln });
)
(
SynthDef(\help_Pdfsm1,
	{ arg out=0, freq=440, sustain=0.05;
		var env;
		env = EnvGen.kr(Env.perc(0.01, sustain, 0.2), doneAction:2);
		Out.ar(out, SinOsc.ar([freq, freq + 0.1.rand2], 0, env))
	}).add;
)
(
var p;
p = Pdfsm(
	[
		Prand([0,1,2],inf),	// signalStream
		IdentityDictionary[	// state 0
			0 -> [ 2, Pseq([67,68,69], 2) ],
			1 -> [ 0, 66 ],
			2 -> [ 1, 65 ]
		],
		IdentityDictionary[	// state 1
			1 -> [ 1, Pseq([69,68,67],2) ],
			\default -> [ 0, 70 ]
		],
		IdentityDictionary[
			0 -> [ 0, 71 ],
			1 -> [ 0, 72 ],
			2 -> [ nil ]	// signalStream is infinitely long,
					// so the fsm only ends because of this nil
					// 2 -> [nil, nil] is also fine
		]
	],
	1, 				// startState
	1 				// repeats
).asStream;
Routine({
	var freq;
	while({ (freq = p.next.postln).notNil },{
		Synth(\help_Pdfsm1, [ \freq, freq.midicps ]);
		0.1.wait;
	})
}).play;
)
(
SynthDef(\help_Pdfsm2,
	{ arg freq, gate=1;
		var n=8, env, osc;
		env = Linen.kr( gate, 0.01, 1, 0.03, 2 );
		osc = {Mix.fill( n, { arg i;
			FSinOsc.ar(freq + Rand(-2.0,2.0), Rand(0, 0.05pi)) ring4:
			FSinOsc.ar(freq * (i+1));
		})}.dup * FSinOsc.kr(Rand(1.5,4.5),{Rand(-0.1pi,0.1pi)}.dup,0.6,env*0.4);
		Out.ar(0, env * osc / (n*4) )
	}).add;
)
(
var n=3, base, penult;
base = [3,4,4,0];
for( 1, n, { arg i;
	penult = Pbind( \degree, Pshuf(base - (i*5), 2), \dur, Pseq([0.2],2) );
	Pset(
		\instrument, \help_Pdfsm2,
		Pdfsm(
			[
				Pseq([	// signalStream
					Pn(1,22 + i),
					Pn(0,4),
					Pn(1,8),
					Pseq([
						Pn(0,3),
						Prand([0,1],8),
						Pn(1,8)
					], 3 ),
					Pn(2,2)
				], 1 ),
				(	// state 0
					0 : [ 0, Pbind( \degree, Pseq(base - i, 1), \dur, Pxrand([0.2,0.3],4) ) ],
					1 : [ 1, Pbind(
							\degree, Pseq(base.reverse - (i*2), 2),
							\dur, Pseq([0.2,0.21],1)
							)
						],
					2 :	[ 2, penult ]
				),
				(	// state 1
					0 : [ 0, Pbind( \degree, Pshuf(base * i.neg, 8), \dur, Pseq([0.08],8) ) ],
					1 : [ 0, Pbind( \degree, Pseq(base - (i*3),3+i), \dur, Pseq([0.11],3+i) ) ],
					2 : [ 2, penult ]
				),
				(	// state 2
					\default : [ 2, Pbind(
									\degree, Prand(base - (i*7), 5),
									\dur, Prand([0.6,0.8],5)
								)
							]
				)
			],
			i % 2 		// startState
		)
	).play;
})
)
::
 |