I was having a conversation on the Archlinux forums[1] about using the alsa-plugins JACK plugin. While one of the bugs that we found has already been fixed in the git alsa-lib, there is another bug relating to buffer sizes.
Currently snd_pcm_hw_params_set_buffer_size_near() doesn't really work when you are using this plugin unless you happen to request a buffer size that almost exactly matches a whole multiple of periods that the JACK is asking for. After some debugging, I discovered the reason is that some plugins, notably the 'plug' plugin that most everybody is going to use to change frequency/format directly attempt to manipulate the buffer size. This causes the code in pcm_ioplug.c to fail because it cannot find a number of periods/period time that can fit the buffer.
The included patch stops this by specifying the list of acceptable buffer sizes as well, so that when other plugins try to change the buffer it always picks one that will work. It does have some problems in the JACK plugin now offers a number of periods/sizes that don't exist but in practice asking for huge buffers just seems to give you a more sane value.
[1] https://bbs.archlinux.org/viewtopic.php?id=250116
--- jack/pcm_jack.c 2019-05-10 06:57:24.000000000 +0000 +++ jack/pcm_jack.c 2019-10-27 03:43:36.053734268 +0000 @@ -428,6 +428,7 @@ unsigned int format = SND_PCM_FORMAT_FLOAT; unsigned int rate = jack_get_sample_rate(jack->client); unsigned int psize_list[MAX_PERIODS_MULTIPLE]; + unsigned int bsize_list[MAX_PERIODS_MULTIPLE]; unsigned int nframes = jack_get_buffer_size(jack->client); unsigned int jack_buffer_bytes = (snd_pcm_format_size(format, nframes) * jack->num_ports); @@ -441,6 +442,9 @@ for (i = 1; i <= ARRAY_SIZE(psize_list); i++) psize_list[i-1] = jack_buffer_bytes * i;
+ for (i = 1; i <= ARRAY_SIZE(bsize_list); i++) + bsize_list[i-1] = jack_buffer_bytes * (i + 1); + jack->sample_bits = snd_pcm_format_physical_width(format); if ((err = snd_pcm_ioplug_set_param_list(&jack->io, SND_PCM_IOPLUG_HW_ACCESS, ARRAY_SIZE(access_list), access_list)) < 0 || @@ -452,6 +456,8 @@ rate, rate)) < 0 || (err = snd_pcm_ioplug_set_param_list(&jack->io, SND_PCM_IOPLUG_HW_PERIOD_BYTES, ARRAY_SIZE(psize_list), psize_list)) < 0 || + (err = snd_pcm_ioplug_set_param_list(&jack->io, SND_PCM_IOPLUG_HW_BUFFER_BYTES, + ARRAY_SIZE(bsize_list), bsize_list)) < 0 || (err = snd_pcm_ioplug_set_param_minmax(&jack->io, SND_PCM_IOPLUG_HW_PERIODS, 2, 64)) < 0) return err;