Click here to Skip to main content
15,881,701 members
Articles / Desktop Programming / MFC
Article

DLL To Decode MP3 To WAV/PCM

Rate me:
Please Sign up or sign in to vote.
4.56/5 (19 votes)
12 Jul 20043 min read 381.4K   9.2K   83   98
Source code to produce a DLL that converts MP3 files to WAV or PCM. It is based on the open source library libmad.

Sample Image - madlldlib.gif

Introduction

libmad is a powerful open source library written in C that decodes MP3 files to uncompressed PCM data. However, it compiles as a static library, limiting its access to C/C++. I have written code (based on the source madlld) that compiles to a DLL, enabling other languages (i.e., C#, Visual Basic etc.) to access the libmad library using the simple interface I've provided.

Programming the API

The DLL source is well documented, so I will concentrate on using the API, and allow the curious to poke around the guts on their own (it is likely that the API is of most interest to most people). However, a brief overview of the source is in order.

madlldlib is codified into a single, simple interface, consisting of just one function, CbMpegAudioDecoder(), which takes the names of the input and output files, and a callback function as parameters. The callback function is passed information about the decoding process (such as total bytes converted and frame count) as it decodes, which in turn can be used to update decoding status in whatever interface the developer chooses.

There is a small file, test.cpp, included in the source which demonstrates the API. It is kept as simple as possible to make it easy to understand.

First, test.cpp defines a function called mycb(). This function is the callback (function pointer) that madlldlib will send decoding status to. It is within this function that you will update status in your application. test.cpp merely formats the output and prints it to STDOUT.

C++
void __stdcall mycb (unsigned long fcnt, 
   unsigned long bcnt, struct mad_header *mhdr) {

    /*
     * If this is the first iteration (frame count is one)
     * then print out the MP3 layer information. Using this logic
     * one can retrieve information about the MP3 file, such as 
     * channels, layer, etc., that might be useful to the calling
     * code.
     */    
    if (fcnt == 1) {
        printf("frame:%d, tot. bytes:%d, layer:%d\n",fcnt,bcnt,mhdr->layer);
    }
    else {
        printf("frame:%d, tot. bytes:%d\n",fcnt,bcnt);
    }
    
}

As you can see, the output is very simple. You must define a function in your code with exactly the parameter types that mycb() has defined above (the names of the parameters, obviously, are up to you).

Next, we move into the main() function. After checking arguments and handling filenames, it calls the decoding function CbMpegAudioDecoder() with the given parameters.

C++
/* call the decoding function with input file,
 * output file, and callback (defined above). */
if (strcmp(argv[2], "pcm")==0) {        /* output raw PCM */
    status = CbMpegAudioDecoder(argv[1], outf, 0, statmsg, mycb);
}
else {                                    /* output as WAV */
    status = CbMpegAudioDecoder(argv[1], outf, 1, statmsg, mycb);
}

Note that it passed our callback function mycb() to CbMpegAudioDecoder(), which will send it information every decoding loop. The other item of interest is the parameter statmsg, which is used to relay any error messages our function may encounter. If, say, the MP3 file is corrupt, this message will end up in statmsg, which you can use to pass on to the user of your application.

The two files, armslen.cpp and armslen_test.cpp, give an example of decoding while sending status via a named pipe. This way you do not have to use madlldlib as a DLL in your project if you don't desire to. armslen.cpp compiles to an executable that takes parameters similar to test.cpp (input filename, output filename, output format). It then generates a named pipe server, and waits until a client program connects (armslen_test.cpp). Once the client makes the connection, armslen.cpp sends the decoding update status to the named pipe while it decodes the MP3. The client can then pick up, parse, and use the status to update its interface.

Source and Contact

The most current code will always be here.

If you have comments or questions, you can direct them to the mailing list madlldlib-dev.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUsing madlldlib for in-memory conversion Pin
leyandrew26-Oct-04 8:22
leyandrew26-Oct-04 8:22 
GeneralRe: Using madlldlib for in-memory conversion Pin
gilad-ap27-Oct-04 4:44
gilad-ap27-Oct-04 4:44 
GeneralRe: Using madlldlib for in-memory conversion Pin
gilad-ap26-Jun-05 16:14
gilad-ap26-Jun-05 16:14 
GeneralConversion file format Pin
Anonymous19-Sep-04 20:29
Anonymous19-Sep-04 20:29 
GeneralYour correct Pin
RJSoft17-Aug-04 10:46
RJSoft17-Aug-04 10:46 
GeneralRe: Your correct Pin
gilad-ap18-Aug-04 4:05
gilad-ap18-Aug-04 4:05 
GeneralTell me whats the easiest to decode mp3 to wav Pin
himanshu_8316-Aug-04 5:25
himanshu_8316-Aug-04 5:25 
GeneralRe: Tell me whats the easiest to decode mp3 to wav Pin
RJSoft17-Aug-04 6:40
RJSoft17-Aug-04 6:40 
Hello Himanshu;

>>I am not getting any success, in my program on how to play mp3.Please Help.

This particular post is about converting a mp3 file to a wav. Not necessarily for playing the mp3 but for the purpose of burning the music to cd disk.

I dont know if your trying to play the buffered output of the conversion. If this is true then you might try to setup wave header information and play the wave with low level api like waveOut.

But if all your really trying to do is play your mp3 files. Then all you really need to do is use mciSendString.

To gilad-anni-padda; My apology for stepping on your post. I am sincerley hoping that you will reply to my inquiry about porting mpg123 to Windows.

I have a deadline coming. My software is being packaged this September. I would rather use the mpg123 library since it will avoid any legal GNU complications.

Thanks RJ
GeneralRe: Tell me whats the easiest to decode mp3 to wav Pin
gilad-ap18-Aug-04 4:08
gilad-ap18-Aug-04 4:08 
GeneralRe: Tell me whats the easiest to decode mp3 to wav Pin
RJSoft19-Aug-04 11:00
RJSoft19-Aug-04 11:00 
GeneralRe: Tell me whats the easiest to decode mp3 to wav Pin
gilad-ap19-Aug-04 17:28
gilad-ap19-Aug-04 17:28 
GeneralRe: Tell me whats the easiest to decode mp3 to wav Pin
RJSoft20-Aug-04 0:34
RJSoft20-Aug-04 0:34 
GeneralRe: Tell me whats the easiest to decode mp3 to wav Pin
RJSoft20-Aug-04 7:44
RJSoft20-Aug-04 7:44 
GeneralRe: Tell me whats the easiest to decode mp3 to wav Pin
RJSoft2-Sep-04 19:35
RJSoft2-Sep-04 19:35 
GeneralRe: Tell me whats the easiest to decode mp3 to wav Pin
Anonymous8-Sep-04 6:32
Anonymous8-Sep-04 6:32 
GeneralHi Pin
RaviChukka18-Jul-05 1:08
RaviChukka18-Jul-05 1:08 
GeneralGNU GPL Pin
richard9914-Jul-04 0:21
richard9914-Jul-04 0:21 
GeneralRe: GNU GPL Pin
gilad-ap14-Jul-04 9:41
gilad-ap14-Jul-04 9:41 
GeneralRe: GNU GPL Pin
gilad-ap17-Jul-04 8:26
gilad-ap17-Jul-04 8:26 
GeneralRe: GNU GPL Pin
richard9929-Jul-04 15:37
richard9929-Jul-04 15:37 
GeneralRe: GNU GPL Pin
gilad-ap4-Aug-04 12:02
gilad-ap4-Aug-04 12:02 
GeneralPiping Pin
RJSoft11-Aug-04 12:11
RJSoft11-Aug-04 12:11 
GeneralRe: Piping Pin
gilad-ap13-Aug-04 4:50
gilad-ap13-Aug-04 4:50 
GeneralRe: Piping Pin
Cookie8083-Oct-04 18:50
Cookie8083-Oct-04 18:50 
GeneralRe: Piping Pin
gilad-ap4-Oct-04 10:12
gilad-ap4-Oct-04 10:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.