From ed4bb30ffe42470f76985a8ec6bb7b8ec1db11ad Mon Sep 17 00:00:00 2001
From: mjbudd77 <mjbudd77@gmail.com>
Date: Fri, 21 Jan 2022 16:57:56 -0500
Subject: [PATCH] Bug fix for SDL sound on FreeBSD OS (Issue #449). Force audio
 spec sample set size to be a power of 2 per SDL documentation.

---
 src/drivers/Qt/sdl-sound.cpp | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/drivers/Qt/sdl-sound.cpp b/src/drivers/Qt/sdl-sound.cpp
index a73aaf6c..dfe79aa8 100644
--- a/src/drivers/Qt/sdl-sound.cpp
+++ b/src/drivers/Qt/sdl-sound.cpp
@@ -190,6 +190,7 @@ InitSound()
 	SDL_AudioSpec spec;
 	const char *driverName;
 	int frmRateSampleAdj = 0;
+	int samplesPerFrame;
 
 	g_config->getOption("SDL.Sound", &sound);
 	if (!sound) 
@@ -219,17 +220,23 @@ InitSound()
 	spec.freq = s_SampleRate = soundrate;
 	spec.format = AUDIO_S16SYS;
 	spec.channels = 1;
-	//spec.samples = 512;
-	spec.samples = (int)( ( (double)s_SampleRate / getBaseFrameRate() ) );
+	spec.samples = 512; // This must stay a power of two per SDL documentation
 	spec.callback = fillaudio;
 	spec.userdata = 0;
 
+	samplesPerFrame = (int)( ( (double)s_SampleRate / getBaseFrameRate() ) );
+
+	if ( samplesPerFrame >= 1024 )
+	{
+		spec.samples = 1024;
+	}
+
 	s_BufferSize = soundbufsize * soundrate / 1000;
 
 	// For safety, set a bare minimum:
-	if (s_BufferSize < spec.samples * 2)
+	if (s_BufferSize < spec.samples * 4)
 	{
-		s_BufferSize = spec.samples * 2;
+		s_BufferSize = spec.samples * 4;
 	}
 	s_BufferSize25 =    s_BufferSize/4;
 	s_BufferSize50 =    s_BufferSize/2;