Hardware platform: STM32F746G-DISC
Development platform: KEIL_5.29
I will not introduce the detailed structure of MP3 files here , but only introduce how to use the MP3 decoding library. If you need to know more about MP3 files, you can refer to this website : https://www.cnblogs.com/ranson7zop/p/7655474.html
This decoding library can be downloaded from the ST company's official website by searching for the keyword " x-cube-audio ". After downloading and decompressing, enter this path: STM32CubeExpansion_Audio_V1.1.1\Middlewares\ST\STM32_Audio\Codecs , and you can see that there are two folders, as shown in the figure below:
Enter the directory of the decoding library, there will be 3 folders, as shown below:
Enter the lib folder, there will be 3 decoding libraries, corresponding to different platforms
I am using STM32F7 here , and of course I choose the lib file of the M7 platform .
Next, create a KEIL project, debug the SD card driver and the audio chip WM8994 driver first, then copy the SpiritDSP_MP3_Dec folder to the project directory, add the lib file in keil , and set the include path, then the MP3 decoding library is added.
Next, let's explain the functions used for MP3 decoding, which can be found in the spiritMP3Dec.h file.
1. void SpiritMP3DecoderInit(
TSpiritMP3Decoder *pDecoder,
fnSpiritMP3ReadCallback* pCallbackFn,
fnSpiritMP3ProcessCallback *pProcessFn,
void * token )
Function: Initialize the MP3 decoder. After each decoding, before decoding a new MP3 file, you must call this function again to complete the initialization of the MP3 decoder.
parameter:
TSpiritMP3Decoder *pDecoder :
Pointer to the decoder structure to be initialized. All decoder variables are stored in this structure. The decoder does not use non-constant static or global variables, so all functions are reentrant.
fnSpiritMP3ReadCallback* pCallbackFn :
Pointer to callback function, the decoder will use this function to retrieve the input mp3 data.
fnSpiritMP3ProcessCallback *pProcessFn :
Callback function pointer. The decoder will use this function to process MDCT coefficients when decoding Layer3 files . This function is not needed at present, so set it to NULL .
void * token :
Optional arguments passed to the callback function.
2. typedef unsigned int (fnSpiritMP3ReadCallback)
(
void * pMP3CompressedData,
unsigned int nMP3DataSizeInChars,
void * token
)
Function: The decoder uses it to read MP3 file data, this function must be implemented.
parameter:
void * pMP3CompressedData :
Store the read mp3 data.
unsigned int nMP3DataSizeInChars :
The size of the MP3 data to be read .
void * token :
Optional parameters for the callback function.
3. unsigned int SpiritMP3Decode (
TSpiritMP3Decoder *pDecoder,
short *pPCMSamples,
unsigned int nSamplesRequired,
TSpiritMP3Info * pMP3Info
);
Function: Decode each frame of MP3 data and store the decoded data in pPCMSamples . If the return value of this function is less than nSamplesRequired , the file pointer has reached the end of the MP3 file and the MP3 file decoding is complete, otherwise continue to call this function for decoding.
parameter:
TSpiritMP3Decoder *pDecoder :
Pointer to an initialized decoder structure.
short *pPCMSamples :
Stores the decoded output PCM data.
unsigned int nSamplesRequired :
The number of samples per audio frame in an MP3 file
TSpiritMP3Info * pMP3Info :
After decoding is completed, the decoding information of the current audio frame is stored. The IsGoodStream member in the TSpiritMP3Info structure is used to determine whether the decoding of the current audio frame is successful. If the decoding is successful, it is 1 , and if it fails, it is 0 .
Here is an introduction to the usage process of the decoding library. For specific codes, please refer to my program.