mirror of
https://github.com/polybar/polybar.git
synced 2024-11-25 13:55:47 -05:00
parent
002eb08b20
commit
d6a5212ef3
3 changed files with 20 additions and 4 deletions
|
@ -41,6 +41,17 @@ namespace math_util {
|
|||
else
|
||||
return cap<ReturnType>(percentage * max_value / 100.0f, 0.0f, max_value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value for percentage of `min_value` to `max_value`
|
||||
*/
|
||||
template <typename ValueType, typename ReturnType = int>
|
||||
ReturnType percentage_to_value(ValueType percentage, ValueType min_value, ValueType max_value) {
|
||||
if (std::is_integral<ReturnType>())
|
||||
return cap<ReturnType>(percentage * (max_value - min_value) / 100.0f + 0.5f, 0, max_value - min_value) + min_value;
|
||||
else
|
||||
return cap<ReturnType>(percentage * (max_value - min_value) / 100.0f, 0.0f, max_value - min_value) + min_value;
|
||||
}
|
||||
}
|
||||
|
||||
POLYBAR_NS_END
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "adapters/alsa.hpp"
|
||||
#include "utils/math.hpp"
|
||||
|
||||
POLYBAR_NS
|
||||
|
||||
|
@ -166,7 +167,7 @@ int alsa_mixer::get_volume() {
|
|||
}
|
||||
}
|
||||
|
||||
return 100.0f * (vol_total / chan_n) / vol_max + 0.5f;
|
||||
return math_util::percentage(vol_total / chan_n, vol_min, vol_max);
|
||||
}
|
||||
|
||||
int alsa_mixer::get_normalized_volume() {
|
||||
|
@ -187,7 +188,7 @@ int alsa_mixer::get_normalized_volume() {
|
|||
}
|
||||
|
||||
if (vol_max - vol_min <= MAX_LINEAR_DB_SCALE * 100)
|
||||
return 100.0f * (vol_total / chan_n - vol_min) / (vol_max - vol_min) + 0.5f;
|
||||
return math_util::percentage(vol_total / chan_n, vol_min, vol_max);
|
||||
|
||||
normalized = pow10((vol_total / chan_n - vol_max) / 6000.0);
|
||||
if (vol_min != SND_CTL_TLV_DB_GAIN_MUTE) {
|
||||
|
@ -205,9 +206,8 @@ void alsa_mixer::set_volume(float percentage) {
|
|||
std::lock_guard<concurrency_util::spin_lock> guard(m_lock);
|
||||
|
||||
long vol_min, vol_max;
|
||||
|
||||
snd_mixer_selem_get_playback_volume_range(m_mixerelement, &vol_min, &vol_max);
|
||||
snd_mixer_selem_set_playback_volume_all(m_mixerelement, vol_max * percentage / 100);
|
||||
snd_mixer_selem_set_playback_volume_all(m_mixerelement, math_util::percentage_to_value<int>(percentage, vol_min, vol_max));
|
||||
}
|
||||
|
||||
void alsa_mixer::set_normalized_volume(float percentage) {
|
||||
|
|
|
@ -41,4 +41,9 @@ int main() {
|
|||
expect(math_util::percentage_to_value(200, 5) == 5);
|
||||
expect(math_util::percentage_to_value(-30, 5) == 0);
|
||||
};
|
||||
|
||||
"ranged_percentage_to_value"_test = [] {
|
||||
expect(math_util::percentage_to_value(50, 200, 300) == 250);
|
||||
expect(math_util::percentage_to_value(50, 1, 5) == 3);
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue