A library to send and receive MIDI messages over USB using the default USB-MIDI drivers on Win/Mac
Fork of USBMIDI by
MIDIMessage.h@2:10d694d6ccdc, 2011-02-20 (annotated)
- Committer:
- simon
- Date:
- Sun Feb 20 13:12:22 2011 +0000
- Revision:
- 2:10d694d6ccdc
- Parent:
- 1:ff74eabe02cd
Fixed up documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 2:10d694d6ccdc | 1 | /* @license The MIT License |
simon | 1:ff74eabe02cd | 2 | * Copyright (c) 2011 mux, simon |
simon | 0:56b095524cf2 | 3 | * |
simon | 1:ff74eabe02cd | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
simon | 1:ff74eabe02cd | 5 | * of this software and associated documentation files (the "Software"), to deal |
simon | 1:ff74eabe02cd | 6 | * in the Software without restriction, including without limitation the rights |
simon | 1:ff74eabe02cd | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
simon | 1:ff74eabe02cd | 8 | * copies of the Software, and to permit persons to whom the Software is |
simon | 1:ff74eabe02cd | 9 | * furnished to do so, subject to the following conditions: |
simon | 1:ff74eabe02cd | 10 | * |
simon | 1:ff74eabe02cd | 11 | * The above copyright notice and this permission notice shall be included in |
simon | 1:ff74eabe02cd | 12 | * all copies or substantial portions of the Software. |
simon | 1:ff74eabe02cd | 13 | * |
simon | 1:ff74eabe02cd | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
simon | 1:ff74eabe02cd | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
simon | 1:ff74eabe02cd | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
simon | 1:ff74eabe02cd | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
simon | 1:ff74eabe02cd | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
simon | 1:ff74eabe02cd | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
simon | 1:ff74eabe02cd | 20 | * THE SOFTWARE. |
simon | 0:56b095524cf2 | 21 | */ |
simon | 0:56b095524cf2 | 22 | |
simon | 0:56b095524cf2 | 23 | #ifndef MBED_MIDIMESSAGE_H |
simon | 0:56b095524cf2 | 24 | #define MBED_MIDIMESSAGE_H |
simon | 0:56b095524cf2 | 25 | |
simon | 1:ff74eabe02cd | 26 | #include "mbed.h" |
simon | 1:ff74eabe02cd | 27 | |
simon | 1:ff74eabe02cd | 28 | // MIDI Message Format |
simon | 1:ff74eabe02cd | 29 | // |
simon | 1:ff74eabe02cd | 30 | // [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ] |
simon | 1:ff74eabe02cd | 31 | // |
simon | 1:ff74eabe02cd | 32 | // MIDI Data Messages (Channel Specific) |
simon | 1:ff74eabe02cd | 33 | // |
simon | 1:ff74eabe02cd | 34 | // Message msg n m |
simon | 1:ff74eabe02cd | 35 | // --------------------------------------------- |
simon | 1:ff74eabe02cd | 36 | // Note Off 0x8 Key Velocity |
simon | 1:ff74eabe02cd | 37 | // Note On 0x9 Key Velocity |
simon | 1:ff74eabe02cd | 38 | // Polyphonic Aftertouch 0xA Key Pressure |
simon | 1:ff74eabe02cd | 39 | // Control Change 0xB Controller Value |
simon | 1:ff74eabe02cd | 40 | // Program Change 0xC Program - |
simon | 1:ff74eabe02cd | 41 | // Channel Aftertouch 0xD Pressure - |
simon | 1:ff74eabe02cd | 42 | // Pitch Wheel 0xE LSB MSB |
simon | 1:ff74eabe02cd | 43 | |
simon | 1:ff74eabe02cd | 44 | #define CABLE_NUM (0<<4) |
simon | 1:ff74eabe02cd | 45 | |
simon | 1:ff74eabe02cd | 46 | /** A MIDI message container */ |
simon | 0:56b095524cf2 | 47 | class MIDIMessage { |
simon | 0:56b095524cf2 | 48 | public: |
simon | 1:ff74eabe02cd | 49 | MIDIMessage() {} |
simon | 1:ff74eabe02cd | 50 | |
simon | 1:ff74eabe02cd | 51 | MIDIMessage(uint8_t *buf) { |
simon | 1:ff74eabe02cd | 52 | *((uint32_t *)data) = *((uint32_t *)buf); |
simon | 1:ff74eabe02cd | 53 | } |
simon | 1:ff74eabe02cd | 54 | |
simon | 1:ff74eabe02cd | 55 | // create messages |
simon | 1:ff74eabe02cd | 56 | |
simon | 1:ff74eabe02cd | 57 | /** Create a NoteOff message |
simon | 1:ff74eabe02cd | 58 | * @param key Key ID |
simon | 1:ff74eabe02cd | 59 | * @param velocity Key velocity (0-127, default = 127) |
simon | 1:ff74eabe02cd | 60 | * @param channel Key channel (0-15, default 0) |
simon | 1:ff74eabe02cd | 61 | * @returns A MIDIMessage |
simon | 1:ff74eabe02cd | 62 | */ |
simon | 1:ff74eabe02cd | 63 | static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) { |
simon | 1:ff74eabe02cd | 64 | MIDIMessage msg; |
simon | 1:ff74eabe02cd | 65 | msg.data[0] = CABLE_NUM | 0x08; |
simon | 1:ff74eabe02cd | 66 | msg.data[1] = 0x80 | (channel & 0x0F); |
simon | 1:ff74eabe02cd | 67 | msg.data[2] = key & 0x7F; |
simon | 1:ff74eabe02cd | 68 | msg.data[3] = velocity & 0x7F; |
simon | 1:ff74eabe02cd | 69 | return msg; |
simon | 1:ff74eabe02cd | 70 | } |
simon | 1:ff74eabe02cd | 71 | |
simon | 1:ff74eabe02cd | 72 | /** Create a NoteOn message |
simon | 1:ff74eabe02cd | 73 | * @param key Key ID |
simon | 1:ff74eabe02cd | 74 | * @param velocity Key velocity (0-127, default = 127) |
simon | 1:ff74eabe02cd | 75 | * @param channel Key channel (0-15, default 0) |
simon | 1:ff74eabe02cd | 76 | * @returns A MIDIMessage |
simon | 1:ff74eabe02cd | 77 | */ |
simon | 1:ff74eabe02cd | 78 | static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) { |
simon | 1:ff74eabe02cd | 79 | MIDIMessage msg; |
simon | 1:ff74eabe02cd | 80 | msg.data[0] = CABLE_NUM | 0x09; |
simon | 1:ff74eabe02cd | 81 | msg.data[1] = 0x90 | (channel & 0x0F); |
simon | 1:ff74eabe02cd | 82 | msg.data[2] = key & 0x7F; |
simon | 1:ff74eabe02cd | 83 | msg.data[3] = velocity & 0x7F; |
simon | 1:ff74eabe02cd | 84 | return msg; |
simon | 1:ff74eabe02cd | 85 | } |
simon | 1:ff74eabe02cd | 86 | |
simon | 1:ff74eabe02cd | 87 | /** Create a PolyPhonic Aftertouch message |
simon | 1:ff74eabe02cd | 88 | * @param key Key ID |
simon | 1:ff74eabe02cd | 89 | * @param pressure Aftertouch pressure (0-127) |
simon | 1:ff74eabe02cd | 90 | * @param channel Key channel (0-15, default 0) |
simon | 1:ff74eabe02cd | 91 | * @returns A MIDIMessage |
simon | 1:ff74eabe02cd | 92 | */ |
simon | 1:ff74eabe02cd | 93 | static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) { |
simon | 1:ff74eabe02cd | 94 | MIDIMessage msg; |
simon | 1:ff74eabe02cd | 95 | msg.data[0] = CABLE_NUM | 0x0A; |
simon | 1:ff74eabe02cd | 96 | msg.data[1] = 0xA0 | (channel & 0x0F); |
simon | 1:ff74eabe02cd | 97 | msg.data[2] = key & 0x7F; |
simon | 1:ff74eabe02cd | 98 | msg.data[3] = pressure & 0x7F; |
simon | 1:ff74eabe02cd | 99 | return msg; |
simon | 1:ff74eabe02cd | 100 | } |
simon | 1:ff74eabe02cd | 101 | |
simon | 1:ff74eabe02cd | 102 | /** Create a Control Change message |
simon | 1:ff74eabe02cd | 103 | * @param control Controller ID |
simon | 1:ff74eabe02cd | 104 | * @param value Controller value (0-127) |
simon | 1:ff74eabe02cd | 105 | * @param channel Controller channel (0-15, default 0) |
simon | 1:ff74eabe02cd | 106 | * @returns A MIDIMessage |
simon | 1:ff74eabe02cd | 107 | */ |
simon | 1:ff74eabe02cd | 108 | static MIDIMessage ControlChange(int control, int value, int channel = 0) { |
simon | 1:ff74eabe02cd | 109 | MIDIMessage msg; |
simon | 1:ff74eabe02cd | 110 | msg.data[0] = CABLE_NUM | 0x0B; |
simon | 1:ff74eabe02cd | 111 | msg.data[1] = 0xB0 | (channel & 0x0F); |
simon | 1:ff74eabe02cd | 112 | msg.data[2] = control & 0x7F; |
simon | 1:ff74eabe02cd | 113 | msg.data[3] = value & 0x7F; |
simon | 1:ff74eabe02cd | 114 | return msg; |
simon | 1:ff74eabe02cd | 115 | } |
simon | 1:ff74eabe02cd | 116 | |
simon | 1:ff74eabe02cd | 117 | /** Create a Program Change message |
simon | 1:ff74eabe02cd | 118 | * @param program Program ID |
simon | 1:ff74eabe02cd | 119 | * @param channel Channel (0-15, default 0) |
simon | 1:ff74eabe02cd | 120 | * @returns A MIDIMessage |
simon | 1:ff74eabe02cd | 121 | */ |
simon | 1:ff74eabe02cd | 122 | static MIDIMessage ProgramChange(int program, int channel = 0) { |
simon | 1:ff74eabe02cd | 123 | MIDIMessage msg; |
simon | 1:ff74eabe02cd | 124 | msg.data[0] = CABLE_NUM | 0x0C; |
simon | 1:ff74eabe02cd | 125 | msg.data[1] = 0xC0 | (channel & 0x0F); |
simon | 1:ff74eabe02cd | 126 | msg.data[2] = program & 0x7F; |
simon | 1:ff74eabe02cd | 127 | msg.data[3] = 0x00; |
simon | 1:ff74eabe02cd | 128 | return msg; |
simon | 1:ff74eabe02cd | 129 | } |
simon | 1:ff74eabe02cd | 130 | |
simon | 1:ff74eabe02cd | 131 | /** Create a Channel Aftertouch message |
simon | 1:ff74eabe02cd | 132 | * @param pressure Pressure |
simon | 1:ff74eabe02cd | 133 | * @param channel Key channel (0-15, default 0) |
simon | 1:ff74eabe02cd | 134 | * @returns A MIDIMessage |
simon | 1:ff74eabe02cd | 135 | */ |
simon | 1:ff74eabe02cd | 136 | static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) { |
simon | 1:ff74eabe02cd | 137 | MIDIMessage msg; |
simon | 1:ff74eabe02cd | 138 | msg.data[0] = CABLE_NUM | 0x0D; |
simon | 1:ff74eabe02cd | 139 | msg.data[1] = 0xD0 | (channel & 0x0F); |
simon | 1:ff74eabe02cd | 140 | msg.data[2] = pressure & 0x7F; |
simon | 1:ff74eabe02cd | 141 | msg.data[3] = 0x00; |
simon | 1:ff74eabe02cd | 142 | return msg; |
simon | 1:ff74eabe02cd | 143 | } |
simon | 1:ff74eabe02cd | 144 | |
simon | 1:ff74eabe02cd | 145 | /** Create a Pitch Wheel message |
simon | 1:ff74eabe02cd | 146 | * @param pitch Pitch (-8192 - 8191, default = 0) |
simon | 1:ff74eabe02cd | 147 | * @param channel Channel (0-15, default 0) |
simon | 1:ff74eabe02cd | 148 | * @returns A MIDIMessage |
simon | 1:ff74eabe02cd | 149 | */ |
simon | 1:ff74eabe02cd | 150 | static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) { |
simon | 1:ff74eabe02cd | 151 | MIDIMessage msg; |
simon | 1:ff74eabe02cd | 152 | int p = pitch + 8192; // 0 - 16383, 8192 is center |
simon | 1:ff74eabe02cd | 153 | msg.data[0] = CABLE_NUM | 0x0E; |
simon | 1:ff74eabe02cd | 154 | msg.data[1] = 0xE0 | (channel & 0x0F); |
simon | 1:ff74eabe02cd | 155 | msg.data[2] = p & 0x7F; |
simon | 1:ff74eabe02cd | 156 | msg.data[3] = (p >> 7) & 0x7F; |
simon | 1:ff74eabe02cd | 157 | return msg; |
simon | 1:ff74eabe02cd | 158 | } |
simon | 1:ff74eabe02cd | 159 | |
simon | 1:ff74eabe02cd | 160 | /** Create an All Notes Off message |
simon | 1:ff74eabe02cd | 161 | * @param channel Channel (0-15, default 0) |
simon | 1:ff74eabe02cd | 162 | * @returns A MIDIMessage |
simon | 1:ff74eabe02cd | 163 | */ |
simon | 1:ff74eabe02cd | 164 | static MIDIMessage AllNotesOff(int channel = 0) { |
simon | 1:ff74eabe02cd | 165 | return ControlChange(123, 0, channel); |
simon | 1:ff74eabe02cd | 166 | } |
simon | 1:ff74eabe02cd | 167 | |
simon | 1:ff74eabe02cd | 168 | // decode messages |
simon | 1:ff74eabe02cd | 169 | |
simon | 1:ff74eabe02cd | 170 | /** MIDI Message Types */ |
simon | 1:ff74eabe02cd | 171 | enum MIDIMessageType { |
simon | 1:ff74eabe02cd | 172 | ErrorType, |
simon | 1:ff74eabe02cd | 173 | NoteOffType, |
simon | 1:ff74eabe02cd | 174 | NoteOnType, |
simon | 1:ff74eabe02cd | 175 | PolyphonicAftertouchType, |
simon | 1:ff74eabe02cd | 176 | ControlChangeType, |
simon | 1:ff74eabe02cd | 177 | ProgramChangeType, |
simon | 1:ff74eabe02cd | 178 | ChannelAftertouchType, |
simon | 1:ff74eabe02cd | 179 | PitchWheelType, |
simon | 1:ff74eabe02cd | 180 | AllNotesOffType |
simon | 1:ff74eabe02cd | 181 | }; |
simon | 1:ff74eabe02cd | 182 | |
simon | 1:ff74eabe02cd | 183 | /** Read the message type |
simon | 1:ff74eabe02cd | 184 | * @returns MIDIMessageType |
simon | 1:ff74eabe02cd | 185 | */ |
simon | 1:ff74eabe02cd | 186 | MIDIMessageType type() { |
simon | 1:ff74eabe02cd | 187 | switch((data[1] >> 4) & 0xF) { |
simon | 1:ff74eabe02cd | 188 | case 0x8: return NoteOffType; |
simon | 1:ff74eabe02cd | 189 | case 0x9: return NoteOnType; |
simon | 1:ff74eabe02cd | 190 | case 0xA: return PolyphonicAftertouchType; |
simon | 1:ff74eabe02cd | 191 | case 0xB: |
simon | 1:ff74eabe02cd | 192 | if(controller() < 120) { // standard controllers |
simon | 1:ff74eabe02cd | 193 | return ControlChangeType; |
simon | 1:ff74eabe02cd | 194 | } else if(controller() == 123) { |
simon | 1:ff74eabe02cd | 195 | return AllNotesOffType; |
simon | 1:ff74eabe02cd | 196 | } else { |
simon | 1:ff74eabe02cd | 197 | return ErrorType; // unsupported atm |
simon | 1:ff74eabe02cd | 198 | } |
simon | 1:ff74eabe02cd | 199 | case 0xC: return ProgramChangeType; |
simon | 1:ff74eabe02cd | 200 | case 0xD: return ChannelAftertouchType; |
simon | 1:ff74eabe02cd | 201 | case 0xE: return PitchWheelType; |
simon | 1:ff74eabe02cd | 202 | default: return ErrorType; |
simon | 1:ff74eabe02cd | 203 | } |
simon | 1:ff74eabe02cd | 204 | } |
simon | 0:56b095524cf2 | 205 | |
simon | 1:ff74eabe02cd | 206 | /** Read the channel number */ |
simon | 1:ff74eabe02cd | 207 | int channel() { |
simon | 1:ff74eabe02cd | 208 | return (data[1] & 0x0F); |
simon | 1:ff74eabe02cd | 209 | } |
simon | 1:ff74eabe02cd | 210 | |
simon | 1:ff74eabe02cd | 211 | /** Read the key ID */ |
simon | 1:ff74eabe02cd | 212 | int key() { |
simon | 1:ff74eabe02cd | 213 | return (data[2] & 0x7F); |
simon | 1:ff74eabe02cd | 214 | } |
simon | 1:ff74eabe02cd | 215 | |
simon | 1:ff74eabe02cd | 216 | /** Read the velocity */ |
simon | 1:ff74eabe02cd | 217 | int velocity() { |
simon | 1:ff74eabe02cd | 218 | return (data[3] & 0x7F); |
simon | 1:ff74eabe02cd | 219 | } |
simon | 0:56b095524cf2 | 220 | |
simon | 1:ff74eabe02cd | 221 | /** Read the controller value */ |
simon | 1:ff74eabe02cd | 222 | int value() { |
simon | 1:ff74eabe02cd | 223 | return (data[3] & 0x7F); |
simon | 1:ff74eabe02cd | 224 | } |
simon | 1:ff74eabe02cd | 225 | |
simon | 1:ff74eabe02cd | 226 | /** Read the aftertouch pressure */ |
simon | 1:ff74eabe02cd | 227 | int pressure() { |
simon | 1:ff74eabe02cd | 228 | if(type() == PolyphonicAftertouchType) { |
simon | 1:ff74eabe02cd | 229 | return (data[3] & 0x7F); |
simon | 1:ff74eabe02cd | 230 | } else { |
simon | 1:ff74eabe02cd | 231 | return (data[2] & 0x7F); |
simon | 1:ff74eabe02cd | 232 | } |
simon | 1:ff74eabe02cd | 233 | } |
simon | 0:56b095524cf2 | 234 | |
simon | 1:ff74eabe02cd | 235 | /** Read the controller number */ |
simon | 1:ff74eabe02cd | 236 | int controller() { |
simon | 1:ff74eabe02cd | 237 | return (data[2] & 0x7F); |
simon | 1:ff74eabe02cd | 238 | } |
simon | 1:ff74eabe02cd | 239 | |
simon | 1:ff74eabe02cd | 240 | /** Read the program number */ |
simon | 1:ff74eabe02cd | 241 | int program() { |
simon | 1:ff74eabe02cd | 242 | return (data[2] & 0x7F); |
simon | 1:ff74eabe02cd | 243 | } |
simon | 1:ff74eabe02cd | 244 | |
simon | 1:ff74eabe02cd | 245 | /** Read the pitch value */ |
simon | 1:ff74eabe02cd | 246 | int pitch() { |
simon | 1:ff74eabe02cd | 247 | int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F); |
simon | 1:ff74eabe02cd | 248 | return p - 8192; // 0 - 16383, 8192 is center |
simon | 1:ff74eabe02cd | 249 | } |
simon | 1:ff74eabe02cd | 250 | |
simon | 1:ff74eabe02cd | 251 | uint8_t data[4]; |
simon | 1:ff74eabe02cd | 252 | }; |
simon | 0:56b095524cf2 | 253 | |
simon | 0:56b095524cf2 | 254 | #endif |