From 8f1e1b87526b6b409ca31287997c8f4ffd199a3c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 14 Nov 2015 21:44:03 -0700 Subject: [PATCH] ALSA: let alsa lib choose period settings Closes #46 --- src/alsa.c | 35 +++++++---------------------------- src/alsa.h | 2 +- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/src/alsa.c b/src/alsa.c index 421b3f4..5bb8856 100644 --- a/src/alsa.c +++ b/src/alsa.c @@ -1312,34 +1312,7 @@ static int outstream_open_alsa(struct SoundIoPrivate *si, struct SoundIoOutStrea return SoundIoErrorOpeningDevice; } - int period_count; - if (device->is_raw) { - period_count = 4; - unsigned int microseconds = 0.25 * outstream->software_latency * 1000000.0; - if ((err = snd_pcm_hw_params_set_period_time_near(osa->handle, hwparams, µseconds, NULL)) < 0) { - outstream_destroy_alsa(si, os); - return SoundIoErrorOpeningDevice; - } - } else { - period_count = 2; - double period_duration = 0.5 * outstream->software_latency; - snd_pcm_uframes_t period_frames = - ceil_dbl_to_uframes(period_duration * (double)outstream->sample_rate); - - if ((err = snd_pcm_hw_params_set_period_size_near(osa->handle, hwparams, &period_frames, NULL)) < 0) { - outstream_destroy_alsa(si, os); - return SoundIoErrorOpeningDevice; - } - } - - snd_pcm_uframes_t period_size; - if ((snd_pcm_hw_params_get_period_size(hwparams, &period_size, NULL)) < 0) { - outstream_destroy_alsa(si, os); - return SoundIoErrorOpeningDevice; - } - osa->period_size = period_size; - - osa->buffer_size_frames = osa->period_size * period_count; + osa->buffer_size_frames = outstream->software_latency * outstream->sample_rate; if ((err = snd_pcm_hw_params_set_buffer_size_near(osa->handle, hwparams, &osa->buffer_size_frames)) < 0) { outstream_destroy_alsa(si, os); return SoundIoErrorOpeningDevice; @@ -1352,6 +1325,12 @@ static int outstream_open_alsa(struct SoundIoPrivate *si, struct SoundIoOutStrea return (err == -EINVAL) ? SoundIoErrorIncompatibleDevice : SoundIoErrorOpeningDevice; } + if ((snd_pcm_hw_params_get_period_size(hwparams, &osa->period_size, NULL)) < 0) { + outstream_destroy_alsa(si, os); + return SoundIoErrorOpeningDevice; + } + + // set channel map osa->chmap->channels = ch_count; for (int i = 0; i < ch_count; i += 1) { diff --git a/src/alsa.h b/src/alsa.h index 25c5587..21271bf 100644 --- a/src/alsa.h +++ b/src/alsa.h @@ -59,7 +59,7 @@ struct SoundIoOutStreamAlsa { struct pollfd *poll_fds; struct SoundIoOsThread *thread; atomic_flag thread_exit_flag; - int period_size; + snd_pcm_uframes_t period_size; int write_frame_count; bool is_paused; atomic_flag clear_buffer_flag;