Don't store subsample offset as long

Only a short is needed.
This commit is contained in:
Clownacy 2020-08-25 02:44:54 +01:00
parent 789d4fb0ea
commit 845ee1b96f

View file

@ -15,7 +15,7 @@ struct Mixer_Sound
signed char *samples; signed char *samples;
size_t frames; size_t frames;
size_t position; size_t position;
unsigned long sample_offset_remainder; // 16.16 fixed-point unsigned short position_subsample;
unsigned long advance_delta; // 16.16 fixed-point unsigned long advance_delta; // 16.16 fixed-point
bool playing; bool playing;
bool looping; bool looping;
@ -65,7 +65,7 @@ Mixer_Sound* Mixer_CreateSound(unsigned int frequency, const unsigned char *samp
sound->frames = length; sound->frames = length;
sound->playing = false; sound->playing = false;
sound->position = 0; sound->position = 0;
sound->sample_offset_remainder = 0; sound->position_subsample = 0;
Mixer_SetSoundFrequency(sound, frequency); Mixer_SetSoundFrequency(sound, frequency);
Mixer_SetSoundVolume(sound, 0); Mixer_SetSoundVolume(sound, 0);
@ -107,7 +107,7 @@ void Mixer_StopSound(Mixer_Sound *sound)
void Mixer_RewindSound(Mixer_Sound *sound) void Mixer_RewindSound(Mixer_Sound *sound)
{ {
sound->position = 0; sound->position = 0;
sound->sample_offset_remainder = 0; sound->position_subsample = 0;
} }
void Mixer_SetSoundFrequency(Mixer_Sound *sound, unsigned int frequency) void Mixer_SetSoundFrequency(Mixer_Sound *sound, unsigned int frequency)
@ -144,7 +144,7 @@ ATTRIBUTE_HOT void Mixer_MixSounds(long *stream, size_t frames_total)
for (size_t frames_done = 0; frames_done < frames_total; ++frames_done) for (size_t frames_done = 0; frames_done < frames_total; ++frames_done)
{ {
// Perform linear interpolation // Perform linear interpolation
const unsigned char subsample = sound->sample_offset_remainder >> 8; const unsigned char subsample = sound->position_subsample >> 8;
const short interpolated_sample = sound->samples[sound->position] * (0x100 - subsample) const short interpolated_sample = sound->samples[sound->position] * (0x100 - subsample)
+ sound->samples[sound->position + 1] * subsample; + sound->samples[sound->position + 1] * subsample;
@ -154,9 +154,9 @@ ATTRIBUTE_HOT void Mixer_MixSounds(long *stream, size_t frames_total)
*stream_pointer++ += (interpolated_sample * sound->volume_r) >> 8; *stream_pointer++ += (interpolated_sample * sound->volume_r) >> 8;
// Increment sample // Increment sample
sound->sample_offset_remainder += sound->advance_delta; const unsigned long next_position_subsample = sound->position_subsample + sound->advance_delta;
sound->position += sound->sample_offset_remainder >> 16; sound->position += next_position_subsample >> 16;
sound->sample_offset_remainder &= 0xFFFF; sound->position_subsample = next_position_subsample & 0xFFFF;
// Stop or loop sample once it's reached its end // Stop or loop sample once it's reached its end
if (sound->position >= sound->frames) if (sound->position >= sound->frames)
@ -169,7 +169,7 @@ ATTRIBUTE_HOT void Mixer_MixSounds(long *stream, size_t frames_total)
{ {
sound->playing = false; sound->playing = false;
sound->position = 0; sound->position = 0;
sound->sample_offset_remainder = 0; sound->position_subsample = 0;
break; break;
} }
} }