From dac2c4df9bdf6d181b55ca98cfa6bec3412fd3e6 Mon Sep 17 00:00:00 2001 From: Jzavrk Date: Mon, 10 Mar 2025 13:28:02 +0100 Subject: [PATCH] output/plugins/SndioOutputPlugin: Fix sndio volume calculation This commit addresses issues #2226 and MusicPlayerDaemon/ncmpc#95. MPD and sndio volume ranges being different, the formula to transform a value from one set to the other is in the form of `(a * x + b) / c` where: - a = output set max value - b = adjustment term - c = input set max value Previous calculation formula had `b = 0`, scaling values too low and rendering increment impossible. Having `b = out_maxval / 2` balances the transformation, ensuring a better spread of values across the output range. Closes #2226 --- NEWS | 2 ++ src/output/plugins/SndioOutputPlugin.cxx | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index c316360e8..520edc7ef 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,6 @@ ver 0.24.1 (not yet released) +* output + - sndio: fix rounding error in volume calculation ver 0.24 (2025/03/11) * protocol diff --git a/src/output/plugins/SndioOutputPlugin.cxx b/src/output/plugins/SndioOutputPlugin.cxx index 1ca877668..b8e76f4f8 100644 --- a/src/output/plugins/SndioOutputPlugin.cxx +++ b/src/output/plugins/SndioOutputPlugin.cxx @@ -139,12 +139,12 @@ SndioOutput::Play(std::span src) void SndioOutput::SetVolume(unsigned int volume) { - sio_setvol(hdl, volume * SIO_MAXVOL / 100); + sio_setvol(hdl, (volume * SIO_MAXVOL + 50) / 100); } static inline unsigned int RawToPercent(int raw_volume) { - return raw_volume < 0 ? 100 : raw_volume * 100 / SIO_MAXVOL; + return raw_volume < 0 ? 100 : (raw_volume * 100 + SIO_MAXVOL / 2) / SIO_MAXVOL; } void