shithub: choc

Download patch

ref: 372ad50b2260e5e03c251ae8442a65bd32334cf6
parent: 3d577f70fb8a41a6609db76bdabae30f064a95bc
author: Simon Howard <[email protected]>
date: Sat Nov 21 11:54:05 EST 2009

Auto-select SDL slice size based on sample rate.

Subversion-branch: /branches/raven-branch
Subversion-revision: 1739

--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -49,6 +49,7 @@
 #include "doomtype.h"
 
 #define LOW_PASS_FILTER
+#define MAX_SOUND_SLICE_TIME 70 /* ms */
 #define NUM_CHANNELS 16
 
 static boolean sound_initialized = false;
@@ -681,11 +682,37 @@
     sound_initialized = false;
 }
 
+// Calculate slice size, based on MAX_SOUND_SLICE_TIME.
+// The result must be a power of two.
 
+static int GetSliceSize(void)
+{
+    int limit;
+    int n;
+
+    limit = (snd_samplerate * MAX_SOUND_SLICE_TIME) / 1000;
+
+    // Try all powers of two, not exceeding the limit.
+
+    for (n=0;; ++n)
+    {
+        // 2^n <= limit < 2^n+1 ?
+
+        if ((1 << (n + 1)) > limit)
+        {
+            return (1 << n);
+        }
+    }
+
+    // Should never happen?
+
+    return 1024;
+}
+
 static boolean I_SDL_InitSound(boolean _use_sfx_prefix)
-{ 
+{
     int i;
-    
+
     use_sfx_prefix = _use_sfx_prefix;
 
     // No sounds yet
@@ -701,7 +728,7 @@
         return false;
     }
 
-    if (Mix_OpenAudio(snd_samplerate, AUDIO_S16SYS, 2, 1024) < 0)
+    if (Mix_OpenAudio(snd_samplerate, AUDIO_S16SYS, 2, GetSliceSize()) < 0)
     {
         fprintf(stderr, "Error initialising SDL_mixer: %s\n", Mix_GetError());
         return false;