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