-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.scd
429 lines (388 loc) · 10.5 KB
/
setup.scd
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
(
//1. server settings
s = Server.local;
s.options.device_(
"ASIO4ALL v2"
// "JackRouter"
// "MOTU Pro Audio"
);
Server.supernova;
s.options.numInputBusChannels = 36;
s.options.numOutputBusChannels = 36;
s.options.numAudioBusChannels = 1024*2;
s.options.blockSize = 512;
s.options.numWireBufs = 64*16;
s.options.memSize = 2.pow(16);
s.options.sampleRate = 48000;
s.newBusAllocators;
/*ServerBoot.removeAll;
ServerTree.removeAll;
ServerQuit.removeAll;*/
//2. init global variables & functions
~out = 0;
~path = PathName(thisProcess.nowExecutingPath).parentPath++"buffers";
//3. piece-specific functions
//3a. render settings
~makeBuffers = {
b = Dictionary.new;
PathName(~path).entries.do{
arg subfolder;
b.add(
subfolder.folderName.asSymbol ->
Array.fill(
subfolder.entries.size,{
arg i;
Buffer.read(s,subfolder.entries[i].fullPath);
})
)
}
};
~encOrder = 3; //encoder order
~encNumChannels = (~encOrder+1).pow(2);
~decOrder = 3; //decoder order
~decNumChannels = (~decOrder+1).pow(2);
// create a single ObjEncoder to hold HOAEncoder for sound objects
~makeEncoder = {
~encoder = ObjEncoder.new(s,~encOrder);
"encoder created".postln;
};
//binaural decoder
~makeDecoder = {
s.bind({
HOADecLebedev26.loadHrirFilters(s);
~decoder = NodeProxy.new(s,\audio,~decNumChannels);
// ~decoder.fadeTime = 2;
~decoder.source = {HOADecLebedev26.ar(~decOrder,
\in.ar(0!~decNumChannels.asInteger),
hrir_Filters:1)};
});
"binarul decoder created".postln;
};
// (Optional: for grouping use only)
// choose either makeEncoder or makeMasterEncoder (plus AddEncoder), cannot have both!!!
// one master encoder and one encoder dictionary to contain all sub encoders (an ObjEncoder)
// ~makeMasterEncoder = {
// ~encoders = Dictionary.new;
// ~masterEncoder = NodeProxy.new(s,\audio,~encNumChannels);
// if(~encoders.at(\default).isNil) {
// ~encoders.add(\default->ObjEncoder.new(s,~encOrder));
// ~masterEncoder.source = {Out.ar(~masterEncoder.bus,In.ar(~encoders.at(\default).encodeBus,~encNumChannels.asInteger))};
// "\default ObjEncoder in ~encoders".postln;
// };
// "masterEncoder created".postln;
// };
// (Optional: for grouping use only)
// add an ObjEncoder to encoders and route to masterEncoder
// ~addEncoder = {arg name;
// if(~encoder.at(name).isNil) {
// ~encoder.add(name->ObjEncoder.new(s,~encOrder));
// ~masterEncoder.add = {Out.ar(~masterEncoder.bus,In.ar(~encoder.at(name).encodeBus,~encNumChannels.asInteger))};
// };
// ("ObjEncoder "++name++" created in ~encoders dictionary").postln;
// };
// sound field transformer
~makeTransformer = {
~transformer = NodeProxy.new(s,\audio,~encNumChannels);
~transformer.fadeTime = 1;
"transformer created".postln;
};
// connect between encoder, transformer, (convertor), decoder (binaural only)
~routing = {
"routing begins".postln;
"//////////////////////".postln;
if(~masterEncoder.isNil.not) {
"grouping mode".postln;
~transformer.source = {
arg pitch=0,yaw=0,roll=0; var in=\in.ar(0!~encNumChannels);
HOATransRotateXYZ.ar(~encOrder,in,pitch,roll,yaw);
};
~masterEncoder<>>~transformer;
"masterEncoder->transformer".postln;
} {
"single encoder mode".postln;
// Ndef(\encoder,{In.ar(~encoder.encodeBus,~encNumChannels.asInteger)});
~transformer.source = {
arg pitch=0,yaw=0,roll=0;
HOATransRotateXYZ.ar(~encOrder,In.ar(~encoder.encodeBus,~encNumChannels.asInteger),pitch,roll,yaw);
};
"encoder->transformer".postln;
};
if(~convertor.isNil.not) {
~transformer<>>~convertor;
"transformer->convertor".postln;
if(~decoder.isNil.not) {
~convertor<>>~decoder;
"convertor->decoder, ready to output".postln;
} {
"no decoder, convertor is ready to output".postln;
};
} {
"no convertor".postln;
if(~decoder.isNil.not) {
~transformer<>>~decoder;
"transformer->decoder, ready to output".postln;
} {
"no decoder, transformer is ready to output".postln;
};
};
"//////////////////////".postln;
"routing finished".postln;
};
//
~makeReverb = {};
~makeConnect = Routine {
s.newBusAllocators;
0.1.wait;
~makeEncoder.value;
0.1.wait;
~makeTransformer.value;
0.1.wait;
if(s.options.device=="ASIO4ALL v2") {
~makeDecoder.value;
0.1.wait;
~routing.value;
0.1.wait;
~decoder.play;
};
if(s.options.device=="MOTU Pro Audio"){
~makeConvertor.value;
0.1.wait;
~routing.value;
0.1.wait;
~convertor.play;
};
};
~makeWorld = {
~world = SSWorld.new;
~world.showView;
"world created".postln;
};
~cleanup = {
s.newBusAllocators;
ServerBoot.removeAll;
ServerTree.removeAll;
ServerQuit.removeAll;
};
//3b. controller settings (OSC receiving functions)
~removeOSC = {
OSCdef.freeAll;
};
//physical actions
//move single object
~grab = {|obj|
OSCdef.freeAll;
OSCdef(\setxGrab,{|msg|
obj.vel.x = msg[1].linlin(-1,1,obj.world.dim.x.neg,obj.world.dim.x);
msg.postln;
},'/right/closed/posz').disable;
OSCdef(\setyGrab,{|msg|
obj.vel.y = msg[1].linlin(-1,1,obj.world.dim.y,obj.world.dim.y.neg);
msg.postln;
},'/right/closed/posx').disable;
OSCdef(\setzGrab,{|msg|
obj.vel.z = msg[1].linlin(-1,1,obj.world.dim.z.neg,obj.world.dim.z);
msg.postln;
},'/right/closed/posy').disable;
OSCdef(\switchGrab,{|msg|
var switch = msg[1];
if(switch!=0) {
OSCdef(\setxGrab).enable;
OSCdef(\setyGrab).enable;
OSCdef(\setzGrab).enable;
} {
OSCdef(\setxGrab).disable;
OSCdef(\setyGrab).disable;
OSCdef(\setzGrab).disable;
}
},'/switch/grab');
};
~attract = {|world|
var dist_x,dist_y,dist_z;
OSCdef(\getxAttract,{|msg|
dist_x = msg[1].linlin(-1,1,world.dim.x.neg,world.dim.x);
},'right/closed/posz').disable;
OSCdef(\getyAttract,{|msg|
dist_y = msg[1].linlin(-1,1,world.dim.y,world.dim.y.neg);
},'right/closed/posx').disable;
OSCdef(\getzAttract,{|msg|
dist_z = msg[1].linlin(-1,1,world.dim.z.neg,world.dim.z);
},'right/closed/posy').disable;
OSCdef(\switchAttract,{|msg|
var switch = msg[1];
if(switch!=0) {
OSCdef(\getxAttract).enable;
OSCdef(\getyAttract).enable;
OSCdef(\getzAttract).enable;
world.objects.do{|obj|
var dir = [dist_x,dist_y,dist_z] - obj.loc.asArray;
var dist = dir.sum(_.pow(2)).sqrt;
obj.extForce_(dir*(world.gravity.asArray.sum(_.pow(2)).sqrt*obj.mass/dist/dist));
};
} {
OSCdef(\getxAttract).disable;
OSCdef(\getyAttract).disable;
OSCdef(\getzAttract).disable;
world.objects.do{|obj|
obj.extForce_([0,0,0]);
};
};
},'/switch/attract');
};
~repulse = {|world|
var dist_x,dist_y,dist_z;
OSCdef(\getxRepulse,{|msg|
dist_x = msg[1].linlin(-1,1,world.dim.x.neg,world.dim.x);
},'right/closed/posz').disable;
OSCdef(\getyRepulse,{|msg|
dist_y = msg[1].linlin(-1,1,world.dim.y,world.dim.y.neg);
},'right/closed/posx').disable;
OSCdef(\getzRepulse,{|msg|
dist_z = msg[1].linlin(-1,1,world.dim.z.neg,world.dim.z);
},'right/closed/posy').disable;
OSCdef(\switchRepulse,{|msg|
var switch = msg[1];
if(switch!=0) {
OSCdef(\getxRepulse).enable;
OSCdef(\getyRepulse).enable;
OSCdef(\getzRepulse).enable;
world.objects.do{|obj|
var dir = obj.loc.asArray - [dist_x,dist_y,dist_z];
var dist = dir.sum(_.pow(2)).sqrt;
obj.extForce_(dir*(world.gravity.asArray.sum(_.pow(2)).sqrt*obj.mass/dist/dist));
};
} {
OSCdef(\getxRepulse).disable;
OSCdef(\getyRepulse).disable;
OSCdef(\getzRepulse).disable;
world.objects.do{|obj|
obj.extForce_([0,0,0]);
};
};
},'/switch/repulse');
};
~gravity = {|world|
//ref: http://planning.cs.uiuc.edu/node102.html
var mul_x=1,mul_y=1,mul_z=1,pitch=0,roll=0;
var orit_x=0,orit_y=0,orit_z=(-1); //initial gravity orientation
var g_min=0.5,g_max=5;
var new_gravity = [0,0,-9.8];
// OSCdef.freeAll;
OSCdef(\getxGravity,{|msg|
mul_x = msg[1].linexp(-1,1,g_min,g_max);
},'right/closed/posz').disable;
OSCdef(\getyGravity,{|msg|
mul_y = msg[1].linexp(-1,1,g_max,g_min);
},'right/closed/posx').disable;
OSCdef(\getzGravity,{|msg|
mul_z = msg[1].linexp(-1,1,g_min,g_max);
},'right/closed/posy').disable;
OSCdef(\getpitchGravity,{|msg|
pitch = msg[1]*pi/2; //transform to rad and counterclockwise
},'right/open/pitch').disable;
// yaw not necessary for gravity control
/*OSCdef(\getyaw,{|msg|
yaw = msg[1]*pi/2*(-1);//transform to rad and counterclockwise
},'right/open/yaw');*/
OSCdef(\getrollGravity,{|msg|
roll = msg[1]*pi*(-1);//transform to rad and counterclockwise
},'right/open/roll').disable;
OSCdef(\switchGravity,{|msg|
var switch = msg[1];
if(switch!=0) {//on
//turn on OSC funcs
OSCdef(\getxGravity).enable;
OSCdef(\getyGravity).enable;
OSCdef(\getzGravity).enable;
OSCdef(\getpitchGravity).enable;
OSCdef(\getrollGravity).enable;
//combine of rotation matrix and then compute (multiply [0,0,1])
orit_x = sin(pitch)*cos(roll);
orit_y = -1*sin(roll);
orit_z = cos(pitch)*cos(roll);
new_gravity = 980*[orit_x*mul_x,orit_y*mul_y,orit_z*mul_z];
world.gravity_(new_gravity);
} {//off
OSCdef(\getxGravity).disable;
OSCdef(\getyGravity).disable;
OSCdef(\getzGravity).disable;
OSCdef(\getpitchGravity).disable;
OSCdef(\getrollGravity).disable;
world.gravity_([0,0,-980]);
};
},'/switch/gravity');
};
~rotate = {|transformer|
var pitch=0, yaw=0, roll=0;
OSCdef(\getpitchRotate,{|msg|
pitch = msg[1].linlin(-1,1,pi/2,-pi/2);
},'/right/open/pitch').disable;
OSCdef(\getyawRotate,{|msg|
yaw = msg[1].linlin(-1,1,pi/2,-pi/2);
},'/right/open/yaw').disable;
OSCdef(\getrollRotate,{|msg|
roll = msg[1].linlin(-1,1,-pi,pi);
},'/right/open/roll').disable;
OSCdef(\switchRotate,{|msg|
var switch = msg[1];
if(switch!=0) {//on
OSCdef(\getpitchRotate).enable;
OSCdef(\getyawRotate).enable;
OSCdef(\getrollRotate).enable;
transformer.set(
\pitch, pitch,
\yaw, yaw,
\roll, roll
);} {//off
OSCdef(\getpitchRotate).disable;
OSCdef(\getyawRotate).disable;
OSCdef(\getrollRotate).disable;
transformer.set(
\pitch,0,
\yaw,0,
\roll,0
);}
},'/switch/rotate');
};
//~spread
//~contract
//~removeObjects
//4. register functions with ServerBoot/Quit/Tree
ServerBoot.add(~makeBuffers);
ServerBoot.add(~makeWorld);
ServerQuit.add({
if(~world.isNil.not) {
~world.objects.do{|obj|
obj.sourceNode.clear;
~world.remove(obj);
};
};
if(~encoder.isNil.not){
~encoder.encoders.do{|enc|
enc.clear;
};
~encoder.encodeBus.free;
~encoder.encodeGroup.free;
};
if(~transformer.isNil.not){
~transformer.clear;
};
if(~decoder.isNil.not){
~decoder.clear;
};
if(~convertor.isNil.not){
~convertor.clear;
};
});
ServerQuit.add(~cleanup);
//5. boot server
s.waitForBoot({
s.sync;
//6. register remaining functions
ServerTree.add({
~world.updateView;
});
s.freeAll;
s.sync;
~makeConnect.reset.play;
});
)