Click here to Skip to main content
Click here to Skip to main content

DLL To Decode MP3 To WAV/PCM

, 12 Jul 2004
Rate this:
Please Sign up or sign in to vote.
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.

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.

    /* 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

About the Author

gilad-ap

United States United States
No Biography provided

Comments and Discussions

 
QuestionHow can we use it with C# Pinmemberbrenntengel@yahoo.fr13-May-14 9:15 
QuestionMp3 to wav PinmemberKalpana Volety30-Apr-14 8:02 
GeneralMy vote of 5 PinmemberRevnge_SevnFold20-Nov-12 4:25 
Generalunable to load DLL 'madlldlib.dll' PinmemberMuwanga Simon Peter27-Mar-10 7:48 
QuestionHow to convert 24 bit, 48 KHz, PCM Stereo audio file to 16 bit, 48 KHz, PCM Stereo PinmemberProgrammervcpp16-Nov-08 20:46 
GeneralEndian Pinmemberds494023-Sep-08 16:42 
GeneralMemory access error in C# Pinmemberpeter.roca16-Sep-08 12:14 
Questionhow to build a new dll using this source code? Pinmemberflyhawkxjtu8-Apr-08 4:18 
hi all!
i want to do some change to this source code and want to build a new dll.
how can i do this?
thank you all.
 
eating cattle,
speaking catttle language,
doing cattle-able things,
become a cattle-alike man

Questionhow to config this program to 16 bit output? Pinmemberflyhawkxjtu8-Apr-08 1:32 
GeneralConvert mp3 file to byte array Pinmemberramyam20-Mar-08 21:25 
GeneralRe: Convert mp3 file to byte array Pinmembergilad-ap4-Apr-08 7:05 
Questionwhere i find "mad.h" ??? Pinmembernyamnyam15-Jan-08 2:01 
AnswerRe: where i find "mad.h" ??? Pinmembergilad-ap4-Apr-08 6:22 
GeneralRe: where i find "mad.h" ??? PinmemberFeroz Gora10-Feb-11 13:04 
GeneralRe: where i find "mad.h" ??? Pinmembersungengyu16-Mar-11 20:58 
GeneralRe: where i find "mad.h" ??? Pinmembersungengyu16-Mar-11 20:58 
Generalcrash with strcpy and strcat in VC++ Pinmemberaddicted_to_cpp16-Nov-07 11:25 
QuestionDecode mp3 to wav on visual basic 6 PinmemberMario Zaizar29-Jan-07 9:38 
AnswerRe: Decode mp3 to wav on visual basic 6 Pinmemberaddicted_to_cpp16-Nov-07 11:20 
AnswerRe: Decode mp3 to wav on visual basic 6 - code sample for DLL Pinmembertsargeant25-Nov-09 22:29 
GeneralRe: Decode mp3 to wav on visual basic 6 - code sample for DLL PinmemberPappsegull13-Feb-11 15:29 
Generalconvert and return blocks[] Pinmemberdonperry26-Dec-06 6:01 
GeneralRe: convert and return blocks[] Pinmembergilad-ap29-Jan-07 9:45 
GeneralLEC Pinmemberchenjunxuman19-Oct-06 14:37 
GeneralExample in C# PinmemberSuPeRRuZaFa19-Jun-06 2:05 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 13 Jul 2004
Article Copyright 2004 by gilad-ap
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid