Depending on how you use the platform, you can open a MP3 file in a variety of ways, minimad.c a way to open the sample file under Unix, and it's not hard to rewrite it as Windows:
CFile file;
if(!file.Open("E:\\A.mp3",CFile::modeRead|CFile::shareDenyRead,NULL))
{
cout<<"can not open file!"<<endl;
return -1;
}
DWORD file_size=file.GetLength();
DWORD file_readerd=0;
if(file_size==0)
{
cout<<"File Error!"<<endl;
return -1;
}
unsigned char * file_buffer=(unsigned char *)malloc(file_size);
do
{
file_readerd=file.Read(file_buffer,file_size);
} while (file_readerd);
free(file_buffer);
file.Close();
The entire MP3 file is mapped to the File_buffer. This file_buffer can be passed on to the decoder decoder. However, the parameters of Mad_decoder_init () and Mad_decoder_run () do not have a place in which to pass this pointer. Minimad,c uses one of the simplest "custom message structures" (private messages structure) to pass the start and length of the buffer to the Mad_decoder structure by Mad_decoder_init (). Then decode the Mad_decoder with Mad_decoder_run ():
static
int decode(unsigned char const *start, unsigned long length)
{
struct buffer buffer;
struct mad_decoder decoder;
int result;
/* initialize our private message structure */
buffer.start = start;
buffer.length = length;
/* configure input, output, and error functions */
mad_decoder_init(&decoder, &buffer,
input, 0 /* header */, 0 /* filter */, output,
error, 0 /* message */);
/* start decoding */
result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
/* release the decoder */
mad_decoder_finish(&decoder);
return result;
}
Combining the previous analysis, we get the general trend of data: first, we call the Mad_stream_buffer () function by the input callback function to bind the MP3 file to a mad_stream structure, and then the decoder separates the frame from the stream. Finally decode a single frame to get PCM.