From 76305e7e6bc5ee232201d11b7622f2d84e265e19 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 14 Jun 2005 05:37:26 +0000 Subject: [PATCH] attempt to fix choppy audio in alsa caused by non-power-of 2 buffer size --- include/alsa_funcs_list.h | 1 + libs/audio/targets/snd_alsa.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/alsa_funcs_list.h b/include/alsa_funcs_list.h index ce376e6a6..4bb2921b0 100644 --- a/include/alsa_funcs_list.h +++ b/include/alsa_funcs_list.h @@ -45,6 +45,7 @@ QF_ALSA_NEED (snd_pcm_uframes_t, snd_pcm_hw_params_set_period_size_near, (snd_pc QF_ALSA_NEED (unsigned int, snd_pcm_hw_params_set_rate_near, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int *dir)) #else QF_ALSA_NEED (int, snd_pcm_hw_params_get_buffer_size, (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)) +QF_ALSA_NEED (int, snd_pcm_hw_params_set_buffer_size, (snd_pcm_t *pcm, const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val)) QF_ALSA_NEED (int, snd_pcm_hw_params_get_period_size, (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir)) QF_ALSA_NEED (int, snd_pcm_hw_params_set_access, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access)) QF_ALSA_NEED (int, snd_pcm_hw_params_set_period_size_near, (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir)) diff --git a/libs/audio/targets/snd_alsa.c b/libs/audio/targets/snd_alsa.c index 47f88be78..b9327d7bf 100644 --- a/libs/audio/targets/snd_alsa.c +++ b/libs/audio/targets/snd_alsa.c @@ -106,6 +106,18 @@ SNDDMA_Init_Cvars (void) static int SNDDMA_GetDMAPos (void); +static snd_pcm_uframes_t +round_buffer_size (snd_pcm_uframes_t sz) +{ + snd_pcm_uframes_t mask = ~0; + + while (sz & mask) { + sz &= mask; + mask <<= 1; + } + return sz; +} + static volatile dma_t * SNDDMA_Init (void) { @@ -325,6 +337,15 @@ SNDDMA_Init (void) qfsnd_strerror (err)); goto error; } + if (buffer_size != round_buffer_size (buffer_size)) { + buffer_size = round_buffer_size (buffer_size); + err = qfsnd_pcm_hw_params_set_buffer_size (pcm, hw, &buffer_size); + if (0 > err) { + Sys_Printf ("ALSA: unable to set buffer size. %s\n", + qfsnd_strerror (err)); + goto error; + } + } sn.samples = buffer_size * sn.channels; // mono samples in buffer sn.speed = rate;