Introduction
In the past lecture we talked about the appearance of MP3 files and its advantages. In this part we are going to demonstrate how to decode and play a MP3 files. As it is mentioned before, a MP3 file by decoding and encryption reduces sound file versus WAV files. Each part of this decoding called frame.
To have proper sound output, we should be able to unzip separated frames in appropriate time and to play the sound we should send these frames to Analog to Digital converter without any interrupt to ban a distortion. Because of limited memory and process, old microcontroller did not have ability to process the frames. The speed rate of converting frames to rare file is lower than play bitrate speed and for this reason they were not able to play the sound. To solve this issue companies, produce hardware decoder.
One of the most known company regarding decoder chips is VLSI company. VS1003 is one of this company hardware decoder products. You can see below an example of portable hardware by this chip and AVR microcontroller.
Despite this, designing with VS1003 had many problems such as bigger PCB, consuming much more energy and high price. But there was no way to solve the problem, using this chip was not avoidance until the ARM microcontroller initialized. They had higher process speed plus lower energy consumption. The higher amount of memory provides engineer with direct decoding and playing song without any additional hardware.
Extracting frames
In the past articles we know the MP3 file structure. To start decoding and playing files, extracting the frames from a file is the first step. As you know a MP3 file consist of a lot of sound frame.
As it shown in above picture, each frame has a header which define a start point of a frame and collection of information about frame. To elaborate look at the below picture:
Each frame has the first (32 bit or 4 byte) which consist of sync and header information. The next 16 bit or 2 bytes, consist of arbitrary evaluation information which means that it is possible to be or not. The next 136 /256 bits contain information of sound channel. As you know a sound file may be mono or stereo and other bits are for sound data. To extract and unzip sound data, first we should extract frames information from header file and then decryption sound bits based on header data.
Decode MP3- Decryption MP3 Header
Az, we mentioned above, Header contain 32 bits which each bit has specific definition. If we categorize each bit by its color and definition, the result is below:
AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
11 bits (A) from bit 21 to bit 31: Identification bits (All of them are 1) are use to identify the start point of a frame.
2 bits (B) bit 19 and 20 are version header which has four types:
- If 00 à MPEG version 2.5
- If 01 à reserved
- If 10 à MPEG version 2
- If 11 à MPEG version 1
2 bits (C) bit 17 and 18 are layer descriptor:
- If 00 à reserved
- If 01 à Layer |||
- If 10 à Layer ||
- If 11 à Layer |
1-bit (D) bit 16 is for authentication:
- If 0 à The CRC part exists
- If 1 à The CRC part does not exist
4 bits (E) from bit 12 to bit 15 show the output bitrate according to the below table:
2 bits (F) bit 10 and bit 11 show the sample rate according to the below:
1-bit (G) bit 9 à Padding bit:
- If 0 à frame is not padded
- If 1 à frame is padded with one extra slot
1-bit (H) bit 8 à This single bit is only informative and private bit.
2 bits (I) bit 6 and bit 7 à These bits show channel mode:
- If 00 à Stereo
- If 01 à Joint stereo (Stereo)
- If 10 à Dual channel (two mono channels)
- If 11 à Single channel (Mono)
2 bits (J) bit 4 and bit 5 only use for extension mode in Joint Stereo.
1-bit (K) bit 3 à This bit shows the copyrighted status:
- If 0 à Audio is not copyrighted
- If 1 à Audio is copyrighted
1-bit (L) bit 2 à This bit shows the original media status
- If 0 à copy of original media
- If 1 à original media
2 bits (M) bit 0 and bit 1 à Emphasis
After four header bytes descriptor, depends on the status of the bit D in the bytes of the header, it is possible to exist two bytes of CRC or not. After these two arbitrary bytes, according to the type of the frame in bit6 or bit7 (single channel or two channel), there are 17- or 32-bytes information about channels and then sound data appears. Consider that in some negligible cases, frame may not have sound data. Four red bytes header and 32 blue bytes channel information are defined for a frame of a file in below picture. The other bytes are sound data. Consider that diagnostic bytes do not exist.
Decode MP3- Decryption sound data
First of all, you need to know about Huffman algorithm for decoding existence data. The principle of Huffman coding is a compressing method which founded by David Huffman. In this method all data are compressed around 20 to 90 percent. In this article we are not able to describe this method and for more information you can read it on the internet. as defined in this picture we should use Huffman tree.
How to build a MP3 player module
Hardware Description
As shown in this picture, we demand a STM32F103RET microcontroller to decode a MP3 file. All the process is in the chip and the hardware is simple.
To convert Digital data to Analog data we use internal (DAC) of microcontroller which able us to sample 1 megabit per second with 12-bit resolution. The SD memory card uses SPI interface to send and receive data to microcontroller.
Software Description
The software is written by Keil MDK compiler and can execute MP3 files with 320 kilobit rate and WAV files with higher resolution. Some parts of code are written in assembly language to increase the speed of MP3 file decoding. Transferring data to DAC is done by DMA to reduce the CPU transaction.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
int main(void){ RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); buffer_Init(); /* Uart init RX / TX buffers */ USART1_Init(); while(disk_initialize(0)==STA_NOINIT) /*Init SdCard Disk*/ { SET_LED(); }RESET_LED(); while(f_mount(0,&fs)!=RES_OK) /*Mount Fat File System*/ { SET_LED(); }RESET_LED(); printf("Zeus@sisoog.com\r\n"); MP3_PlayAudioFile("start.mp3"); /*Play Mp3 File*/ play_wave("start.wav"); /*Play Wave File*/ while(1) { } } |
According to the above software, after installing and configuration of the memory card, microcontroller’s code searches for start.mp3 file name in the memory card. If this file exist microcontroller execute it and after end of execution searches for start.wav file to play it.