-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicCellularAutomata.sc
More file actions
255 lines (214 loc) · 6.35 KB
/
Copy pathBasicCellularAutomata.sc
File metadata and controls
255 lines (214 loc) · 6.35 KB
1
2
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
BasicCA {
/* abstract class for two state, 1D Cellular Automata.
Subclasses will override methods related to rule creation and derivation.
This superclass will handle conversion to patterns
*/
var <>width, <>prevState, <>nextState, <>started, <>history, <>midiout, <>windowSize, <>windowPos, <>window, <>scale, <>legato, <>tempo, <>cleanDisplay, <>myTask;
*new {|width, firstState, midiout|
^super.new.init(width, firstState, midiout)
}
init {|width, firstState, midiout|
this.width = width;
this.midiout = midiout;
if (firstState == nil,
{ this.nextState = this.randomGenerate() },
{ this.nextState = firstState; this.width = this.nextState.size()}
);
this.prevState = "";
this.nextState.do { this.prevState = this.prevState ++ "0" };
this.started = false;
this.history = [this.prevState];
this.windowSize = 7; // by default; set with this.setWindow
this.windowPos = 0;
this.window = "";
this.cleanDisplay = false;
this.legato = true;
this.tempo = 1;
}
getNext {
// abstract
}
displayHistory {
this.history.do {|state, i|
if (i >0 , {state.postln});
};
}
randomGenerate {
var res ="";
this.width.do { res = res ++ 2.rand.asString };
^res;
}
singleCellLine {
var res = "";
this.width.do { res = res ++ "0"};
res = res[0..(width/2).asInteger] ++ "1"++ res[((width/2).asInteger +1)..];
this.nextState = res.copy;
}
/*symmetricalRandomLine{
}*/
setWindow { |size, pos, center = true|
if (size > this.width, {size = this.width });
if (center,
{ pos = ((this.width - size)/2).asInteger});
// make sure everything fits, else trim window
if ((pos + size) > this.width,
{size = this.width - pos; "trimmed window".postln; });
this.windowPos = pos;
this.windowSize = size;
this.started = false;
}
shiftWindow { |dist|
var newStart, newEnd;
newStart = this.windowPos + dist;
newEnd = this.windowPos + this.windowSize + dist;
if (newStart < 0, {newStart = 0});
this.setWindow(this.windowSize, newStart, center:false);
}
displayCurrent {
// called by playNext, shows complete state, with window bracketted off
var outputString = "";
this.nextState.do { |val, i|
if (i == this.windowPos, {outputString = outputString ++ "["});
if (this.cleanDisplay, {if (val.asString == "1", {val = "x"}, {val = " "});});
outputString = outputString ++ val.asString;
if (i == (this.windowPos + this.windowSize - 1), {outputString = outputString ++ "]"});
};
outputString.postln;
}
getCurrentAsString {|includeWindow = false|
var outputString = "";
this.nextState.do {|val,i|
outputString = outputString ++ val.asString;
}
^outputString;
}
playNext {
var patternFeed = [], pbs;
this.windowVals();
this.displayCurrent();
this.windowSize.do { |i|
// [this.windowSize, i,this.nextState[this.windowPos + i].asString, this.prevState[this.windowPos + i].asString].postln;
if (this.nextState[this.windowPos + i].asString != this.prevState[this.windowPos + i].asString,
{
switch (this.nextState[this.windowPos + i].asString,
"0", {patternFeed = patternFeed.add([i,\noteOff])},
"1", {patternFeed = patternFeed.add([i,\noteOn])}
);
});
// this.getNext;
// patternFeed.postln;
/*^patternFeed;*/
};
this.getNext;
^patternFeed;
}
windowVals {
var win;
this.windowSize.do {|i| win = win ++ this.nextState[i + this.windowPos].asString};
this.window = win.copy;
}
playThru { |tempo = 1|
var patternFeed;
this.tempo = tempo;
this.myTask = Task.new({loop {
this.patternFeedToEvent(this.playNext());
this.tempo.wait} }).play;
}
patternFeedToEvent {|patternFeed|
patternFeed.do {|feed, i|
( type:\midi,
midiout: this.midiout,
midicmd: feed[1],
degree: feed[0],
root: -6,
scale: this.scale ).play;
};
}
}
CARules {
var <>rulesDict, <>ruleKeys, <>ruleNum;
*new {|rules, ruleKeys|
^super.new.init(rules, ruleKeys)
}
init {|rules, ruleKeys|
this.ruleKeys = ruleKeys;
this.rulesDict = this.createRules(rules);
}
createRules {|rules|
var rulesDict = Dictionary.new;
if (rules.isString,
{ rulesDict = this.makeRulesFromString(rules)},
{ rulesDict = this.makeRulesFromInt(rules)});
^rulesDict;
}
makeRulesFromString { |rules|
var rulesDict = Dictionary.new();
if (rules.size != ruleKeys.size, { postf("rule string needs % ones or zeros", this.ruleKeys.size); ^nil; });
rules.do { |rule, i|
rule = rule.asString;
if ((rule != "1") && (rule != "0"), {postln(["rules must contain only 0 and 1:", rule])});
rulesDict.add(this.ruleKeys[i] -> rule);
};
this.setRuleNumFromString(rulesDict);
^rulesDict;
}
setRuleNumFromString { |rulesDict|
var reverseKeys, total = 0;
reverseKeys = this.ruleKeys.reverse();
reverseKeys.postln;
reverseKeys.do { |key, i|
if (key.isInteger != true, {key = key.asString});
total = total + (rulesDict[key].asInteger*(2**i));
};
this.ruleNum = total;
}
makeRulesFromInt { |rules|
var ruleArray, rulesDict = Dictionary.new();
if (rules.isInteger != true, {"must be integer (or string)".postln; ^nil});
if ((rules < 0) || (rules > (2**this.ruleKeys.size - 1)), {"rule number out of range".postln; ^nil});
ruleArray = rules.asBinaryString;
if (ruleArray.size < this.ruleKeys.size, {ruleArray = ruleArray.padLeft((this.ruleKeys.size) , string:"0")});
if (ruleArray.size > this.ruleKeys.size, {ruleArray = ruleArray[(ruleArray.size - this.ruleKeys.size)..]});
ruleArray.do { |rule, i|
rulesDict.add(this.ruleKeys[i] -> rule.asString);
};
this.ruleNum = rules;
^rulesDict;
}
showRules {
this.ruleKeys.do {|key| [key, this.rulesDict[key]].postln};
}
}
CA_Transmitter {
var <> myCA, <>host, <>tempo;
classvar <>classVars;
*new { |myCA, tempo|
^super.new.init(myCA, tempo) }
*initClass {
// static constructor
}
init { |myCA, tempo |
this.myCA = myCA;
this.tempo = tempo;
this.host = NetAddr("localhost", 4859);
}
transmit {
TempoClock.default.sched(0,{this.sendPatterns();
this.tempo} );
/* Routine.new(
{loop {
this.sendPatterns();
this.tempo.yield}
}).play;*/
}
sendPatterns {
// let's send the array as a string!
"here!".postln;
this.host.sendMsg("/current", this.myCA.getCurrentAsString());
this.host.sendMsg("/window", this.myCA.windowSize, this.myCA.windowPos);
}
}
/* TODO
- create re-init, for when resetting stuff
- gen single cell, gen symetrical random (include this in sublcass args?)
*/