shithub: cstory

Download patch

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

Update miniaudio backend

--- a/src/Backends/Audio/miniaudio.cpp
+++ b/src/Backends/Audio/miniaudio.cpp
@@ -23,13 +23,8 @@
 static void (*organya_callback)(void);
 static unsigned int organya_callback_milliseconds;
 
-static void Callback(ma_device *device, void *output_stream, const void *input_stream, ma_uint32 frames_total)
+static void MixSoundsAndUpdateOrganya(long *stream, size_t frames_total)
 {
-	(void)device;
-	(void)input_stream;
-
-	short *stream = (short*)output_stream;
-
 	ma_mutex_lock(&organya_mutex);
 
 	if (organya_callback_milliseconds == 0)
@@ -68,18 +63,40 @@
 		}
 	}
 
-	// Clamp output, and convert from 8-bit to 16-bit
-	for (unsigned int i = 0; i < frames_total * 2; ++i)
+	ma_mutex_unlock(&organya_mutex);
+}
+
+static void Callback(ma_device *device, void *output_stream, const void *input_stream, ma_uint32 frames_total)
+{
+	(void)device;
+	(void)input_stream;
+
+	short *stream = (short*)output_stream;
+
+	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
 
-	ma_mutex_unlock(&organya_mutex);
+		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;
+	}
 }
 
 bool AudioBackend_Init(void)