Debian Codegraph @

examples/book/digital-artists/chapter10/Listing10.2.ck

raw
1 // Listing 10.2 Keyboard organ controlled by Hid event
2 // Hid object
3 Hid hi; // (1) Makes a new Hid object...
4
5 // message to convey data from Hid device
6 HidMsg msg; // (2) ...and Hid message holder.
7
8 // device number: which keyboard to open
9 0 => int device; // (3) Keyboard device number.
10
11 // open keyboard; or exit if fail to open
12 if( !hi.openKeyboard( device ) ) me.exit(); // (4) Opens it, exits if failed.
13
14 // print a message! // (5) Prints message if success.
15 <<< "keyboard '" + hi.name() + "' ready", "" >>>;
16
17 // sound chain for Hid keyboard controlled organ
18 BeeThree organ => JCRev r => dac; // (6) Organ UGen through reverb to dac.
19
20 // infinite event loop
21 while( true )
22 {
23 // wait for event
24 hi => now; // (7) Waits for keyboard event.
25 // get message
26 while( hi.recv( msg ) ) // (8) loops over all messages (keys pressed).
27 {
28 // button (key) down, play a Note
29 if( msg.isButtonDown() )
30 { // (9) If keyDown, set frequency from keycode...
31 // take ascii value of button, convert to freq
32 Std.mtof( msg.ascii ) => organ.freq;
33 // sound the note
34 1 => organ.noteOn; // (10) ... and play a note.
35 }
36 else // button up, noteOff
37 {
38 // deactivate the note
39 0 => organ.noteOff; // (11) End the note on keyUp.
40 }
41 }
42 }