user3038744
user3038744

Reputation: 86

Read data from wav file before applying FFT

it's the first time when I'm working with wave files. 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.

Output data gives me (after updates) values from 0 to 255, so values are proper PCM values for 8bit modulation. But, any idea what BUFFER_SIZE would be prefferable to correctly read those values?

WAV file I'm using: http://www.wavsource.com/movies/2001.htm (daisy.wav)

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

Upvotes: 1

Views: 373

Answers (1)

Malcolm McLean
Malcolm McLean

Reputation: 6404

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. So BUFFER_SIZE is a variable. 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. So BUFFER_SIZE needs to be large enough to hold a bite-sized chunk, but not so large that you require excessive memory. 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. 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. 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. Size you need depends on the lowest frequency you are interested in.

Upvotes: 0

Related Questions