Reading MP3 files [closed]
I want to read MP3 files in C++ and I prefer t开发者_Python百科o write my own code for this. Basically to learn how the filetype works. I want to read all the bits of hex data of a MP3 file and have my speakers play it. :) I have no idea where to start since I don't yet know how data is actually stored into a MP3 file.
Thanks for your help
Start by reading up on the structure on an mp3 file. Then, if you're still interested, find a good tutorial on how to decode the audio data in each frame. It's pretty complex, so you'll need to have a fair bit of time to do it from scratch.
You can buy the specification for the MP3 format from here. It's about 160EUR.
Oh, and by the way, it's not "hex data". If you're still at the point where you refer to any non-human readable data as "hexadecimal" (which is a number system), delving head-first into a binary data format that involves complex decoding/encoding algorithms might be a bit too much for you for the time being.
How about starting by writing a player that can play .wav files? (Any reasonable .wav file, mind you, taking into account the file format and the different audio formats)
I did a bit of research, maybe it will help you.
File structure
An MP3 file is made up of multiple MP3 frames, which consist of a header and a data block. This sequence of frames is called an elementary stream. Frames are not independent items ("byte reservoir") and therefore cannot be extracted on arbitrary frame boundaries. The MP3 Data blocks contain the (compressed) audio information in terms of frequencies and amplitudes. The diagram shows that the MP3 Header consists of a sync word, which is used to identify the beginning of a valid frame. This is followed by a bit indicating that this is the MPEG standard and two bits that indicate that layer 3 is used; hence MPEG-1 Audio Layer 3 or MP3. After this, the values will differ, depending on the MP3 file. ISO/IEC 11172-3 defines the range of values for each section of the header along with the specification of the header. Most MP3 files today contain ID3 metadata, which precedes or follows the MP3 frames; as noted in the diagram.Source code of LAME
LAME is an MP3 libary, but it's also open-source, wich means that you can download the source code and look into it. http://sourceforge.net/projects/lame/files/lame/3.98.4/lame-3.98.4.tar.gz/download I took a look at it myself, and it seems to be written in C or C++, so you're lucky. If you examine the source code of LAME, and understand how it works, maybe you can succeed in making an MP3 library of your own. Just note that LAME is not a library for playhing MP3 files, but for encoding them. But as it creates MP3's in stead of reading them, I think the structure should be a bit more clear then with any other MP3 player library.
Explanation of MP3 file format
This also seems to explain how MP3 works fairly well, but it's pure theory, so you'll need a lot of patience. http://www.mp3-converter.com/mp3codec/
Well, it would be tough :)
Anyway, your application should consist of two parts - decompression library / routines and your main backend, which would take some decompressed data block and actually play it.
I would recommend "Data Compression" book by David Salomon to understand how your decoder should actually do it. The variants for your second parts are, unfortunately, too broad to cover them in one sentence.
You could also try investigating some open-source mp3
decoders / decoding libs like this one.
If you insist on doing it yourself, you pretty much have to start with the spec:
http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html
You'll want to use some decoder library for your task, something like http://sourceforge.net/projects/mpg123net/
There is also MAD decoder... http://www.underbit.com/products/mad/
To properly read mp3 file there is lots of math involved, and I guess that you really don't want to go into that one...
精彩评论