USBAudio example using a microphone

Dependencies:   USBDevice mbed

Committer:
samux
Date:
Fri Dec 16 12:31:41 2011 +0000
Revision:
0:539ec61e1fbb
works with m0 and m3 (sinus) but the code is different to have the same result...

Who changed what in which revision?

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