You are viewing an older revision! See the latest version

USBAudio

The USBAudio class enables the mbed to be recognized as an audio device. With this interface, you can receive audio packets from the 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. You can also connect the USB power to VIN to power the mbed when connected.

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 example for the USBAudio library

#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");
    }
}

Import programUSBAudio_HelloWorld

USBAudio Hello World

API

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

More example

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

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

/media/uploads/samux/pcm.png

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:

/media/uploads/samux/pcm_stereo.png


All wikipages