]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
use 4 periods in ALSA instead of 2 for lower latency if possible
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 1 Oct 2009 03:05:06 +0000 (03:05 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 1 Oct 2009 03:05:06 +0000 (03:05 +0000)
request a power of 2 buffer size in ALSA (maybe better compatibility)
thanks to Diablo-D3 for a patch I based this functionality on

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@9268 d7cf8633-e32d-0410-b094-e92efae38249

snd_alsa.c

index 4601e9c4da7af434e3ebec72f0514212ee7c80a6..f5922f0f2241ff586095cab5d7d2ec8d91a9bf36 100644 (file)
@@ -30,7 +30,7 @@
 #include "snd_main.h"
 
 
-#define NB_PERIODS 2
+#define NB_PERIODS 4
 
 static snd_pcm_t* pcm_handle = NULL;
 static snd_pcm_sframes_t expected_delay = 0;
@@ -222,7 +222,14 @@ seqdone:
                goto init_error;
        }
 
-       buffer_size = requested->speed / 5;
+       // pick a buffer size that is a power of 2 (by masking off low bits)
+       buffer_size = i = (int)(requested->speed * 0.15f);
+       while (buffer_size & (buffer_size-1))
+               buffer_size &= (buffer_size-1);
+       // then check if it is the nearest power of 2 and bump it up if not
+       if (i - buffer_size >= buffer_size >> 1)
+               buffer_size *= 2;
+
        err = snd_pcm_hw_params_set_buffer_size_near (pcm_handle, hw_params, &buffer_size);
        if (err < 0)
        {
@@ -231,6 +238,8 @@ seqdone:
                goto init_error;
        }
 
+       // pick a period size near the buffer_size we got from ALSA
+       snd_pcm_hw_params_get_buffer_size (hw_params, &buffer_size);
        buffer_size /= NB_PERIODS;
        err = snd_pcm_hw_params_set_period_size_near(pcm_handle, hw_params, &buffer_size, 0);
        if (err < 0)