View Full Version : MP3 encoder/decoder?

08-12-2010, 08:01 PM
Hey people,

I want to do a little audio project that involves cutting mp3's. Therefore I need not only a decoder but also an encoder. Does anyone know a good library? I prefer to use one single lib that can do both without too much hassle.

Thanks. :)

08-12-2010, 08:37 PM
I've just looked at LAME:

Download link (http://sourceforge.net/projects/lame/files/lame/3.98.4/)

But there's one thing I cant figure out. There is a pascal header in there, containing the following functions:

Function beInitStream(var pbeConfig: TBE_CONFIG; var dwSample: LongWord; var dwBufferSize: LongWord; var phbeStream: THBE_STREAM ): BE_Err; cdecl; external 'Lame_enc.dll';
//Function beEncodeChunk(hbeStream: THBE_STREAM; nSamples: LongWord; pSample: PSmallInt;pOutput: PByte; var pdwOutput: LongWord): BE_Err; cdecl; external 'Lame_enc.dll';
Function beEncodeChunk(hbeStream: THBE_STREAM; nSamples: LongWord; var pSample;var pOutput; var pdwOutput: LongWord): BE_Err; cdecl; external 'Lame_enc.dll';
Function beDeinitStream(hbeStream: THBE_STREAM; var pOutput; var pdwOutput: LongWord): BE_Err; cdecl; external 'Lame_enc.dll';
Function beCloseStream(hbeStream: THBE_STREAM): BE_Err; cdecl; external 'Lame_enc.dll';
Procedure beVersion(var pbeVersion: TBE_VERSION); cdecl; external 'Lame_enc.dll';

However, there's also an include directory in the archive, containing a C++ header. I quickly scanned through it and I found out that it enables you to decode MP3 aswell. For example:

/* required call to initialize decoder */
hip_t CDECL hip_decode_init(void);

/* cleanup call to exit decoder */
int CDECL hip_decode_exit(hip_t gfp);

int CDECL hip_decode( hip_t gfp
, unsigned char * mp3buf
, size_t len
, short pcm_l[]
, short pcm_r[]

/* same as hip_decode, and also returns mp3 header data */
int CDECL hip_decode_headers( hip_t gfp
, unsigned char* mp3buf
, size_t len
, short pcm_l[]
, short pcm_r[]
, mp3data_struct* mp3data

Just wondering why the pascal header doesn't have these defs. Is there any translation somewhere?


08-12-2010, 11:58 PM
I'm not aware of any newer units, but it looks like those are newer versions of some lame_decode_* functions

But should be easy enough :)

lameInt = longint; // Use cint from ctypes in fpc, or smallint on linux

mp3data_struct = packed record
framesize: lameInt;

nsamp: longword;

framenum: lameInt;

function hip_decode_init: pointer; cdecl; external 'Lame_enc.dll';
function hip_decode_exit(gfp: pointer): longint; cdecl; external 'Lame_enc.dll';
function hip_decode(gfp: pointer; mp3buf: pointer; len: longint; PcmL, PcmR: pword): longint; cdecl; external 'Lame_enc.dll';
function hip_decode_headers(gfp: pointer; mp3buf: pointer; len: longint; PcmL, PcmR: pword; var mp3data: mp3data_struct): longint; cdecl; external 'Lame_enc.dll';

09-12-2010, 12:21 AM
Thanks. :)

I'm not very good at porting C(++) yet. I'm afraid to make subtle mistakes that will cost me hours of my precious time to fix. Well, maybe I should just try it someday. :)

As you may expect: The actual header is way bigger that that sample. About 1200 lines. It might be good to have a pascal version of it. However, the decode functionality of LAME is based on mpg123 code that was released under GPL, so some people may want to go down other paths instead.

Thus far, I think that using mpg123 for decoding and LAME for encoding might be "easiest". I can also attempt a header port and use LAME for both encoding and decoding.

Does anyone know any alternatives? I'm sure I have more options, but I'd like to know more of them. ;)

09-12-2010, 01:24 AM
You could write a decoder yourself... :P

That might be a too big undertaking. I'm not well aware of any other good decoders

I would write one if it wasn't so hard to get my hands on a real and complete specification. Just for the sake of getting rid of irritating semi-crossplatform dependencies. I got MP1 working, but I haven't even been able to test it since I can't find an encoder for it... :)

09-12-2010, 10:56 AM
lol. This supposed to be a project for a few weeks, so writing my own decoder is out of the question. ;)

09-12-2010, 11:32 PM
mpg123 is a excellent decoder, but you should stick with lame, it's both a encoder and decoder.

10-12-2010, 10:26 AM
I agree. Using one lib is nicer. But do you have those headers that let me use the encode functionality of LAME?

10-12-2010, 03:47 PM
Maybe this one?
http://www.koders.com/delphi/fid42112A8BB9D5ED5F8E5B4B41966F20FB8C0BD9BB.aspx?s =md5

10-12-2010, 07:16 PM
Erm... I've been following this thread quite closely since I find it may be extremely useful for some of my code - so I looked into the above link and can I just say that if you use a platform that has the Windows unit, like windows, all is nice. However, on linux I noticed that by default there is no Libc supplied with FPC (or at least not this version) so i tried a few of the units that are suggested by the FPC wiki to replace it and no luck. Any solutions for us non-windows folk?

Just a thought but unless I'm missing something here (other than clib) this is quite annoying =p

Edit: aaahahaaaa... Found the linking error:

As it happens on fpc 2.4.0, Libc is NOT compiled and its in

now thats nice, but that folder is protected - you have to sudo or gksudo to modify stuff there. If you use an IDE such as geany or fp and don't sudo or gksudo then it tries to compile LibC - fails and then tries to read LibC and fails and thus says there is no LibC. Annoying.

Or alternatively, I'm told you can go to http://sourceforge.net/projects/freeclx/ and get the kylix utils there. They should also contain LibC as part of gLibC... Sorry for the unnecessary post, but some may find this useful.

Final Edit: And if you sudo it, you still get some errors while linking ending in a super massive epic fail. Solution: Copy ALL files from libc folder to same folder you have lame.pas and compile it. Should work with less complaints... Don't know if you all have to go through this but this seems to be the only solution I have found on Ubuntu 10.10 Maverick with latest updates (all update sources except unsupported)

10-12-2010, 08:39 PM
code_glitch, are you talking about clib or libc? And in case of libc, are you talking about the legacy Kylix unit libc, or the c runtime library? :)

If it's because you need to link to libc, then you shouldn't look for a fpc unit. Then simply write {$l c} in your code, or add -k-lc to the compiler command line, or use dynamic linking

10-12-2010, 10:17 PM
I was referring to LibC. As it turns out they are two different things: and I got confused. I just posted all that to clear it up for any other clutsy people like me :) and cheers for those flags. Will definitely use them rather than my cumbersome procedure.

13-12-2010, 10:08 AM
Maybe it isn't such a good idea to use an encoder and decoder for this. MP3 is a lossy format, and encoding it again will have bad effects on quality. I only need a lib to read/write MP3's container format. I'll leave the individual frames intact, so no quality is lost.

I've been looking into the FFmpeg library. I think it has what I need, but there aren't any good tutorials on it. ???

13-12-2010, 07:52 PM
Ah, good ol FFMpeg. I wondered if they had a lib with pascal headers?? I only knew it in the video/audio trans/en/de-coding.... Tell me if you find a good tut - dying to play with this in pascal :)

16-12-2010, 05:57 PM
I wrote a small program to parse mp3 files down to frame level. Cutting the file should be just a simple matter of omitting the undesired frames, but I haven't tried it. I hope it'll be helpful: http://imcold.evilhosting.org/files/mp3parse.pas.html

16-12-2010, 06:12 PM
Thanks. may come in handy. ;)

I've found some FFmpeg headers here :D

http://ultrastardx.svn.sourceforge.net/viewvc/ultrastardx/trunk/src/lib/ffmpeg/avcodec.pas?revision=2763&view=markup (Link)

26-06-2011, 06:56 PM
I have no interest in writing MP3's, however i'd like to use them with OpenAL that i'm exploring. There was a sample application with mpg123 but i wasn't able to find pascal header to it in any of the links in below (mpg123.pas):