Save As .wav wrapping due to rounding

• Apr 11, 2010 - 23:14
Type
Functional
Severity
S4 - Minor
Status
closed
Project

When saving a .wav file, sometimes the waveform is wrapping. After compiling the code and debugging exportaudio.cpp, it turns out multiplying the buffer[i] by the gain can generate a result greater than one. The example I found was if buffer[i] = 0.00904430355876684, gain gets calcuated to 110.568529325932 during the first pass. The second pass results in buffer[i] *= gain being 1.000015, causing the waveform to wrap. The fix is straight forward:

#define MAX_BUFFER_VALUE 0.999999
#define MIN_BUFFER_VALUE -0.999999
for (unsigned i = 0; i < FRAMES * 2; ++i) {
buffer[i] *= gain;
if (buffer[i] > MAX_BUFFER_VALUE)
buffer[i] = MAX_BUFFER_VALUE;
else if (buffer[i] < MIN_BUFFER_VALUE)
buffer[i] = MIN_BUFFER_VALUE;
}

Ken


Comments

Status (old) needs info closed
Status needs info closed

We compute the gain with 0.99 / peak for years now and I didn't read any wrapping issue. Normalisation could be improved further, but it's beyond the scope of this issue. I close the issue.