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