| 1 |
//----------------------------------------------------------------------------- |
| 2 |
// name: midiout.ck |
| 3 |
// desc: example of sending MIDI messages |
| 4 |
// note: for a good explanation of how MIDI messages work, see after the code |
| 5 |
// also see midiout2.ck for sending by common channel voice message |
| 6 |
//----------------------------------------------------------------------------- |
| 7 |
|
| 8 |
// instantiate a MIDI out object |
| 9 |
MidiOut mout; |
| 10 |
// a message to work with |
| 11 |
MidiMsg msg; |
| 12 |
// open a MIDI device for output |
| 13 |
if( !mout.open(0) ) me.exit(); |
| 14 |
|
| 15 |
<<< "MIDI output device opened...", "" >>>; |
| 16 |
|
| 17 |
// loop |
| 18 |
while( true ) |
| 19 |
{ |
| 20 |
// MIDI note on message |
| 21 |
// 0x90 + channel (0 in this case) |
| 22 |
0x90 => msg.data1; |
| 23 |
// pitch |
| 24 |
60 => msg.data2; |
| 25 |
// velocity |
| 26 |
127 => msg.data3; |
| 27 |
// print |
| 28 |
<<< "sending NOTE ON message...", "" >>>; |
| 29 |
// send MIDI message |
| 30 |
mout.send( msg ); |
| 31 |
|
| 32 |
// let time pass |
| 33 |
1::second => now; |
| 34 |
|
| 35 |
// MIDI note off message |
| 36 |
// 0x80 + channel (0 in this case) |
| 37 |
0x80 => msg.data1; |
| 38 |
// pitch |
| 39 |
60 => msg.data2; |
| 40 |
// velocity |
| 41 |
0 => msg.data3; |
| 42 |
// print |
| 43 |
<<< "sending NOTE OFF message...", "" >>>; |
| 44 |
// send MIDI message |
| 45 |
mout.send( msg ); |
| 46 |
|
| 47 |
// let time pass |
| 48 |
1::second => now; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
//----------------------------------------------------------------------------- |
| 53 |
// MIDI Messages (how they work) |
| 54 |
// thanks to Dave Marshall for this excellent and clear explanation |
| 55 |
// https://users.cs.cf.ac.uk/dave/Multimedia/node158.html |
| 56 |
//----------------------------------------------------------------------------- |
| 57 |
// MIDI messages are used by MIDI devices to communicate with each other. |
| 58 |
// |
| 59 |
// Structure of MIDI messages: |
| 60 |
// MIDI message includes a status byte and up to two data bytes. |
| 61 |
// |
| 62 |
// Status byte |
| 63 |
// The most significant bit of status byte is set to 1. |
| 64 |
// The 4 low-order bits identify which channel it belongs to (four bits produce 16 possible channels). |
| 65 |
// The 3 remaining bits identify the message. |
| 66 |
// The most significant bit of data byte is set to 0. |
| 67 |
// |
| 68 |
// Classification of MIDI messages: |
| 69 |
// ----- voice messages |
| 70 |
// ---- channel messages -----| |
| 71 |
// | ----- mode messages |
| 72 |
// | |
| 73 |
// MIDI messages ----| |
| 74 |
// | ---- common messages |
| 75 |
// ----- system messages -----|---- real-time messages |
| 76 |
// ---- exclusive messages |
| 77 |
// A. Channel messages: |
| 78 |
// - messages that are transmitted on individual channels rather that globally to all devices in the MIDI network. |
| 79 |
// |
| 80 |
// A.1. Channel voice messages: |
| 81 |
// Instruct the receiving instrument to assign particular sounds to its voice |
| 82 |
// Turn notes on and off |
| 83 |
// Alter the sound of the currently active note or notes |
| 84 |
// |
| 85 |
// Voice Message Status Byte Data Byte1 Data Byte2 |
| 86 |
// ------------- ----------- ----------------- ----------------- |
| 87 |
// Note off 8x Key number Note Off velocity |
| 88 |
// Note on 9x Key number Note on velocity |
| 89 |
// Polyphonic Key Pressure Ax Key number Amount of pressure |
| 90 |
// Control Change Bx Controller number Controller value |
| 91 |
// Program Change Cx Program number None |
| 92 |
// Channel Pressure Dx Pressure value None |
| 93 |
// Pitch Bend Ex MSB LSB |
| 94 |
// Notes: `x' in status byte hex value stands for a channel number. |
| 95 |
// |
| 96 |
// Example: a Note On message is followed by two bytes, one to identify the note, and on to specify the velocity. |
| 97 |
// To play note number 80 with maximum velocity on channel 13, the MIDI device would send these three hexadecimal byte values: 9C 50 7F |
| 98 |
// |
| 99 |
// A.2. Channel mode messages: - Channel mode messages are a special case of the Control Change message ( Bx or 1011nnnn). |
| 100 |
// The difference between a Control message and a Channel Mode message, which share the same status byte value, is in the first data byte. |
| 101 |
// Data byte values 121 through 127 have been reserved in the Control Change message for the channel mode messages. |
| 102 |
// |
| 103 |
// Channel mode messages determine how an instrument will process MIDI voice messages. |
| 104 |
// |
| 105 |
// 1st Data Byte Description Meaning of 2nd Data Byte |
| 106 |
// ------------- ---------------------- ------------------------ |
| 107 |
// 79 Reset all controllers None; set to 0 |
| 108 |
// 7A Local control 0 = off; 127 = on |
| 109 |
// 7B All notes off None; set to 0 |
| 110 |
// 7C Omni mode off None; set to 0 |
| 111 |
// 7D Omni mode on None; set to 0 |
| 112 |
// 7E Mono mode on (Poly mode off) ** |
| 113 |
// 7F Poly mode on (Mono mode off) None; set to 0 |
| 114 |
// ** if value = 0 then the number of channels used is determined by the receiver; all other values set a specific number of channels, beginning with the current basic channel. |
| 115 |
// |
| 116 |
// B. System Messages: |
| 117 |
// System messages carry information that is not channel specific, such as timing signal for synchronization, positioning information in pre-recorded MIDI sequences, and detailed setup information for the destination device. |
| 118 |
// |
| 119 |
// B.1. System real-time messages: |
| 120 |
// messages related to synchronization |
| 121 |
// |
| 122 |
// System Real-Time Message Status Byte |
| 123 |
// ------------------------ ----------- |
| 124 |
// Timing Clock F8 |
| 125 |
// Start Sequence FA |
| 126 |
// Continue Sequence FB |
| 127 |
// Stop Sequence FC |
| 128 |
// Active Sensing FE |
| 129 |
// System Reset FF |
| 130 |
// |
| 131 |
// B.2. System common messages: |
| 132 |
// contain the following unrelated messages |
| 133 |
// |
| 134 |
// System Common Message Status Byte Number of Data Bytes |
| 135 |
// --------------------- ----------- -------------------- |
| 136 |
// MIDI Timing Code F1 1 |
| 137 |
// Song Position Pointer F2 2 |
| 138 |
// Song Select F3 1 |
| 139 |
// Tune Request F6 None |
| 140 |
// |
| 141 |
// B.3. System exclusive message: |
| 142 |
// (a) Messages related to things that cannot be standardized, (b) addition to the original MIDI specification. |
| 143 |
// |
| 144 |
// It is just a stream of bytes, all with their high bits set to 0, bracketed by a pair of system exclusive start and end messages (F0 and F7). |
| 145 |
//----------------------------------------------------------------------------- |