简体   繁体   English

在应用FFT之前从wav文件读取数据

[英]Read data from wav file before applying FFT

it's the first time when I'm working with wave files. 这是我第一次使用wave文件。 The problem is that I don't exactly understand how to properly read stored data. 问题是我不完全了解如何正确读取存储的数据。 My code for reading: 我的阅读代码:

    uint8_t* buffer = new uint8_t[BUFFER_SIZE];
    std::cout << "Buffering data... " << std::endl;
    while ((bytesRead = fread(buffer, sizeof buffer[0], BUFFER_SIZE / (sizeof buffer[0]), wavFile)) > 0)
    {
        //do sth with buffer data
    }

Sample file header gives me information that data is PCM (1 channel) with 8 bits per sample and sampling rate is 11025Hz. 样本文件头为我提供的信息是,数据为PCM(1通道),每个样本8位,采样率为11025Hz。

Output data gives me (after updates) values from 0 to 255, so values are proper PCM values for 8bit modulation. 输出数据为我提供(更新后)从0到255的值,因此这些值是用于8位调制的正确PCM值。 But, any idea what BUFFER_SIZE would be prefferable to correctly read those values? 但是,您知道什么BUFFER_SIZE可以正确读取这些值吗?

WAV file I'm using: http://www.wavsource.com/movies/2001.htm (daisy.wav) 我正在使用的WAV文件: http ://www.wavsource.com/movies/2001.htm(daisy.wav)

TXT output: https://paste.ee/p/pXGvm TXT输出: https : //paste.ee/p/pXGvm

You've got two common situations. 您有两种常见情况。 The first is where the WAV file represents a short audio sample and you want to read the whole thing into memory and manipulate it. 第一个是WAV文件代表一个简短的音频样本,您想将整个内容读入内存并进行操作。 So BUFFER_SIZE is a variable. 因此BUFFER_SIZE是一个变量。 Basically you seek to the end of the file to get its size, then load it. 基本上,您寻求文件的末尾以获取其大小,然后加载它。

The second common situation is that the WAV file represent fairly long audio recording, and you want to process it piecewise, often by writing to an output device in real time. 第二种常见情况是WAV文件表示相当长的音频记录,并且您希望通过实时写入输出设备来分段处理它。 So BUFFER_SIZE needs to be large enough to hold a bite-sized chunk, but not so large that you require excessive memory. 因此,BUFFER_SIZE需要足够大以容纳一口大小的块,但又不能太大以至于需要过多的内存。 Now often the size of a "frame" of audio is given by the output device itself, it expects 25 samples per second to synchronise with video or something similar. 现在,音频“帧”的大小通常由输出设备本身提供,它期望每秒25个样本与视频或类似内容同步。 You generally need a double buffer to ensure that you can always meet the demand for more samples when the DAC (digital to analogue converter) runs out. 通常,您需要一个双缓冲器,以确保当DAC(数模转换器)用完时,您始终可以满足更多样本的需求。 Then on giving out a sample you load the next chunk of data from disk. 然后在给出样本时,从磁盘加载下一个数据块。 Sometimes there isn't a "right" value for the chunk size, you've just got to go with something fairly sensible that balances memory footprint against the number of calls. 有时,块大小没有一个“正确的”值,您只需要考虑一些合理的方法即可平衡内存占用和调用次数。

If you need to do FFT, it's normal to use a buffer size that is a power of two, to make the fast transform simpler. 如果需要进行FFT,通常使用2的幂的缓冲区大小来简化快速转换。 Size you need depends on the lowest frequency you are interested in. 您需要的大小取决于您感兴趣的最低频率。

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

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