From 751c21cd37018093dd8f8e8bc7b2cf9c2333c8d8 Mon Sep 17 00:00:00 2001 From: julio-b Date: Wed, 9 Oct 2019 00:13:58 +0000 Subject: [PATCH] feat(pulse): Show volume in decibels (#1894) Adds `%decibels%` token to the pulseaudio module --- include/adapters/pulseaudio.hpp | 1 + include/modules/pulseaudio.hpp | 1 + src/adapters/pulseaudio.cpp | 7 +++++++ src/modules/pulseaudio.cpp | 4 ++++ 4 files changed, 13 insertions(+) diff --git a/include/adapters/pulseaudio.hpp b/include/adapters/pulseaudio.hpp index 3d372848..39eceb66 100644 --- a/include/adapters/pulseaudio.hpp +++ b/include/adapters/pulseaudio.hpp @@ -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); diff --git a/include/modules/pulseaudio.hpp b/include/modules/pulseaudio.hpp index 15a84df5..48c381c5 100644 --- a/include/modules/pulseaudio.hpp +++ b/include/modules/pulseaudio.hpp @@ -50,6 +50,7 @@ namespace modules { int m_interval{5}; atomic m_muted{false}; atomic m_volume{0}; + atomic m_decibels{0}; }; } diff --git a/src/adapters/pulseaudio.cpp b/src/adapters/pulseaudio.cpp index 9e65eeba..666bf5d7 100644 --- a/src/adapters/pulseaudio.cpp +++ b/src/adapters/pulseaudio.cpp @@ -154,6 +154,13 @@ int pulseaudio::get_volume() { return static_cast(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 */ diff --git a/src/modules/pulseaudio.cpp b/src/modules/pulseaudio.cpp index d65e3bca..8fe066cc 100644 --- a/src/modules/pulseaudio.cpp +++ b/src/modules/pulseaudio.cpp @@ -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;