fix(alsa): Remove upper bound to get_volume (#2184)

Fixes #2173 

* feat(alsa): Remove upper bound to get_volume

* Add tests. Trim unnecessary function.
This commit is contained in:
joaquin garmendia 2020-09-20 07:26:17 -05:00 committed by GitHub
parent 2f4cffc0fb
commit bb15e33a2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 7 deletions

View File

@ -35,20 +35,31 @@ namespace math_util {
/**
* Calculate the percentage for a value
* within given range
* between min_value and max_value
*/
template <typename ValueType, typename ReturnType = int>
ReturnType percentage(ValueType value, ValueType min_value, ValueType max_value) {
template<typename ValueType, typename ReturnType = int>
ReturnType unbounded_percentage(ValueType value, ValueType min_value, ValueType max_value){
auto upper = (max_value - min_value);
auto lower = static_cast<float>(value - min_value);
ValueType percentage = (lower / upper) * 100.0f;
if (std::is_integral<ReturnType>())
percentage += 0.5f;
return cap<ReturnType>(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 <typename ValueType, typename ReturnType = int>
ReturnType percentage(ValueType value, ValueType min_value, ValueType max_value) {
auto raw_percentage = unbounded_percentage<ValueType, ReturnType>(value, min_value, max_value);
return cap<ReturnType>(raw_percentage, 0.0f, 100.0f);
}
/**
* Calculates percentage for a value and
* clamps it to a percentage between 0 and 100
*/
template <typename ValueType, typename ReturnType = int>
ReturnType percentage(ValueType value, ValueType max_value) {

View File

@ -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);

View File

@ -25,6 +25,15 @@ TEST(Math, cap) {
EXPECT_EQ(0, math_util::cap<float>(1.0f, 0.0f, 0.0f));
}
TEST(Math, unbounded_percentage) {
EXPECT_EQ(101.0f, (math_util::unbounded_percentage<float, float>(101.0f, 0.0f, 100.0f)));
EXPECT_EQ(102, (math_util::unbounded_percentage<float, int>(101.5f, 0.0f, 100.0f)));
EXPECT_EQ(110.0f, (math_util::unbounded_percentage<float, float>(12.0f, -10.0f, 10.0f)));
EXPECT_EQ(150.0f, (math_util::unbounded_percentage<float, float>(11.5f, 10.0f, 11.0f)));
EXPECT_EQ(-50.0f, (math_util::unbounded_percentage<float, float>(-50.0f, 0.0f, 100.0f)));
EXPECT_EQ(-50.0f, (math_util::unbounded_percentage<float, float>(9.5f, 10.0f, 11.0f)));
}
TEST(Math, percentage) {
EXPECT_EQ(55.0f, (math_util::percentage<float, float>(5.5f, 0.0f, 10.0f)));
EXPECT_EQ(56, (math_util::percentage<float, int>(5.55f, 0.0f, 10.0f)));