Debian Codegraph @

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

raw
1 // Listing 11.1 MIDI output note on/off boilerplate
2
3 // MIDI out setup, make a MidiOut object, open it on a device
4 MidiOut mout; // (1) Makes a new MidiOut
5
6 // MIDI Port (Window > Device Browser > MIDI > Output)
7 0 => int port; // (2) MIDI port number (varies)
8
9 // try to open that port, fail gracefully
10 if( !mout.open(port) ) // (3) Tries to open the MIDI port
11 {
12 <<< "Error: MIDI port did not open on port: ", port >>>;
13 me.exit();
14 }
15
16 // Make a MIDI msg holder for sending
17 MidiMsg msg; // (4) Makes a MIDI message holder
18
19 // utility function to send MIDI out notes
20 fun void MIDInote(int onoff, int note, int velocity)
21 { // (5) Function to send MIDI noteOn/Off
22 if (onoff == 0) 128 => msg.data1; // (6) If noteOff, set status byte to 128...
23 else 144 => msg.data1; // (7) ...else set status byte to 144.
24 note => msg.data2;
25 velocity => msg.data3;
26 mout.send(msg);
27 }
28
29 // loop
30 while( true )
31 {
32 Math.random2(60,100) => int note;
33 Math.random2(30,127) => int velocity;
34 MIDInote(1, note, velocity); // (8) Test noteOn on random note and velocity...
35 .1::second => now;
36 MIDInote(0, note ,velocity); // (9) ...and test noteOff.
37 .1::second => now;
38 }