Fix an occational invalid memory read

Stupid floating-point rounding errors. Had to undo a fancy
optimisation to avoid it.
This commit is contained in:
Clownacy 2020-01-09 09:10:24 +00:00
parent c831031ba4
commit 449a09b09e

View file

@ -75,28 +75,21 @@ static void MixSounds(float *stream, unsigned int frames_total)
{ {
float *steam_pointer = stream; float *steam_pointer = stream;
unsigned int frames_done = 0; for (unsigned int frames_done = 0; frames_done < frames_total; ++frames_done)
while (frames_done != frames_total)
{ {
const unsigned int frames_to_do = MIN((unsigned int)ceil((sound->frames - sound->position) / sound->advance_delta), frames_total - frames_done); // Get two samples, and normalise them to 0-1
const float sample1 = (sound->samples[(size_t)sound->position] - 128.0f) / 128.0f;
const float sample2 = (sound->samples[(size_t)sound->position + 1] - 128.0f) / 128.0f;
for (unsigned int i = 0; i < frames_to_do; ++i) // Perform linear interpolation
{ const float interpolated_sample = sample1 + ((sample2 - sample1) * (float)fmod(sound->position, 1.0));
// Get two samples, and normalise them to 0-1
const float sample1 = (sound->samples[(size_t)sound->position] - 128.0f) / 128.0f;
const float sample2 = (sound->samples[(size_t)sound->position + 1] - 128.0f) / 128.0f;
// Perform linear interpolation *steam_pointer++ += interpolated_sample * sound->volume_l;
const float interpolated_sample = sample1 + ((sample2 - sample1) * (float)fmod(sound->position, 1.0)); *steam_pointer++ += interpolated_sample * sound->volume_r;
*steam_pointer++ += interpolated_sample * sound->volume_l; sound->position += sound->advance_delta;
*steam_pointer++ += interpolated_sample * sound->volume_r;
sound->position += sound->advance_delta; if (sound->position >= sound->frames)
}
if ((size_t)sound->position >= sound->frames)
{ {
if (sound->looping) if (sound->looping)
{ {
@ -109,8 +102,6 @@ static void MixSounds(float *stream, unsigned int frames_total)
break; break;
} }
} }
frames_done += frames_to_do;
} }
} }
} }