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