Skip to content

Commit

Permalink
Move Presets to Objects (#4944)
Browse files Browse the repository at this point in the history
FxUserPreset and ModulatorPreset had static banks which was fine
but made me squeamish so undo that. Closes #4921
  • Loading branch information
baconpaul authored Aug 29, 2021
1 parent 786b535 commit 14b2e18
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 107 deletions.
27 changes: 15 additions & 12 deletions src/common/FxPresetAndClipboardManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@

namespace Surge
{
namespace FxUserPreset
namespace Storage
{

static std::unordered_map<int, std::vector<Preset>> scannedPresets;
static bool haveScannedPresets = false;

std::unordered_map<int, std::vector<Preset>> getPresetsByType() { return scannedPresets; }
std::unordered_map<int, std::vector<FxUserPreset::Preset>> FxUserPreset::getPresetsByType()
{
return scannedPresets;
}

void doPresetRescan(SurgeStorage *storage, bool forceRescan)
void FxUserPreset::doPresetRescan(SurgeStorage *storage, bool forceRescan)
{
if (haveScannedPresets && !forceRescan)
return;
Expand Down Expand Up @@ -199,7 +199,7 @@ void doPresetRescan(SurgeStorage *storage, bool forceRescan)
}
}

std::vector<Preset> getPresetsForSingleType(int id)
std::vector<FxUserPreset::Preset> FxUserPreset::getPresetsForSingleType(int id)
{
if (scannedPresets.find(id) == scannedPresets.end())
{
Expand All @@ -208,9 +208,12 @@ std::vector<Preset> getPresetsForSingleType(int id)
return scannedPresets[id];
}

bool hasPresetsForSingleType(int id) { return scannedPresets.find(id) != scannedPresets.end(); }
bool FxUserPreset::hasPresetsForSingleType(int id)
{
return scannedPresets.find(id) != scannedPresets.end();
}

void saveFxIn(SurgeStorage *storage, FxStorage *fx, const std::string &s)
void FxUserPreset::saveFxIn(SurgeStorage *storage, FxStorage *fx, const std::string &s)
{
char fxName[TXT_SIZE];
fxName[0] = 0;
Expand Down Expand Up @@ -308,10 +311,10 @@ void saveFxIn(SurgeStorage *storage, FxStorage *fx, const std::string &s)
pfile << "</single-fx>\n";
pfile.close();

Surge::FxUserPreset::doPresetRescan(storage, true);
doPresetRescan(storage, true);
}

void loadPresetOnto(const Preset &p, SurgeStorage *storage, FxStorage *fxbuffer)
void FxUserPreset::loadPresetOnto(const Preset &p, SurgeStorage *storage, FxStorage *fxbuffer)
{
fxbuffer->type.val.i = p.type;

Expand Down Expand Up @@ -347,7 +350,7 @@ void loadPresetOnto(const Preset &p, SurgeStorage *storage, FxStorage *fxbuffer)
fxbuffer->p[i].deform_type = p.dt[i];
}
}
} // namespace FxUserPreset
} // namespace Storage

namespace FxClipboard
{
Expand Down
64 changes: 35 additions & 29 deletions src/common/FxPresetAndClipboardManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,50 @@

namespace Surge
{
namespace FxUserPreset
namespace Storage
{
struct Preset
struct FxUserPreset
{
std::string file;
std::string name;
fs::path subPath{};
bool isFactory{false};
int type{-1};
float p[n_fx_params];
bool ts[n_fx_params], er[n_fx_params], da[n_fx_params];
int dt[n_fx_params];

Preset()
struct Preset
{
type = 0;
isFactory = false;
std::string file;
std::string name;
fs::path subPath{};
bool isFactory{false};
int type{-1};
float p[n_fx_params];
bool ts[n_fx_params], er[n_fx_params], da[n_fx_params];
int dt[n_fx_params];

for (int i = 0; i < n_fx_params; ++i)
Preset()
{
p[i] = 0.0;
ts[i] = false;
er[i] = false;
da[i] = false;
dt[i] = -1;
type = 0;
isFactory = false;

for (int i = 0; i < n_fx_params; ++i)
{
p[i] = 0.0;
ts[i] = false;
er[i] = false;
da[i] = false;
dt[i] = -1;
}
}
}
};
};

void doPresetRescan(SurgeStorage *storage, bool forceRescan = false);
std::unordered_map<int, std::vector<Preset>> getPresetsByType();
std::vector<Preset> getPresetsForSingleType(int type_id);
bool hasPresetsForSingleType(int type_id);
std::unordered_map<int, std::vector<Preset>> scannedPresets;
bool haveScannedPresets{false};

void saveFxIn(SurgeStorage *s, FxStorage *fxdata, const std::string &fn);
void doPresetRescan(SurgeStorage *storage, bool forceRescan = false);
std::unordered_map<int, std::vector<Preset>> getPresetsByType();
std::vector<Preset> getPresetsForSingleType(int type_id);
bool hasPresetsForSingleType(int type_id);

void loadPresetOnto(const Preset &p, SurgeStorage *s, FxStorage *fxbuffer);
} // namespace FxUserPreset
void saveFxIn(SurgeStorage *s, FxStorage *fxdata, const std::string &fn);

void loadPresetOnto(const Preset &p, SurgeStorage *s, FxStorage *fxbuffer);
};
} // namespace Storage

namespace FxClipboard
{
Expand Down
17 changes: 8 additions & 9 deletions src/common/ModulatorPresetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@

namespace Surge
{
namespace ModulatorPreset
namespace Storage
{

const static std::string PresetDir = "Modulator Presets";
const static std::string PresetXtn = ".modpreset";

void savePresetToUser(const fs::path &location, SurgeStorage *s, int scene, int lfoid)
void ModulatorPreset::savePresetToUser(const fs::path &location, SurgeStorage *s, int scene,
int lfoid)
{
auto lfo = &(s->getPatch().scene[scene].lfo[lfoid]);
int lfotype = lfo->shape.val.i;
Expand Down Expand Up @@ -121,7 +122,8 @@ void savePresetToUser(const fs::path &location, SurgeStorage *s, int scene, int
/*
* Given a completed path, load the preset into our storage
*/
void loadPresetFrom(const fs::path &location, SurgeStorage *s, int scene, int lfoid)
void ModulatorPreset::loadPresetFrom(const fs::path &location, SurgeStorage *s, int scene,
int lfoid)
{
auto lfo = &(s->getPatch().scene[scene].lfo[lfoid]);
// ToDo: Inverse of above
Expand Down Expand Up @@ -221,13 +223,10 @@ void loadPresetFrom(const fs::path &location, SurgeStorage *s, int scene, int lf
}
}

static std::vector<Category> scanedPresets;
static bool haveScanedPresets = false;

/*
* Note: Clients rely on this being sorted by category path if you change it
*/
std::vector<Category> getPresets(SurgeStorage *s)
std::vector<ModulatorPreset::Category> ModulatorPreset::getPresets(SurgeStorage *s)
{
if (haveScanedPresets)
return scanedPresets;
Expand Down Expand Up @@ -331,10 +330,10 @@ std::vector<Category> getPresets(SurgeStorage *s)
return res;
}

void forcePresetRescan()
void ModulatorPreset::forcePresetRescan()
{
haveScanedPresets = false;
scanedPresets.clear();
}
} // namespace ModulatorPreset
} // namespace Storage
} // namespace Surge
62 changes: 34 additions & 28 deletions src/common/ModulatorPresetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,43 @@ class SurgeStorage;

namespace Surge
{
namespace ModulatorPreset
namespace Storage
{
/*
* Given a storage, scene, and LFO, stream stream it to a file relative to the location
* in the user directory LFO presets area
*/
void savePresetToUser(const fs::path &location, SurgeStorage *s, int scene, int lfo);
struct ModulatorPreset
{
/*
* Given a storage, scene, and LFO, stream stream it to a file relative to the location
* in the user directory LFO presets area
*/
void savePresetToUser(const fs::path &location, SurgeStorage *s, int scene, int lfo);

/*
* Given a compelted path, load the preset into our storage
*/
void loadPresetFrom(const fs::path &location, SurgeStorage *s, int scene, int lfo);
/*
* Given a compelted path, load the preset into our storage
*/
void loadPresetFrom(const fs::path &location, SurgeStorage *s, int scene, int lfo);

/*
* What are the presets we have? In some form of category order
*/
struct Preset
{
std::string name;
fs::path path;
};
/*
* What are the presets we have? In some form of category order
*/
struct Preset
{
std::string name;
fs::path path;
};

struct Category
{
std::string name;
std::string path;
std::string parentPath;
std::vector<Preset> presets;
};
struct Category
{
std::string name;
std::string path;
std::string parentPath;
std::vector<Preset> presets;
};

std::vector<Category> getPresets(SurgeStorage *s);
void forcePresetRescan();
} // namespace ModulatorPreset
std::vector<Category> getPresets(SurgeStorage *s);
void forcePresetRescan();

std::vector<Category> scanedPresets;
bool haveScanedPresets{false};
};
} // namespace Storage
} // namespace Surge
8 changes: 8 additions & 0 deletions src/common/SurgeStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

#include "strnatcmp.h"
#include "libMTSClient.h"
#include "FxPresetAndClipboardManager.h"
#include "ModulatorPresetManager.h"

// FIXME probably remove this when we remove the hardcoded hack below
#include "MSEGModulationHelper.h"
Expand Down Expand Up @@ -643,6 +645,12 @@ SurgeStorage::SurgeStorage(std::string suppliedDataPath) : otherscene_clients(0)
Surge::Storage::getUserDefaultValue(this, Surge::Storage::InitialPatchName, "Init Saw");
initPatchCategory = Surge::Storage::getUserDefaultValue(
this, Surge::Storage::InitialPatchCategory, "Templates");

fxUserPreset = std::make_unique<Surge::Storage::FxUserPreset>();
fxUserPreset->doPresetRescan(this);

modulatorPreset = std::make_unique<Surge::Storage::ModulatorPreset>();
modulatorPreset->forcePresetRescan();
}

void SurgeStorage::createUserDirectory()
Expand Down
12 changes: 12 additions & 0 deletions src/common/SurgeStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,15 @@ class MTSClient;

/* storage layer */

namespace Surge
{
namespace Storage
{
struct FxUserPreset;
struct ModulatorPreset;
} // namespace Storage
} // namespace Surge

class alignas(16) SurgeStorage
{
public:
Expand Down Expand Up @@ -1011,6 +1020,9 @@ class alignas(16) SurgeStorage
std::vector<int> wtOrdering;
std::vector<int> wtCategoryOrdering;

std::unique_ptr<Surge::Storage::FxUserPreset> fxUserPreset;
std::unique_ptr<Surge::Storage::ModulatorPreset> modulatorPreset;

std::string wtpath;
std::string datapath;
std::string userDefaultFilePath;
Expand Down
40 changes: 20 additions & 20 deletions src/gui/SurgeGUIEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1970,33 +1970,33 @@ juce::PopupMenu SurgeGUIEditor::makeLfoMenu(const juce::Point<int> &where)
lfoSubMenu.addItem("[?] LFO Presets", [hurl]() { juce::URL(hurl).launchInDefaultBrowser(); });
lfoSubMenu.addSeparator();

lfoSubMenu.addItem(Surge::GUI::toOSCaseForMenu("Save " + what + " As..."), [this, currentLfoId,
what]() {
// Prompt for a name
promptForMiniEdit(
"preset", "Enter the name for " + what + " preset:", what + " Preset Name",
juce::Point<int>{}, [this, currentLfoId](const std::string &s) {
Surge::ModulatorPreset::savePresetToUser(string_to_path(s), &(this->synth->storage),
current_scene, currentLfoId);
});
// and save
});
lfoSubMenu.addItem(
Surge::GUI::toOSCaseForMenu("Save " + what + " As..."), [this, currentLfoId, what]() {
// Prompt for a name
promptForMiniEdit(
"preset", "Enter the name for " + what + " preset:", what + " Preset Name",
juce::Point<int>{}, [this, currentLfoId](const std::string &s) {
this->synth->storage.modulatorPreset->savePresetToUser(
string_to_path(s), &(this->synth->storage), current_scene, currentLfoId);
});
// and save
});

auto presetCategories = Surge::ModulatorPreset::getPresets(&(synth->storage));
auto presetCategories = this->synth->storage.modulatorPreset->getPresets(&(synth->storage));
if (!presetCategories.empty())
{
lfoSubMenu.addSeparator();
}

std::function<void(juce::PopupMenu & m, const Surge::ModulatorPreset::Category &cat)>
std::function<void(juce::PopupMenu & m, const Surge::Storage::ModulatorPreset::Category &cat)>
recurseCat;
recurseCat = [this, currentLfoId, presetCategories,
&recurseCat](juce::PopupMenu &m, const Surge::ModulatorPreset::Category &cat) {
recurseCat = [this, currentLfoId, presetCategories, &recurseCat](
juce::PopupMenu &m, const Surge::Storage::ModulatorPreset::Category &cat) {
for (const auto &p : cat.presets)
{
auto action = [this, p, currentLfoId]() {
Surge::ModulatorPreset::loadPresetFrom(p.path, &(this->synth->storage),
current_scene, currentLfoId);
this->synth->storage.modulatorPreset->loadPresetFrom(
p.path, &(this->synth->storage), current_scene, currentLfoId);

auto newshape = this->synth->storage.getPatch()
.scene[current_scene]
Expand Down Expand Up @@ -2044,7 +2044,7 @@ juce::PopupMenu SurgeGUIEditor::makeLfoMenu(const juce::Point<int> &where)

lfoSubMenu.addSeparator();
lfoSubMenu.addItem(Surge::GUI::toOSCaseForMenu("Rescan Presets"),
[]() { Surge::ModulatorPreset::forcePresetRescan(); });
[this]() { this->synth->storage.modulatorPreset->forcePresetRescan(); });

return lfoSubMenu;
}
Expand Down Expand Up @@ -3054,8 +3054,8 @@ juce::PopupMenu SurgeGUIEditor::makeDataMenu(const juce::Point<int> &where)
this->synth->storage.refresh_patchlist();
this->scannedForMidiPresets = false;

Surge::FxUserPreset::doPresetRescan(&(this->synth->storage));
Surge::ModulatorPreset::forcePresetRescan();
this->synth->storage.fxUserPreset->doPresetRescan(&(this->synth->storage), true);
this->synth->storage.modulatorPreset->forcePresetRescan();

// Rescan for skins
auto r = this->currentSkin->root;
Expand Down
Loading

0 comments on commit 14b2e18

Please sign in to comment.