-1

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.

Ritesh Banka
  • 173
  • 1
  • 1
  • 13

2 Answers2

2

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.

Thomas
  • 4,980
  • 2
  • 15
  • 30
2

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 )

Community
  • 1
  • 1
Carl
  • 43,122
  • 10
  • 80
  • 104
  • 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