diff --git a/src/Backends/Audio/SDL2.cpp b/src/Backends/Audio/SDL2.cpp index 92adc16a..724958f4 100644 --- a/src/Backends/Audio/SDL2.cpp +++ b/src/Backends/Audio/SDL2.cpp @@ -20,15 +20,8 @@ static unsigned long output_frequency; static void (*organya_callback)(void); static unsigned int organya_callback_milliseconds; -static void Callback(void *user_data, Uint8 *stream_uint8, int len) +static void MixSoundsAndUpdateOrganya(long *stream, size_t frames_total) { - (void)user_data; - - short *stream = (short*)stream_uint8; - unsigned int frames_total = len / sizeof(short) / 2; - - memset(stream, 0, len); - if (organya_callback_milliseconds == 0) { Mixer_MixSounds(stream, frames_total); @@ -60,16 +53,38 @@ static void Callback(void *user_data, Uint8 *stream_uint8, int len) organya_countdown -= frames_to_do; } } +} - // Clamp output, and convert from 8-bit to 16-bit - for (unsigned int i = 0; i < frames_total * 2; ++i) +static void Callback(void *user_data, Uint8 *stream_uint8, int len) +{ + (void)user_data; + + short *stream = (short*)stream_uint8; + const size_t frames_total = len / sizeof(short) / 2; + + size_t frames_done = 0; + + while (frames_done != frames_total) { - if (stream[i] > 0x7F) - stream[i] = 0x7F00; - else if (stream[i] < -0x7F) - stream[i] = -0x7F00; - else - stream[i] <<= 8; + long mix_buffer[0x800 * 2]; // 2 because stereo + + size_t subframes = MIN(0x800, frames_total - frames_done); + + memset(mix_buffer, 0, subframes * sizeof(long) * 2); + + MixSoundsAndUpdateOrganya(mix_buffer, subframes); + + for (size_t i = 0; i < subframes * 2; ++i) + { + if (mix_buffer[i] > 0x7FFF) + *stream++ = 0x7FFF; + else if (mix_buffer[i] < -0x7FFF) + *stream++ = -0x7FFF; + else + *stream++ = mix_buffer[i]; + } + + frames_done += subframes; } } diff --git a/src/Backends/Audio/SoftwareMixer.cpp b/src/Backends/Audio/SoftwareMixer.cpp index 7f0098e0..71501550 100644 --- a/src/Backends/Audio/SoftwareMixer.cpp +++ b/src/Backends/Audio/SoftwareMixer.cpp @@ -135,21 +135,21 @@ void Mixer_SetSoundPan(Mixer_Sound *sound, long pan) } // Most CPU-intensive function in the game (2/3rd CPU time consumption in my experience), so marked with ATTRIBUTE_HOT so the compiler considers it a hot spot (as it is) when optimizing -ATTRIBUTE_HOT void Mixer_MixSounds(short *stream, unsigned int frames_total) +ATTRIBUTE_HOT void Mixer_MixSounds(long *stream, size_t frames_total) { for (Mixer_Sound *sound = sound_list_head; sound != NULL; sound = sound->next) { if (sound->playing) { - short *stream_pointer = stream; + long *stream_pointer = stream; - for (unsigned int frames_done = 0; frames_done < frames_total; ++frames_done) + for (size_t frames_done = 0; frames_done < frames_total; ++frames_done) { // Perform linear interpolation const unsigned char subsample = sound->sample_offset_remainder >> 8; - const signed char interpolated_sample = ((sound->samples[sound->position] * (0x100 - subsample)) >> 8) - + ((sound->samples[sound->position + 1] * subsample) >> 8); + const short interpolated_sample = sound->samples[sound->position] * (0x100 - subsample) + + sound->samples[sound->position + 1] * subsample; // Mix, and apply volume *stream_pointer++ += (interpolated_sample * sound->volume_l) >> 8; diff --git a/src/Backends/Audio/SoftwareMixer.h b/src/Backends/Audio/SoftwareMixer.h index f33014d7..d3e09158 100644 --- a/src/Backends/Audio/SoftwareMixer.h +++ b/src/Backends/Audio/SoftwareMixer.h @@ -13,4 +13,4 @@ void Mixer_RewindSound(Mixer_Sound *sound); void Mixer_SetSoundFrequency(Mixer_Sound *sound, unsigned int frequency); void Mixer_SetSoundVolume(Mixer_Sound *sound, long volume); void Mixer_SetSoundPan(Mixer_Sound *sound, long pan); -void Mixer_MixSounds(short *stream, unsigned int frames_total); +void Mixer_MixSounds(long *stream, size_t frames_total);