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