feat: allow setting volume directly

This commit is contained in:
Brenno Lemos 2023-10-15 11:08:30 -03:00
parent 64d7fae03a
commit c9e129cda2
2 changed files with 15 additions and 3 deletions

View File

@ -66,7 +66,8 @@ class AudioBackend {
AudioBackend(std::function<void()> on_updated_cb, private_constructor_tag tag); AudioBackend(std::function<void()> on_updated_cb, private_constructor_tag tag);
~AudioBackend(); ~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); void setIgnoredSinks(const Json::Value& config);

View File

@ -3,6 +3,7 @@
#include <fmt/core.h> #include <fmt/core.h>
#include <pulse/error.h> #include <pulse/error.h>
#include <pulse/subscribe.h> #include <pulse/subscribe.h>
#include <pulse/volume.h>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
@ -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); 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<double>(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<double>(PA_VOLUME_NORM) / 100; double volume_tick = static_cast<double>(PA_VOLUME_NORM) / 100;
pa_volume_t change = volume_tick; pa_volume_t change = volume_tick;
pa_cvolume pa_volume = pa_volume_; pa_cvolume pa_volume = pa_volume_;
max_volume = std::min(max_volume, static_cast<int>(PA_VOLUME_UI_MAX)); max_volume = std::min(max_volume, static_cast<uint16_t>(PA_VOLUME_UI_MAX));
if (change_type == ChangeType::Increase) { if (change_type == ChangeType::Increase) {
if (volume_ < max_volume) { if (volume_ < max_volume) {