From b6e6d00dac6cd75e58ca017417cf171e9692a30a Mon Sep 17 00:00:00 2001 From: ronso0 Date: Fri, 10 Nov 2023 23:08:06 +0100 Subject: [PATCH] Effects: read effect data for decks added after initial setup --- src/effects/effectsmanager.cpp | 34 +++++++++++++++- src/effects/effectsmanager.h | 6 +++ .../presets/effectchainpresetmanager.cpp | 39 ++++++++++++++++++- .../presets/effectchainpresetmanager.h | 4 ++ 4 files changed, 80 insertions(+), 3 deletions(-) diff --git a/src/effects/effectsmanager.cpp b/src/effects/effectsmanager.cpp index 76edf192aff..ee64cbf1003 100644 --- a/src/effects/effectsmanager.cpp +++ b/src/effects/effectsmanager.cpp @@ -27,7 +27,8 @@ EffectsManager::EffectsManager( : m_pConfig(pConfig), m_pChannelHandleFactory(pChannelHandleFactory), m_loEqFreq(ConfigKey(kMixerProfile, kLowEqFrequency), 0., 22040), - m_hiEqFreq(ConfigKey(kMixerProfile, kHighEqFrequency), 0., 22040) { + m_hiEqFreq(ConfigKey(kMixerProfile, kHighEqFrequency), 0., 22040), + m_initializedFromEffectsXml(false) { qRegisterMetaType("EffectChainMixMode"); m_pBackendManager = EffectsBackendManagerPointer(new EffectsBackendManager()); @@ -72,6 +73,12 @@ void EffectsManager::setup() { addStandardEffectChains(); addOutputEffectChain(); // EQ and QuickEffect chain slots are initialized when PlayerManager creates decks. + // Now read effects.xml to load defaults or restore previous states of standard + // effect chains and QuickEffect chain, though only for decks that have been + // created by now. For decks added later on see addDeck(). + // Note: flip this bool now so any deck potentially being added while + // readEffectsXml() is running is also initialized. + m_initializedFromEffectsXml = true; readEffectsXml(); } @@ -131,6 +138,12 @@ EffectChainPointer EffectsManager::getStandardEffectChain(int unitNumber) const void EffectsManager::addDeck(const ChannelHandleAndGroup& deckHandleGroup) { addEqualizerEffectChain(deckHandleGroup); addQuickEffectChain(deckHandleGroup); + // If a deck is added after setup() was run we need to read effects.xml + // again to initialize its QuickEffect chain, either with defaults or the + // previous state. + if (m_initializedFromEffectsXml) { + readEffectsXmlSingleDeck(deckHandleGroup.m_name); + } } void EffectsManager::addEqualizerEffectChain(const ChannelHandleAndGroup& deckHandleGroup) { @@ -202,6 +215,25 @@ void EffectsManager::readEffectsXml() { m_pVisibleEffectsList->readEffectsXml(doc, m_pBackendManager); } +void EffectsManager::readEffectsXmlSingleDeck(const QString& deckGroup) { + QDir settingsPath(m_pConfig->getSettingsPath()); + QFile file(settingsPath.absoluteFilePath(kEffectsXmlFile)); + QDomDocument doc; + + if (file.open(QIODevice::ReadOnly)) { + doc.setContent(&file); + } + file.close(); + + EffectChainPresetPointer pQuickEffectChainPreset = + m_pChainPresetManager->readEffectsXmlSingleDeck(doc, deckGroup); + + auto pQuickEffectChainSlot = m_quickEffectChains.value(deckGroup); + if (pQuickEffectChainSlot) { + pQuickEffectChainSlot->loadChainPreset(pQuickEffectChainPreset); + } +} + void EffectsManager::saveEffectsXml() { QDomDocument doc(EffectXml::kRoot); doc.setContent(EffectXml::kFileHeader); diff --git a/src/effects/effectsmanager.h b/src/effects/effectsmanager.h index 10209cd44f1..de5109ab331 100644 --- a/src/effects/effectsmanager.h +++ b/src/effects/effectsmanager.h @@ -85,6 +85,7 @@ class EffectsManager { void addQuickEffectChain(const ChannelHandleAndGroup& deckHandleGroup); void readEffectsXml(); + void readEffectsXmlSingleDeck(const QString& deckGroup); void saveEffectsXml(); QSet m_registeredInputChannels; @@ -112,5 +113,10 @@ class EffectsManager { ControlPotmeter m_loEqFreq; ControlPotmeter m_hiEqFreq; + // This is set true when setup() is run. Then, the initial decks (their EQ + // and QuickEffect chains) have been initialized, either with defaults or the + // previous state read from effects.xml + bool m_initializedFromEffectsXml; + DISALLOW_COPY_AND_ASSIGN(EffectsManager); }; diff --git a/src/effects/presets/effectchainpresetmanager.cpp b/src/effects/presets/effectchainpresetmanager.cpp index a9965ff8e56..8ad44594cf2 100644 --- a/src/effects/presets/effectchainpresetmanager.cpp +++ b/src/effects/presets/effectchainpresetmanager.cpp @@ -658,14 +658,19 @@ bool EffectChainPresetManager::savePresetXml(EffectChainPresetPointer pPreset) { return success; } -EffectsXmlData EffectChainPresetManager::readEffectsXml( - const QDomDocument& doc, const QStringList& deckStrings) { +EffectChainPresetPointer EffectChainPresetManager::getDefaultQuickEffectPreset() { EffectManifestPointer pDefaultQuickEffectManifest = m_pBackendManager->getManifest( FilterEffect::getId(), EffectBackendType::BuiltIn); auto defaultQuickEffectChainPreset = EffectChainPresetPointer(pDefaultQuickEffectManifest ? new EffectChainPreset(pDefaultQuickEffectManifest) : new EffectChainPreset()); + return defaultQuickEffectChainPreset; +} + +EffectsXmlData EffectChainPresetManager::readEffectsXml( + const QDomDocument& doc, const QStringList& deckStrings) { + auto defaultQuickEffectChainPreset = getDefaultQuickEffectPreset(); QList standardEffectChainPresets; QHash quickEffectPresets; @@ -784,6 +789,36 @@ EffectsXmlData EffectChainPresetManager::readEffectsXml( return EffectsXmlData{quickEffectPresets, standardEffectChainPresets}; } +EffectChainPresetPointer EffectChainPresetManager::readEffectsXmlSingleDeck( + const QDomDocument& doc, const QString& deckString) { + QDomElement root = doc.documentElement(); + + auto pQuickEffectChainPreset = getDefaultQuickEffectPreset(); + + // Read name of last loaded QuickEffect preset + QDomElement quickEffectPresetsElement = + XmlParse::selectElement(root, EffectXml::kQuickEffectChainPresets); + QDomNodeList quickEffectNodeList = + quickEffectPresetsElement.elementsByTagName( + EffectXml::kChainPresetName); + for (int i = 0; i < quickEffectNodeList.count(); ++i) { + QDomElement presetNameElement = quickEffectNodeList.at(i).toElement(); + if (presetNameElement.isNull()) { + continue; + } + if (presetNameElement.attribute(QStringLiteral("group")) == deckString) { + auto pPreset = m_effectChainPresets.value(presetNameElement.text()); + if (pPreset != nullptr || presetNameElement.text() == kNoEffectString) { + // Replace the default preset. + // Load empty preset if the chain was cleared explicitly ('---' preset) + pQuickEffectChainPreset = pPreset; + } + } + } + + return pQuickEffectChainPreset; +} + void EffectChainPresetManager::saveEffectsXml(QDomDocument* pDoc, const EffectsXmlData& data) { // Save presets for current state of standard chains QDomElement rootElement = pDoc->documentElement(); diff --git a/src/effects/presets/effectchainpresetmanager.h b/src/effects/presets/effectchainpresetmanager.h index 8bd326b9661..0482b302fa0 100644 --- a/src/effects/presets/effectchainpresetmanager.h +++ b/src/effects/presets/effectchainpresetmanager.h @@ -69,7 +69,11 @@ class EffectChainPresetManager : public QObject { bool savePreset(EffectChainPresetPointer pPreset); void updatePreset(EffectChainPointer pChainSlot); + EffectChainPresetPointer getDefaultQuickEffectPreset(); + EffectsXmlData readEffectsXml(const QDomDocument& doc, const QStringList& deckStrings); + EffectChainPresetPointer readEffectsXmlSingleDeck( + const QDomDocument& doc, const QString& deckString); void saveEffectsXml(QDomDocument* pDoc, const EffectsXmlData& data); signals: