From a9dac1b2dfedcf8a4c4b0322789d30633af80f8b Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Wed, 19 Jul 2023 01:16:47 +1000 Subject: [PATCH] Detect setting changes loading Voice JSON --- source/frontend/StarVoice.cpp | 44 ++++++++++++++++++++++++++++------- source/frontend/StarVoice.hpp | 2 ++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/source/frontend/StarVoice.cpp b/source/frontend/StarVoice.cpp index f27c000..202444e 100644 --- a/source/frontend/StarVoice.cpp +++ b/source/frontend/StarVoice.cpp @@ -140,20 +140,39 @@ Voice::~Voice() { } void Voice::init() { - resetEncoder(); - if (m_inputEnabled) - openDevice(); + resetEncoder(); + resetDevice(); +} + + +template +inline bool change(T& value, T newValue) { + bool changed = value != newValue; + value = move(newValue); + return changed; } void Voice::loadJson(Json const& config) { - m_enabled = config.getBool("enabled", m_enabled); - m_inputEnabled = config.getBool("inputEnabled", m_inputEnabled); - m_deviceName = config.optQueryString("inputDevice"); + { + bool enabled = shouldEnableInput(); + m_enabled = config.getBool("enabled", m_enabled); + m_inputEnabled = config.getBool("inputEnabled", m_inputEnabled); + if (shouldEnableInput() != enabled) + resetDevice(); + } + + if (change(m_deviceName, config.optQueryString("inputDevice"))) + resetDevice(); + m_threshold = config.getFloat("threshold", m_threshold); m_inputVolume = config.getFloat("inputVolume", m_inputVolume); m_outputVolume = config.getFloat("outputVolume", m_outputVolume); - m_inputMode = VoiceInputModeNames.getLeft(config.getString("inputMode", "PushToTalk")); - m_channelMode = VoiceChannelModeNames.getLeft(config.getString("channelMode", "Mono")); + + if (change(m_inputMode, VoiceInputModeNames.getLeft(config.getString("inputMode", "PushToTalk")))) + m_lastInputTime = 0; + + if (change(m_channelMode, VoiceChannelModeNames.getLeft(config.getString("channelMode", "Mono")))) + resetEncoder(); } @@ -478,7 +497,7 @@ bool Voice::receive(SpeakerPtr speaker, std::string_view view) { } void Voice::setInput(bool input) { - m_lastInputTime = input ? Time::monotonicMilliseconds() + 1000 : 0; + m_lastInputTime = (m_deviceOpen && input) ? Time::monotonicMilliseconds() + 1000 : 0; } OpusDecoder* Voice::createDecoder(int channels) { @@ -505,6 +524,13 @@ void Voice::resetEncoder() { opus_encoder_ctl(m_encoder.get(), OPUS_SET_BITRATE(channels == 2 ? 50000 : 24000)); } +void Voice::resetDevice() { + if (shouldEnableInput()) + openDevice(); + else + closeDevice(); +} + void Voice::openDevice() { closeDevice(); diff --git a/source/frontend/StarVoice.hpp b/source/frontend/StarVoice.hpp index d6bc467..18e677a 100644 --- a/source/frontend/StarVoice.hpp +++ b/source/frontend/StarVoice.hpp @@ -147,8 +147,10 @@ public: private: static Voice* s_singleton; void resetEncoder(); + void resetDevice(); void openDevice(); void closeDevice(); + inline bool shouldEnableInput() const { return m_enabled && m_inputEnabled; } bool playSpeaker(SpeakerPtr const& speaker, int channels);