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:
parent
2f4cffc0fb
commit
bb15e33a2a
@ -35,20 +35,31 @@ namespace math_util {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the percentage for a value
|
* Calculate the percentage for a value
|
||||||
* within given range
|
* between min_value and max_value
|
||||||
*/
|
*/
|
||||||
template<typename ValueType, typename ReturnType = int>
|
template<typename ValueType, typename ReturnType = int>
|
||||||
ReturnType percentage(ValueType value, ValueType min_value, ValueType max_value) {
|
ReturnType unbounded_percentage(ValueType value, ValueType min_value, ValueType max_value){
|
||||||
auto upper = (max_value - min_value);
|
auto upper = (max_value - min_value);
|
||||||
auto lower = static_cast<float>(value - min_value);
|
auto lower = static_cast<float>(value - min_value);
|
||||||
ValueType percentage = (lower / upper) * 100.0f;
|
ValueType percentage = (lower / upper) * 100.0f;
|
||||||
if (std::is_integral<ReturnType>())
|
if (std::is_integral<ReturnType>())
|
||||||
percentage += 0.5f;
|
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>
|
template <typename ValueType, typename ReturnType = int>
|
||||||
ReturnType percentage(ValueType value, ValueType max_value) {
|
ReturnType percentage(ValueType value, ValueType max_value) {
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,6 +25,15 @@ TEST(Math, cap) {
|
|||||||
EXPECT_EQ(0, math_util::cap<float>(1.0f, 0.0f, 0.0f));
|
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) {
|
TEST(Math, percentage) {
|
||||||
EXPECT_EQ(55.0f, (math_util::percentage<float, float>(5.5f, 0.0f, 10.0f)));
|
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)));
|
EXPECT_EQ(56, (math_util::percentage<float, int>(5.55f, 0.0f, 10.0f)));
|
||||||
|
Loading…
Reference in New Issue
Block a user