Debian Codegraph @

examples/hid/joy-fm.ck

raw
1 //-----------------------------------------------------------------------------
2 // name: joy-fm.ck
3 // desc: uses first 3 axes of joystick to control mf, cf, index
4 // for FM Synthesis
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: Spencer Salazar
14 //-----------------------------------------------------------------------------
15
16 // which joystick
17 0 => int device;
18 // get from command line
19 if( me.args() ) me.arg(0) => Std.atoi => device;
20
21 // modulator to carrier
22 SinOsc m => SinOsc c => Envelope e => dac;
23
24 // carrier frequency
25 220 => c.freq;
26 // modulator frequency
27 550 => m.freq;
28 // index of modulation
29 1000 => m.gain;
30
31 // phase modulation is FM synthesis (sync is 2)
32 2 => c.sync;
33
34 // attack
35 10::ms => e.duration;
36 .5 => e.gain;
37 // variables
38 int base;
39 float a0;
40 float a1;
41 float a2;
42 int count;
43
44 // start things
45 set( base, a0, a1, a2 );
46
47 // HID input and HID message
48 Hid hi;
49 HidMsg msg;
50
51 // try
52 if( !hi.openJoystick( device ) ) me.exit();
53 <<< "joystick '" + hi.name() + "' ready...", "" >>>;
54
55 // infinite time loop
56 while( true )
57 {
58 // wait on event
59 hi => now;
60 // loop over messages
61 while( hi.recv( msg ) )
62 {
63 if( msg.isAxisMotion() )
64 {
65 if( msg.which == 0 ) msg.axisPosition => a0;
66 else if( msg.which == 1 ) msg.axisPosition => a1;
67 else if( msg.which == 2 ) msg.axisPosition => a2;
68 set( base, a0, a1, a2 );
69 }
70
71 else if( msg.isButtonDown() )
72 {
73 msg.which => base;
74 count++;
75 if( count == 1 ) e.keyOn();
76 set( base, a0, a1, a2 );
77 }
78
79 else if( msg.isButtonUp() )
80 {
81 msg.which => base;
82 count--;
83 if( !count ) e.keyOff();
84 }
85 }
86 }
87
88 // mapping function
89 fun void set( int base, float v0, float v1, float v2 )
90 {
91 // modulator frequency
92 ( 500 + 5*base + ( 500 * v0) ) => m.freq;
93 // carrier frequency
94 ( 220 + (220 * v2) ) => c.freq;
95 // index of modulation
96 ( 1000 * (v1+1) ) => m.gain;
97 <<< "carrier:", c.freq(), "modulator:", m.freq(), "index:", m.gain() >>>;
98 }