Skip to end of metadata
Go to start of metadata

Audio Class Loopback Example

This example emulates an audio headset device. The playback (speaker) interface allows you to stream audio data from the host, and the record (microphone) interface streams it back to the host, hence creating a loopback. This example requires a special audio driver, which is provided.

Note that this example does not require audio codec hardware.

This example will allow you to accomplish the following tasks:

  • Initialize the Audio class
  • Create an audio class instance
  • Add the audio class instance to the Full-Speed configuration
  • Add the audio class instance to the High-Speed configuration (if available)
  • Add the microphone input terminal to the audio class instance
  • Add the microphone USB output terminal to the audio class instance
  • Add the mute/volume feature unit to the microphone input terminal
  • Add the speaker input terminal to the audio class instance
  • Add the mute/volume feature unit to the speaker input terminal
  • Associate the entities together
  • Create the microphone audio streaming interface
  • Create the speaker audio streaming interface

Location

The example implementation is located in /examples/usb/device/all/ex_usbd_audio_loopback.c. It requires a special audio codec driver, which is implemented in the following files:

  • /examples/usb/device/all/ex_usbd_audio_drv_simulation.c
  • /examples/usb/device/all/ex_usbd_audio_drv_simulation.h
  • /examples/usb/device/all/ex_usbd_audio_drv_simulation_data.c

Running the Demo Application

For the purpose of this demo, we will assume that you are using Microsoft Windows 7 or later.  

The loopback demo requires the following components on the host PC side:

  • USB or jack headphone, speaker or headset with built-in speaker
  • A music player (for example, Windows Media Player)
  • Sound Manager (accessible via menu Start > Control Panel > Sound)

The loopback demo is built using an audio function topology that is defined in the file ex_usbd_audio_loopback.c. It is composed of:

  • Two Input terminals of type analog mic IN and USB OUT
  • Two Output terminals of type USB IN and speaker
  • Two Feature units to manage volume and mute controls for microphone and speaker parts
  • Two AudioStreaming interfaces (one record and one playback), each of which is associated with one of the Input terminals

Figure - Loopback Demo shows the principle of the loopback demo which can be seen as a simulated headset.

Figure - Loopback Demo

(1) The Windows audio driver opens the record and playback streams by selecting the first operational interface of each AudioStreaming (AS) interface. This step is done automatically for the microphone AS interface when the audio device is connected to the PC. For the speaker AS, you will have to ensure that it is enabled in the Sound Manager (cf. below).

(2) Both streams must be open in order to start the streams communication. The loopback task will basically perform two sequential operations seamlessly: processing playback stream and processing record stream. The loopback task detects that the playback stream is open and retrieves one of the isochronous OUT data buffers stored in the audio class. The playback buffer is stored in a circular buffer. Playback data comes from any music player. If the host sends some requests to change the volume or to mute/unmute the playback stream, the loopback task will apply the volume or mute change on the playback data accordingly.

(3) The loopback task continues its execution by processing the record stream. If the record stream is open, it obtains a playback buffer from the circular buffer. This one becomes a record buffer. The loopback task passes the record buffer to the audio class which will take care of submitting record data via isochronous IN transfers . The host will forward record data to a headphone for instance. You should hear the song from the music player (if some settings explained below for the microphone are correct). If the host sends some requests to change the volume or to mute/unmute the record stream, the loopback task will apply the volume or mute change on the record data accordingly.

(4) The Windows audio driver closes streams by selecting the default AudioStreaming interface for the microphone and speaker. This action is done only if you decide to disable the microphone and speaker from the Sound Manager.

Upon connection of your audio device, the simulated headset device should appear in two lists. The microphone should be listed in the recording devices list of the Sound Manager as shown in  Figure - Sound Manager - Microphone in Recording Devices List, whereas the speaker part should be listed in the playback devices list as shown in  Figure - Sound Manager - Speaker in Playback Devices List. In this example, the playback and recording device are identified as "Micrium Audio Product" (.ProductStrPtr field of  USBD_DEV_CFG set to this string).

Sound Manager - Microphone in Recording Devices List
Figure - Sound Manager - Microphone in Recording Devices List
Sound Manager - Speaker in Playback Devices List
Figure - Sound Manager - Speaker in Playback Devices List

In order to listen to the song played by the music player, you need to ensure that:

  • Speaker: the speaker appears in the list as the "Default Device" as shown in  Figure - Sound Manager - Speaker in Playback Devices List. If this is not the case, right-click on the "Micrium Audio Product" speaker and select "Set as Default Device". 
  • Microphone: the volume level is not 0 and the microphone is not muted. Refer to Figure - Sound Manager - Microphone Levels
  • Microphone: the playthrough feature is enabled. Select your microphone, click the button "Properties", go to the tab "Listen" and select "Listen to this device". Ensure that the Windows audio driver will playback the record data through your headphone by checking the "Playback through this device" list (cf.  Figure - Sound Manager - Microphone Playthrough for Loopback Demo). Do not select "Default Playback Device" as it could be the "Micrium Audio Product" speaker. In that case you won't hear anything as the music player and the microphone share the same speaker of your audio device. Explicitly select another speaker device (for example, your headphone jack).
 Sound Manager - Microphone Levels
Figure - Sound Manager - Microphone Levels
Sound Manager - Microphone Playthrough for Loopback Demo
Figure - Sound Manager - Microphone Playthrough for Loopback Demo

API

This example offers only one API named Ex_USBD_Audio_Init(). This function is normally called from a USB device core example.