How to interpret 32 bit float wav data? I am reading the wav data as I32 and then dividing it by 2^31, but not getting the right result.
-
4Is the data in the files actually 32-bit floats? If so, reading it as int32 clearly won't work. – Mats Petersson Feb 14 '13 at 22:43
-
wav data can be encoded as big endian or little endian. You might have to swap the bytes first. – Feb 14 '13 at 22:46
-
First, define the right result. – Alexey Frunze Feb 14 '13 at 22:57
2 Answers
The values are float. You must assign them to float values and use them as such. If you are reading float values into int32 values you are doing the equivalent of a reinterpret_cast.
float f1 = 1.0e5;
int i1 = *reinterpret_cast<int*>( &f1 );
// i1 = 1203982336, not 100000
if you cannot avoid getting the wrong value, then to get the 'correct' value back, you need to reverse reinterpretation
float f2 = *reinterpret_cast<float*>( &i1 );
// f2 = 1.0e5
the endianness of the data may still need to be adjusted.

- 4,980
- 2
- 15
- 30
Look at your RIFF header. This will tell you the endianness of the Wav file.
Use that endian-ness to read in your 32-bit floats correctly. For example: If your system is little endian (say, based on an x86 processor) and your wav file is big endian(say, created on an old PPC mac), you will need to do a 32-bit endian swap after you read in the data so that your float variable actually contains data that makes sense (this is usually a value between -1.0f and 1.0f )
-
Thanks everyone! Reading the data as float solved the problem. The data was stored as 32 bit float, so reading it as an int really didn't make sense. – Ritesh Banka Feb 15 '13 at 14:16