From a9d16fff8392afc331b552cb798cd44d12642740 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Fri, 10 Nov 2017 11:04:19 -1000 Subject: [PATCH] Much more realistic period size for ALSA buffering --- lib/audiodev/ALSA.cpp | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/audiodev/ALSA.cpp b/lib/audiodev/ALSA.cpp index 790cd07..73c40d5 100644 --- a/lib/audiodev/ALSA.cpp +++ b/lib/audiodev/ALSA.cpp @@ -201,14 +201,10 @@ struct ALSAAudioVoiceEngine : BaseAudioVoiceEngine if (!snd_pcm_hw_params_test_rate(m_pcm, hwParams, 96000, 0)) { bestRate = 96000; - m_mixInfo.m_sampleRate = 96000.0; - m_5msFrames = 96000 * 5 / 1000; } else if (!snd_pcm_hw_params_test_rate(m_pcm, hwParams, 48000, 0)) { bestRate = 48000; - m_mixInfo.m_sampleRate = 48000.0; - m_5msFrames = 48000 * 5 / 1000; } else { @@ -228,18 +224,9 @@ struct ALSAAudioVoiceEngine : BaseAudioVoiceEngine return; } m_mixInfo.m_sampleRate = bestRate; + m_5msFrames = bestRate * 5 / 1000; - snd_pcm_uframes_t bufSz = m_5msFrames * 3; - if ((errr = snd_pcm_hw_params_set_buffer_size_near(m_pcm, hwParams, &bufSz))) - { - snd_pcm_hw_params_free(hwParams); - snd_pcm_close(m_pcm); - m_pcm = nullptr; - Log.report(logvisor::Error, "Can't set buffer size. %s\n", snd_strerror(errr)); - return; - } - - snd_pcm_uframes_t periodSz = bufSz / 2; + snd_pcm_uframes_t periodSz = m_5msFrames * 4; if ((errr = snd_pcm_hw_params_set_period_size_near(m_pcm, hwParams, &periodSz, 0))) { snd_pcm_hw_params_free(hwParams); @@ -249,6 +236,16 @@ struct ALSAAudioVoiceEngine : BaseAudioVoiceEngine return; } + snd_pcm_uframes_t bufSz = periodSz * 2; + if ((errr = snd_pcm_hw_params_set_buffer_size_near(m_pcm, hwParams, &bufSz))) + { + snd_pcm_hw_params_free(hwParams); + snd_pcm_close(m_pcm); + m_pcm = nullptr; + Log.report(logvisor::Error, "Can't set buffer size. %s\n", snd_strerror(errr)); + return; + } + /* Write parameters */ if ((errr = snd_pcm_hw_params(m_pcm, hwParams))) {