USBAudio example using a microphone

Dependencies:   USBDevice mbed

Committer:
samux
Date:
Tue Dec 20 11:41:31 2011 +0000
Revision:
7:6b0012b8fd01
create USBAudioOUT class. all is working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 7:6b0012b8fd01 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 7:6b0012b8fd01 2 *
samux 7:6b0012b8fd01 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 7:6b0012b8fd01 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 7:6b0012b8fd01 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 7:6b0012b8fd01 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 7:6b0012b8fd01 7 * Software is furnished to do so, subject to the following conditions:
samux 7:6b0012b8fd01 8 *
samux 7:6b0012b8fd01 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 7:6b0012b8fd01 10 * substantial portions of the Software.
samux 7:6b0012b8fd01 11 *
samux 7:6b0012b8fd01 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 7:6b0012b8fd01 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 7:6b0012b8fd01 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 7:6b0012b8fd01 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 7:6b0012b8fd01 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 7:6b0012b8fd01 17 */
samux 7:6b0012b8fd01 18
samux 7:6b0012b8fd01 19 #ifndef USBAudioOUTOUT_H
samux 7:6b0012b8fd01 20 #define USBAudioOUTOUT_H
samux 7:6b0012b8fd01 21
samux 7:6b0012b8fd01 22 /* These headers are included for child class. */
samux 7:6b0012b8fd01 23 #include "USBEndpoints.h"
samux 7:6b0012b8fd01 24 #include "USBDescriptor.h"
samux 7:6b0012b8fd01 25 #include "USBDevice_Types.h"
samux 7:6b0012b8fd01 26
samux 7:6b0012b8fd01 27 #include "USBDevice.h"
samux 7:6b0012b8fd01 28
samux 7:6b0012b8fd01 29
samux 7:6b0012b8fd01 30 /**
samux 7:6b0012b8fd01 31 * USBAudioOUT example
samux 7:6b0012b8fd01 32 * #include "mbed.h"
samux 7:6b0012b8fd01 33 * #include "USBAudioOUT.h"
samux 7:6b0012b8fd01 34 *
samux 7:6b0012b8fd01 35 * // frequency: 8 kHz
samux 7:6b0012b8fd01 36 * #define FREQ 8000
samux 7:6b0012b8fd01 37 *
samux 7:6b0012b8fd01 38 * // 1 channel: mono
samux 7:6b0012b8fd01 39 * #define NB_CHA 1
samux 7:6b0012b8fd01 40 *
samux 7:6b0012b8fd01 41 * // 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
samux 7:6b0012b8fd01 42 * #define AUDIO_LENGTH_PACKET (FREQ/500) * NB_CHA
samux 7:6b0012b8fd01 43 *
samux 7:6b0012b8fd01 44 * USBAudioOUT audio(FREQ, NB_CHA, 0x1111, 0x78ab);
samux 7:6b0012b8fd01 45 *
samux 7:6b0012b8fd01 46 * AnalogIn mic(p20);
samux 7:6b0012b8fd01 47 *
samux 7:6b0012b8fd01 48 * int16_t buf[AUDIO_LENGTH_PACKET/2];
samux 7:6b0012b8fd01 49 *
samux 7:6b0012b8fd01 50 * int main() {
samux 7:6b0012b8fd01 51 * double mic_mean = 0.0;
samux 7:6b0012b8fd01 52 * double mic_value;
samux 7:6b0012b8fd01 53 *
samux 7:6b0012b8fd01 54 * // compute average value of the microphone. We can then center the audio signal sent to the computer
samux 7:6b0012b8fd01 55 * for (int j = 0; j < 1000; j++) {
samux 7:6b0012b8fd01 56 * mic_value = (mic.read_u16() >> 3);
samux 7:6b0012b8fd01 57 * mic_mean = (mic_mean*j + mic_value)/(j+1);
samux 7:6b0012b8fd01 58 * }
samux 7:6b0012b8fd01 59 *
samux 7:6b0012b8fd01 60 * while (1) {
samux 7:6b0012b8fd01 61 * for (int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
samux 7:6b0012b8fd01 62 * buf[i] = (mic.read_u16() >> 3) - mic_mean;
samux 7:6b0012b8fd01 63 * if (i != AUDIO_LENGTH_PACKET/2) {
samux 7:6b0012b8fd01 64 * wait_us(80);
samux 7:6b0012b8fd01 65 * }
samux 7:6b0012b8fd01 66 * }
samux 7:6b0012b8fd01 67 * audio.write((uint8_t *)buf);
samux 7:6b0012b8fd01 68 * }
samux 7:6b0012b8fd01 69 * }
samux 7:6b0012b8fd01 70 * @endcode
samux 7:6b0012b8fd01 71 */
samux 7:6b0012b8fd01 72 class USBAudioOUT: public USBDevice {
samux 7:6b0012b8fd01 73 public:
samux 7:6b0012b8fd01 74
samux 7:6b0012b8fd01 75 /**
samux 7:6b0012b8fd01 76 * Constructor
samux 7:6b0012b8fd01 77 *
samux 7:6b0012b8fd01 78 * @param frequency frequency in Hz (default: 48000)
samux 7:6b0012b8fd01 79 * @param channel_nb channel number (1 or 2) (default: 1)
samux 7:6b0012b8fd01 80 * @param vendor_id Your vendor_id
samux 7:6b0012b8fd01 81 * @param product_id Your product_id
samux 7:6b0012b8fd01 82 * @param product_release Your preoduct_release
samux 7:6b0012b8fd01 83 */
samux 7:6b0012b8fd01 84 USBAudioOUT(uint32_t frequency = 48000, uint8_t channel_nb = 1, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100);
samux 7:6b0012b8fd01 85
samux 7:6b0012b8fd01 86
samux 7:6b0012b8fd01 87 /**
samux 7:6b0012b8fd01 88 * Write an audio packet
samux 7:6b0012b8fd01 89 *
samux 7:6b0012b8fd01 90 * @param buf audio packet
samux 7:6b0012b8fd01 91 * @returns true if successful
samux 7:6b0012b8fd01 92 */
samux 7:6b0012b8fd01 93 bool write(uint8_t * buf);
samux 7:6b0012b8fd01 94
samux 7:6b0012b8fd01 95
samux 7:6b0012b8fd01 96 protected:
samux 7:6b0012b8fd01 97
samux 7:6b0012b8fd01 98 /*
samux 7:6b0012b8fd01 99 * Called by USBDevice layer. Set configuration of the device.
samux 7:6b0012b8fd01 100 * For instance, you can add all endpoints that you need on this function.
samux 7:6b0012b8fd01 101 *
samux 7:6b0012b8fd01 102 * @param configuration Number of the configuration
samux 7:6b0012b8fd01 103 * @returns true if class handles this request
samux 7:6b0012b8fd01 104 */
samux 7:6b0012b8fd01 105 virtual bool USBCallback_setConfiguration(uint8_t configuration);
samux 7:6b0012b8fd01 106
samux 7:6b0012b8fd01 107 /*
samux 7:6b0012b8fd01 108 * Get string product descriptor
samux 7:6b0012b8fd01 109 *
samux 7:6b0012b8fd01 110 * @returns pointer to the string product descriptor
samux 7:6b0012b8fd01 111 */
samux 7:6b0012b8fd01 112 virtual uint8_t * stringIproductDesc();
samux 7:6b0012b8fd01 113
samux 7:6b0012b8fd01 114 /*
samux 7:6b0012b8fd01 115 * Get string interface descriptor
samux 7:6b0012b8fd01 116 *
samux 7:6b0012b8fd01 117 * @returns pointer to the string interface descriptor
samux 7:6b0012b8fd01 118 */
samux 7:6b0012b8fd01 119 virtual uint8_t * stringIinterfaceDesc();
samux 7:6b0012b8fd01 120
samux 7:6b0012b8fd01 121 /*
samux 7:6b0012b8fd01 122 * Get configuration descriptor
samux 7:6b0012b8fd01 123 *
samux 7:6b0012b8fd01 124 * @returns pointer to the configuration descriptor
samux 7:6b0012b8fd01 125 */
samux 7:6b0012b8fd01 126 virtual uint8_t * configurationDesc();
samux 7:6b0012b8fd01 127
samux 7:6b0012b8fd01 128 /*
samux 7:6b0012b8fd01 129 * Called by USBDevice layer. Set interface/alternate of the device.
samux 7:6b0012b8fd01 130 *
samux 7:6b0012b8fd01 131 * @param interface Number of the interface to be configured
samux 7:6b0012b8fd01 132 * @param alternate Number of the alternate to be configured
samux 7:6b0012b8fd01 133 * @returns true if class handles this request
samux 7:6b0012b8fd01 134 */
samux 7:6b0012b8fd01 135 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
samux 7:6b0012b8fd01 136
samux 7:6b0012b8fd01 137
samux 7:6b0012b8fd01 138 /*
samux 7:6b0012b8fd01 139 * Callback called on each Start of Frame event
samux 7:6b0012b8fd01 140 */
samux 7:6b0012b8fd01 141 virtual void SOF(int frameNumber);
samux 7:6b0012b8fd01 142
samux 7:6b0012b8fd01 143 /*
samux 7:6b0012b8fd01 144 * Callback called when a packet has been sent
samux 7:6b0012b8fd01 145 */
samux 7:6b0012b8fd01 146 virtual bool EP3_IN_callback();
samux 7:6b0012b8fd01 147
samux 7:6b0012b8fd01 148 private:
samux 7:6b0012b8fd01 149
samux 7:6b0012b8fd01 150 // stream available ?
samux 7:6b0012b8fd01 151 volatile bool available;
samux 7:6b0012b8fd01 152
samux 7:6b0012b8fd01 153 // FREQ
samux 7:6b0012b8fd01 154 uint32_t FREQ;
samux 7:6b0012b8fd01 155
samux 7:6b0012b8fd01 156 // size of the maximum packet for the isochronous endpoint
samux 7:6b0012b8fd01 157 uint32_t PACKET_SIZE_ISO;
samux 7:6b0012b8fd01 158
samux 7:6b0012b8fd01 159 // mono, stereo,...
samux 7:6b0012b8fd01 160 uint8_t channel_nb;
samux 7:6b0012b8fd01 161
samux 7:6b0012b8fd01 162 // channel config: master, left, right
samux 7:6b0012b8fd01 163 uint8_t channel_config;
samux 7:6b0012b8fd01 164
samux 7:6b0012b8fd01 165 // mute state
samux 7:6b0012b8fd01 166 uint8_t mute;
samux 7:6b0012b8fd01 167
samux 7:6b0012b8fd01 168 // Volume Current Value
samux 7:6b0012b8fd01 169 uint16_t volCur;
samux 7:6b0012b8fd01 170
samux 7:6b0012b8fd01 171 // Volume Minimum Value
samux 7:6b0012b8fd01 172 uint16_t volMin;
samux 7:6b0012b8fd01 173
samux 7:6b0012b8fd01 174 // Volume Maximum Value
samux 7:6b0012b8fd01 175 uint16_t volMax;
samux 7:6b0012b8fd01 176
samux 7:6b0012b8fd01 177 // Volume Resolution
samux 7:6b0012b8fd01 178 uint16_t volRes;
samux 7:6b0012b8fd01 179
samux 7:6b0012b8fd01 180 // Buffer containing one audio packet
samux 7:6b0012b8fd01 181 uint8_t * buf_stream;
samux 7:6b0012b8fd01 182
samux 7:6b0012b8fd01 183 // callback to update volume
samux 7:6b0012b8fd01 184 FunctionPointer updateVol;
samux 7:6b0012b8fd01 185
samux 7:6b0012b8fd01 186 // boolean showing that the SOF handler has been called. Useful for readNB.
samux 7:6b0012b8fd01 187 volatile bool SOF_handler;
samux 7:6b0012b8fd01 188
samux 7:6b0012b8fd01 189 volatile bool interruptIN;
samux 7:6b0012b8fd01 190 volatile bool writeIN;
samux 7:6b0012b8fd01 191
samux 7:6b0012b8fd01 192 };
samux 7:6b0012b8fd01 193
samux 7:6b0012b8fd01 194 #endif