adamstark/AudioFile

Max-int overflow

Slay68 opened this issue · 3 comments

Hi Adam,

In a short test, filling the buffer with some sine values, I noticed the resulting wave had a negative peak value, where it should have been the max positive value. It seems to be that a 1.0 float value in the buffer is saved as 32768 (for the 16-bit format). This is the piece of code for saving the file:

            else if (bitDepth == 16)
            {
                int16_t sampleAsInt = (int16_t) (samples[channel][i] * (T)32768.);
                addInt16ToFileData (fileData, sampleAsInt);
            }

The easiest solution would be to replace the 32768 value by a 32767, hereby 'losing' the max nagative -32768 value. Or using an intermediate 32-bit value, which is clipped between 32767 and -32768 afterwards.

Regards and thanks for your code,

Ruud

Hi there, I have now fixed this and it is on the main master branch. Let me know if it solves your problem,

Thanks,

Adam

Hi Adam, problem solved, thanks!

Great! Pleased to hear that :)