Audiolib: Overflow in RateScale computation (dependent on sampling rate and pitch)
The following statement is repeated across multiple locations in the audiolib source code:
voice->RateScale = divideu32(voice->SamplingRate * voice->PitchScale, MV_MixRate);
Where voice->PitchScale
is computed from the pitch offset, which can be specified through definesound
in CON. MV_MixRate
is defined in the sound options menu, one of either 22015Hz, 44100Hz or 48000Hz.
Issues occur once one attempts to specify a high pitch offset for soundfiles with a high sampling rate, for example a pitch of 700 with a sampling rate of 44100 Hz. This does occur in practice: for example the EBIKE found in the DNF2013 mod uses increasing offsets to pitch up the engine sound of the bike when it moves, and 44100Hz sounds are commonly used in most games today.
With a pitch_offset of 700, voice->PitchScale = 98193
, and therefore we have
voice->SamplingRate * voice->PitchScale = 44'100 * 98'193 = 4'330'311'300 > 2^32
i.e. we get a uint_32 overflow for the intermediate value, and the resulting RateScale will be incorrect. For lower sampling rates the overflow does not occur, resulting in inconsistent pitch depending on the sampling rate of the file. Of course, having to multiply first is necessary because otherwise we would get rounding errors, so I believe increasing the integer precision for this division is necessary to resolve the issue.
Git blame tells me that this goes all the way back to 1cdd18b0, where audiolib was introduced from JonoF's port.