alsa-lib: Revert "A try to fix one rouding problem in refine"
PATCH 1/1 alsa-lib: Revert "A try to fix one rouding problem in refine"
Greetings,
When attempting to use ALSA directly (without any sound server such as pulseaudio) with a configuration using the plug plugin, using parameters where sample rate resampling is necessary, snd_pcm_hw_params or snd_pcm_set_params may fail, returning EINVAL.
This is because refinement rule 7 in pcm_params.c requires the period_time to remain an interval rather than a single value if period_time does not perfectly divide period_size. The code that was introduced with this change converts the interval to one of its extrema, leading to -EINVAL, indicating an empty inteval, being returned by snd_pcm_hw_rule_mulkdiv.
I encountered this when attempting to play 44100 Hz audio with firefox and deadbeef in a configuration that required the plug plugin to resample the audio to 48000 Hz for a slave dmix plugin. The follwing debug output (enabled with RULES_DEBUG in pcm_params.c) was produced at the end of configuration:
refine_soft '(null)' (begin)
ACCESS: MMAP_INTERLEAVED MMAP_NONINTERLEAVED
FORMAT: S16_LE
SUBFORMAT: STDSAMPLE_BITS: ALLFRAME_BITS: ALL
CHANNELS: 1
RATE: 44100PERIOD_TIME: 21000PERIOD_SIZE: [926 926]PERIOD_BYTES: ALL
PERIODS: (4 5)BUFFER_TIME: (104988 104989)BUFFER_SIZE: 4630BUFFER_BYTES: ALLTICK_TIME: 0
Rule 0 (0x7f8d59fd9530): FORMAT=S16_LE -> S16_LE SAMPLE_BITS=[1 4294967295] Rule 1 (0x7f8d59fd9480): SAMPLE_BITS=[1 4294967295] -> 16 FORMAT=S16_LE SAMPLE_BITS=16 Rule 2 (0x7f8d59fd93f0): SAMPLE_BITS=16 -> 16 FRAME_BITS=[1 4294967295] CHANNELS=1 Rule 3 (0x7f8d59fd9360): FRAME_BITS=[1 4294967295] -> 16 SAMPLE_BITS=16 CHANNELS=1 Rule 4 (0x7f8d59fd92d0): FRAME_BITS=16 -> 16 PERIOD_BYTES=ALL PERIOD_SIZE=926 Rule 5 (0x7f8d59fd92d0): FRAME_BITS=16 -> 16 BUFFER_BYTES=[1 4294967295] BUFFER_SIZE=4630 Rule 6 (0x7f8d59fd93f0): CHANNELS=1 -> 1 FRAME_BITS=16 SAMPLE_BITS=16
Rule 7 (0x7f8d59fd92d0): RATE=44100 -> NONE PERIOD_SIZE=926 PERIOD_TIME=21000 refine_soft '(null)' (end--22)
PERIOD_SIZE previously had a value of (926 927), which would have allowed rule 7 to run correctly, before being converted to [926 926] in snd_pcm_rate_hw_refine_cchange.
I believe this arose from a similar issue:
https://bbs.archlinux.org/viewtopic.php?id=259785
This reverts commit 37728639ae05de702825d96bd1d42e24ae772248. In doing so, it allows for period_size to remain an open interval in this case and for configuration to proceed successfully.
I am unsure if this is the case, but I suspect that the bug that 37728639ae05de702825d96bd1d42e24ae772248 was originally meant to address was later resolved by de88f0e58617e0fef265ec136efccb297e735571.
Thank you for reading this, and I apologize for any parts of it that were unclear. Please let me know if there is any other information that I can provide.
Regards,
George
---
src/pcm/pcm_rate.c +1 -45
@@ -34,10 +34,6 @@#include "bswap.h" #include <inttypes.h> -#if 0 -#define DEBUG_REFINE -#endif -#ifndef PIC /* entry for static linking */ const char *_snd_module_pcm_rate ="" ;
@@ -207,10 +203,7 @@ static int snd_pcm_rate_hw_refine_cchange(snd_pcm_t *pcm, snd_pcm_hw_params_t *p {snd_pcm_rate_t *rate = pcm->private_data;snd_interval_t t; -#ifdef DEBUG_REFINE -snd_output_t *out; -#endif -const snd_interval_t *sbuffer_size, *buffer_size; +const snd_interval_t *sbuffer_size;const snd_interval_t *srate, *crate;int err; unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
@@ -229,44 +222,7 @@ static int snd_pcm_rate_hw_refine_cchange(snd_pcm_t *pcm, snd_pcm_hw_params_t *p err =_snd_pcm_hw_param_set_interval (params,SND_PCM_HW_PARAM_BUFFER_SIZE , &t);if (err <0 )return err; - buffer_size =snd_pcm_hw_param_get_interval (params,SND_PCM_HW_PARAM_BUFFER_SIZE ); -/* -* this condition probably needs more work: -* in case when the buffer_size is known and we are looking -* for best period_size, we should prefer situation when -* (buffer_size / period_size) * period_size == buffer_size -*/ -if (snd_interval_single (buffer_size) && buffer_size->integer) { -snd_interval_t *period_size; - period_size = (snd_interval_t *)snd_pcm_hw_param_get_interval (params,SND_PCM_HW_PARAM_PERIOD_SIZE ); -if (!snd_interval_checkempty (period_size) && - period_size->openmin && period_size->openmax && - period_size->min +1 == period_size->max) { -if (period_size->min >0 && (buffer_size->min / period_size->min) * period_size->min == buffer_size->min) { -snd_interval_set_value (period_size, period_size->min); - }else if ((buffer_size->max / period_size->max) * period_size->max == buffer_size->max) { -snd_interval_set_value (period_size, period_size->max); - } - } - } -#ifdef DEBUG_REFINE -snd_output_stdio_attach (&out, stderr,0 ); -snd_output_printf (out,"REFINE (params):\n" ); -snd_pcm_hw_params_dump (params, out); -snd_output_printf (out,"REFINE (slave params):\n" ); -snd_pcm_hw_params_dump (sparams, out); -snd_output_close (out); -#endif err =_snd_pcm_hw_params_refine (params, links, sparams); -#ifdef DEBUG_REFINE -snd_output_stdio_attach (&out, stderr,0 ); -snd_output_printf (out,"********************\n" ); -snd_output_printf (out,"REFINE (params) (%i):\n" , err); -snd_pcm_hw_params_dump (params, out); -snd_output_printf (out,"REFINE (slave params):\n" ); -snd_pcm_hw_params_dump (sparams, out); -snd_output_close (out); -#endif if (err <0 )return err;return 0 ;
Quoted 14 lines · George Matsumura
Greetings,
When attempting to use ALSA directly (without any sound server such as pulseaudio) with a configuration using the plug plugin, using parameters where sample rate resampling is necessary,
snd_pcm_hw_paramsorsnd_pcm_set_paramsmay fail, returning EINVAL.This is because refinement rule 7 in
pcm_params.crequires theperiod_timeto remain an interval rather than a single value ifperiod_timedoes not perfectly divideperiod_size. The code that was introduced with this change converts the interval to one of its extrema, leading to-EINVAL, indicating an empty inteval, being returned bysnd_pcm_hw_rule_mulkdiv.
Quoted 5 lines
I encountered this when attempting to play 44100 Hz audio with firefox and deadbeef in a configuration that required the plug plugin to resample the audio to 48000 Hz for a slave dmix plugin. The follwing debug output (enabled with
RULES_DEBUGinpcm_params.c) was produced at the end of configuration:
Can you replicate this issue with aplay (use similar stream parameters as firefox)? Please, show the exact command to reproduce this.
Also, only "if block" should be eventually reverted. The debug prints should be preserved.
Jaroslav
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat
Quoted 2 lines · On Sat, 19 Sep 2026 17:02:51 +0200 Jaroslav Kysela <[email protected]> wrote:
Can you replicate this issue with aplay (use similar stream parameters as firefox)? Please, show the exact command to reproduce this.
Certainly, I apologize for not doing so in my original message. I was able to replicate the issue with the following aplay command, with parameters similar to what I believe deadbeef was using:
aplay -f float_le -r 44100 --buffer-size=8192 --period-size=1024 \
<testfile>.raw
The following asound.conf was used:
defaults.pcm.!card 1
defaults.ctl.!card 1
pcm.!default {
type plug
slave {
pcm "dmix"
rate 48000
}
}Quoted 2 lines
Also, only "if block" should be eventually reverted. The debug prints should be preserved.
A patch with this change exclusively is provided.
Thank you for the quick reply and your time in evaluating this. Do not hesistate to let me know if anything else would be helpful.
Regards,
George
---
src/pcm/pcm_rate.c +1 -21
@@ -210,7 +210,7 @@ static int snd_pcm_rate_hw_refine_cchange(snd_pcm_t *pcm, snd_pcm_hw_params_t *p#ifdef DEBUG_REFINE snd_output_t *out;#endif -const snd_interval_t *sbuffer_size, *buffer_size; +const snd_interval_t *sbuffer_size;const snd_interval_t *srate, *crate;int err; unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
@@ -229,26 +229,6 @@ static int snd_pcm_rate_hw_refine_cchange(snd_pcm_t *pcm, snd_pcm_hw_params_t *p err =_snd_pcm_hw_param_set_interval (params,SND_PCM_HW_PARAM_BUFFER_SIZE , &t);if (err <0 )return err; - buffer_size =snd_pcm_hw_param_get_interval (params,SND_PCM_HW_PARAM_BUFFER_SIZE ); -/* -* this condition probably needs more work: -* in case when the buffer_size is known and we are looking -* for best period_size, we should prefer situation when -* (buffer_size / period_size) * period_size == buffer_size -*/ -if (snd_interval_single (buffer_size) && buffer_size->integer) { -snd_interval_t *period_size; - period_size = (snd_interval_t *)snd_pcm_hw_param_get_interval (params,SND_PCM_HW_PARAM_PERIOD_SIZE ); -if (!snd_interval_checkempty (period_size) && - period_size->openmin && period_size->openmax && - period_size->min +1 == period_size->max) { -if (period_size->min >0 && (buffer_size->min / period_size->min) * period_size->min == buffer_size->min) { -snd_interval_set_value (period_size, period_size->min); - }else if ((buffer_size->max / period_size->max) * period_size->max == buffer_size->max) { -snd_interval_set_value (period_size, period_size->max); - } - } - }#ifdef DEBUG_REFINE snd_output_stdio_attach (&out, stderr,0 );snd_output_printf (out,"REFINE (params):\n" );
Quoted 17 lines · George Matsumura
Quoted 2 lines · On Sat, 19 Sep 2026 17:02:51 +0200 Jaroslav Kysela <[email protected]> wrote:
Can you replicate this issue with aplay (use similar stream parameters as firefox)? Please, show the exact command to reproduce this.
Certainly, I apologize for not doing so in my original message. I was able to replicate the issue with the following aplay command, with parameters similar to what I believe deadbeef was using:
aplay -f
float_le-r 44100 --buffer-size=8192 --period-size=1024 \
<testfile>.rawThe following asound.conf was used:
defaults.pcm.!card 1
defaults.ctl.!card 1
Quoted 7 lines
pcm.!default {
type plug
slave {
pcm "dmix"
rate 48000
}
}Test with default dmix definition (replace 3 with card 1):
$ aplay -v -D plug:dmix:3 -f float_le -r 44100 --buffer-size=8192 --period-size=1024 /dev/zero
I've tried two USB and one Intel HDA:
buffer_size : 8467 period_size : 940 period_time : 21333
buffer_size : 8467 period_size : 940 period_time : 21333
Identical with your config:
pcm.dmixT {
type plug
slave {
pcm "dmix:1"
rate 48000
}
}$ aplay -v -D dmixT -f float_le -r 44100 --buffer-size=8192 --period-size=1024 /dev/zero
buffer_size : 8467 period_size : 940 period_time : 21333
Do you have standard dmix defaults ?
$ alsactl dump-cfg | grep -E "^defaults.pcm.dmix" defaults.pcm.dmix.max_periods 0 defaults.pcm.dmix.channels 2 defaults.pcm.dmix.rate 48000 defaults.pcm.dmix.format unchanged defaults.pcm.dmix.card 'defaults.pcm.card' defaults.pcm.dmix.device 'defaults.pcm.device'
Show verbose log from aplay when working to see the period setup on the dmix side (and the used kernel driver). Also, do you use alsa-lib 1.2.16 ?
Jaroslav
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat
(Resending this to adjust for mailing list limits on message size)
Quoted 1 line · On Sun, 20 Sep 2026 17:18:02 +0200 Jaroslav Kysela <[email protected]> wrote:
Do you have standard dmix defaults ?
I obtain the following output for the dmix default configuration:
$ alsactl dump-cfg | grep -E "^defaults.pcm.dmix" defaults.pcm.dmix.max_periods 0 defaults.pcm.dmix.channels 2 defaults.pcm.dmix.rate 48000 defaults.pcm.dmix.format unchanged defaults.pcm.dmix.card 'defaults.pcm.card' defaults.pcm.dmix.device 'defaults.pcm.device'
I also obtain the follwing output from aplay:
$ aplay -v -D dmixT -f float_le -r 44100 --buffer-size=8192 --period-size=1024 /dev/zero Playing raw data '/dev/zero' : Float 32 bit Little Endian, Rate 44100 Hz, Mono aplay: set_params:1462: Unable to install hw params: ACCESS: RW_INTERLEAVED FORMAT: FLOAT_LE SUBFORMAT: STD SAMPLE_BITS: 32 FRAME_BITS: 32 CHANNELS: 1 RATE: NONE PERIOD_TIME: 21000 PERIOD_SIZE: NONE PERIOD_BYTES: (3704 3708) PERIODS: (8 9) BUFFER_TIME: (188979 188980) BUFFER_SIZE: 8334 BUFFER_BYTES: 33336 TICK_TIME: 0
when using the following asound.conf contents:
pcm.dmixT {
type plug
slave {
pcm "dmix:1"
rate 48000
}
}Here is the output of the same command with REFINE_DEBUG and RULES_DEBUG enabled in pcm_params.c:
https://gitlab.com/-/snippets/6058384
Quoted 1 line
Show verbose log from aplay when working to see the period setup on the dmix side (and the used kernel driver). Also, do you use alsa-lib 1.2.16 ?
I am indeed using alsa-lib 1.2.16. I wasn't able to find anything relevant in the last lines of dmesg, however, I believe I am using the snd_soc_sst_cht_bsw_rt5645 driver. I apologize if this is not the information that was requested.
Thank you again for your help with this.
Regards,
George
Quoted 12 lines · George Matsumura
I also obtain the follwing output from aplay:
$ aplay -v -D dmixT -f float_le -r 44100 --buffer-size=8192 --period-size=1024 /dev/zero Playing raw data '/dev/zero' : Float 32 bit Little Endian, Rate 44100 Hz, Mono aplay: set_params:1462: Unable to install hw params: ACCESS: RW_INTERLEAVED FORMAT: FLOAT_LE SUBFORMAT: STD SAMPLE_BITS: 32 FRAME_BITS: 32 CHANNELS: 1 RATE: NONE
Quoted 8 lines
PERIOD_TIME: 21000 PERIOD_SIZE: NONE PERIOD_BYTES: (3704 3708) PERIODS: (8 9) BUFFER_TIME: (188979 188980) BUFFER_SIZE: 8334 BUFFER_BYTES: 33336 TICK_TIME: 0
Could you also show the output with patched alsa-lib ?
Quoted 6 lines
I am indeed using alsa-lib 1.2.16. I wasn't able to find anything relevant in the last lines of dmesg, however, I believe I am using the
snd_soc_sst_cht_bsw_rt5645driver. I apologize if this is not the information that was requested.Thank you again for your help with this.
It seems that the Intel SST driver may be the culprit (different constaints in the kernel code). Could you try to force dmix to use fixed period and buffer sizes ?
pcm.dmixT {
type plug
slave {
pcm {
type dmix
ipc_key 1024
ipc_perm 0600
slave {
pcm "hw:1"
period_size 1024
buffer_size 4096
format "S16_LE"
rate 48000
}
}
}
}Jaroslav
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat
Quoted 1 line · On Mon, 21 Sep 2026 20:49:18 +0200 Jaroslav Kysela <[email protected]> wrote:
Could you also show the output with patched alsa-lib ?
Certainly, I obtain the following output from alsa-lib with patched snd_pcm_rate_hw_refine_cchange:
$ aplay -v -D dmixT -f float_le -r 44100 --buffer-size=8192 --period-size=1024 /dev/zero
Playing raw data '/dev/zero' : Float 32 bit Little Endian, Rate 44100 Hz, Mono
Plug PCM: Linear Integer <-> Linear Float conversion PCM (S16_LE)
Its setup is:
stream : PLAYBACK
access : RW_INTERLEAVED
format : FLOAT_LE
subformat : STD
channels : 1
rate : 44100
exact rate : 44100 (44100/1)
msbits : 32
buffer_size : 8334
period_size : 926
period_time : 21000
tstamp_mode : ENABLE
tstamp_type : MONOTONIC
period_step : 1
avail_min : 926
period_event : 0
start_threshold : 8334
stop_threshold : 8334
silence_threshold: 0
silence_size : 0
boundary : 4691624911813214208
Slave: Rate conversion PCM (48000, sformat=S16_LE)
Converter: linear-interpolation
Protocol version: 10003
Its setup is:
stream : PLAYBACK
access : MMAP_INTERLEAVED
format : S16_LE
subformat : STD
channels : 1
rate : 44100
exact rate : 44100 (44100/1)
msbits : 16
buffer_size : 8334
period_size : 926
period_time : 21000
tstamp_mode : ENABLE
tstamp_type : MONOTONIC
period_step : 1
avail_min : 926
period_event : 0
start_threshold : 8334
stop_threshold : 8334
silence_threshold: 0
silence_size : 0
boundary : 4691624911813214208
Slave: Route conversion PCM (sformat=S16_LE)
Transformation table:
0 <- 0
1 <- 0
Its setup is:
stream : PLAYBACK
access : MMAP_INTERLEAVED
format : S16_LE
subformat : STD
channels : 1
rate : 48000
exact rate : 48000 (48000/1)
msbits : 16
buffer_size : 9072
period_size : 1008
period_time : 21000
tstamp_mode : ENABLE
tstamp_type : MONOTONIC
period_step : 1
avail_min : 1008
period_event : 0
start_threshold : 9072
stop_threshold : 9072
silence_threshold: 0
silence_size : 0
boundary : 5107081977438142464
Slave: Direct Stream Mixing PCM
Its setup is:
stream : PLAYBACK
access : MMAP_INTERLEAVED
format : S16_LE
subformat : STD
channels : 2
rate : 48000
exact rate : 48000 (48000/1)
msbits : 16
buffer_size : 9072
period_size : 1008
period_time : 21000
tstamp_mode : ENABLE
tstamp_type : MONOTONIC
period_step : 1
avail_min : 1008
period_event : 0
start_threshold : 9072
stop_threshold : 9072
silence_threshold: 0
silence_size : 0
boundary : 5107081977438142464
Hardware PCM card 1 'chtrt5650' device 0 subdevice 0
Its setup is:
stream : PLAYBACK
access : MMAP_INTERLEAVED
format : S16_LE
subformat : STD
channels : 2
rate : 48000
exact rate : 48000 (48000/1)
msbits : 16
buffer_size : 16128
period_size : 1008
period_time : 21000
tstamp_mode : ENABLE
tstamp_type : MONOTONIC
period_step : 1
avail_min : 1008
period_event : 0
start_threshold : 1
stop_threshold : 9079256848778919936
silence_threshold: 0
silence_size : 9079256848778919936
boundary : 9079256848778919936
appl_ptr : 0
hw_ptr : 0with this asound.conf:
defaults.pcm.!card 1
defaults.ctl.!card 1
pcm.dmixT {
type plug
slave {
pcm "dmix"
rate 48000
}
}Audio output works correctly in this case.
Quoted 3 lines
It seems that the Intel SST driver may be the culprit (different constaints in the kernel code). Could you try to force dmix to use fixed period and buffer sizes ?
I apologize if I translated the config you sent incorrectly, but here are outputs I obtain from test aplay commands:
$ aplay -v -D dmixT -f float_le -r 44100 --buffer-size=8192 --period-size=1024 /dev/zero ALSA lib pcm_direct.c:1287:(snd1_pcm_direct_initialize_slave) [error.pcm] unable to set buffer size ALSA lib pcm_dmix.c:1012:(snd_pcm_dmix_open) [error.pcm] unable to initialize slave aplay: main:850: audio open error: Invalid argument
$ aplay -v -D dmixT /dev/zero ALSA lib pcm_direct.c:1287:(snd1_pcm_direct_initialize_slave) [error.pcm] unable to set buffer size ALSA lib pcm_dmix.c:1012:(snd_pcm_dmix_open) [error.pcm] unable to initialize slave aplay: main:850: audio open error: Invalid argument
with this asound.conf:
pcm.dmixT {
type plug
slave {
pcm {
type dmix
ipc_key 1024
ipc_perm 0600
slave {
pcm "hw:1"
period_size 1024
buffer_size 4096
format "S16_LE"
rate 48000
}
}
}
}I had originally thought that due to the error occuring in the refinement process, as shown in the snippet below from the unpatched library with REFINE_DEBUG and RULES_DEBUG enabled, that it was due to alsa-lib rather than the kernel driver. However, I would not able to explain the differences of our results and I apologize if I submitted a patch for the wrong component.
https://gitlab.com/-/snippets/6058384
Regards,
George