Lab 4: Play a WAV Audio File from an SD Card with Harmony v3


After completing this exercise, you will understand how to configure and add the Audio Decoder Library (for decoding audio files in WAV format) to the project developed in the previous lab (Lab 3: SD card reader support to load audio files) with the MPLAB® Harmony 3 Configurator (MHC). You will also learn how to integrate these modules into an MPLAB Harmony project. This section will really start to show you the power of Harmony!


In this exercise, we extend the previous lab, Lab 3: SD card reader support to load audio files and add new functionality to it. This application reads a WAV audio file from the SD card on the PIC32 Multimedia Expansion Board (MEB) II and streams it over the PIC32 I²S interface through the audio CODEC. Here, we implement the functionality of an audio player (for WAV files only). The application switches to the next track when the current track playing is completed (assuming that there is more than one track on the SD card). This application also demonstrates the configuration and integration of the additional modules using MHC, thereby extending its functionality.

The naming of functions and variables used in this exercise have been changed from APP_TONE_TEXTFILE_SDCARD_ to APP_SDCARD_AUDIO_. Otherwise, the basic application flow remains the same.

Figure 4.1: APP_Tasks()

The application sub-tasks for this lab are shown in this figure.

Figure 4.2: APP_SDCARD_AUDIO_Tasks()

The initial states in the APP_SDCARD_AUDIO_Tasks() are the same as those found in the previous lab:

  1. Open the CODEC to obtain a handle to the CODEC buffer.
  2. Register a CODEC buffer event handler function (called when the buffer transfer was completed).
  3. Mount the file system for the SD card.
  4. Set the current drive path for the SD card.

You will add a new state for this lab, STATE RUNNING. This state searches all directories on the SD card and saves a list of WAV audio files into a table.


Figure 4.3: Audio Player State Machine

The audio player state machine then starts to run. The audio player states perform the task of reading the audio data from the opened WAV audio file, decoding the WAV raw data through the decoder, and then passes the decoded buffer to the audio CODEC for playing. The audio player states also handle track change events and waits for the current audio buffer completion event from the audio CODEC. You will see these states described in more detail in Step 4.


Lab Source Files and Solutions:

If you haven't already, download all lab source files and solutions for the SD card audio player labs.

This project has been verified to work with the following versions of software tools:
MPLAB X IDE v5.25, MPLAB XC32 Compiler v2.30, MPLAB Harmony MHC v3.3.2, DEV_PACKS v3.5.0, CORE v3.5.0, CSP v3.5.0, BSP v3.5.0, AUDIO v3.4.0, and USB v3.3.0

As the tools are regularly updated, there may be occasional issues while using newer versions. If that is the case, we recommend using the same version as specified in the project.

The archived versions of our tools can be found below:
MPLAB Harmony
MPLAB X IDE and XC32 Compiler
Note that multiple versions of all these tools can co-exist on the same computer.


This lab builds off the work you performed in the previous lab. If you did not complete SD card Audio Player Lab 3, please start Lab 4 using the Lab 3 solution project (found under the firmware folder). Verify it works as expected before continuing with this lab.

All steps must be completed before you will be ready to build, download, and run the application.

Lab Index

Step 1: Copy Source Files and Rename Project for Lab 4

Step 2: Add WAV Decoder Functionality

Step 3: Generate Harmony Code

Step 4: Include Application-specific Source Files, Add Required Code, and Build the Project

Step 5: Review the Application code

Step 6: Debug your Application


You should be able to hear audio tracks on your headphones connected to the MEB II. If you did not configure the decoder correctly or copied audio files with a file format other than WAV, you may not hear any audio at all or may hear distorted audio.


In this exercise, we successfully played an audio track stored in the form of a WAV file on an SD card. We extended the previous lab, SD Card Audio Player Lab 3, by adding audio decoder functionality using MHC. The decoder parsed the WAV files and extracted information including the audio sampling rate, bits per sample, and ultimately provided the application with the raw audio data samples. We also extended the application by adding a separate state machine that traversed through the SD card folder structure and saved the WAV audio files in a table. The application configured a CODEC driver based on the sampling rate information provided by the decoder and passed the audio samples read from the decoder to the CODEC for playing.


With this exercise, we have all the major pieces of a basic audio player glued together. We have the SD card driver and a file system to hold the audio files, decoder functionality to decode audio files of the WAV file format, and a CODEC driver for digital to analog conversion of audio. In effect, we have a full-fledged audio player. As a next step, you may extend the application by adding decoder support for other audio file formats like MP3 or WMA using MHC.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.