Lab 4: Play a WAV Audio File from an SD Card with MPLAB® Harmony v3
Purpose
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 start to show you the power of Harmony!
Overview
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 application sub-tasks for this lab are shown in the following figure.
The initial states in the APP_SDCARD_AUDIO_Tasks() are the same as those found in the previous lab:
- Open the CODEC to obtain a handle to the CODEC buffer.
- Register a CODEC buffer event handler function (called when the buffer transfer was completed).
- Mount the file system for the SD card.
- 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.
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 passing 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.
Procedure
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.
Results
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.
Analysis
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, and 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.
Conclusions
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.