USB device stack

Dependents:   blinky_max32630fthr FTHR_USB_serial FTHR_OLED HSP_RPC_GUI_3_0_1 ... more

Fork of USBDevice by mbed official

Embed: (wiki syntax)

« Back to documentation index

USBAudio Class Reference

USBAudio Class Reference

USBAudio example. More...

#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 69 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_infrequency in Hz (default: 48000)
channel_nb_inchannel number (1 or 2) (default: 1)
frequency_outfrequency in Hz (default: 8000)
channel_nb_out_inchannel number (1 or 2) (default: 1)
vendor_idYour vendor_id
product_idYour product_id
product_releaseYour 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:
functionFunction to attach

Definition at line 133 of file USBAudio.h.

void attach ( T *  tptr,
void(T::*)(void)  mptr 
)

Attach a nonstatic void/void member function to update the volume.

Parameters:
tptrObject pointer
mptrMember function pointer

Definition at line 144 of file USBAudio.h.

float getVolume (  )

Get current volume between 0.0 and 1.0.

Returns:
volume

Definition at line 117 of file USBAudio.cpp.

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:
bufpointer 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:
bufpointer 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_readpointer on a buffer which will be filled with an audio packet
buf_writepointer 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:
bufpointer on the audio packet which will be sent
Returns:
true if successful

Definition at line 101 of file USBAudio.cpp.