From 1c33a2d790d494fd92085bd9a5ed1574ce203439 Mon Sep 17 00:00:00 2001 From: Themaister Date: Fri, 28 May 2010 15:59:49 +0200 Subject: [PATCH] Changed stuff --- alsa.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/alsa.c b/alsa.c index 030cb183f2..de2614c998 100644 --- a/alsa.c +++ b/alsa.c @@ -24,6 +24,8 @@ static void* __alsa_init(const char* device, int rate, int latency) if ( device != NULL ) alsa_dev = device; + //fprintf(stderr, "Opening device: %s\n", alsa_dev); + TRY_ALSA(snd_pcm_open(&alsa->pcm, alsa_dev, SND_PCM_STREAM_PLAYBACK, 0)); unsigned int latency_usec = latency * 1000; @@ -38,17 +40,29 @@ static void* __alsa_init(const char* device, int rate, int latency) TRY_ALSA(snd_pcm_hw_params_set_format(alsa->pcm, params, format)); TRY_ALSA(snd_pcm_hw_params_set_channels(alsa->pcm, params, channels)); TRY_ALSA(snd_pcm_hw_params_set_rate(alsa->pcm, params, rate, 0)); - TRY_ALSA(snd_pcm_hw_params_set_buffer_time_near(alsa->pcm, params, &latency_usec, NULL)); - latency_usec = latency * 250; - TRY_ALSA(snd_pcm_hw_params_set_period_time_near(alsa->pcm, params, &latency_usec, NULL)); + + // We test if we can run the latencies we are allowed, if not, fallback to *_near. + + if ( snd_pcm_hw_params_set_buffer_time_max(alsa->pcm, params, &latency_usec, NULL) < 0) + { + latency_usec = latency * 1000; + TRY_ALSA(snd_pcm_hw_params_set_buffer_time_near(alsa->pcm, params, &latency_usec, NULL)) + } + + latency_usec = (latency < 32) ? 10000 : latency * 250; + if ( snd_pcm_hw_params_set_period_time_max(alsa->pcm, params, &latency_usec, NULL) ) + { + latency_usec = (latency < 32) ? 10000 : latency * 250; + TRY_ALSA(snd_pcm_hw_params_set_period_time_near(alsa->pcm, params, &latency_usec, NULL)); + } TRY_ALSA(snd_pcm_hw_params(alsa->pcm, params)); snd_pcm_uframes_t alsa_sizes; snd_pcm_hw_params_get_period_size(params, &alsa_sizes, NULL); - fprintf(stderr, "Period size: %d\n", (int)alsa_sizes); + //fprintf(stderr, "ALSA Period size: %d frames\n", (int)alsa_sizes); snd_pcm_hw_params_get_buffer_size(params, &alsa_sizes); - fprintf(stderr, "Buffer size: %d\n", (int)alsa_sizes); + //fprintf(stderr, "Buffer size: %d frames\n", (int)alsa_sizes); if (snd_pcm_sw_params_malloc(&sw_params) < 0) goto error;