-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWiiOSCClient.sc
530 lines (387 loc) · 11.3 KB
/
WiiOSCClient.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
OSCHID {
/*@
shortDesc: Deals with OSC-based Human Interface Devices
longDesc: Treat your touchOSC or OSCeleteon as you would any other HID
seeAlso: OscSlot
spec: A dictionary mapping keys to OscSlots
@*/
classvar <>dEBUG;
var <calibrate;
var <>spec, <actionSpec;
var <>remote;
var resp;
*new { arg netAddr;
/*
desc: Creates a new OSCHID. There should be one of these per device.
netAddr: An optional argument for the address of the device.
ex:
k = OSCHID.new.spec_((
ax: OscSlot(\realtive, '/hand/x'),
ay: OscSlot(\realtive, '/hand/y'),
az: OscSlot(\realtive, '/hand/z')
));
k.callibrate = false;
*/
^super.new.init(netAddr);
}
init { arg netAddr;
var action;
calibrate = true;
("Set DarWIIn part to" + NetAddr.langPort).postln;
("In calibration mode").postln;
dEBUG = true;
//roll = 0;
//pitch = 0;
remote = netAddr;
remote.isNil.if({remote = NetAddr("127.0.0.1", 5601)});
resp = [];
}
at { | controlName |
^this.spec.atFail(controlName, {
Error("invalid control name").throw
});
}
setAction{ |key,keyAction|
/*@
desc: Set the action for a particular slot by key
key: The key for the slot.
keyAction: The Action, which takes the slot as an argument.
ex:
k.setAction(ax, {|val|
val.value.postln;
});
@*/
this.at( key).action_( keyAction );
}
removeAction{ |key|
this.at( key).action_( {} );
}
calibrate_ { |bool|
/*@
desc: Turn callibration on or off
bool: true for on, false for off
ex:
When callibration is true, relative slots do not call their actions
k.calibrate = true;
k.setAction(ax, {|val|
val.value.postln;
});
k.callibrate = false;
@*/
calibrate = bool;
this.spec.do({ |slot|
slot.calibrate = calibrate;
});
}
}
WiiOSCClient : OSCHID{
/*@
shortDesc: A class that communicates with DarWiinRemote OSC
longDesc: This class has all the same methods as WiiMote, so if that class ever gets fixed, it will be possible to switch back to it with minimal effort. To use this class, start DarWiinRemote OSC and set its preferences so its OSC out port is the language port of SuperCollider
seeAlso: WiiMote
@*/
// For Usage, see the documentation at the bottom of this file
// classvar <>dEBUG;
// var <calibrate;
var <>id;
var <battery;
var <ext_type;
var <>closeAction, <>connectAction, <>disconnectAction;
var <calibration;
var <remote_led, <>remote_buttons, <>remote_motion, <>remote_ir;
var <>nunchuk_buttons, <>nunchuk_motion, <>nunchuk_stick;
var <>classic_buttons, <>classic_stick1, <>classic_stick2, <>classic_analog;
var <>dumpEvents = false;
var batterylevel;
var ir;
deviceSpec {
^(
ax: OscHIDSlot(\relative, '/wii/acc', nil, 1),
ay: OscHIDSlot(\relative, '/wii/acc', nil, 2),
az: OscHIDSlot(\relative, '/wii/acc', nil, 3),
apitch: OscHIDSlot(\relative, '/wii/orientation', nil, 1),
aroll: OscHIDSlot(\relative, '/wii/orientation', nil, 2),
bA: OscHIDSlot(\button, '/wii/button/a'),
bB: OscHIDSlot(\button, '/wii/button/b'),
bOne: OscHIDSlot(\button, '/wii/button/one'),
bTwo: OscHIDSlot(\button, '/wii/button/two'),
bMinus: OscHIDSlot(\button, '/wii/button/minus'),
bHome: OscHIDSlot(\button, '/wii/button/home'),
bPlus: OscHIDSlot(\button, '/wii/button/plus'),
bUp: OscHIDSlot(\button, '/wii/button/up'),
bDown: OscHIDSlot(\button, '/wii/button/down'),
bLeft: OscHIDSlot(\button, '/wii/button/left'),
bRight: OscHIDSlot(\button, '/wii/button/right')
/* px: { remote_ir[0] },
py: { remote_ir[1] },
angle: { remote_ir[2] },
tracking: { remote_ir[3] },
*/
// when i'm less lazy
/*
nax: { nunchuk_motion[0] },
nay: { nunchuk_motion[1] },
naz: { nunchuk_motion[2] },
nao: { nunchuk_motion[3] },
nsx: { nunchuk_stick[0] },
nsy: { nunchuk_stick[1] },
nbZ: { nunchuk_buttons[0] },
nbC: { nunchuk_buttons[1] },
cbX: { classic_buttons[0] },
cbY: { classic_buttons[1] },
cbA: { classic_buttons[2] },
cbB: { classic_buttons[3] },
cbL: { classic_buttons[4] },
cbR: { classic_buttons[5] },
cbZL: { classic_buttons[6] },
cbZR: { classic_buttons[7] },
cbUp: { classic_buttons[8] },
cbDown: { classic_buttons[9] },
cbLeft: { classic_buttons[10] },
cbRight: { classic_buttons[11] },
cbMinus: { classic_buttons[12] },
cbHome: { classic_buttons[13] },
cbPlus: { classic_buttons[14] },
csx1: { classic_stick1[0] },
csy1: { classic_stick1[1] },
csx2: { classic_stick2[0] },
csy2: { classic_stick2[1] },
caleft: { classic_analog[0] },
caright: { classic_analog[1] }
*/ )
}
*new { arg netAddr;
^super.new.init(netAddr);
}
init { arg netAddr;
/*
var action;
calibrate = true;
("Set DarWIIn part to" + NetAddr.langPort).postln;
("In calibration mode").postln;
dEBUG = true;
//roll = 0;
//pitch = 0;
remote = netAddr;
remote.isNil.if({remote = NetAddr("127.0.0.1", 5601)});
resp = [];
*/
super.init(netAddr);
spec = this.deviceSpec;
}
}
/*
Acceleration {
var <>x, <>y, <>z;
//var <>speedX, <>speedY, <>speedZ;
*new{ arg x, y, z;
^super.new.copyArgs(x,y,z);
}
}
*/
OscHIDSlot {
/*@
shortDesc: A wrapper class for OSCresponderNodes
longDesc: Sets up an OSCresponderNode for a given key and optionally scales it based on minimum and maximum input received so far. This class is designed to mimmic GeneralHIDSlot.
seeAlso: OSCHID GeneralHIDSlot OSCresponderNode
action: Sets the action for the slot. This will not get called for \relative slots until the calibrations is set to false
bus: Returns the bus to which this slot is mapped, if a bus has been created.
@*/
var <osckey, <>action, <>value, <>rawValue, <responder, <bus, busAction, index, type, scale;
var <>max, <>min, >calibrate;
*new{ arg type, osckey, action, index = 1, scale = 256;
/*@
desc: Create a new Slot
type: \relative for inputs that should be scaled. \button for inputs that should not be
osckey: The OSC message to listen for
action: The action to call upon receiving a new OSC message
index: The index of the message to listen for. In most cases this will be one, but if your device bundles x, y z coordinates together and you want to listen for y, this would be 2. Or for z, it would be 3.
ex:
w = OSCHID.new.spec_((
ax: OscSlot(\relative, '/wii/acc', nil, 1),
ay: OscSlot(\relative, '/wii/acc', nil, 2),
az: OscSlot(\relative, '/wii/acc', nil, 3)
));
@*/
^super.new.init(type, osckey, action, index, nil, scale);
}
*match { arg type, osckey, action, index, equals, scale = 256;
^super.new.init(type, osckey, action, index, equals, scale);
}
init { arg typ, key, act, ind = 1, eq, scl = 256;
var resp_func;
osckey = key;
action = act;
//busAction = {};
index = ind.asInt;
type = typ;
scale = scl;
calibrate = true;
responder = OSCresponderNode(nil, osckey, { |t, r, msg|
var test;
//WiiOSCClient.dEBUG.if ({
//msg.post; " ".post; msg[index].postln;
test = true;
eq.notNil.if ({
test = false;
(eq.size ==2).if ({
(msg[eq.first] == eq.last).if ({
test = true;
});
});
});
if (test, {
rawValue = msg[index];
min.isNil.if({ min = rawValue});
max.isNil.if({ max = rawValue});
if( rawValue < min, {
min = rawValue;
"% % min % \n".postf(osckey, index, min);
});
if (rawValue > max, {
max = rawValue;
"% % max % \n".postf(osckey, index, max);
});
//});
if (type == \relative, {
//value = rawValue / scale;
value = rawValue - min;
(max != min).if({
value = value / (max - min);
});
}, { value = rawValue});
calibrate.not.if({
this.action.notNil.if({this.action.value(this);});
busAction.notNil.if({busAction.value(this);});
});
})
}).add;
}
createBus{ |s|
/*@
desc: Create a control bus on the specified server, and map the incoming values to this bus.
@*/
s = s ? Server.default;
if ( bus.isNil, {
bus = Bus.control( s, 1 );
},{
if ( bus.index.isNil, {
bus = Bus.control( s, 1 );
});
});
/* if ( s.serverRunning.not and: s.isLocal, {
"Server seems not running, so bus will be invalid".warn;
});*/
busAction = { |v| bus.set( v.value ); };
}
freeBus{
/*@
desc: Free the bus on the server
@*/
busAction = {};
bus.free;
}
// JITLib support
kr{
/*@
desc: JitLib support
@*/
this.createBus;
^In.kr( bus );
}
}
WiiRamp {
var <>upslope, <>downslope, <>hold;
var rout, max, min, direction, level, slope;
*new { |upslope =15, downslope = 150, hold = 10|
^super.new.init(upslope, downslope, hold);
}
init { arg up, down, top;
upslope = up;
downslope = down;
hold = top;
rout = Routine { arg inVal;
var counter;
level = inVal;
slope = 0;
max = inVal;
min = inVal;
counter = hold;
direction = 0;
//switch = false;
inVal = inVal.yield;
true.while({
if ((inVal > level) , { // current position is below input
direction = 1; // so go up
counter = hold; // reset counter
if ((inVal > max), { // this is a new target
max = inVal;
slope = (max - level) / upslope; // compute slope
})
});
if (level < max, { direction = 1; });
if ((direction == 1), { // go up
level = level + slope;
if (((level >= max) ||
(slope < 0.0000000000001)), {
direction = 0;
max = 0;
});
}, {
if (direction == 0, { // hold at the top
//"hold".postln;
counter = counter -1;
if ( counter == 0, { // until the counter runs out
direction = -1;
counter = hold;
})
} , {
if (( inVal < level), { // if not holding or going up, check if
// we need to go down
min = inVal;
slope = (level - min) / downslope;
level = level - slope;
});
});
});
inVal = level.yield;
});
};
}
next { |inval|
^rout.next(inval);
}
}
// Ok, here is the documentation!
/*
// First, you create a new instance of WiiOSCClient,
// which starts in calibration mode
w = WiiOSCClient.new;
// If you have not already done so, open up DarwiinRemote OSC and get it talking to your wii.
// Then go to preferences of that application and set the OSC port to the language port
// Of SuperCollider. You will see a message in the post window telling you what port
// that is .... or you will see a lot of min and max messages, which lets you know it's
// already callibrating
// move your wiimote about as if you were playing it. It will scale it's output accordingly
// now that you're done callibrating, turn callibration mode off
w.calibrate = false;
// The WiiOSCClient is set up to behave very much like a HID client and is furthermore
// designed for drop-in-place compatibility if anybody ever sorts out the WiiMote code
// that SuperCollider pretends to support.
// To get at a particular aspect of the data, you set an action per slot:
w.setAction(\ax, {|val|
val.value; // is the scaled data from \ax - the X axis of the accelerometre.
// It should be between 0-1, scaled according to how you waved your arms during
// the callibration period
});
// You can use a WiiRamp to provide some lag
(
r = WiiRamp (20, 200, 15);
w.setAction(\ax, {|val|
var scaled, lagged;
scaled = ((val.value * 2) - 1).abs;
lagged = r.next(scaled);
// now do somehting with lagged
});
)
*/