shithub: cstory

Download patch

ref: 295673b813aee028a82b7edd0b9387abe9416a83
parent: 5a3e641492a7fe2e9dbd073c412fb8388e85370b
author: Clownacy <[email protected]>
date: Thu Apr 18 21:24:08 EDT 2019

Sound cleanup and optimisation

PlaySoundObject is also more like it was in the original source code

--- a/src/Sound.cpp
+++ b/src/Sound.cpp
@@ -153,12 +153,12 @@
 	SDL_UnlockAudioDevice(audioDevice);
 }
 
-void SOUNDBUFFER::Mix(float (*buffer)[2], size_t samples)
+void SOUNDBUFFER::Mix(float *buffer, size_t frames)
 {
 	if (!playing) //This sound buffer isn't playing
 		return;
 
-	for (size_t sample = 0; sample < samples; sample++)
+	for (size_t i = 0; i < frames; ++i)
 	{
 		const double freqPosition = frequency / FREQUENCY; //This is added to position at the end
 		
@@ -174,8 +174,8 @@
 		const float sampleConvert = (sampleA - 128.0f) / 128.0f;
 		
 		//Mix
-		buffer[sample][0] += (float)(sampleConvert * volume * volume_l);
-		buffer[sample][1] += (float)(sampleConvert * volume * volume_r);
+		*buffer++ += (float)(sampleConvert * volume * volume_l);
+		*buffer++ += (float)(sampleConvert * volume * volume_r);
 		
 		//Increment position
 		samplePosition += freqPosition;
@@ -199,23 +199,20 @@
 }
 
 //Sound mixer
-void AudioCallback(void *userdata, uint8_t *stream, int len)
+void AudioCallback(void *userdata, Uint8 *stream, int len)
 {
 	(void)userdata;
 
-	float (*buffer)[2] = (float(*)[2])stream;
-	const size_t samples = len / (sizeof(float) * 2);
+	float *buffer = (float*)stream;
+	const size_t frames = len / (sizeof(float) * 2);
 
 	//Clear stream
-	for (size_t sample = 0; sample < samples; ++sample)
-	{
-		buffer[sample][0] = 0.0f;
-		buffer[sample][1] = 0.0f;
-	}
+	for (size_t i = 0; i < frames * 2; ++i)
+		buffer[i] = 0.0f;
 	
 	//Mix sounds to primary buffer
 	for (SOUNDBUFFER *sound = soundBuffers; sound != NULL; sound = sound->next)
-		sound->Mix(buffer, samples);
+		sound->Mix(buffer, frames);
 }
 
 //Sound things
@@ -270,22 +267,21 @@
 {
 	if (lpSECONDARYBUFFER[no])
 	{
-		if (mode == -1)
+		switch (mode)
 		{
-			lpSECONDARYBUFFER[no]->Play(true);
-		}
-		else if ( mode )
-		{
-			if ( mode == 1 )
-			{
+			case 0:
 				lpSECONDARYBUFFER[no]->Stop();
+				break;
+
+			case 1:
+				lpSECONDARYBUFFER[no]->Stop();
 				lpSECONDARYBUFFER[no]->SetCurrentPosition(0);
 				lpSECONDARYBUFFER[no]->Play(false);
-			}
-		}
-		else
-		{
-			lpSECONDARYBUFFER[no]->Stop();
+				break;
+
+			case -1:
+				lpSECONDARYBUFFER[no]->Play(true);
+				break;
 		}
 	}
 }
--- a/src/Sound.h
+++ b/src/Sound.h
@@ -23,7 +23,7 @@
 		void Play(bool bLooping);
 		void Stop();
 		
-		void Mix(float (*buffer)[2], size_t samples);
+		void Mix(float *buffer, size_t frames);
 		
 		SOUNDBUFFER *next;