/
MandelValue.sc
254 lines (205 loc) · 4.5 KB
/
MandelValue.sc
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
/*
MandelValue
(c) 2011 by Patrick Borgeat <patrick@borgeat.de>
http://www.cappel-nord.de
Part of BenoitLib
http://github.com/cappelnord/BenoitLib
http://www.the-mandelbrots.de
A value instance for MandelSpace
*/
MandelValue : AbstractFunction {
var <key, <>bdl, <decorator, quant;
var space;
var <bus, busDependant;
var <>sourcePullInterval = 0.05;
var sourceRoutine;
var <>doHeal = true;
var lastUpdateBeats;
var dropFunction = nil;
*new {|space, key|
^super.new.init(space, key);
}
init {|aspace, akey|
space = aspace;
key = akey.asSymbol;
}
value {
^this.getValue(); // why?
}
valueArray {|array|
^this.getValue(); // why?
}
asStream {
^Routine({
while {true} {
this.getValue().yield;
};
});
}
asBus {
^bus.isNil.if({this.prCreateBus}, {bus});
}
asBusPlug {
^BusPlug.for(this.asBus);
}
prCreateBus {
this.freeBus;
space.hub.server.serverRunning.if ({
bus = Bus.control(space.hub.server, 1);
bus.set(this.getValue());
busDependant = {|changed, what, value| bus.set(value)};
this.addDependant(busDependant);
}, {
"Server is not running! You have to re-evaluate.".warn;
// dummy Bus to fail silently
^Bus.control(space.hub.server, 1);
});
^bus;
}
freeBus {
this.removeDependant(busDependant);
bus.free;
bus = nil;
}
prepareForProxySynthDef {|proxy|
^this.asBus.prepareForProxySynthDef(proxy);
}
kr {
^this.asBus.kr;
}
ar {
^{K2A.ar(this.asBus.kr)};
}
tr {
^{InTrig.kr(this.asBus)};
}
quant {
quant.isNil.not.if({
^quant
}, {
^space.quant;
});
}
quant_ {|val|
quant = val.asQuant;
}
getValue {|useDecorator=true|
(useDecorator && decorator.notNil).if ({
^decorator.value(bdl.value, space, key);
}, {
^bdl.value;
});
}
setBy {
bdl.value(); // oh no!
^bdl.setBy;
}
setValue {|value, schedBeats, who, doSend=true, strategy=\time|
who = who ? space.hub.name;
schedBeats.isNil.if {
this.quant.isNil.if ({
schedBeats = 0.0;
}, {
schedBeats = this.quant.nextTimeOnGrid(space.hub.clock);
});
};
doSend.if {space.sendValue([key, value], schedBeats, strategy)};
^this.prSetBDL(value, schedBeats, who);
}
tryHealValue {|value, schedBeats, who|
((bdl.setAtBeat < schedBeats) && doHeal).if {
("MandelSpace Value " ++ key.asString ++ " got healed!").postln;
this.setValue(value, schedBeats, who, doSend: false);
}
}
// maybe remove this, move to setValue
prSetBDL {|value, schedBeats, who|
bdl.isNil.if {
bdl = MandelBDL(nil, "Nobody", 0);
bdl.onChangeFunc = {this.update;};
};
^bdl.schedule(value, schedBeats, who);
}
decorator_ {|func|
decorator = func;
try {this.update;} {};
}
update {|theChanger, what ... moreArgs|
if(lastUpdateBeats != space.hub.clock.beats) {
space.prScheduleUpdate(key, {this.changed(\value, this.getValue())});
lastUpdateBeats = space.hub.clock.beats;
};
}
<>> {|proxy, key=\in|
proxy.isNil.if {^proxy};
// if the Proxy doesn't allready has a bus we map through assignment
proxy.bus.isNil.if {
proxy.source = this;
^proxy;
};
// otherwise map
^proxy.map(key, this.asBusPlug);
}
<<> {|source, key=\in|
var bus = try {source.asBus};
var stream, val;
this.unmap(\source);
source.isNil.if ({
^this; // early exit - just disconnect
});
bus.isNil.if ({
"Could not set source - not compatible to Bus!".warn;
}, {
stream = Pkr(bus).asStream;
sourceRoutine = {
var lastVal = nil;
while({true}, {
val = stream.next;
if(val != lastVal) {
this.setValue(val, strategy:\stream);
lastVal = val;
};
sourcePullInterval.wait;
});
}.fork;
});
^this;
}
unmap {|key|
// (key == \source).if {
sourceRoutine.stop;
sourceRoutine = nil;
// };
}
canHeal {
bdl.isNil.if {^false};
^(doHeal && (bdl.setBy == space.hub.name) && ((bdl.setAtBeat) + 4 < space.hub.clock.beats) && (bdl.setAtBeat > 0));
}
freeRessources {
this.freeBus;
}
asString {
^("MandelValue: " ++ this.getValue().asString);
}
drop {|schedBeats|
this.setValue("Last drop issued by" + space.hub.name + "at" + space.hub.clock.beats, schedBeats);
}
setDrop {|drop|
var fired = false;
dropFunction.isNil.not.if {
this.clearDrop();
};
dropFunction = {
fired.not.if {
fired = true;
drop.value();
{this.clearDrop}.defer;
};
};
this.addDependant(dropFunction);
}
clearDrop {
this.removeDependant(dropFunction);
dropFunction = nil;
}
}