library

Dependents:   USB_CDC_MSD_Hello

Committer:
sherckuith
Date:
Fri Aug 24 02:01:51 2012 +0000
Revision:
0:d5bb9a9c3e24
[mbed] converted /USB_CDC_MSD_Hello/USBDevice

Who changed what in which revision?

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