Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Common / Streams / ControlPatterns.sc
blobc17664917ac51d9ad7b2d2ff7b2f07b5743c5ec9
1 //human device interface pattern. pulls values from devices like gamepads etc.
3 Phid : Pattern {
4         var <>element, <>locID, <>repeats;
6         //element can be key or index
7         *new { arg element, locID=0, repeats=inf;
8                 ^super.newCopyArgs(element, locID, repeats)
9         }
10         storeArgs { ^[element,locID,repeats] }
11         embedInStream { arg event;
12                 var all, device, spec, elements, deviceName, min, max;
14                 all = HIDDeviceService.devices;
15                 if(all.isNil, {
16                         HIDDeviceService.buildDeviceList;
17                         all = HIDDeviceService.devices;
18                 });
19                 device = all.at(locID);
20                 if(device.isNil, { "device not found".error; ^nil });
21                 if(element.isNumber.not, {
22                         element = HIDDeviceService.keyToIndex(element, locID);
23                         if(element.isNil, { "key not found".error; ^nil });
24                 });
25                 elements = device.elements;
26                 min = elements.at(element).min;
27                 max = elements.at(element).max;
28                 spec = ControlSpec.new(min, max, 'lin', 1);
29                 if((min === 0) and: {max === 1}, {
30                                 repeats.value(event).do({
31                                         event = device.value(element).yield
32                                 });
33                                 ^event
34                 }, {
35                                 repeats.value(event).do({
36                                         event = spec.unmap(device.value(element)).yield
37                                 });
38                                 ^event;
39                 })
40         }