From: Jaroslav Kysela Date: Fri, 5 Jun 2020 11:34:37 +0000 (+0200) Subject: amixer: improve the raw percentual volume rounding X-Git-Tag: v1.2.3~2 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=361734165eee70874ecaab8a647b046d112b8971;p=alsa-utils.git amixer: improve the raw percentual volume rounding In commit "ae9ddeb63443cc2c46e0f0b915466cca0f800372" the rint() was changed to ceil(). Revert it back. The rint() rounding is more precise for most cases. Also, handle the special case where the percentual value is greather then zero. Set the min + 1 value in this case. At last, fix the return value in convert_prange() when range is zero. Signed-off-by: Jaroslav Kysela --- diff --git a/amixer/amixer.c b/amixer/amixer.c index 4c19a58..7f1bb3b 100644 --- a/amixer/amixer.c +++ b/amixer/amixer.c @@ -192,16 +192,22 @@ static int convert_prange(long val, long min, long max) int tmp; if (range == 0) - return 0; + return min; val -= min; tmp = rint((double)val/(double)range * 100); return tmp; } -/* Function to convert from percentage to volume. val = percentage */ +/* Function to convert from percentage to volume. perc = percentage */ +static long convert_prange1(long perc, long min, long max) +{ + long tmp; -#define convert_prange1(val, min, max) \ - ceil((val) * ((max) - (min)) * 0.01 + (min)) + tmp = rint(perc * (max - min) * 0.01); + if (tmp == 0 && perc > 0) + tmp++; + return tmp + min; +} struct volume_ops { int (*get_range)(snd_mixer_elem_t *elem, long *min, long *max);