This repository was archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClusterBasic.sc
269 lines (228 loc) · 7.19 KB
/
ClusterBasic.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
255
256
257
258
259
260
261
262
263
264
265
266
/*
Cluster Library
Copyright 2009-2012 Miguel Negrão.
Cluster Library: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Cluster Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Cluster Library. If not, see <http://www.gnu.org/licenses/>.
*/
ClusterBasic {
var <items;
//no arguments
prCollectNoArgs{ |selector|
var return = items.collect(_.tryPerform(selector));
"prcollectsimple";
^this.getReturnClusterObject(return);
}
//with arguments
prCollectWithArgs{ |selector,argArray|
var return = items.collect{ |item,i| item.tryPerform(*([selector]++argArray[i]))};
"!!!prCollectWithArgs";
^this.getReturnClusterObject(return);
}
getReturnClusterObject{ |returnArray|
var clusterClasses;
if((returnArray[0].class == this.oclass) ){
^this
}{
if((returnArray[0].isNil) ){
^nil
} { //this needs to be fixed, it should return the class corresponding to the oclass.
clusterClasses = ClusterBasic.allSubclasses;
//clusterClasses.remove(this.class); // ?
clusterClasses = clusterClasses.collect(_.oclass);
if(clusterClasses.includes(returnArray[0].class)){
^("Cluster"++returnArray[0].class.asCompileString).compile.value.fromArray(returnArray)
}{
^ClusterArg(returnArray)
}
}
}
}
//expand a set of arguments into an array of size items.size
prExpandArgs{ |args|
("prExpand: "++args);
^ClusterBasic.expandArray(args,this);
}
prExpandCollect{ |selector,args|
if(args.size == 0){
"args nil";
^this.prCollectNoArgs(selector)
}{
"args not nil";
^this.prCollectWithArgs(selector,this.prExpandArgs(args))
}
}
prCheckSize{ |clusterobject|
if(clusterobject.items.size != items.size){
Error("Cluster sizes mismatch. ClusterObject with size "++clusterobject.items.size++
" vs ClusterObject with size "++items.size++" \n "++[clusterobject.items,items]).throw
}
}
//default implementation of collected classes.
// will work, but it's not possible to pass arguments by name. e.g. busnum: 3
doesNotUnderstand{ arg selector...args;
//("ClusterBasic:doesNotUnderstand | base class "++this.class++" | selector "++selector).postln;
if(this.respondsTo(selector)){
^this.prExpandCollect(selector,args)
}{
//if method not implemented than call Object's doesNotUnderstand
//"items: %".format(items).postln;
//"class % doesn't implement selector %".format(items[0].class, selector).postln;
^super.doesNotUnderstand(*([selector]++args));
}
}
respondsTo{ |selector|
^items[0].respondsTo(selector)
}
oclass{ ^this.class.oclass }
dopost{ "items:".postln; items.do(_.postln) }
printOn { arg stream;
stream << this.class.asString << items ;
}
// Class methods wrapping
*fromArray{ |array|
if(array.collect{ |x| x.class }.as(Set).size>1){
Error("ClusterBasic: "++array++" - Items should be all of the same class").throw
};
^super.newCopyArgs(array);
}
*new{ |array|
^this.fromArray(array);
}
*expandArray{ |array,clusterObject|
var recursF1,recursF2,finalF;
recursF1 = { |array,clusterobject|
array.collect{ |item|
if(item.isArray && item.isString.not){
recursF1.(item,clusterobject)
}{
if(item.class.superclasses.includes(ClusterBasic)){
clusterObject.prCheckSize(item);
switch(item.class)
{ClusterBus}{ item.index }
{ClusterBuffer}{ item.bufnum }
{item}
}{
ClusterArg(clusterobject.items.collect{ item })
}
}
}
};
recursF2 = { |array,i|
array.collect{ |item|
if(item.isArray && item.isString.not){
"is array";
recursF2.(item,i)
}{
"is clusterarg";
item.items[i]
}
}
};
finalF = { |array,clusterobject|
clusterobject.items.collect{ |adas,i|
("line "++i);
recursF2.(array,i)
}};
^finalF.(recursF1.(array,clusterObject), clusterObject)
}
*searchArrayForClusterRec{ |array|
//return the first element that is a ClusterArg
^array.inject(None(), { |state,item|
if(state.isDefined) {
state
} {
if(item.class.superclasses.includes(ClusterBasic)){
Some(item)
}{
if(item.isArray && item.isString.not){
this.searchArrayForClusterRec(item)
} {
state
}
}
}
})
}
*searchArrayForCluster{ |array|
var x = this.searchArrayForClusterRec(array);
if (x.isEmpty ) {
Error("arguments must have at least one Cluster class instance").throw
} {
^x.get
}
}
// is there a case beyond getters and setters for classvars that a class method is called with no arguments and no defaults ?
*prCollectSimple{ |selector,referenceCluster|
("prCollectSimple - this class: "++this.class);
^ClusterArg([this.oclass.perform(selector)]);
}
*prCollectWithArgs{ |selector,argArray,referenceCluster|
("prCollectWithArgs - this class: "++this.class);
^referenceCluster.items.collect{ |item,i| this.oclass.perform(*([selector]++argArray[i]))};
}
*prExpandCollect{ |selector,args, referenceCluster|
var f;
("prExpandCollect - this class: "++this.class);
if(args.size == 0){
"*prExpandCollect - no arguments - not doing anything at the moment - probably you want a class var, just get it directly".postln;
//^this.prCollectSimple(selector)
}{
f = { |array|
array.collect{ |item|
if(item.isArray && item.isString.not && (item.class != M) ){
f.(item)
}{
if( item.class == M ){
item.asClusterArg
}{
item
}
}
}
};
args = f.(args);
referenceCluster = referenceCluster ?? { this.searchArrayForCluster(args) };
^this.prCollectWithArgs(selector, this.expandArray(args,referenceCluster), referenceCluster)
}
}
*doesNotUnderstand{ arg selector...args;
var cluster;
//("doesNotUnderstand - "++selector++" this class: "++this.class++" args is "++args).postln;
if(this.oclass.class.findRespondingMethodFor(selector).notNil){
^super.newCopyArgs(this.prExpandCollect(selector,args))
}
}
*newExpandCollect { |selector, args,referenceCluster|
^super.newCopyArgs(this.prExpandCollect(selector,args, referenceCluster))
}
*oclass{ ^Object }
clusterfy{ ^this }
deCluster{ ^items[0] }
clApplyF{ |func|
^ClusterArg(items.collect{ |item| { func.(item) } })
}
//explicit overloading of methods from Object
changed { arg what ... moreArgs;
^this.doesNotUnderstand(*([\changed,what]++moreArgs));
}
addDependant { arg dependant;
^this.doesNotUnderstand(\addDependant,dependant);
}
removeDependant { arg dependant;
^this.doesNotUnderstand(\removeDependant,dependant);
}
release {
^this.doesNotUnderstand(\release)
}
releaseDependants {
^this.doesNotUnderstand(\releaseDependants)
}
}