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