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

Last modified by Microchip on 2024/02/06 09:20

Warning: This tutorial uses the old Harmony configuration tool (MPLAB® Harmony Configurator (MHC)). New Harmony projects should use the MPLAB Code Configurator (MCC) instead.

 

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.

Note: 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.

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

Application sub-tasks

Figure 4.1: APP_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.

Application states

Figure 4.2: APP_SDCARD_AUDIO_Tasks()

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.

Application flow diagram

Figure 4.3: Audio Player State Machine

Back to Top

Lab Source Files and Solutions

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

Note: 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:

Note that multiple versions of all these tools can co-exist on the same computer.

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

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.

Back to Top