| 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; |
| 5 |
|
| 6 |
// MIDI Port (Window > Device Browser > MIDI > Output) |
| 7 |
0 => int port; |
| 8 |
|
| 9 |
// try to open that port, fail gracefully |
| 10 |
if( !mout.open(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; |
| 18 |
|
| 19 |
// utility function to send MIDI out notes |
| 20 |
fun void MIDInote(int onoff, int note, int velocity) |
| 21 |
{ |
| 22 |
if (onoff == 0) 128 => msg.data1; |
| 23 |
else 144 => msg.data1; |
| 24 |
note => msg.data2; |
| 25 |
velocity => msg.data3; |
| 26 |
mout.send(msg); |
| 27 |
} |
| 28 |
|
| 29 |
// declare and initialize array of MIDI notes |
| 30 |
[57,57,64,64,66,66,64,62,62,61,61,59,59,57] @=> int myNotes[]; |
| 31 |
|
| 32 |
// quarter note and half note durations |
| 33 |
0.3 :: second => dur q; |
| 34 |
0.8 :: second => dur h; |
| 35 |
[ q, q, q, q, q, q, h, q, q, q, q, q, q, h] @=> dur myDurs[]; |
| 36 |
|
| 37 |
// loop for length of array |
| 38 |
for (0 => int i; i < myNotes.cap(); i++) |
| 39 |
{ |
| 40 |
MIDInote(1, myNotes[i], 100); |
| 41 |
myDurs[i] => now; |
| 42 |
MIDInote(0, myNotes[i], 100); |
| 43 |
0.2 :: second => now; |
| 44 |
} |