From bfb3aeac1cd968954627e3f5a7b3d62a58e9a45e Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sun, 7 Jun 2020 18:30:54 +0200 Subject: [PATCH] bluetooth: backend-native: Round volume to closest instead of up Part-of: --- src/modules/bluetooth/backend-native.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/modules/bluetooth/backend-native.c b/src/modules/bluetooth/backend-native.c index 2e4b39b3e..dc3a01df8 100644 --- a/src/modules/bluetooth/backend-native.c +++ b/src/modules/bluetooth/backend-native.c @@ -124,11 +124,14 @@ static uint32_t hfp_features = "" static pa_volume_t hsp_gain_to_volume(uint16_t gain) { - pa_volume_t volume = (pa_volume_t) (gain * PA_VOLUME_NORM / HSP_MAX_GAIN); + pa_volume_t volume = (pa_volume_t) (( + gain * PA_VOLUME_NORM + /* Round to closest by adding half the denominator */ + + HSP_MAX_GAIN / 2 + ) / HSP_MAX_GAIN); - /* increment volume by one to correct rounding errors */ - if (volume < PA_VOLUME_NORM) - volume++; + if (volume > PA_VOLUME_NORM) + volume = PA_VOLUME_NORM; return volume; }