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 USBAudio_H
mbed2f 0:7610d342c76e 20 #define USBAudio_H
mbed2f 0:7610d342c76e 21
mbed2f 0:7610d342c76e 22 /* These headers are included for child class. */
mbed2f 0:7610d342c76e 23 #include "USBEndpoints.h"
mbed2f 0:7610d342c76e 24 #include "USBDescriptor.h"
mbed2f 0:7610d342c76e 25 #include "USBDevice_Types.h"
mbed2f 0:7610d342c76e 26
mbed2f 0:7610d342c76e 27 #include "USBDevice.h"
mbed2f 0:7610d342c76e 28
mbed2f 0:7610d342c76e 29
mbed2f 0:7610d342c76e 30 /**
mbed2f 0:7610d342c76e 31 * USBAudio example
mbed2f 0:7610d342c76e 32 *
mbed2f 0:7610d342c76e 33 * #include "mbed.h"
mbed2f 0:7610d342c76e 34 * #include "USBAudio.h"
mbed2f 0:7610d342c76e 35 *
mbed2f 0:7610d342c76e 36 * Serial pc(USBTX, USBRX);
mbed2f 0:7610d342c76e 37 *
mbed2f 0:7610d342c76e 38 * // frequency: 48 kHz
mbed2f 0:7610d342c76e 39 * #define FREQ 32000
mbed2f 0:7610d342c76e 40 *
mbed2f 0:7610d342c76e 41 * // 1 channel: mono
mbed2f 0:7610d342c76e 42 * #define NB_CHA 1
mbed2f 0:7610d342c76e 43 *
mbed2f 0:7610d342c76e 44 * // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1
mbed2f 0:7610d342c76e 45 * #define AUDIO_LENGTH_PACKET 32 * 2 * 1
mbed2f 0:7610d342c76e 46 *
mbed2f 0:7610d342c76e 47 * // USBAudio
mbed2f 0:7610d342c76e 48 * USBAudio audio(FREQ, NB_CHA);
mbed2f 0:7610d342c76e 49 *
mbed2f 0:7610d342c76e 50 * int main() {
mbed2f 0:7610d342c76e 51 * int16_t buf[AUDIO_LENGTH_PACKET/2];
mbed2f 0:7610d342c76e 52 *
mbed2f 0:7610d342c76e 53 * while (1) {
mbed2f 0:7610d342c76e 54 * // read an audio packet
mbed2f 0:7610d342c76e 55 * audio.read((uint8_t *)buf);
mbed2f 0:7610d342c76e 56 *
mbed2f 0:7610d342c76e 57 *
mbed2f 0:7610d342c76e 58 * // print packet received
mbed2f 0:7610d342c76e 59 * pc.printf("recv: ");
mbed2f 0:7610d342c76e 60 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
mbed2f 0:7610d342c76e 61 * pc.printf("%d ", buf[i]);
mbed2f 0:7610d342c76e 62 * }
mbed2f 0:7610d342c76e 63 * pc.printf("\r\n");
mbed2f 0:7610d342c76e 64 * }
mbed2f 0:7610d342c76e 65 * }
mbed2f 0:7610d342c76e 66 * @endcode
mbed2f 0:7610d342c76e 67 */
mbed2f 0:7610d342c76e 68 class USBAudio: public USBDevice {
mbed2f 0:7610d342c76e 69 public:
mbed2f 0:7610d342c76e 70
mbed2f 0:7610d342c76e 71 /**
mbed2f 0:7610d342c76e 72 * Constructor
mbed2f 0:7610d342c76e 73 *
mbed2f 0:7610d342c76e 74 * @param frequency frequency in Hz (default: 32000)
mbed2f 0:7610d342c76e 75 * @param channel_nb channel number (1 or 2) (default: 1)
mbed2f 0:7610d342c76e 76 * @param vendor_id Your vendor_id
mbed2f 0:7610d342c76e 77 * @param product_id Your product_id
mbed2f 0:7610d342c76e 78 * @param product_release Your preoduct_release
mbed2f 0:7610d342c76e 79 */
mbed2f 0:7610d342c76e 80 USBAudio(uint32_t frequency = 32000, uint8_t channel_nb = 1, uint16_t vendor_id = 0x7bb2, uint16_t product_id = 0x1115, uint16_t product_release =0x0104);
mbed2f 0:7610d342c76e 81
mbed2f 0:7610d342c76e 82 /**
mbed2f 0:7610d342c76e 83 * Get current volume between 0.0 and 1.0
mbed2f 0:7610d342c76e 84 *
mbed2f 0:7610d342c76e 85 * @returns volume
mbed2f 0:7610d342c76e 86 */
mbed2f 0:7610d342c76e 87 float getVolume();
mbed2f 0:7610d342c76e 88
mbed2f 0:7610d342c76e 89 /**
mbed2f 0:7610d342c76e 90 * Read an audio packet. warning: blocking
mbed2f 0:7610d342c76e 91 *
mbed2f 0:7610d342c76e 92 * @param buf pointer on a buffer which will be filled with an audio packet
mbed2f 0:7610d342c76e 93 *
mbed2f 0:7610d342c76e 94 * @returns true if successfull
mbed2f 0:7610d342c76e 95 */
mbed2f 0:7610d342c76e 96 bool read(uint8_t * buf);
mbed2f 0:7610d342c76e 97
mbed2f 0:7610d342c76e 98 /**
mbed2f 0:7610d342c76e 99 * Try to read an audio packet. warning: non blocking
mbed2f 0:7610d342c76e 100 *
mbed2f 0:7610d342c76e 101 * @param buf pointer on a buffer which will be filled if an audio packet is available
mbed2f 0:7610d342c76e 102 *
mbed2f 0:7610d342c76e 103 * @returns true if successfull
mbed2f 0:7610d342c76e 104 */
mbed2f 0:7610d342c76e 105 bool readNB(uint8_t * buf);
mbed2f 0:7610d342c76e 106
mbed2f 0:7610d342c76e 107
mbed2f 0:7610d342c76e 108 /** attach a handler to update the volume
mbed2f 0:7610d342c76e 109 *
mbed2f 0:7610d342c76e 110 * @param function Function to attach
mbed2f 0:7610d342c76e 111 *
mbed2f 0:7610d342c76e 112 */
mbed2f 0:7610d342c76e 113 void attach(void(*fptr)(void)) {
mbed2f 0:7610d342c76e 114 updateVol.attach(fptr);
mbed2f 0:7610d342c76e 115 }
mbed2f 0:7610d342c76e 116
mbed2f 0:7610d342c76e 117 /** Attach a nonstatic void/void member function to update the volume
mbed2f 0:7610d342c76e 118 *
mbed2f 0:7610d342c76e 119 * @param tptr Object pointer
mbed2f 0:7610d342c76e 120 * @param mptr Member function pointer
mbed2f 0:7610d342c76e 121 *
mbed2f 0:7610d342c76e 122 */
mbed2f 0:7610d342c76e 123 template<typename T>
mbed2f 0:7610d342c76e 124 void attach(T *tptr, void(T::*mptr)(void)) {
mbed2f 0:7610d342c76e 125 updateVol.attach(tptr, mptr);
mbed2f 0:7610d342c76e 126 }
mbed2f 0:7610d342c76e 127
mbed2f 0:7610d342c76e 128
mbed2f 0:7610d342c76e 129 protected:
mbed2f 0:7610d342c76e 130
mbed2f 0:7610d342c76e 131 /*
mbed2f 0:7610d342c76e 132 * Called by USBDevice layer. Set configuration of the device.
mbed2f 0:7610d342c76e 133 * For instance, you can add all endpoints that you need on this function.
mbed2f 0:7610d342c76e 134 *
mbed2f 0:7610d342c76e 135 * @param configuration Number of the configuration
mbed2f 0:7610d342c76e 136 * @returns true if class handles this request
mbed2f 0:7610d342c76e 137 */
mbed2f 0:7610d342c76e 138 virtual bool USBCallback_setConfiguration(uint8_t configuration);
mbed2f 0:7610d342c76e 139
mbed2f 0:7610d342c76e 140 /*
mbed2f 0:7610d342c76e 141 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
mbed2f 0:7610d342c76e 142 * This is used to handle extensions to standard requests
mbed2f 0:7610d342c76e 143 * and class specific requests
mbed2f 0:7610d342c76e 144 *
mbed2f 0:7610d342c76e 145 * @returns true if class handles this request
mbed2f 0:7610d342c76e 146 */
mbed2f 0:7610d342c76e 147 virtual bool USBCallback_request();
mbed2f 0:7610d342c76e 148
mbed2f 0:7610d342c76e 149 /*
mbed2f 0:7610d342c76e 150 * Get string product descriptor
mbed2f 0:7610d342c76e 151 *
mbed2f 0:7610d342c76e 152 * @returns pointer to the string product descriptor
mbed2f 0:7610d342c76e 153 */
mbed2f 0:7610d342c76e 154 virtual uint8_t * stringIproductDesc();
mbed2f 0:7610d342c76e 155
mbed2f 0:7610d342c76e 156 /*
mbed2f 0:7610d342c76e 157 * Get string interface descriptor
mbed2f 0:7610d342c76e 158 *
mbed2f 0:7610d342c76e 159 * @returns pointer to the string interface descriptor
mbed2f 0:7610d342c76e 160 */
mbed2f 0:7610d342c76e 161 virtual uint8_t * stringIinterfaceDesc();
mbed2f 0:7610d342c76e 162
mbed2f 0:7610d342c76e 163 /*
mbed2f 0:7610d342c76e 164 * Get configuration descriptor
mbed2f 0:7610d342c76e 165 *
mbed2f 0:7610d342c76e 166 * @returns pointer to the configuration descriptor
mbed2f 0:7610d342c76e 167 */
mbed2f 0:7610d342c76e 168 virtual uint8_t * configurationDesc();
mbed2f 0:7610d342c76e 169
mbed2f 0:7610d342c76e 170 /*
mbed2f 0:7610d342c76e 171 * Called by USBDevice layer. Set interface/alternate of the device.
mbed2f 0:7610d342c76e 172 *
mbed2f 0:7610d342c76e 173 * @param interface Number of the interface to be configured
mbed2f 0:7610d342c76e 174 * @param alternate Number of the alternate to be configured
mbed2f 0:7610d342c76e 175 * @returns true if class handles this request
mbed2f 0:7610d342c76e 176 */
mbed2f 0:7610d342c76e 177 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
mbed2f 0:7610d342c76e 178
mbed2f 0:7610d342c76e 179 /*
mbed2f 0:7610d342c76e 180 * Called by USBDevice on Endpoint0 request completion
mbed2f 0:7610d342c76e 181 * if the 'notify' flag has been set to true. Warning: Called in ISR context
mbed2f 0:7610d342c76e 182 *
mbed2f 0:7610d342c76e 183 * In this case it is used to indicate that a HID report has
mbed2f 0:7610d342c76e 184 * been received from the host on endpoint 0
mbed2f 0:7610d342c76e 185 *
mbed2f 0:7610d342c76e 186 * @param buf buffer received on endpoint 0
mbed2f 0:7610d342c76e 187 * @param length length of this buffer
mbed2f 0:7610d342c76e 188 */
mbed2f 0:7610d342c76e 189 virtual void USBCallback_requestCompleted(uint8_t * buf, uint16_t length);
mbed2f 0:7610d342c76e 190
mbed2f 0:7610d342c76e 191 /*
mbed2f 0:7610d342c76e 192 * Callback called on each Start of Frame event
mbed2f 0:7610d342c76e 193 */
mbed2f 0:7610d342c76e 194 virtual void SOF(int frameNumber);
mbed2f 0:7610d342c76e 195
mbed2f 0:7610d342c76e 196 private:
mbed2f 0:7610d342c76e 197
mbed2f 0:7610d342c76e 198 // stream available ?
mbed2f 0:7610d342c76e 199 volatile bool available;
mbed2f 0:7610d342c76e 200
mbed2f 0:7610d342c76e 201 // FREQ
mbed2f 0:7610d342c76e 202 uint32_t FREQ;
mbed2f 0:7610d342c76e 203
mbed2f 0:7610d342c76e 204 // size of the maximum packet for the isochronous endpoint
mbed2f 0:7610d342c76e 205 uint32_t PACKET_SIZE_ISO;
mbed2f 0:7610d342c76e 206
mbed2f 0:7610d342c76e 207 // mono, stereo,...
mbed2f 0:7610d342c76e 208 uint8_t channel_nb;
mbed2f 0:7610d342c76e 209
mbed2f 0:7610d342c76e 210 // channel config: master, left, right
mbed2f 0:7610d342c76e 211 uint8_t channel_config;
mbed2f 0:7610d342c76e 212
mbed2f 0:7610d342c76e 213 // mute state
mbed2f 0:7610d342c76e 214 uint8_t mute;
mbed2f 0:7610d342c76e 215
mbed2f 0:7610d342c76e 216 // Volume Current Value
mbed2f 0:7610d342c76e 217 uint16_t volCur;
mbed2f 0:7610d342c76e 218
mbed2f 0:7610d342c76e 219 // Volume Minimum Value
mbed2f 0:7610d342c76e 220 uint16_t volMin;
mbed2f 0:7610d342c76e 221
mbed2f 0:7610d342c76e 222 // Volume Maximum Value
mbed2f 0:7610d342c76e 223 uint16_t volMax;
mbed2f 0:7610d342c76e 224
mbed2f 0:7610d342c76e 225 // Volume Resolution
mbed2f 0:7610d342c76e 226 uint16_t volRes;
mbed2f 0:7610d342c76e 227
mbed2f 0:7610d342c76e 228 // Buffer containing one audio packet
mbed2f 0:7610d342c76e 229 uint8_t * buf_stream;
mbed2f 0:7610d342c76e 230
mbed2f 0:7610d342c76e 231 // callback to update volume
mbed2f 0:7610d342c76e 232 FunctionPointer updateVol;
mbed2f 0:7610d342c76e 233
mbed2f 0:7610d342c76e 234 // boolean showing that the SOF handler has been called. Useful for readNB.
mbed2f 0:7610d342c76e 235 volatile bool SOF_handler;
mbed2f 0:7610d342c76e 236
mbed2f 0:7610d342c76e 237 };
mbed2f 0:7610d342c76e 238
mbed2f 0:7610d342c76e 239 #endif