From 92c70bda08472d880e23df30661376d526406230 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Wed, 5 Oct 2022 18:43:44 -0400 Subject: [PATCH] add per user volume slider --- res/css/application-low-priority.css | 8 +++++++ src/abaddon.cpp | 6 +++++ src/audio/manager.cpp | 14 +++++++++++- src/audio/manager.hpp | 3 +++ src/windows/voicewindow.cpp | 33 ++++++++++++++++++++++++---- src/windows/voicewindow.hpp | 3 +++ 6 files changed, 62 insertions(+), 5 deletions(-) diff --git a/res/css/application-low-priority.css b/res/css/application-low-priority.css index 5078d22..130033f 100644 --- a/res/css/application-low-priority.css +++ b/res/css/application-low-priority.css @@ -87,3 +87,11 @@ has to be separate to allow main.css to override certain things .app-window colorswatch { box-shadow: 0 1px rgba(0, 0, 0, 0); } + +.app-window scale { + padding-top: 0px; + padding-bottom: 0px; + margin-top: 0px; + margin-bottom: 0px; + color: @text_color; +} diff --git a/src/abaddon.cpp b/src/abaddon.cpp index 6dc5fd1..8a92de7 100644 --- a/src/abaddon.cpp +++ b/src/abaddon.cpp @@ -438,6 +438,12 @@ void Abaddon::OnVoiceConnected() { } }); + wnd->signal_user_volume_changed().connect([this](Snowflake id, double volume) { + if (const auto ssrc = m_discord.GetSSRCOfUser(id); ssrc.has_value()) { + m_audio->SetVolumeSSRC(*ssrc, volume); + } + }); + wnd->show(); wnd->signal_hide().connect([this, wnd]() { m_discord.DisconnectFromVoice(); diff --git a/src/audio/manager.cpp b/src/audio/manager.cpp index 8321c3e..37f696e 100644 --- a/src/audio/manager.cpp +++ b/src/audio/manager.cpp @@ -28,13 +28,18 @@ void data_callback(ma_device *pDevice, void *pOutput, const void *pInput, ma_uin AudioManager *mgr = reinterpret_cast(pDevice->pUserData); if (mgr == nullptr) return; std::lock_guard _(mgr->m_mutex); + std::lock_guard _2(mgr->m_volume_ssrc_mutex); auto *pOutputF32 = static_cast(pOutput); for (auto &[ssrc, pair] : mgr->m_sources) { + double volume = 1.0; + if (const auto vol_it = mgr->m_volume_ssrc.find(ssrc); vol_it != mgr->m_volume_ssrc.end()) { + volume = vol_it->second; + } auto &buf = pair.first; const size_t n = std::min(static_cast(buf.size()), static_cast(frameCount * 2ULL)); for (size_t i = 0; i < n; i++) { - pOutputF32[i] += buf[i] / 32768.F; + pOutputF32[i] += volume * buf[i] / 32768.F; } buf.erase(buf.begin(), buf.begin() + n); } @@ -180,6 +185,13 @@ void AudioManager::SetMuteSSRC(uint32_t ssrc, bool mute) { } } +void AudioManager::SetVolumeSSRC(uint32_t ssrc, double volume) { + std::lock_guard _(m_volume_ssrc_mutex); + volume *= 0.01; + constexpr const double E = 2.71828182845904523536; + m_volume_ssrc[ssrc] = (std::exp(volume) - 1) / (E - 1); +} + void AudioManager::OnCapturedPCM(const int16_t *pcm, ma_uint32 frames) { if (m_opus_buffer == nullptr || !m_should_capture) return; diff --git a/src/audio/manager.hpp b/src/audio/manager.hpp index 277f2f3..4d2299e 100644 --- a/src/audio/manager.hpp +++ b/src/audio/manager.hpp @@ -31,6 +31,7 @@ public: void SetPlayback(bool playback); void SetMuteSSRC(uint32_t ssrc, bool mute); + void SetVolumeSSRC(uint32_t ssrc, double volume); [[nodiscard]] bool OK() const; @@ -62,8 +63,10 @@ private: std::atomic m_should_playback = true; std::unordered_set m_muted_ssrcs; + std::unordered_map m_volume_ssrc; mutable std::mutex m_muted_ssrc_mutex; + mutable std::mutex m_volume_ssrc_mutex; public: using type_signal_opus_packet = sigc::signal; diff --git a/src/windows/voicewindow.cpp b/src/windows/voicewindow.cpp index 847ba81..ed9971e 100644 --- a/src/windows/voicewindow.cpp +++ b/src/windows/voicewindow.cpp @@ -10,16 +10,26 @@ class VoiceWindowUserListEntry : public Gtk::ListBoxRow { public: VoiceWindowUserListEntry(Snowflake id) - : m_main(Gtk::ORIENTATION_HORIZONTAL) + : m_main(Gtk::ORIENTATION_VERTICAL) + , m_horz(Gtk::ORIENTATION_HORIZONTAL) , m_avatar(32, 32) , m_mute("Mute") { m_name.set_halign(Gtk::ALIGN_START); m_name.set_hexpand(true); m_mute.set_halign(Gtk::ALIGN_END); - m_main.add(m_avatar); - m_main.add(m_name); - m_main.add(m_mute); + m_volume.set_range(0.0, 200.0); + m_volume.set_value_pos(Gtk::POS_LEFT); + m_volume.set_value(100.0); + m_volume.signal_value_changed().connect([this]() { + m_signal_volume.emit(m_volume.get_value()); + }); + + m_horz.add(m_avatar); + m_horz.add(m_name); + m_horz.add(m_mute); + m_main.add(m_horz); + m_main.add(m_volume); add(m_main); show_all_children(); @@ -38,18 +48,26 @@ public: private: Gtk::Box m_main; + Gtk::Box m_horz; LazyImage m_avatar; Gtk::Label m_name; Gtk::CheckButton m_mute; + Gtk::Scale m_volume; public: using type_signal_mute_cs = sigc::signal; + using type_signal_volume = sigc::signal; type_signal_mute_cs signal_mute_cs() { return m_signal_mute_cs; } + type_signal_volume signal_volume() { + return m_signal_volume; + } + private: type_signal_mute_cs m_signal_mute_cs; + type_signal_volume m_signal_volume; }; VoiceWindow::VoiceWindow(Snowflake channel_id) @@ -82,6 +100,9 @@ void VoiceWindow::SetUsers(const std::unordered_set &user_ids) { row->signal_mute_cs().connect([this, id](bool is_muted) { m_signal_mute_user_cs.emit(id, is_muted); }); + row->signal_volume().connect([this, id](double volume) { + m_signal_user_volume_changed.emit(id, volume); + }); m_user_list.add(*row); } } @@ -105,4 +126,8 @@ VoiceWindow::type_signal_deafen VoiceWindow::signal_deafen() { VoiceWindow::type_signal_mute_user_cs VoiceWindow::signal_mute_user_cs() { return m_signal_mute_user_cs; } + +VoiceWindow::type_signal_user_volume_changed VoiceWindow::signal_user_volume_changed() { + return m_signal_user_volume_changed; +} #endif diff --git a/src/windows/voicewindow.hpp b/src/windows/voicewindow.hpp index 50943f4..da4987e 100644 --- a/src/windows/voicewindow.hpp +++ b/src/windows/voicewindow.hpp @@ -34,14 +34,17 @@ public: using type_signal_mute = sigc::signal; using type_signal_deafen = sigc::signal; using type_signal_mute_user_cs = sigc::signal; + using type_signal_user_volume_changed = sigc::signal; type_signal_mute signal_mute(); type_signal_deafen signal_deafen(); type_signal_mute_user_cs signal_mute_user_cs(); + type_signal_user_volume_changed signal_user_volume_changed(); private: type_signal_mute m_signal_mute; type_signal_deafen m_signal_deafen; type_signal_mute_user_cs m_signal_mute_user_cs; + type_signal_user_volume_changed m_signal_user_volume_changed; }; #endif