Skip to content

Commit

Permalink
plugin: preserve state when saving from editor
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Dec 9, 2024
1 parent e7a9ed2 commit 7a3be92
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
18 changes: 9 additions & 9 deletions plugin/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct YsfxEditor::Impl {
void updateInfo();
void grabInfoAndUpdate();
void chooseFileAndLoad();
void loadFile(const juce::File &file);
void loadFile(const juce::File &file, bool keepState);
void popupRecentFiles();
void popupRecentOpts();
void popupPresets();
Expand Down Expand Up @@ -260,7 +260,7 @@ void YsfxEditor::filesDropped(const juce::StringArray &files, int x, int y)
if (files.size() == 1) {
juce::File file{files[0]};
if (file.existsAsFile()) {
m_impl->loadFile(files[0]);
m_impl->loadFile(files[0], false);
}
}
}
Expand Down Expand Up @@ -486,7 +486,7 @@ void YsfxEditor::Impl::chooseFileAndLoad()
mustAskConfirmation,
[this, normalLoad, result]() {
if (normalLoad) saveScaling();
loadFile(result);
loadFile(result, false);
},
this->m_self
);
Expand Down Expand Up @@ -557,15 +557,15 @@ void YsfxEditor::Impl::loadScaling()
}
}

void YsfxEditor::Impl::loadFile(const juce::File &file)
void YsfxEditor::Impl::loadFile(const juce::File &file, bool keepState)
{
{
juce::ScopedLock lock{m_pluginProperties->getLock()};
m_pluginProperties->setValue("load_path", file.getParentDirectory().getFullPathName());
m_pluginProperties->save();
}

m_proc->loadJsfxFile(file.getFullPathName(), nullptr, true);
m_proc->loadJsfxFile(file.getFullPathName(), nullptr, true, keepState);
relayoutUILater();

juce::RecentlyOpenedFilesList recent = loadRecentFiles();
Expand All @@ -592,7 +592,7 @@ void YsfxEditor::Impl::popupRecentFiles()
ysfx_is_compiled(m_info->effect.get()),
[this, selectedFile]() {
saveScaling();
loadFile(selectedFile);
loadFile(selectedFile, false);
},
this->m_self
);
Expand Down Expand Up @@ -952,7 +952,7 @@ void YsfxEditor::Impl::connectUI()
ysfx_is_compiled(fx),
[this, file]() {
resetScaling(file);
loadFile(file);
loadFile(file, false);
},
this->m_self
);
Expand All @@ -973,8 +973,8 @@ void YsfxEditor::Impl::connectUI()
}
};

m_ideView->onFileSaved = [this](const juce::File &file) { loadFile(file); };
m_ideView->onReloadRequested = [this](const juce::File &file) { loadFile(file); };
m_ideView->onFileSaved = [this](const juce::File &file) { loadFile(file, true); };
m_ideView->onReloadRequested = [this](const juce::File &file) { loadFile(file, true); };

m_infoTimer.reset(FunctionalTimer::create([this]() { grabInfoAndUpdate(); }));
m_infoTimer->startTimer(100);
Expand Down
19 changes: 15 additions & 4 deletions plugin/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct YsfxProcessor::Impl : public juce::AudioProcessorListener {
public:
explicit SliderNotificationUpdater(Impl *impl) : m_impl{impl} {}
void addSlidersToNotify(uint64_t mask, int group) { m_sliderMask[group].fetch_or(mask); }
void updateTouch(uint64_t mask, int group) { m_touchMask[group].exchange(mask); };
void updateTouch(uint64_t mask, int group) { m_touchMask[group].exchange(mask); }

protected:
void handleAsyncUpdate() override;
Expand Down Expand Up @@ -257,11 +257,22 @@ YsfxParameter *YsfxProcessor::getYsfxParameter(int sliderIndex)
return static_cast<YsfxParameter *>(getParameters()[paramIndex]);
}

void YsfxProcessor::loadJsfxFile(const juce::String &filePath, ysfx_state_t *initialState, bool async)
void YsfxProcessor::loadJsfxFile(const juce::String &filePath, ysfx_state_t *initialState, bool async, bool preserveState)
{
Impl::LoadRequest::Ptr loadRequest{new Impl::LoadRequest};
loadRequest->filePath = filePath;

if (preserveState) {
jassert(!initialState);

{
AudioProcessorSuspender sus(*this);
sus.lockCallbacks();
ysfx_t *fx = m_impl->m_fx.get();
initialState = ysfx_save_state(fx);
}
}

if (m_impl->m_failedLoad.load() == RetryState::retrying) {
{
const juce::ScopedLock sl(m_impl->m_loadLock);
Expand Down Expand Up @@ -666,10 +677,10 @@ void YsfxProcessor::setStateInformation(const void *data, int sizeInBytes)
state.slider_count = (uint32_t)sliders.size();
state.data = (uint8_t *)dataBlock.getData();
state.data_size = dataBlock.getSize();
loadJsfxFile(path.getFullPathName(), &state, false);
loadJsfxFile(path.getFullPathName(), &state, false, false);
}
else {
loadJsfxFile(path.getFullPathName(), nullptr, false);
loadJsfxFile(path.getFullPathName(), nullptr, false, false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class YsfxProcessor : public juce::AudioProcessor {
~YsfxProcessor() override;

YsfxParameter *getYsfxParameter(int sliderIndex);
void loadJsfxFile(const juce::String &filePath, ysfx_state_t *initialState, bool async);
void loadJsfxFile(const juce::String &filePath, ysfx_state_t *initialState, bool async, bool preserveState);
void loadJsfxPreset(YsfxInfo::Ptr info, ysfx_bank_shared bank, uint32_t index, PresetLoadMode load, bool async);
bool presetExists(const char *preset_name);
void reloadBank();
Expand Down

0 comments on commit 7a3be92

Please sign in to comment.