Question

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.

Was it helpful?

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

OTHER TIPS

Firstly you need to read header information in the first 44 bytes as can be seen in the folowing link: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

From there, you can get BitsPerSample and cast read bytes to short integer if it is 16, char if it is 8 or int if it is 32

short[] data = new short[NumSamples * NumChannels];
fread(data, sizeof(short), NumSamples * NumChannels, audio);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top