Schedule voice config save when updating settings from Lua

This commit is contained in:
Kae 2023-07-19 19:04:31 +10:00
parent 35b1c36b17
commit 620c23e70a
3 changed files with 20 additions and 10 deletions

View File

@ -211,7 +211,7 @@ void ClientApplication::applicationInit(ApplicationControllerPtr appController)
appController->setUpdateTrackWindow(assets->json("/client.config:updateTrackWindow").toFloat()); appController->setUpdateTrackWindow(assets->json("/client.config:updateTrackWindow").toFloat());
if (auto jVoice = configuration->get("voice")) if (auto jVoice = configuration->get("voice"))
m_voice->loadJson(jVoice.toObject()); m_voice->loadJson(jVoice.toObject(), true);
m_voice->init(); m_voice->init();
} }

View File

@ -141,7 +141,9 @@ Voice::~Voice() {
m_thread.finish(); m_thread.finish();
if (m_nextSaveTime)
save(); save();
closeDevice(); closeDevice();
s_singleton = nullptr; s_singleton = nullptr;
@ -154,46 +156,54 @@ void Voice::init() {
template <typename T> template <typename T>
inline bool change(T& value, T newValue) { inline bool change(T& value, T newValue, bool& out) {
bool changed = value != newValue; bool changed = value != newValue;
out |= changed;
value = move(newValue); value = move(newValue);
return changed; return changed;
} }
void Voice::loadJson(Json const& config) { void Voice::loadJson(Json const& config, bool skipSave) {
// Not all keys are required // Not all keys are required
bool changed = false;
{ {
bool enabled = shouldEnableInput(); 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);
if (shouldEnableInput() != enabled) if (shouldEnableInput() != enabled) {
changed = true;
resetDevice(); resetDevice();
} }
}
if (config.contains("deviceName") // Make sure null-type key exists if (config.contains("deviceName") // Make sure null-type key exists
&& change(m_deviceName, config.optString("deviceName"))) && change(m_deviceName, config.optString("deviceName"), changed))
resetDevice(); 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);
if (change(m_loopBack, config.getBool("loopBack", m_loopBack))) if (change(m_loopBack, config.getBool("loopBack", m_loopBack), changed))
m_clientSpeaker->playing = false; m_clientSpeaker->playing = false;
if (auto inputMode = config.optString("inputMode")) { if (auto inputMode = config.optString("inputMode")) {
if (change(m_inputMode, VoiceInputModeNames.getLeft(*inputMode))) if (change(m_inputMode, VoiceInputModeNames.getLeft(*inputMode), changed))
m_lastInputTime = 0; m_lastInputTime = 0;
} }
if (auto channelMode = config.optString("channelMode")) { if (auto channelMode = config.optString("channelMode")) {
if (change(m_channelMode, VoiceChannelModeNames.getLeft(*channelMode))) { if (change(m_channelMode, VoiceChannelModeNames.getLeft(*channelMode), changed)) {
closeDevice(); closeDevice();
resetEncoder(); resetEncoder();
resetDevice(); resetDevice();
} }
} }
if (changed && !skipSave)
scheduleSave();
} }

View File

@ -113,7 +113,7 @@ public:
void init(); void init();
void loadJson(Json const& config); void loadJson(Json const& config, bool skipSave = false);
Json saveJson() const; Json saveJson() const;
void save() const; void save() const;