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.
Dependencies: ADXL345 DebounceIn USBDevice mbed
USBAudio Class Reference
#include <USBAudio.h>
Inherits USBDevice.
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.   | |
| bool | write (uint8_t *buf) | 
| Write an audio packet.   | |
| 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   | |
| template<typename T > | |
| void | attach (T *tptr, void(T::*mptr)(void)) | 
| Attach a nonstatic void/void member function to update the volume.   | |
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 68 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 | ( | void(*)(void) | fptr ) | 
attach a handler to update the volume
- Parameters:
 - 
  
function Function to attach  
Definition at line 132 of file USBAudio.h.
| 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 143 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 63 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 71 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 83 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 101 of file USBAudio.cpp.
Generated on Tue Jul 12 2022 18:13:03 by
 1.7.2