foxygit / doom Log in
commit 372ad50b2260e5e03c251ae8442a65bd32334cf6
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sat Nov 21 16:54:05 2009 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sat Nov 21 16:54:05 2009 +0000

    Auto-select SDL slice size based on sample rate.

    Subversion-branch: /branches/raven-branch
    Subversion-revision: 1739
---
 src/i_sdlsound.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/src/i_sdlsound.c b/src/i_sdlsound.c
index 3273907e..0b3f8aa3 100644
--- 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 @@ static void I_SDL_ShutdownSound(void)
     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 @@ static boolean I_SDL_InitSound(boolean _use_sfx_prefix)
         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;