From acc8b7890a0b3878b226f56454eeffc80843fdee Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 20 Nov 2004 22:17:31 +0000 Subject: option to use ALSA default fragment number and size git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@295 fefdeb5f-60dc-0310-8127-8f9354f1896f --- polyp/alsa-util.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'polyp/alsa-util.c') diff --git a/polyp/alsa-util.c b/polyp/alsa-util.c index 188ba077..eaa21d49 100644 --- a/polyp/alsa-util.c +++ b/polyp/alsa-util.c @@ -30,8 +30,9 @@ #include "sample.h" #include "xmalloc.h" -int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, struct pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *buffer_size) { - int ret = 0; +int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, struct pa_sample_spec *ss, uint32_t *periods, snd_pcm_uframes_t *period_size) { + int ret = -1; + snd_pcm_uframes_t buffer_size; snd_pcm_hw_params_t *hwparams = NULL; static const snd_pcm_format_t format_trans[] = { [PA_SAMPLE_U8] = SND_PCM_FORMAT_U8, @@ -42,21 +43,39 @@ int pa_alsa_set_hw_params(snd_pcm_t *pcm_handle, struct pa_sample_spec *ss, uint [PA_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE, [PA_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE, }; - + assert(pcm_handle && ss && periods && period_size); + if (snd_pcm_hw_params_malloc(&hwparams) < 0 || snd_pcm_hw_params_any(pcm_handle, hwparams) < 0 || snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0 || snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[ss->format]) < 0 || snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &ss->rate, NULL) < 0 || snd_pcm_hw_params_set_channels(pcm_handle, hwparams, ss->channels) < 0 || - snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, periods, NULL) < 0 || - snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, buffer_size) < 0 || - snd_pcm_hw_params(pcm_handle, hwparams) < 0) { - ret = -1; - } + (*periods > 0 && snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, periods, NULL) < 0) || + (*period_size > 0 && snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, period_size, NULL) < 0) || + snd_pcm_hw_params(pcm_handle, hwparams) < 0) + goto finish; + + if (snd_pcm_prepare(pcm_handle) < 0) + goto finish; + if (snd_pcm_hw_params_current(pcm_handle, hwparams) < 0) + goto finish; + + if (snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size) < 0 || + snd_pcm_hw_params_get_period_size(hwparams, period_size, NULL) < 0) + goto finish; + + assert(buffer_size > 0 && *period_size > 0); + *periods = buffer_size / *period_size; + assert(*periods > 0); + + ret = 0; + +finish: if (hwparams) snd_pcm_hw_params_free(hwparams); + return ret; } -- cgit