diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-19 01:16:47 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-19 01:16:47 +1000 |
commit | a9dac1b2dfedcf8a4c4b0322789d30633af80f8b (patch) | |
tree | 0b0d1a9487d7dea96dc8943b85f2a102b25fe3ec /source/frontend/StarVoice.cpp | |
parent | e1645f37fc72e7733b64c51ffbee0370e13cbe29 (diff) |
Detect setting changes loading Voice JSON
Diffstat (limited to 'source/frontend/StarVoice.cpp')
-rw-r--r-- | source/frontend/StarVoice.cpp | 44 |
1 files changed, 35 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 <typename T> +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(); |