Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
USBAudio Class Reference
#include <USBAudio.h>
Inherits USBDevice.
Public Types | |
enum | AudioSampleCorrectType |
Audio Jitter value. More... | |
Public Member Functions | |
USBAudio (uint32_t frequency_in=48000, uint8_t channel_nb_in=1, uint32_t frequency_out=8000, uint8_t channel_nb_out=1, uint16_t vendor_id=0x7bb8, uint16_t product_id=0x1111, uint16_t product_release=0x0100) | |
Constructor. | |
float | getVolume () |
Get current volume between 0.0 and 1.0. | |
bool | read (uint8_t *buf) |
Read an audio packet. | |
bool | readNB (uint8_t *buf) |
Try to read an audio packet. | |
uint32_t | readSync (uint8_t *buf) |
read last received packet if some. | |
bool | write (uint8_t *buf) |
Write an audio packet. | |
void | writeSync (uint8_t *buf, AudioSampleCorrectType jitter_nb=NoCorrection) |
Write packet in endpoint fifo. | |
bool | readWrite (uint8_t *buf_read, uint8_t *buf_write) |
Write and read an audio packet at the same time (on the same frame) | |
void | attach (void(*fptr)(void)) |
attach a handler to update the volume | |
void | attachTx (void(*fptr)(void)) |
attach a handler to Tx Done | |
void | attachRx (void(*fptr)(void)) |
attach a handler to Rx Done | |
template<typename T > | |
void | attach (T *tptr, void(T::*mptr)(void)) |
Attach a nonstatic void/void member function to update the volume. | |
template<typename T > | |
void | attachTx (T *tptr, void(T::*mptr)(void)) |
Attach a nonstatic void/void member function to Tx Done. | |
template<typename T > | |
void | attachRx (T *tptr, void(T::*mptr)(void)) |
Attach a nonstatic void/void member function to Rx Done. | |
void | attach (Callback< void()> &cb) |
Attach a Callback to update the volume. | |
void | attachTx (Callback< void()> &cb) |
attach a Callback to Tx Done | |
void | attachRx (Callback< void()> &cb) |
attach a Callback to Rx Done |
Detailed Description
USBAudio example.
#include "mbed.h" #include "USBAudio.h" Serial pc(USBTX, USBRX); // frequency: 48 kHz #define FREQ 48000 // 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 48 * 2 * 1 // USBAudio USBAudio audio(FREQ, NB_CHA); int main() { int16_t buf[AUDIO_LENGTH_PACKET/2]; while (1) { // read an audio packet audio.read((uint8_t *)buf); // print packet received pc.printf("recv: "); for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) { pc.printf("%d ", buf[i]); } pc.printf("\r\n"); } }
Definition at line 69 of file USBAudio.h.
Member Enumeration Documentation
Audio Jitter value.
Definition at line 127 of file USBAudio.h.
Constructor & Destructor Documentation
USBAudio | ( | uint32_t | frequency_in = 48000 , |
uint8_t | channel_nb_in = 1 , |
||
uint32_t | frequency_out = 8000 , |
||
uint8_t | channel_nb_out = 1 , |
||
uint16_t | vendor_id = 0x7bb8 , |
||
uint16_t | product_id = 0x1111 , |
||
uint16_t | product_release = 0x0100 |
||
) |
Constructor.
- Parameters:
-
frequency_in frequency in Hz (default: 48000) channel_nb_in channel number (1 or 2) (default: 1) frequency_out frequency in Hz (default: 8000) channel_nb_out_in channel number (1 or 2) (default: 1) vendor_id Your vendor_id product_id Your product_id product_release Your preoduct_release
Definition at line 25 of file USBAudio.cpp.
Member Function Documentation
void attach | ( | T * | tptr, |
void(T::*)(void) | mptr | ||
) |
Attach a nonstatic void/void member function to update the volume.
- Parameters:
-
tptr Object pointer mptr Member function pointer
Definition at line 181 of file USBAudio.h.
void attach | ( | void(*)(void) | fptr ) |
attach a handler to update the volume
- Parameters:
-
function Function to attach
Definition at line 154 of file USBAudio.h.
void attach | ( | Callback< void()> & | cb ) |
Attach a Callback to update the volume.
- Parameters:
-
cb Callback to attach
Definition at line 210 of file USBAudio.h.
void attachRx | ( | Callback< void()> & | cb ) |
attach a Callback to Rx Done
- Parameters:
-
cb Callback to attach
Definition at line 226 of file USBAudio.h.
void attachRx | ( | void(*)(void) | fptr ) |
attach a handler to Rx Done
- Parameters:
-
function Function to attach
Definition at line 170 of file USBAudio.h.
void attachRx | ( | T * | tptr, |
void(T::*)(void) | mptr | ||
) |
Attach a nonstatic void/void member function to Rx Done.
- Parameters:
-
tptr Object pointer mptr Member function pointer
Definition at line 201 of file USBAudio.h.
void attachTx | ( | void(*)(void) | fptr ) |
attach a handler to Tx Done
- Parameters:
-
function Function to attach
Definition at line 162 of file USBAudio.h.
void attachTx | ( | T * | tptr, |
void(T::*)(void) | mptr | ||
) |
Attach a nonstatic void/void member function to Tx Done.
- Parameters:
-
tptr Object pointer mptr Member function pointer
Definition at line 191 of file USBAudio.h.
void attachTx | ( | Callback< void()> & | cb ) |
attach a Callback to Tx Done
- Parameters:
-
cb Callback to attach
Definition at line 218 of file USBAudio.h.
float getVolume | ( | ) |
bool read | ( | uint8_t * | buf ) |
Read an audio packet.
During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Blocking
- Parameters:
-
buf pointer on a buffer which will be filled with an audio packet
- Returns:
- true if successfull
Definition at line 65 of file USBAudio.cpp.
bool readNB | ( | uint8_t * | buf ) |
Try to read an audio packet.
During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Non Blocking
- Parameters:
-
buf pointer on a buffer which will be filled if an audio packet is available
- Returns:
- true if successfull
Definition at line 73 of file USBAudio.cpp.
uint32_t readSync | ( | uint8_t * | buf ) |
read last received packet if some.
- Parameters:
-
buf pointer on a buffer which will be filled if an audio packet is available
- Returns:
- the packet length
Definition at line 127 of file USBAudio.cpp.
bool readWrite | ( | uint8_t * | buf_read, |
uint8_t * | buf_write | ||
) |
Write and read an audio packet at the same time (on the same frame)
- Parameters:
-
buf_read pointer on a buffer which will be filled with an audio packet buf_write pointer on the audio packet which will be sent
- Returns:
- true if successful
Definition at line 85 of file USBAudio.cpp.
bool write | ( | uint8_t * | buf ) |
Write an audio packet.
During a frame, only a single writing (you can't write and read an audio packet during the same frame)can be done using this method.
- Parameters:
-
buf pointer on the audio packet which will be sent
- Returns:
- true if successful
Definition at line 103 of file USBAudio.cpp.
void writeSync | ( | uint8_t * | buf, |
AudioSampleCorrectType | jitter_nb = NoCorrection |
||
) |
Write packet in endpoint fifo.
assuming tx fifo is empty
- Parameters:
-
buf pointer on the audio packet which will be sent jitter_nb : AudioSampleCorrecttype
Definition at line 118 of file USBAudio.cpp.
Generated on Tue Jul 12 2022 14:26:54 by
