C++ 读取 MP3 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2968656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading MP3 files
提问by Midas
I want to read MP3 files in C++ and I prefer to 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.
我想用 C++ 读取 MP3 文件,我更喜欢为此编写自己的代码。基本上是为了了解文件类型的工作原理。我想读取 MP3 文件的所有十六进制数据位并让我的扬声器播放它。:) 我不知道从哪里开始,因为我还不知道数据实际上是如何存储到 MP3 文件中的。
Thanks for your help
谢谢你的帮助
回答by jball
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.
首先阅读mp3 文件的结构。然后,如果您仍然感兴趣,请找到有关如何解码每帧中的音频数据的好教程。它非常复杂,因此您需要有相当多的时间从头开始。
回答by Matti Virkkunen
You can buy the specification for the MP3 format from here. It's about 160EUR.
您可以从这里购买 MP3 格式的规范。大约 160 欧元。
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)
从编写一个可以播放 .wav 文件的播放器开始怎么样?(任何合理的 .wav 文件,请注意,考虑到文件格式和不同的音频格式)
回答by RobinJ
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 reservtheitroad") 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.
一个 MP3 文件由多个 MP3 帧组成,这些帧由一个头部和一个数据块组成。该帧序列称为基本流。帧不是独立的项目(“字节库”),因此不能在任意帧边界上提取。MP3 数据块包含频率和幅度方面的(压缩)音频信息。该图显示 MP3 标头由一个同步字组成,用于标识有效帧的开头。后面跟着一个表示这是MPEG标准的位和两个表示使用第3层的位;因此 MPEG-1 音频第 3 层或 MP3。此后,值将有所不同,具体取决于 MP3 文件。ISO/IEC 11172-3 定义了报头每个部分的值范围以及报头规范。今天大多数 MP3 文件都包含 ID3 元数据,它位于 MP3 帧之前或之后;如图所示。
Source code of LAME
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/downloadI 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.
LAME 是一个 MP3 库,但它也是开源的,这意味着您可以下载源代码并查看它。 http://sourceforge.net/projects/lame/files/lame/3.98.4/lame-3.98.4.tar.gz/download我自己看了一下,好像是C或者C++写的,所以你很幸运。如果您检查 LAME 的源代码,并了解它的工作原理,也许您可以成功制作自己的 MP3 库。请注意,LAME 不是用于播放 MP3 文件的库,而是用于对其进行编码的库。但是因为它创建 MP3 而不是读取它们,我认为结构应该比任何其他 MP3 播放器库更清晰。
Explanation of MP3 file format
MP3文件格式说明
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/
这似乎也解释了 MP3 的工作原理,但它是纯理论的,因此您需要很大的耐心。 http://www.mp3-converter.com/mp3codec/
回答by M. Williams
Well, it would be tough :)
嗯,这会很艰难:)
Anyway, your application should consist of two parts - decompression library / routinesand your main backend, which would take some decompressed data block and actually play it.
无论如何,您的应用程序应该由两部分组成 -解压缩库/例程和您的主后端,这将需要一些解压缩的数据块并实际播放。
I would recommend "Data Compression"book by David Salomonto understand how your decoder should actually do it. The variants for your second parts are, unfortunately, too broad to cover them in one sentence.
我会推荐David Salomon 的“数据压缩”一书,以了解您的解码器实际上应该如何做。不幸的是,您的第二部分的变体过于广泛,无法用一句话来涵盖它们。
You could also try investigating some open-source mp3
decoders / decoding libslike this one.
回答by Daniel Mo?mondor
You'll want to use some decoder library for your task, something like http://sourceforge.net/projects/mpg123net/
您需要为您的任务使用一些解码器库,例如http://sourceforge.net/projects/mpg123net/
There is also MAD decoder... http://www.underbit.com/products/mad/
还有MAD解码器... 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...
要正确读取 mp3 文件涉及很多数学,我想你真的不想进入那个......
回答by Neil N
If you insist on doing it yourself, you pretty much have to start with the spec:
如果你坚持自己做,你几乎必须从规范开始: