shithub: cstory

Download patch

ref: 845ee1b96f76a9a322992152f1a2e9930ff396e9
parent: 789d4fb0ea6cb3ed53e40100a146205511de1cbc
author: Clownacy <Clownacy@users.noreply.github.com>
date: Mon Aug 24 22:44:54 EDT 2020

Don't store subsample offset as long

Only a short is needed.

--- a/src/Backends/Audio/SoftwareMixer.cpp
+++ b/src/Backends/Audio/SoftwareMixer.cpp
@@ -15,8 +15,8 @@
 	signed char *samples;
 	size_t frames;
 	size_t position;
-	unsigned long sample_offset_remainder;  // 16.16 fixed-point
-	unsigned long advance_delta;            // 16.16 fixed-point
+	unsigned short position_subsample;
+	unsigned long advance_delta; // 16.16 fixed-point
 	bool playing;
 	bool looping;
 	short volume;    // 8.8 fixed-point
@@ -65,7 +65,7 @@
 	sound->frames = length;
 	sound->playing = false;
 	sound->position = 0;
-	sound->sample_offset_remainder = 0;
+	sound->position_subsample = 0;
 
 	Mixer_SetSoundFrequency(sound, frequency);
 	Mixer_SetSoundVolume(sound, 0);
@@ -107,7 +107,7 @@
 void Mixer_RewindSound(Mixer_Sound *sound)
 {
 	sound->position = 0;
-	sound->sample_offset_remainder = 0;
+	sound->position_subsample = 0;
 }
 
 void Mixer_SetSoundFrequency(Mixer_Sound *sound, unsigned int frequency)
@@ -144,7 +144,7 @@
 			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 unsigned char subsample = sound->position_subsample >> 8;
 
 				const short interpolated_sample = sound->samples[sound->position] * (0x100 - subsample)
 				                                      + sound->samples[sound->position + 1] * subsample;
@@ -154,9 +154,9 @@
 				*stream_pointer++ += (interpolated_sample * sound->volume_r) >> 8;
 
 				// Increment sample
-				sound->sample_offset_remainder += sound->advance_delta;
-				sound->position += sound->sample_offset_remainder >> 16;
-				sound->sample_offset_remainder &= 0xFFFF;
+				const unsigned long next_position_subsample = sound->position_subsample + sound->advance_delta;
+				sound->position += next_position_subsample >> 16;
+				sound->position_subsample = next_position_subsample & 0xFFFF;
 
 				// Stop or loop sample once it's reached its end
 				if (sound->position >= sound->frames)
@@ -169,7 +169,7 @@
 					{
 						sound->playing = false;
 						sound->position = 0;
-						sound->sample_offset_remainder = 0;
+						sound->position_subsample = 0;
 						break;
 					}
 				}