shithub: choc

Download patch

ref: e80490d431d940f1e549c3a0426a5815d0be4dbf
parent: 144849eee5804a0306d23f07a5be9877f242a1bf
author: Simon Howard <fraggle@gmail.com>
date: Sun May 17 09:54:19 EDT 2009

Always use an SDL buffer size that is a power of two. Reduce buffer size
to 70ms.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 1524

--- a/pcsound/pcsound_sdl.c
+++ b/pcsound/pcsound_sdl.c
@@ -32,7 +32,7 @@
 #include "pcsound.h"
 #include "pcsound_internal.h"
 
-#define SOUND_SLICE_TIME 100 /* ms */
+#define MAX_SOUND_SLICE_TIME 70 /* ms */
 #define SQUARE_WAVE_AMP 0x2000
 
 // If true, we initialised SDL and have the responsibility to shut it 
@@ -164,6 +164,33 @@
     }
 }
 
+// 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 = (pcsound_sample_rate * 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 int PCSound_SDL_Init(pcsound_callback_func callback_func)
 {
     int slicesize;
@@ -179,7 +206,7 @@
             return 0;
         }
 
-        slicesize = (SOUND_SLICE_TIME * pcsound_sample_rate) / 1000;
+        slicesize = GetSliceSize();
 
         if (Mix_OpenAudio(pcsound_sample_rate, AUDIO_S16SYS, 2, slicesize) < 0)
         {
--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -49,7 +49,7 @@
 #include "doomdef.h"
 
 #define LOW_PASS_FILTER
-#define SOUND_SLICE_TIME 100 /* ms */
+#define MAX_SOUND_SLICE_TIME 70 /* ms */
 #define NUM_CHANNELS 16
 
 static boolean sound_initialised = false;
@@ -65,7 +65,6 @@
 
 extern int mb_used;
 
-
 // When a sound stops, check if it is still playing.  If it is not, 
 // we can mark the sound data as CACHE to be freed back for other
 // means.
@@ -667,10 +666,36 @@
     sound_initialised = 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(void)
 { 
     int i;
-    int slicesize;
     
     // No sounds yet
 
@@ -690,9 +715,7 @@
         return false;
     }
 
-    slicesize = (snd_samplerate * SOUND_SLICE_TIME) / 1000;
-
-    if (Mix_OpenAudio(snd_samplerate, AUDIO_S16SYS, 2, slicesize) < 0)
+    if (Mix_OpenAudio(snd_samplerate, AUDIO_S16SYS, 2, GetSliceSize()) < 0)
     {
         fprintf(stderr, "Error initialising SDL_mixer: %s\n", Mix_GetError());
         return false;