简体   繁体   English

如何使用fread和fwrite从文件读取pcm样本?

[英]how to read pcm samples from a file using fread and fwrite?

I want to read pcm samples from a file using fread and want to determine the signal strength of the samples.How do I go about it? 我想使用fread从文件中读取pcm样本,并确定样本的信号强度,该如何处理?

For reading, how many bytes constitute 1 pcm sample? 为了读取,多少个字节构成1 pcm样本? Can I read more than 1 pcm sample at a time? 我一次可以读取超过1 pcm的样本吗? This is for WAV and AAC files. 这适用于WAV和AAC文件。

You have to understand that WAV-files (and even more so AAC-files) are not all the same. 您必须了解WAV文件(甚至更多的是AAC文件)都不尽相同。 I will only explain about WAV-files, you'll hopefully understand how it is with AAC-files then. 我只会解释WAV文件,希望您能理解AAC文件的情况。 As you pointed out, a WAV-file has PCM-encoded data. 如您所指出的,WAV文件具有PCM编码的数据。 However that can be: 8-bit, 16-bit, 32-bit, ...Mono, Stereo, 5.1, ...,8kHz, 16kHz, 44.1kHz, etc. Depending on these values you have to interpret the data (eg when reading it with the fread()-function) differently. 但是可以是:8位,16位,32位,...单声道,立体声,5.1,...,8kHz,16kHz,44.1kHz等。根据这些值,您必须解释数据(例如,当用fread()函数读取它的方式不同时。 Therefore WAV-files have a header. 因此,WAV文件具有标头。 You have to read that header first, in the standard way (I do not know the details). 您必须先以标准方式阅读该标头(我不知道详细信息)。 Then you know how to read the actual data. 然后,您知道如何读取实际数据。 Since it is not that easy, I suggest you use on of the libraries out there, that read WAV-files for you, eg http://www.mega-nerd.com/libsndfile/ . 由于并非如此,因此建议您使用其中的库,这些库会为您读取WAV文件,例如http://www.mega-nerd.com/libsndfile/ Of course you can also google or use SO to find others. 当然,您也可以使用google或使用SO查找其他人。 Or you do it the hard way and find out how WAV-file headers look like and decode that data first, then move on to the actual PCM-encoded data. 或者,您可以通过艰苦的方式来完成工作,然后找出WAV文件头的外观并首先对该数据进行解码,然后继续进行实际的PCM编码数据。

I have no experience tackling with WAV file, but once read data from mp3 file. 我没有处理WAV文件的经验,但是曾经从mp3文件中读取数据。 As to the mp3 file, each 576 pcm samples are encoded into a frame. 对于mp3文件,每个576 pcm样本均被编码为一个帧。 All the frames are stored directly into a file alone with some side information. 所有帧都直接与一些附带信息一起单独存储到文件中。 When processing encoded data, I read binary data from the mp3 file and stored in a buffer, decoding buffered data and extract what is meaningful to me. 处理编码数据时,我从mp3文件中读取二进制数据并存储在缓冲区中,对缓冲的数据进行解码并提取对我有意义的内容。

I think processing wav file(which stores pcm samples per my understand) is not quite different. 我认为处理wav文件(根据我的理解,该文件存储pcm样本)并没有太大不同。 You can read the binary data from file directly and perform some transformation according to wav encoding specification. 您可以直接从文件读取二进制数据,并根据wav编码规范执行一些转换。

The file itself does not know what kind of data even what format of the data is in it. 文件本身甚至不知道数据的格式,甚至不知道数据的格式。 You can take everything in a file as bytes(even plain text), read byte from file interpreting the binary data yourself. 您可以将文件中的所有内容作为字节(甚至是纯文本),从文件中读取字节,自己解释二进制数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM