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 }


[Not found]

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:



See more related questions