This is early stages of my project, the idea of this project is to be able to mix a guitar with windows sounds in reverse such as instrumental background music or trance music perhaps or maybe another fellow guitarist you may have downloaded from the internet. Microphone or guitar pin is p19 I would use a microphone for drums:) and that it for the moment, the code makes the mbed act as usb speaker that excepts a guitar or microphone input, but with a twist it all in reverse like a guitar reverse effects pedal but only you can mix anything you can get from the internet or any windows sound.

Dependencies:   mbed

Committer:
mbed2f
Date:
Sun Jan 08 17:28:24 2012 +0000
Revision:
0:7610d342c76e

        

Who changed what in which revision?

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