[pulseaudio-discuss] [PATCH 2/3] volume: Print a warning when volume is clipped in pa_sw_volume_multiply/divide()

Georg Chini georg at chini.tk
Fri Apr 28 09:13:57 UTC 2017


When the volume exceeds PA_VOLUME_MAX in pa_sw_volume_multiply() or
pa_sw_volume_divide(), volume settings are insanely high and the
user should be notified about it.

This patch adds volume clamping to pa_sw_volume_divide() and prints
a warning when the volume is clipped in both functions.
---
 src/pulse/volume.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/pulse/volume.c b/src/pulse/volume.c
index 1667b940..73e78370 100644
--- a/src/pulse/volume.c
+++ b/src/pulse/volume.c
@@ -199,16 +199,23 @@ pa_volume_t pa_cvolume_min_mask(const pa_cvolume *a, const pa_channel_map *cm, p
 }
 
 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) {
+    uint64_t result;
 
     pa_return_val_if_fail(PA_VOLUME_IS_VALID(a), PA_VOLUME_INVALID);
     pa_return_val_if_fail(PA_VOLUME_IS_VALID(b), PA_VOLUME_INVALID);
 
     /* cbrt((a/PA_VOLUME_NORM)^3*(b/PA_VOLUME_NORM)^3)*PA_VOLUME_NORM = a*b/PA_VOLUME_NORM */
 
-    return (pa_volume_t) PA_CLAMP_VOLUME((((uint64_t) a * (uint64_t) b + (uint64_t) PA_VOLUME_NORM / 2ULL) / (uint64_t) PA_VOLUME_NORM));
+    result = ((uint64_t) a * (uint64_t) b + (uint64_t) PA_VOLUME_NORM / 2ULL) / (uint64_t) PA_VOLUME_NORM;
+
+    if (result > (uint64_t)PA_VOLUME_MAX)
+        pa_log_warn("pa_sw_volume_multiply: Volume exceeds maximum allowed value and will be clipped. Please check your volume settings");
+
+    return (pa_volume_t) PA_CLAMP_VOLUME(result);
 }
 
 pa_volume_t pa_sw_volume_divide(pa_volume_t a, pa_volume_t b) {
+    uint64_t result;
 
     pa_return_val_if_fail(PA_VOLUME_IS_VALID(a), PA_VOLUME_INVALID);
     pa_return_val_if_fail(PA_VOLUME_IS_VALID(b), PA_VOLUME_INVALID);
@@ -216,7 +223,12 @@ pa_volume_t pa_sw_volume_divide(pa_volume_t a, pa_volume_t b) {
     if (b <= PA_VOLUME_MUTED)
         return 0;
 
-    return (pa_volume_t) (((uint64_t) a * (uint64_t) PA_VOLUME_NORM + (uint64_t) b / 2ULL) / (uint64_t) b);
+    result = ((uint64_t) a * (uint64_t) PA_VOLUME_NORM + (uint64_t) b / 2ULL) / (uint64_t) b;
+
+    if (result > (uint64_t)PA_VOLUME_MAX)
+        pa_log_warn("pa_sw_volume_divide: Volume exceeds maximum allowed value and will be clipped. Please check your volume settings");
+
+    return (pa_volume_t) PA_CLAMP_VOLUME(result);
 }
 
 /* Amplitude, not power */
-- 
2.11.0



More information about the pulseaudio-discuss mailing list