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