Detect setting changes loading Voice JSON

This commit is contained in:
Kae 2023-07-19 01:16:47 +10:00
parent e1645f37fc
commit a9dac1b2df
2 changed files with 37 additions and 9 deletions

View File

@ -141,19 +141,38 @@ Voice::~Voice() {
void Voice::init() { void Voice::init() {
resetEncoder(); resetEncoder();
if (m_inputEnabled) resetDevice();
openDevice(); }
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) { void Voice::loadJson(Json const& config) {
{
bool enabled = shouldEnableInput();
m_enabled = config.getBool("enabled", m_enabled); m_enabled = config.getBool("enabled", m_enabled);
m_inputEnabled = config.getBool("inputEnabled", m_inputEnabled); m_inputEnabled = config.getBool("inputEnabled", m_inputEnabled);
m_deviceName = config.optQueryString("inputDevice"); if (shouldEnableInput() != enabled)
resetDevice();
}
if (change(m_deviceName, config.optQueryString("inputDevice")))
resetDevice();
m_threshold = config.getFloat("threshold", m_threshold); m_threshold = config.getFloat("threshold", m_threshold);
m_inputVolume = config.getFloat("inputVolume", m_inputVolume); m_inputVolume = config.getFloat("inputVolume", m_inputVolume);
m_outputVolume = config.getFloat("outputVolume", m_outputVolume); 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) { 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) { 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)); opus_encoder_ctl(m_encoder.get(), OPUS_SET_BITRATE(channels == 2 ? 50000 : 24000));
} }
void Voice::resetDevice() {
if (shouldEnableInput())
openDevice();
else
closeDevice();
}
void Voice::openDevice() { void Voice::openDevice() {
closeDevice(); closeDevice();

View File

@ -147,8 +147,10 @@ public:
private: private:
static Voice* s_singleton; static Voice* s_singleton;
void resetEncoder(); void resetEncoder();
void resetDevice();
void openDevice(); void openDevice();
void closeDevice(); void closeDevice();
inline bool shouldEnableInput() const { return m_enabled && m_inputEnabled; }
bool playSpeaker(SpeakerPtr const& speaker, int channels); bool playSpeaker(SpeakerPtr const& speaker, int channels);