shithub: cstory

Download patch

ref: 3fa4a91dc1a9658f0ce44ff4c2869f0922854fce
parent: 70e431d35dcc5e5623f4873b6b6614f21c06352f
author: Clownacy <Clownacy@users.noreply.github.com>
date: Wed Jun 24 13:03:13 EDT 2020

Reword audio backend logic

This will make it easier to integrate into the enhanced branch, and
also improved audio quality slightly (samples are mixed as 16-bit
instead of 8-bit).

--- a/src/Backends/Audio/SDL2.cpp
+++ b/src/Backends/Audio/SDL2.cpp
@@ -20,15 +20,8 @@
 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 @@
 			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;
 	}
 }
 
--- a/src/Backends/Audio/SoftwareMixer.cpp
+++ b/src/Backends/Audio/SoftwareMixer.cpp
@@ -135,21 +135,21 @@
 }
 
 // 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;
--- a/src/Backends/Audio/SoftwareMixer.h
+++ b/src/Backends/Audio/SoftwareMixer.h
@@ -13,4 +13,4 @@
 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);