SCDoc: Use proper static string constants instead of comparing string literals.
[supercollider.git] / Help / Control / GeneralHIDSpec.html
blob659658f8d0c39d21121164f627b0fa3a72ce9b37
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.48">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000; min-height: 12.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
14 span.s1 {color: #0000bf}
15 span.s2 {color: #000000}
16 span.s3 {color: #007300}
17 span.s4 {color: #606060}
18 span.s5 {color: #bf0000}
19 span.Apple-tab-span {white-space:pre}
20 </style>
21 </head>
22 <body>
23 <p class="p1">// =====================================================================</p>
24 <p class="p1">// GeneralHIDSpec</p>
25 <p class="p1">// =====================================================================</p>
26 <p class="p2"><br></p>
27 <p class="p3"><span class="s1">GeneralHIDSpec</span> is a class to create access by names to slots of a <span class="s1">GeneralHIDDevice</span>.</p>
28 <p class="p2"><br></p>
29 <p class="p2"><br></p>
30 <p class="p1">// Look for the devices that are attached:</p>
31 <p class="p3"><span class="s1">GeneralHID</span>.buildDeviceList;</p>
32 <p class="p2"><br></p>
33 <p class="p1">// Get the list of devices:</p>
34 <p class="p3">d = <span class="s1">GeneralHID</span>.deviceList;</p>
35 <p class="p2"><br></p>
36 <p class="p1">// Check which devices have been found:</p>
37 <p class="p3"><span class="s1">GeneralHID</span>.postDevices;</p>
38 <p class="p2"><br></p>
39 <p class="p1">// Pick the 6th device and open it and create an instance of it:</p>
40 <p class="p3">a = <span class="s1">GeneralHID</span>.open( d[5] )</p>
41 <p class="p2"><br></p>
42 <p class="p1">// Get info on the device to see if it is the right one:</p>
43 <p class="p3">a.info;</p>
44 <p class="p2"><br></p>
45 <p class="p1">// Start eventloop:</p>
46 <p class="p3"><span class="s1">GeneralHID</span>.startEventLoop</p>
47 <p class="p2"><br></p>
48 <p class="p1">// Get the capabilities of the device in a readable format:</p>
49 <p class="p3">a.caps;</p>
50 <p class="p2"><br></p>
51 <p class="p1">// See if data is coming in:</p>
52 <p class="p3">a.debug_( <span class="s1">true</span> );</p>
53 <p class="p2"><br></p>
54 <p class="p1">// make an instance of a spec:</p>
55 <p class="p3">b = <span class="s1">GeneralHIDSpec</span>.new( a );</p>
56 <p class="p2"><br></p>
57 <p class="p1"><span class="s2">( </span>// defining the spec.</p>
58 <p class="p1">// trick: look at the debug output, by using each input thing on the gamepad and use the first two numbers posted as indexes</p>
59 <p class="p1">// example output of debug: [ 3, 2, 0.49803921568627, nil ]</p>
60 <p class="p1">// as a result of moving the y-axis of the right joystick.</p>
61 <p class="p1">// so we create this slot:</p>
62 <p class="p3">b.add( <span class="s3">\ry</span>, [3,2] );</p>
63 <p class="p2"><br></p>
64 <p class="p1">// and so on...</p>
65 <p class="p3">b.add( <span class="s3">\lx</span>, [3,0] );</p>
66 <p class="p3">b.add( <span class="s3">\ly</span>, [3,1] );</p>
67 <p class="p3">b.add( <span class="s3">\rx</span>, [3,5] );</p>
68 <p class="p2"><br></p>
69 <p class="p3">b.add( <span class="s3">\b1</span>, [1,288] );</p>
70 <p class="p3">b.add( <span class="s3">\b2</span>, [1,289] );</p>
71 <p class="p3">b.add( <span class="s3">\b3</span>, [1,290] );</p>
72 <p class="p3">b.add( <span class="s3">\b4</span>, [1,291] );</p>
73 <p class="p3">b.add( <span class="s3">\b5</span>, [1,292] );</p>
74 <p class="p3">b.add( <span class="s3">\b6</span>, [1,293] );</p>
75 <p class="p3">b.add( <span class="s3">\b7</span>, [1,294] );</p>
76 <p class="p3">b.add( <span class="s3">\b8</span>, [1,295] );</p>
77 <p class="p2"><br></p>
78 <p class="p3">b.add( <span class="s3">\b5</span>, [1,292] );</p>
79 <p class="p3">b.add( <span class="s3">\b6</span>, [1,293] );</p>
80 <p class="p3">b.add( <span class="s3">\b7</span>, [1,294] );</p>
81 <p class="p3">b.add( <span class="s3">\b8</span>, [1,295] );</p>
82 <p class="p2"><br></p>
83 <p class="p3">b.add( <span class="s3">\cx</span>, [3,16] );</p>
84 <p class="p3">b.add( <span class="s3">\cy</span>, [3,17] );</p>
85 <p class="p2"><br></p>
86 <p class="p3">b.add( <span class="s3">\bl</span>, [1,296] );</p>
87 <p class="p3">b.add( <span class="s3">\br</span>, [1,297] );</p>
88 <p class="p2"><br></p>
89 <p class="p3">b.add( <span class="s3">\bj1</span>, [1,298] );</p>
90 <p class="p3">b.add( <span class="s3">\bj2</span>, [1,299] );</p>
91 <p class="p3">)</p>
92 <p class="p2"><br></p>
93 <p class="p1">// Stop it:</p>
94 <p class="p3">a.debug_( <span class="s1">false</span> );</p>
95 <p class="p2"><br></p>
96 <p class="p1">// look at the map</p>
97 <p class="p3">b.map.postcs;</p>
98 <p class="p2"><br></p>
99 <p class="p1">// store it with a name:</p>
100 <p class="p3">b.save( <span class="s4">"Impact"</span> );</p>
101 <p class="p2"><br></p>
102 <p class="p1">// find matching, previously stored specs:</p>
103 <p class="p3">c = b.find;</p>
104 <p class="p2"><br></p>
105 <p class="p1">// load from file:</p>
106 <p class="p3">b.fromFile( <span class="s4">"Impact"</span> );</p>
107 <p class="p2"><br></p>
108 <p class="p1">// the loading and storing mechanism works across SC sessions, as they are stored to file, and GeneralHIDSpec loads the device info of all stored specs at startup of the language.<span class="Apple-converted-space"> </span></p>
109 <p class="p2"><br></p>
110 <p class="p1">/// A GeneralHIDDevice automatically has a spec, so we can access it even faster:</p>
111 <p class="p2"><br></p>
112 <p class="p3">a.spec;</p>
113 <p class="p2"><br></p>
114 <p class="p1">// there are shortcuts for the methods:</p>
115 <p class="p1"><span class="s2">add( key, slotids ) </span>// define a key to a slot</p>
116 <p class="p1"><span class="s2">at( key ) </span>// get the slot</p>
117 <p class="p1"><span class="s2">value( key ) </span>// get the value</p>
118 <p class="p3">value_( key, value ) <span class="s5">// set the value<span class="Apple-converted-space"> </span></span></p>
119 <p class="p3">action_( key, action ) <span class="s5">// set the action</span></p>
120 <p class="p1"><span class="s2">bus( key ) </span>// get the bus</p>
121 <p class="p3">createBus( key, server ) <span class="s5">// create the bus</span></p>
122 <p class="p3">freeBus( key ) <span class="s5">// free the bus</span></p>
123 <p class="p1"><span class="s2">createAllBuses( server ) </span>// create buses for all defined slots</p>
124 <p class="p1"><span class="s2">freeAllBuses </span>// free all buses</p>
125 <p class="p2"><br></p>
126 <p class="p1">// to find and set a spec:</p>
127 <p class="p3">a.findSpec;</p>
128 <p class="p3">a.setSpec( <span class="s4">"Impact"</span> );</p>
129 <p class="p2"><br></p>
130 <p class="p1">// post the mapping</p>
131 <p class="p3">a.spec.map</p>
132 <p class="p2"><br></p>
133 <p class="p1">// examples of use:</p>
134 <p class="p3">s = <span class="s1">Server</span>.local.boot;</p>
135 <p class="p2"><br></p>
136 <p class="p1">// create a bus</p>
137 <p class="p3">b.createBus( <span class="s3">\rx</span>, s );</p>
138 <p class="p3">b.createBus( <span class="s3">\b1</span>, s );</p>
139 <p class="p2"><br></p>
140 <p class="p2"><br></p>
141 <p class="p1">// simple example:</p>
142 <p class="p3">(</p>
143 <p class="p4">SynthDef<span class="s2">( </span><span class="s3">\hidbus_help</span><span class="s2">, { </span>|out=0,amp=0.5|</p>
144 <p class="p3"><span class="Apple-tab-span"> </span><span class="s1">Out</span>.ar( out, <span class="s1">SinOsc</span>.ar( 300, 0, 0.2*(amp-0.5) ) );</p>
145 <p class="p3">}).load( s );</p>
146 <p class="p3">)</p>
147 <p class="p2"><br></p>
148 <p class="p3">x = <span class="s1">Synth</span>.new( <span class="s3">\hidbus_help</span> );</p>
149 <p class="p3">x.map( <span class="s3">\amp</span>, b.at( <span class="s3">\rx</span> ).bus );</p>
150 <p class="p3">x.free;</p>
151 <p class="p2"><br></p>
152 <p class="p1"><span class="s2">( </span>// a nicer version:</p>
153 <p class="p4">SynthDef<span class="s2">( </span><span class="s3">\hidbus_help</span><span class="s2">, { </span>|out=0,amp=0.5,amp2=0|</p>
154 <p class="p3"><span class="s1">Out</span>.ar( out, <span class="s1">SinOsc</span>.ar( 300, 0, 0.2*(amp-0.5).lag( 0.1 ) * amp2.lag(0.01,0.99)</p>
155 <p class="p3">) );</p>
156 <p class="p3">}).load( s );</p>
157 <p class="p3">)</p>
158 <p class="p2"><br></p>
159 <p class="p3">x = <span class="s1">Synth</span>.new( <span class="s3">\hidbus_help</span> );</p>
160 <p class="p3">x.map( <span class="s3">\amp</span>, b.at( <span class="s3">\rx</span> ).bus );</p>
161 <p class="p3">x.map( <span class="s3">\amp2</span>, b.at( <span class="s3">\b1</span> ).bus );</p>
162 <p class="p3">x.free;</p>
163 <p class="p2"><br></p>
164 <p class="p1"><span class="s2">( </span>// an even nicer version:</p>
165 <p class="p4">SynthDef<span class="s2">( </span><span class="s3">\hidbus_help</span><span class="s2">, { </span>|out=0,freqadd=0,amp=0,fmmul=200|</p>
166 <p class="p3"><span class="s1">Out</span>.ar( out, <span class="s1">SinOsc</span>.ar( 300 + (freqadd.lag(0.2,1)*fmmul), 0, 0.2*amp.lag</p>
167 <p class="p3">(0.01,0.99) ) );</p>
168 <p class="p3">}).load( s );</p>
169 <p class="p3">)</p>
170 <p class="p2"><br></p>
171 <p class="p2"><br></p>
172 <p class="p1">// if you want to have buses for all the things defined in the spec, you can use:</p>
173 <p class="p3">b.createAllBuses( s );</p>
174 <p class="p2"><br></p>
175 <p class="p3">(</p>
176 <p class="p3">x = [ <span class="s1">Synth</span>.new( <span class="s3">\hidbus_help</span> ), <span class="s1">Synth</span>.new( <span class="s3">\hidbus_help</span> ) ];</p>
177 <p class="p3">x[0].map( <span class="s3">\freqadd</span>, b.bus( <span class="s3">\ly</span> ) );</p>
178 <p class="p3">x[0].map( <span class="s3">\amp</span>, b.bus( <span class="s3">\b6</span> ) );</p>
179 <p class="p2"><br></p>
180 <p class="p3">x[1].map( <span class="s3">\freqadd</span>, b.bus( <span class="s3">\lx</span> ) );</p>
181 <p class="p3">x[1].map( <span class="s3">\amp</span>, b.bus( <span class="s3">\b7</span> ) );</p>
182 <p class="p2"><br></p>
183 <p class="p3">y = [ <span class="s1">Synth</span>.new( <span class="s3">\hidbus_help</span>, [<span class="s3">\fmmul</span>,400] ), <span class="s1">Synth</span>.new( <span class="s3">\hidbus_help</span>, [<span class="s3">\fmmul</span>,400] ) ];</p>
184 <p class="p3">y[0].map( <span class="s3">\freqadd</span>, b.bus( <span class="s3">\ry</span> ) );</p>
185 <p class="p3">y[0].map( <span class="s3">\amp</span>, b.bus( <span class="s3">\b5</span> ) );</p>
186 <p class="p2"><br></p>
187 <p class="p3">y[1].map( <span class="s3">\freqadd</span>, b.bus( <span class="s3">\rx</span> ) );</p>
188 <p class="p3">y[1].map( <span class="s3">\amp</span>, b.bus( <span class="s3">\b6</span> ) );</p>
189 <p class="p3">)</p>
190 <p class="p2"><br></p>
191 <p class="p1">// see what's going on on the server</p>
192 <p class="p3">s.queryAllNodes( <span class="s1">true</span> );</p>
193 <p class="p2"><br></p>
194 <p class="p1">// free the synths</p>
195 <p class="p3">y.do{ <span class="s1">|it|</span> it.free; }; x.do{ <span class="s1">|it|</span> it.free; };<span class="Apple-converted-space"> </span></p>
196 <p class="p2"><br></p>
197 <p class="p2"><br></p>
198 <p class="p2"><br></p>
199 <p class="p1">// To free all buses</p>
200 <p class="p3">b.freeAllBuses;</p>
201 <p class="p2"><br></p>
202 <p class="p1">// Close the device after use:</p>
203 <p class="p2"><br></p>
204 <p class="p3">a.close;</p>
205 <p class="p2"><br></p>
206 <p class="p3"><span class="s1">GeneralHID</span>.stopEventLoop</p>
207 <p class="p2"><br></p>
208 </body>
209 </html>