Debian Codegraph @

examples/book/digital-artists/chapter11/Listing11.2.ck

raw
1 // Listing 11.2 Simple piano synthesizer controlled by MIDI input
2
3 // Setup MIDI input, set a port number, try to open it
4 MidiIn min; // (1) Makes a MidiIn object
5
6 // MIDI Port (ChucK Menu: Window > Device Browser > MIDI > Input)
7 0 => int port;
8
9 // open the port, fail gracefully
10 if( !min.open(port) ) // (2) Tries to open it on port, handles failure
11 {
12 <<< "Error: MIDI port did not open on port: ", port >>>;
13 me.exit();
14 }
15
16 // holder for received messages
17 MidiMsg msg; // (3) Makes object to hold MIDI messages
18
19 // make an instrument to play
20 Rhodey piano => dac; // (4) Rhodey piano to play with MIDI controller
21
22 // loop
23 while( true ) // (5) Infinite loop
24 {
25 min => now; // (6) Sleeps until a MIDI input message comes
26 // advance when receive MIDI msg
27 while( min.recv(msg) )
28 {
29 <<< msg.data1, msg.data2, msg.data3 >>>;
30 if (msg.data1 == 144) {
31 Std.mtof(msg.data2) => piano.freq;
32 msg.data3/127.0 => piano.gain;
33 1 => piano.noteOn;
34 }
35 else {
36 1 => piano.noteOff;
37 }
38 }
39 }