From bb15e33a2ad0b809a0a8356877d5ef7325491405 Mon Sep 17 00:00:00 2001 From: joaquin garmendia Date: Sun, 20 Sep 2020 07:26:17 -0500 Subject: [PATCH] fix(alsa): Remove upper bound to get_volume (#2184) Fixes #2173 * feat(alsa): Remove upper bound to get_volume * Add tests. Trim unnecessary function. --- include/utils/math.hpp | 21 ++++++++++++++++----- src/adapters/alsa/mixer.cpp | 4 ++-- tests/unit_tests/utils/math.cpp | 9 +++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/include/utils/math.hpp b/include/utils/math.hpp index e6186e54..d76b218a 100644 --- a/include/utils/math.hpp +++ b/include/utils/math.hpp @@ -35,20 +35,31 @@ namespace math_util { /** * Calculate the percentage for a value - * within given range + * between min_value and max_value */ - template - ReturnType percentage(ValueType value, ValueType min_value, ValueType max_value) { + template + ReturnType unbounded_percentage(ValueType value, ValueType min_value, ValueType max_value){ auto upper = (max_value - min_value); auto lower = static_cast(value - min_value); ValueType percentage = (lower / upper) * 100.0f; if (std::is_integral()) percentage += 0.5f; - return cap(percentage, 0.0f, 100.0f); + return percentage; } /** - * Calculate the percentage for a value + * Calculates percentage for a value and + * clamps it to a percentage between 0 and 100 + */ + template + ReturnType percentage(ValueType value, ValueType min_value, ValueType max_value) { + auto raw_percentage = unbounded_percentage(value, min_value, max_value); + return cap(raw_percentage, 0.0f, 100.0f); + } + + /** + * Calculates percentage for a value and + * clamps it to a percentage between 0 and 100 */ template ReturnType percentage(ValueType value, ValueType max_value) { diff --git a/src/adapters/alsa/mixer.cpp b/src/adapters/alsa/mixer.cpp index 34f51a27..b83a18e1 100644 --- a/src/adapters/alsa/mixer.cpp +++ b/src/adapters/alsa/mixer.cpp @@ -110,7 +110,7 @@ namespace alsa { } } - return math_util::percentage(vol_total / chan_n, vol_min, vol_max); + return math_util::unbounded_percentage(vol_total / chan_n, vol_min, vol_max); } /** @@ -133,7 +133,7 @@ namespace alsa { } if (vol_max - vol_min <= MAX_LINEAR_DB_SCALE * 100) { - return math_util::percentage(vol_total / chan_n, vol_min, vol_max); + return math_util::percentage(vol_total / chan_n, vol_min, vol_max); } normalized = pow(10, (vol_total / chan_n - vol_max) / 6000.0); diff --git a/tests/unit_tests/utils/math.cpp b/tests/unit_tests/utils/math.cpp index 8096df41..c02d8b02 100644 --- a/tests/unit_tests/utils/math.cpp +++ b/tests/unit_tests/utils/math.cpp @@ -25,6 +25,15 @@ TEST(Math, cap) { EXPECT_EQ(0, math_util::cap(1.0f, 0.0f, 0.0f)); } +TEST(Math, unbounded_percentage) { + EXPECT_EQ(101.0f, (math_util::unbounded_percentage(101.0f, 0.0f, 100.0f))); + EXPECT_EQ(102, (math_util::unbounded_percentage(101.5f, 0.0f, 100.0f))); + EXPECT_EQ(110.0f, (math_util::unbounded_percentage(12.0f, -10.0f, 10.0f))); + EXPECT_EQ(150.0f, (math_util::unbounded_percentage(11.5f, 10.0f, 11.0f))); + EXPECT_EQ(-50.0f, (math_util::unbounded_percentage(-50.0f, 0.0f, 100.0f))); + EXPECT_EQ(-50.0f, (math_util::unbounded_percentage(9.5f, 10.0f, 11.0f))); +} + TEST(Math, percentage) { EXPECT_EQ(55.0f, (math_util::percentage(5.5f, 0.0f, 10.0f))); EXPECT_EQ(56, (math_util::percentage(5.55f, 0.0f, 10.0f)));