This content relates to a deprecated version of Mbed

Mbed 2 is now deprecated. For the latest version please see the Mbed OS documentation.

The USBAudio class enables the mbed to be recognized as an audio device. With this interface, you can receive and send audio packets from and to a computer (play a music,...) over USB. For instance you can connect a speaker or an I2S/I2C chip to the mbed and play the stream received from the computer.

The USB connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768 and the LPC11U24
  • The on-board USB connector of the FRDM-KL25Z

Change the default sound board

To send audio packets to the mbed, you have to change the default sound board used by the Operating system.
On Windows, you can do this by clicking on:

  • control panel
  • Hardware and Sound
  • Manage audio device in the Sound section
  • Select the Mbed Audio device and press Set default

Hello World

Import program

00001 // Hello World example for the USBAudio library
00003 #include "mbed.h"
00004 #include "USBAudio.h"
00006 Serial pc(USBTX, USBRX);
00008 // frequency: 48 kHz
00009 #define FREQ 48000
00011 // 1 channel: mono
00012 #define NB_CHA 1
00014 // 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
00015 #define AUDIO_LENGTH_PACKET 48 * 2 * 1
00017 // USBAudio
00018 USBAudio audio(FREQ, NB_CHA);
00020 int main() {
00021     int16_t buf[AUDIO_LENGTH_PACKET/2];
00023     while (1) {
00024         // read an audio packet
00025 *)buf);
00027         // print packet received
00028         pc.printf("recv: ");
00029         for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
00030             pc.printf("%d ", buf[i]);
00031         }
00032         pc.printf("\r\n");
00033     }
00034 }


Import library

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)
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.

More examples

The following program is sending to a speaker all audio packets received. This means that you can play a music on your computer and listen it on your Mbed.

Import programUSBAUDIO_speaker

USBAudio speaker example

The USBAudio playback example sends back to the computer all audio packets received. You can then listen for incoming audio packets with audacity for instance.

Import programUSBAudioPlayback

USBAudio example: playback

In details

Audio packet length

In this section, I will explain what kind of packets are received according to the frequency and the number of channels.

An audio packet is received each millisecond. So let's say that a frequency of 48 kHz has been chosen with 2 channels (stereo). Knowing that each sample of a packet are 16 bits long, 48 * 2 bytes will be received each millisecond for one channel. In total, for 2 channels, 48 * 2 * 2 bytes will be received.

Compute the length packet

AUDIO_LENGTH_PACKET = (FREQ / 500) * nb_channel

How to interpret an audio packet ?

The read() function fills an uint8_t array. But these data has to be interpreted as 16 bits signed data (PCM). Then PCM values can be handled according to the number of channels.

MONO: single channel


STEREO: 2 channels

When there are 2 channels, values for channel 1 and values for channel 2 will alternate as explained in the following diagram:


All wikipages