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