The adoption of the MP3 format caused a revolution in digital sound compression technology by enabling audio files to become much smaller. If you want audio messages or music to be part of your project then you can easily make it happen. You just need any standard MMC or SD memory card, a few chips and a little time…
By Milan Rajic – MikroElektronika – Software Department
Before we start, it is necessary to format the MMC card and save the sound1. mp3 file on it (the card should be formatted in FAT16, i.e. FAT format). The quality of sound coded in MP3 format depends on sampling rate and bitrate. Similar to an audio CD, most MP3 files are sampled 44.1 kHz. The MP3 file’s bitrate indicates the quality of the compressed audio compared to the original uncompressed one, i.e. its fidelity. A bitrate of 64 kbit/s is sufficient for speech reproduction, while it has to be 128 kbit/s or more for music reproduction. In this example a music file with a bitrate of 128 kbit/s is used.
Hardware
The sound contained in this file is coded in the MP3 format so that an MP3 decoder is needed for its decoding. In our example, the VS1011E chip is used for this purpose. This chip decodes MP3 records and performs digital-to-analog conversion of the signal in order to produce a signal that can be fed to audio speakers over a small audio amplifier. Considering that MMC/SD cards use sections of 512 bytes in size, a microcontroller with 512 byte RAM or more is needed for the purpose of controlling MP3 decoding process. We have chosen the PIC18F4520 with 1536 byte RAM.
Software
The program controlling the operation of this device consists of five steps:
Step 1: Initialization of the SPI module of the microcontroller.
Step 2: Initialization of the compiler’s Mmc_FAT16 library, which enables MP3 fi les to be read from MMC or SD cards.
Step 3: Reading a part of the file.
Step 4: Sending data to the MP3 decoder buffer.
Step 5: If the end of the file is not reached, jump to step 3.
Testing
It is recommended to start testing the device operation with low bitrate and increase it gradually. The MP3 decoder buffer has a size of 2048 bytes. If the buffer is loaded with a part of MP3 file with 128 kbit/s bitrate, it will contain twice the number of sound samples than when it is loaded with a part of file with 256 kbit/s bitrate.
Accordingly, if the bitrate of the file is lower it will take twice as long to encode the buffer content. If we overdo the bitrate of the file it may happen that buffer content is encoded before the microcontroller can manage to read the next part of the file from the card and write it in the buffer, which will cause the sound to be discontinuous. If this happens, we can reduce the MP3 file’s bitrate or use a quartz-crystal 8MHz or more. Refer to Schematic 1.
Anyway, you don’t have to worry about this as our program has been tested on several microcontroller families with different crystal values and it is capable of decoding MP3 files of average and high quality. On the other hand, a low bitrate means that buffer decoder is filled with sound of longer duration. It may happen that the decoder doesn’t decode the buffer content before we try to reload it. In order to avoid this, it is necessary to make sure that the decoder is ready to receive a new data before it has been sent. In other words, it is necessary to wait until decoder’s data request signal (DREQ) is set to logic one (1).
Enhancements
This example may also be extended after being tested. The DREQ signal can be periodically tested. A routine for volume control or built-in Bass/Treble enhancer control etc. may be incorporated in the program as well. The MMC library enables you to select a file with a different name. In this way it is possible to create a set of MP3 messages, sounds or songs to be used in other applications and send appropriate MP3
files to the decoder depending on the needs.
Below is a list of ready to use functions contained in the Mmc_FAT16 Library. This library is integrated in mikroC PRO for PIC compiler.
Mmc_Fat_Append() Write at the end of the file
Mmc_Fat_Assign()* Assign file for FAT operations
Mmc_Fat_Delete() Delete file
Mmc_Fat_Get_File_Date() Get file date and time
Mmc_Fat_Get_File_Size() Get file size
Mmc_Fat_Get_Swap_File() Create a swap file
Mmc_Fat_Init()* Init card for FAT operations
Mmc_Fat_QuickFormat()
Mmc_Fat_Read()* Read data from file
Mmc_Fat_Reset()* Open file for reading
Mmc_Fat_Rewrite() Open file for writing
Mmc_Fat_Set_File_Date() Set file date and time
Mmc_Fat_Write() Write data to file
* Mmc_FAT16 functions used in program
Other mikroC for PIC functions used in program:
Spi_Init_Advanced() Initialize microcontroller SPI module
GO TO
Code for this example written for PIC® microcontrollers in C, Basic and Pascal as well as the programs written for dsPIC® and AVR® microcontrollers can be found on our web site: www.mikroe.com/en/article/