From b0e1b245b471f2c71d7b74ce882fbaa318e3ab91 Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Fri, 2 Oct 2020 07:27:46 -0400 Subject: alsa: Use larger playback buffers for higher bitrates (existing ones apply up to 48KHz, scale up linearly to 192KHz) Change-Id: Iac32d49b8073b63a5d40fd21f41437e6051cb8de --- firmware/target/hosted/pcm-alsa.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'firmware/target/hosted') diff --git a/firmware/target/hosted/pcm-alsa.c b/firmware/target/hosted/pcm-alsa.c index f33b42429c..c7df99e23d 100644 --- a/firmware/target/hosted/pcm-alsa.c +++ b/firmware/target/hosted/pcm-alsa.c @@ -81,9 +81,9 @@ static unsigned int sample_rate = 0; static unsigned int real_sample_rate = 0; static snd_pcm_t *handle = NULL; -static snd_pcm_sframes_t buffer_size = MIX_FRAME_SAMPLES * 32; /* ~16k */ -static snd_pcm_sframes_t period_size = MIX_FRAME_SAMPLES * 4; /* ~4k */ -static sample_t *frames; +static snd_pcm_sframes_t buffer_size; +static snd_pcm_sframes_t period_size; +static sample_t *frames = NULL; static const void *pcm_data = 0; static size_t pcm_size = 0; @@ -103,6 +103,18 @@ static int set_hwparams(snd_pcm_t *handle) snd_pcm_hw_params_t *params; snd_pcm_hw_params_malloc(¶ms); + /* Size playback buffers based on sample rate */ + if (pcm_sampr > SAMPR_96) { + buffer_size = MIX_FRAME_SAMPLES * 32 * 4; /* ~64k */ + period_size = MIX_FRAME_SAMPLES * 4 * 4; /* ~16k */ + } else if (pcm_sampr > SAMPR_48) { + buffer_size = MIX_FRAME_SAMPLES * 32 * 2; /* ~32k */ + period_size = MIX_FRAME_SAMPLES * 4 * 2; /* ~8k */ + } else { + buffer_size = MIX_FRAME_SAMPLES * 32; /* ~16k */ + period_size = MIX_FRAME_SAMPLES * 4; /* ~4k */ + } + /* choose all parameters */ err = snd_pcm_hw_params_any(handle, params); if (err < 0) @@ -195,7 +207,7 @@ static int set_swparams(snd_pcm_t *handle) printf("Unable to determine current swparams for playback: %s\n", snd_strerror(err)); goto error; } - /* start the transfer when the buffer is haalmost full */ + /* start the transfer when the buffer is half full */ err = snd_pcm_sw_params_set_start_threshold(handle, swparams, buffer_size / 2); if (err < 0) { -- cgit v1.2.3