# wavwrite with 32 bits per sample seems to overflow?

I am trying to generate a 32bit PCM wav file with octave, the file shall contain a single full-scale sample (0db impulse). I am getting the expected result when using 24 bits per sample:

>> wavwrite(1, 96000, 24, 'foo.wav');
ans =  1.00000                          # as expected


However, when using 32 bits per sample, the value seems to overflow.

>> wavwrite(1, 96000, 32, 'foo.wav');
ans = -1                                # WHAT ?!


I am expecting the second wavread('foo.wav') to return 1 not -1. I have checked the wav file with a hex editor, the sample raw value is 0x80000000, I am expecting it to be 0x7fffffff. Is it an octave bug or am I missing something?

Using 0.99999 instead of 1 generates the expected result.

• Check the documentation -- different bit formats have different max and min values. It's possible that for 32 bits the max is 1.0-eps. – MBaz Aug 2 '17 at 22:43
• using matlab, audiowrite('foo.wav', 1, 96000, 'BitsPerSample', 32); works fine... Could be a problem of representation: Q31 can't represent 1. – Florent Aug 2 '17 at 22:47
• It sounds like an octave bug. Matlab (R2014b) produces the expected result. – jaket Aug 2 '17 at 22:49
• this could be the bug : Octave writes in 32bit int (type 1) whereas Matlab writes 32bit normalized floats (type 3) – Florent Aug 2 '17 at 23:00

It seems like it is a bug. Octave's wavwrite() function writes 32-bit wav in 32 bits int (type 1 format) instead of normalized 32-bit floats (type 3 format). Unfortunately, 1 can't be represented in 32-bit type 1 format which is why you get this result.
The solution could be to use the audiowrite() function, or to write 0x7fffffff instead of 1.