Debian Codegraph @

examples/hid/mouse-fm.ck

raw
1 //-----------------------------------------------------------------------------
2 // name: mouse-fm.ck
3 // desc: uses first X/Y axes of a mouse to control modulation frequency
4 // and index of modulation for FM Synthesis
5 // ALSO SEE mouse-cursor.ck for mouse cursor tracking...
6 // (MouseCursor is both absolute and scaled and is a different
7 // and simpler mechanism for mouse position input)
8 //
9 // note: select between mice/trackpads by specifying device number;
10 // to see a list of devices and their numbers, either...
11 // 1) view the Device Browser window in miniAudicle (select
12 // "Human Interface Devices" in the drop-down menu)
13 // OR 2) from the command line:
14 // > chuck --probe
15 //
16 // author: Spencer Salazar
17 //-----------------------------------------------------------------------------
18
19 // which mouse
20 0 => int device;
21 // get from command line
22 if( me.args() ) me.arg(0) => Std.atoi => device;
23
24 // modulator to carrier
25 SinOsc m => SinOsc c => Envelope e => dac;
26
27 // carrier frequency
28 220 => c.freq;
29 // modulator frequency
30 550 => m.freq;
31 // index of modulation
32 1000 => m.gain;
33
34 // phase modulation is FM synthesis (sync is 2)
35 2 => c.sync;
36
37 // attack
38 10::ms => e.duration;
39 .5 => e.gain;
40 // variables
41 int base;
42 float a0;
43 float a1;
44 float a2;
45 int count;
46
47 // start things
48 set( base, a0, a1, a2 );
49
50 // HID input and HID message
51 Hid hi;
52 HidMsg msg;
53
54 // try
55 if( !hi.openMouse( device ) ) me.exit();
56 <<< "mouse '" + hi.name() + "' ready...", "" >>>;
57
58 // infinite time loop
59 while( true )
60 {
61 // wait on event
62 hi => now;
63 // loop over messages
64 while( hi.recv( msg ) )
65 {
66 if( msg.isMouseMotion() )
67 {
68 msg.deltaX * .001 + a0 => a0;
69 //else if( msg.which == 1 ) msg.fdata => a1;
70 msg.deltaY * .001 + a1 => a1;
71 set( base, a0, a1, a2 );
72 }
73
74 else if( msg.isButtonDown() )
75 {
76 msg.which => base;
77 count++;
78 if( count == 1 ) e.keyOn();
79 set( base, a0, a1, a2 );
80 }
81
82 else if( msg.isButtonUp() )
83 {
84 msg.which => base;
85 count--;
86 if( !count ) e.keyOff();
87 }
88 }
89 }
90
91 // mapping function
92 fun void set( int base, float v0, float v1, float v2 )
93 {
94 // modulator frequency
95 ( 500 + 5*base + ( 500 * v0) ) => m.freq;
96 // carrier frequency
97 ( 220 + (220 * v2) ) => c.freq;
98 // index of modulation
99 ( 1000 * (v1+1) ) => m.gain;
100 <<< "carrier:", c.freq(), "modulator:", m.freq(), "index:", m.gain() >>>;
101 }