feat(pulse): Show volume in decibels (#1894)

Adds `%decibels%` token to the pulseaudio module
This commit is contained in:
julio-b 2019-10-09 00:13:58 +00:00 committed by Patrick Ziegler
parent 1fc6942482
commit 751c21cd37
4 changed files with 13 additions and 0 deletions

View File

@ -38,6 +38,7 @@ class pulseaudio {
int process_events();
int get_volume();
double get_decibels();
void set_volume(float percentage);
void inc_volume(int delta_perc);
void set_mute(bool mode);

View File

@ -50,6 +50,7 @@ namespace modules {
int m_interval{5};
atomic<bool> m_muted{false};
atomic<int> m_volume{0};
atomic<double> m_decibels{0};
};
}

View File

@ -154,6 +154,13 @@ int pulseaudio::get_volume() {
return static_cast<int>(pa_cvolume_max(&cv) * 100.0f / PA_VOLUME_NORM + 0.5f);
}
/**
* Get volume in decibels
*/
double pulseaudio::get_decibels() {
return pa_sw_volume_to_dB(pa_cvolume_max(&cv));
}
/**
* Set volume to given percentage
*/

View File

@ -66,11 +66,13 @@ namespace modules {
// Get volume and mute state
m_volume = 100;
m_decibels = PA_DECIBEL_MININFTY;
m_muted = false;
try {
if (m_pulseaudio) {
m_volume = m_volume * m_pulseaudio->get_volume() / 100.0f;
m_decibels = m_pulseaudio->get_decibels();
m_muted = m_muted || m_pulseaudio->is_muted();
}
} catch (const pulseaudio_error& err) {
@ -81,11 +83,13 @@ namespace modules {
if (m_label_volume) {
m_label_volume->reset_tokens();
m_label_volume->replace_token("%percentage%", to_string(m_volume));
m_label_volume->replace_token("%decibels%", string_util::floating_point(m_decibels, 2, true));
}
if (m_label_muted) {
m_label_muted->reset_tokens();
m_label_muted->replace_token("%percentage%", to_string(m_volume));
m_label_muted->replace_token("%decibels%", string_util::floating_point(m_decibels, 2, true));
}
return true;