SD card interface

Committer:
lharoon
Date:
Mon Oct 08 11:14:07 2012 +0000
Revision:
0:22612ae617a0
1st edition

Who changed what in which revision?

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