Samuel Mokrani
/
USBAudio_micro
USBAudio example using a microphone
USBDevice/USBAudioOUT/USBAudioOUT.h
- Committer:
- samux
- Date:
- 2011-12-20
- Revision:
- 7:6b0012b8fd01
File content as of revision 7:6b0012b8fd01:
/* Copyright (c) 2010-2011 mbed.org, MIT License * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef USBAudioOUTOUT_H #define USBAudioOUTOUT_H /* These headers are included for child class. */ #include "USBEndpoints.h" #include "USBDescriptor.h" #include "USBDevice_Types.h" #include "USBDevice.h" /** * USBAudioOUT example * #include "mbed.h" * #include "USBAudioOUT.h" * * // frequency: 8 kHz * #define FREQ 8000 * * // 1 channel: mono * #define NB_CHA 1 * * // 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 * #define AUDIO_LENGTH_PACKET (FREQ/500) * NB_CHA * * USBAudioOUT audio(FREQ, NB_CHA, 0x1111, 0x78ab); * * AnalogIn mic(p20); * * int16_t buf[AUDIO_LENGTH_PACKET/2]; * * int main() { * double mic_mean = 0.0; * double mic_value; * * // compute average value of the microphone. We can then center the audio signal sent to the computer * for (int j = 0; j < 1000; j++) { * mic_value = (mic.read_u16() >> 3); * mic_mean = (mic_mean*j + mic_value)/(j+1); * } * * while (1) { * for (int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) { * buf[i] = (mic.read_u16() >> 3) - mic_mean; * if (i != AUDIO_LENGTH_PACKET/2) { * wait_us(80); * } * } * audio.write((uint8_t *)buf); * } * } * @endcode */ class USBAudioOUT: public USBDevice { public: /** * Constructor * * @param frequency frequency in Hz (default: 48000) * @param channel_nb channel number (1 or 2) (default: 1) * @param vendor_id Your vendor_id * @param product_id Your product_id * @param product_release Your preoduct_release */ 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); /** * Write an audio packet * * @param buf audio packet * @returns true if successful */ bool write(uint8_t * buf); protected: /* * Called by USBDevice layer. Set configuration of the device. * For instance, you can add all endpoints that you need on this function. * * @param configuration Number of the configuration * @returns true if class handles this request */ virtual bool USBCallback_setConfiguration(uint8_t configuration); /* * Get string product descriptor * * @returns pointer to the string product descriptor */ virtual uint8_t * stringIproductDesc(); /* * Get string interface descriptor * * @returns pointer to the string interface descriptor */ virtual uint8_t * stringIinterfaceDesc(); /* * Get configuration descriptor * * @returns pointer to the configuration descriptor */ virtual uint8_t * configurationDesc(); /* * Called by USBDevice layer. Set interface/alternate of the device. * * @param interface Number of the interface to be configured * @param alternate Number of the alternate to be configured * @returns true if class handles this request */ virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate); /* * Callback called on each Start of Frame event */ virtual void SOF(int frameNumber); /* * Callback called when a packet has been sent */ virtual bool EP3_IN_callback(); private: // stream available ? volatile bool available; // FREQ uint32_t FREQ; // size of the maximum packet for the isochronous endpoint uint32_t PACKET_SIZE_ISO; // mono, stereo,... uint8_t channel_nb; // channel config: master, left, right uint8_t channel_config; // mute state uint8_t mute; // Volume Current Value uint16_t volCur; // Volume Minimum Value uint16_t volMin; // Volume Maximum Value uint16_t volMax; // Volume Resolution uint16_t volRes; // Buffer containing one audio packet uint8_t * buf_stream; // callback to update volume FunctionPointer updateVol; // boolean showing that the SOF handler has been called. Useful for readNB. volatile bool SOF_handler; volatile bool interruptIN; volatile bool writeIN; }; #endif