Debian Codegraph @

examples/hid/joy-noise.ck

raw
1
2 //-----------------------------------------------------------------------------
3 // name: joy-noise.ck
4 // desc: using joystick to control noise (first 3 axes + buttons)
5 //
6 // note: select between joysticks by specifying device number;
7 // to see a list of devices and their numbers, either...
8 // 1) view the Device Browser window in miniAudicle (select
9 // "Human Interface Devices" in the drop-down menu)
10 // OR 2) from the command line:
11 // > chuck --probe
12 //
13 // author: Ge Wang (https://ccrma.stanford.edu/~ge/)
14 //-----------------------------------------------------------------------------
15
16 // HID input and HID message
17 Hid hi;
18 HidMsg msg;
19
20 // which joystick
21 0 => int device;
22 // get from command line
23 if( me.args() ) me.arg(0) => Std.atoi => device;
24
25 // open joystick
26 if( !hi.openJoystick( device ) ) me.exit();
27 <<< "joystick '" + hi.name() + "' ready...", "" >>>;
28
29 // patch
30 Noise n => BiQuad f => Envelope e => Pan2 p => dac;
31 e => Echo echo => p;
32 e => Echo echo2 => p;
33 e => Echo echo3 => p;
34
35 // set biquad pole radius
36 .99 => f.prad;
37 // set biquad gain
38 .05 => f.gain;
39 // set equal zeros
40 1 => f.eqzs;
41 // our float
42 0.0 => float t;
43 // set echo
44 echo.delay() * 2 => echo2.max => echo2.delay;
45 echo.delay() * 3 => echo3.max => echo3.delay;
46 echo.gain( .75 );
47 echo.gain() * .25 => echo2.gain;
48 echo2.gain() * .25 => echo3.gain;
49 // set gain
50 .5 => n.gain;
51 // attack
52 10::ms => e.duration;
53
54 // variables
55 0 => float value;
56 1 => float factor;
57 0 => float q;
58 0 => int count;
59
60 // initialize
61 set( value, factor, q );
62
63 // infinite time loop
64 while( true )
65 {
66 // wait on event
67 hi => now;
68 // recv message
69 while( hi.recv( msg ) )
70 {
71 // axis 0: map to pan
72 if( msg.isAxisMotion() && msg.which == 0 )
73 { msg.axisPosition => p.pan; }
74
75 // axis 1: map to pfreq
76 if( msg.isAxisMotion() && msg.which == 1 )
77 { -msg.axisPosition => value; set( value, factor, q ); }
78
79 // axis 2: map to Q
80 if( msg.isAxisMotion() && msg.which == 2 )
81 { msg.axisPosition => q; set( value, factor, q ); }
82
83 // button down (any)
84 if( msg.isButtonDown() )
85 { count++; if( count ) e.keyOn(); msg.which + 1 => factor; set( value, factor, q ); }
86
87 // button up (any)
88 if( msg.isButtonUp() )
89 { count--; if( !count ) e.keyOff(); }
90 }
91 }
92
93 // do actual mapping control
94 fun void set( float value, float factor, float q )
95 {
96 (value * 440 + 480) * factor => f.pfreq;
97
98 // hacked q mapping
99 if( q >= 0 )
100 { .99+.009*(Math.pow(2,q)-1) => f.prad; .25+.5*(1-q) => e.gain; }
101 else
102 { .99-.09*(Math.pow(2,-q)-1) => f.prad; 1 => e.gain; }
103
104 if( count )
105 <<< "pan:", p.pan(), "resonance:", f.pfreq(), "radius:", f.prad() >>>;
106 }