Mistake on this page?
Report an issue in GitHub or email us

USBAudio

USBAudio class hierarchy

You can use the USBAudio interface to send and receive audio data over USB. Once a USB program is loaded onto the Mbed board, you can send audio data to your PC by selecting Mbed Audio as your PC's microphone input. Your Mbed Enabled board can receive audio data from your PC if you select Mbed Audio as your PC's speaker output.

USBAudio class reference

Public Member Functions
 USBAudio (bool connect=true, uint32_t frequency_rx=48000, uint8_t channel_count_rx=1, uint32_t frequency_tx=8000, uint8_t channel_count_tx=1, uint32_t buffer_ms=10, uint16_t vendor_id=0x7bb8, uint16_t product_id=0x1111, uint16_t product_release=0x0100)
 Basic constructor. More...
 USBAudio (USBPhy *phy, uint32_t frequency_rx, uint8_t channel_count_rx, uint32_t frequency_tx, uint8_t channel_count_tx, uint32_t buffer_ms, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
 Fully featured constructor. More...
virtual ~USBAudio ()
 Destroy this object. More...
void connect ()
 Connect USBAudio. More...
void disconnect ()
 Disconnect USBAudio. More...
bool read (uint8_t *buf, uint32_t size)
 Read audio data. More...
void read_nb (uint8_t *buf, uint32_t size, uint32_t *actual)
 Nonblocking audio data read. More...
uint32_t read_overflows (bool clear=false)
 Return the number read packets dropped due to overflow. More...
bool read_ready ()
 Check if the audio read channel is open. More...
void read_wait_ready ()
 Wait until the audio read channel is open. More...
bool write (uint8_t *buf, uint32_t size)
 Write audio data. More...
void write_nb (uint8_t *buf, uint32_t size, uint32_t *actual)
 Nonblocking audio data write. More...
uint32_t write_underflows (bool clear=false)
 Return the number write packets not sent due to underflow. More...
bool write_ready ()
 Check if the audio write channel is open. More...
void write_wait_ready ()
 Wait until the audio write channel is open. More...
float get_volume ()
 Get current volume between 0.0 and 1.0. More...
void attach (mbed::Callback< void()> &cb)
 Attach a Callback to update the volume. More...
void attach_tx (mbed::Callback< void(AudioEvent)> &cb)
 attach a Callback to Tx Done More...
void attach_rx (mbed::Callback< void(AudioEvent)> &cb)
 attach a Callback to Rx Done More...
Protected Member Functions
virtual void callback_state_change (DeviceState new_state)
 Called when USB changes state. More...
virtual void callback_request (const setup_packet_t *setup)
 Called by USBDevice on Endpoint0 request. More...
virtual void callback_request_xfer_done (const setup_packet_t *setup, bool aborted)
 Called by USBDevice on data stage completion. More...
void init ()
 Initialize this instance. More...
void deinit ()
 Power down this instance. More...
bool configured ()
 Check if the device is configured. More...
void sof_enable ()
 Enable the start of frame interrupt. More...
void sof_disable ()
 Disable the start of frame interrupt. More...
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, mbed::Callback< void()> callback=NULL)
 Add an endpoint. More...
template<typename T >
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, void(T::*callback)())
 Add an endpoint. More...
void endpoint_remove (usb_ep_t endpoint)
 Remove an endpoint. More...
void endpoint_remove_all ()
 Remove all non-zero endpoints. More...
void endpoint_stall (usb_ep_t endpoint)
 Stall an endpoint. More...
void endpoint_unstall (usb_ep_t endpoint)
 Un-stall an endpoint. More...
uint32_t endpoint_max_packet_size (usb_ep_t endpoint)
 Get the current maximum size for this endpoint. More...
void endpoint_abort (usb_ep_t endpoint)
 Abort the current transfer on this endpoint. More...
bool read_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 start a read on the given endpoint More...
uint32_t read_finish (usb_ep_t endpoint)
 Get the status of a read. More...
bool write_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 Write a data to the given endpoint. More...
uint32_t write_finish (usb_ep_t endpoint)
 Get the status of a write. More...
virtual void callback_power (bool powered)
 Called by USBDevice layer on power state change. More...
virtual void callback_sof (int frame_number)
 Called by USBDevice layer on each new USB frame. More...
virtual void callback_reset ()
 Called by USBDevice layer on bus reset. More...
void complete_request (RequestResult result, uint8_t *data=NULL, uint32_t size=0)
 Called to complete the setup stage of a callback request. More...
void complete_request_xfer_done (bool success)
 Called to complete the data stage of a callback request. More...
void complete_set_configuration (bool success)
 Called to complete a set configuration command. More...
void complete_set_interface (bool success)
 Called to complete a set interface command. More...
uint8_t * find_descriptor (uint8_t descriptor_type, uint8_t index=0)
 Find a descriptor type inside the configuration descriptor. More...
const usb_ep_table_tendpoint_table ()
 Get the endpoint table of this device. More...
virtual void start_process ()
 Callback called to indicate the USB processing needs to be done. More...
virtual void lock ()
 Acquire exclusive access to this instance USBDevice. More...
virtual void unlock ()
 Release exclusive access to this instance USBDevice. More...
virtual void assert_locked ()
 Assert that the current thread of execution holds the lock. More...

USBAudio square wave example

This example outputs an audio square wave over USB.

// This example simply generates a square wave.
// Use a program like Audacity to record and hear the square wave, or route microphone input to output device.
#include "mbed.h"
#include "USBAudio.h"
#include <math.h>

int16_t square_wave(uint32_t freq_hz, uint16_t amplitude, float time_s)
{
    float period = (float)1 / freq_hz;
    if (fmod(time_s, period) > period / 2) {
        return amplitude / 2;
    } else {
        return -(amplitude / 2);
    }
}

int main() {
    uint32_t tx_freq = 16000;
    USBAudio audio(true, 8000, 2, tx_freq, 1, 10, 0x7bb8, 0x1112, 0x0100);
    float cur_time = 0;
    while (true) {
        uint16_t samples[64];
        for (int i = 0; i < 64; i++) {
            samples[i] = square_wave(100, 5000, cur_time);
            cur_time += 1.0 / tx_freq;
        }
        if (!audio.write((uint8_t *)&samples, sizeof(samples))) {
            audio.write_wait_ready();
        }
    }
}

USBAudio loopback example

This example loops input audio to the Mbed board back to the host PC, so that you may record the audio or listen to it through headphones or speakers.

// Audio loopback example use:
// 1. Select "Mbed Audio" as your PC's default speaker and microphone devices.
// 2. Play some sound (YouTube, audio file, etc.) on your PC with Mbed board connected to your PC via the target's USB.
// 3. Record using a program such as Audacity with the Mbed board connected and with the audio on your PC playing.
// 4. The audio that is playing on your PC will be recorded by Audacity via USB loopback.
#include "mbed.h"
#include "USBAudio.h"

int main() {

    USBAudio audio(true, 44100, 2, 44100, 2);

    printf("Looping audio\r\n");
    static uint8_t buf[128];
    while (true) {
        if (!audio.read(buf, sizeof(buf))) {
            memset(buf, 0, sizeof(buf));
        }
        audio.write(buf, sizeof(buf));
    }
}

USBAudio play sound data example

This example loads raw audio data to your board's flash. That data then plays on the host PC over USB. We have tested this example with the NXP FRDM-K64F, which has 1 MB of flash memory. If you are using a board that has less than 1 MB of flash memory, delete data from the end of the data array, and set NUM_ELEMENTS accordingly until the program size is small enough to flash without exceeding storage. Follow the link below, and click Ctrl + s to save the raw code view for main.cpp.

main.cpp

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.