From c9e129cda2c5dd7fd3f22509d06e4fb853ce1938 Mon Sep 17 00:00:00 2001 From: Brenno Lemos Date: Sun, 15 Oct 2023 11:08:30 -0300 Subject: [PATCH] feat: allow setting volume directly --- include/util/audio_backend.hpp | 3 ++- src/util/audio_backend.cpp | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/util/audio_backend.hpp b/include/util/audio_backend.hpp index 9435a842..a0c36ea1 100644 --- a/include/util/audio_backend.hpp +++ b/include/util/audio_backend.hpp @@ -66,7 +66,8 @@ class AudioBackend { AudioBackend(std::function on_updated_cb, private_constructor_tag tag); ~AudioBackend(); - void changeVolume(ChangeType change_type, double step = 1, int max_volume = 100); + void changeVolume(uint16_t volume, uint16_t max_volume = 100); + void changeVolume(ChangeType change_type, double step = 1, uint16_t max_volume = 100); void setIgnoredSinks(const Json::Value& config); diff --git a/src/util/audio_backend.cpp b/src/util/audio_backend.cpp index c230c1c9..ddb2ab6f 100644 --- a/src/util/audio_backend.cpp +++ b/src/util/audio_backend.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -203,12 +204,22 @@ void AudioBackend::serverInfoCb(pa_context *context, const pa_server_info *i, vo pa_context_get_source_info_list(context, sourceInfoCb, data); } -void AudioBackend::changeVolume(ChangeType change_type, double step, int max_volume) { +void AudioBackend::changeVolume(uint16_t volume, uint16_t max_volume) { + double volume_tick = static_cast(PA_VOLUME_NORM) / 100; + pa_cvolume pa_volume = pa_volume_; + + volume = std::min(volume, max_volume); + pa_cvolume_set(&pa_volume, pa_volume_.channels, volume * volume_tick); + + pa_context_set_sink_volume_by_index(context_, sink_idx_, &pa_volume, volumeModifyCb, this); +} + +void AudioBackend::changeVolume(ChangeType change_type, double step, uint16_t max_volume) { double volume_tick = static_cast(PA_VOLUME_NORM) / 100; pa_volume_t change = volume_tick; pa_cvolume pa_volume = pa_volume_; - max_volume = std::min(max_volume, static_cast(PA_VOLUME_UI_MAX)); + max_volume = std::min(max_volume, static_cast(PA_VOLUME_UI_MAX)); if (change_type == ChangeType::Increase) { if (volume_ < max_volume) {