Search code examples
c++caudiovoicevoice-recognition

Obtaining real values from a WAV file c++


So I'm working on a very importan school project. I know now how to read everything from a WAVE file, including the data. The thing is that not only I need the real~ data values, but the sign as well. The file is 16 bps but I have no idea how to get an actual value, like, -365 or +19934. This is what I do so far

leer = fread(&sbyte, 1, X, audio);

What number should I put instead of "X". 4?

I defined sbyte as a signed char, but signed char goes only from -128 to 127, which means that it doesn't give me the information needed.

I need those numbers to do some analysis. If you could help me here, cause I'm way too lost.

Thanks.


Solution

  • From this page: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

    16-bit samples are stored as 2's-complement signed integers, ranging from -32768 to 32767.

    In this case you want to use a 16-bit data type, which in C++/C is a short

    short data;
    fread(&data, sizeof(short), 1, audio);
    

    This will read 2 bytes for you, and store them in the short. You will want to do that in a loop