1 #ifndef AL_PRESETHANDLER_H
2 #define AL_PRESETHANDLER_H
45 #include <condition_variable>
53 #include "al/protocol/al_OSC.hpp"
54 #include "al/system/al_Time.hpp"
55 #include "al/ui/al_Parameter.hpp"
56 #include "al/ui/al_ParameterServer.hpp"
68 typedef std::map<std::string, std::vector<ParameterField>> ParameterStates;
88 PresetHandler(TimeMasterMode timeMasterMode = TimeMasterMode::TIME_MASTER_CPU,
89 std::string rootDirectory =
"presets",
bool verbose =
false);
119 void storePreset(
int index, std::string name =
"",
bool overwrite =
true);
143 void recallPresetSynchronous(std::string name);
174 ParameterStates &endValues,
double factor = 1.0);
176 std::map<int, std::string> availablePresets();
177 std::string getPresetName(
int index);
178 std::string getCurrentPresetName() {
return mCurrentPresetName; }
191 int getCurrentPresetIndex();
193 float getMorphTime();
194 void setMorphTime(
float time);
195 void setMaxMorphTime(
float time);
196 void stopMorphing() { mTotalSteps.store(0); }
197 void morphTo(ParameterStates ¶meterStates,
float morphTime);
198 void morphTo(std::string presetName,
float morphTime);
200 void setMorphStepTime(
float stepTime) { mMorphInterval = stepTime; }
208 void setSubDirectory(std::string directory);
209 std::string getSubDirectory() {
return mSubDir; }
214 void setRootPath(std::string path);
230 std::function<
void(
int index,
void *sender,
void *userData)> cb,
231 void *userData =
nullptr);
239 std::function<
void(
int index, std::string name,
void *userData)> cb,
240 void *userData =
nullptr);
249 typedef const std::function<void(std::string)> PresetMapCallback;
259 return this->registerParameter(param);
265 return this->registerParameterBundle(bundle);
268 std::vector<ParameterMeta *> parameters() {
return mParameters; }
270 std::string buildMapPath(std::string mapName,
bool useSubDirectory =
false);
272 std::vector<std::string> availablePresetMaps();
274 std::map<int, std::string> readPresetMap(std::string mapName =
"default");
276 void setCurrentPresetMap(std::string mapName =
"default",
277 bool autoCreate =
false);
279 void setPresetMap(std::map<int, std::string> presetsMap) {
280 mPresetsMap = presetsMap;
283 void storeCurrentPresetMap(std::string mapName =
"",
284 bool useSubDirectory =
false);
295 void changeParameterValue(std::string presetName, std::string parameterPath,
310 [[deprecated]]
void verbose(
bool isVerbose) { mVerbose = isVerbose; }
311 void setVerbose(
bool isVerbose =
true) { mVerbose = isVerbose; }
312 bool verbose() {
return mVerbose; }
330 bool overwrite =
true);
332 void setTimeMaster(TimeMasterMode masterMode);
334 void startCpuThread();
335 void stopCpuThread();
344 ParameterStates getBundleStates(
ParameterBundle *bundle, std::string
id);
346 bool mVerbose{
false};
347 bool mUseCallbacks{
true};
348 std::string mRootDir;
351 std::string mCurrentMapName;
352 std::string mCurrentPresetName;
353 std::vector<ParameterMeta *> mParameters;
355 std::vector<std::string> mSkipParameters;
356 std::mutex mSkipParametersLock;
358 std::map<std::string, std::vector<ParameterBundle *>> mBundles;
362 std::mutex mFileLock;
364 std::mutex mTargetLock;
365 ParameterStates mTargetValues;
366 ParameterStates mStartValues;
368 TimeMasterMode mTimeMasterMode{TimeMasterMode::TIME_MASTER_CPU};
370 Parameter mMorphTime{
"morphTime",
"", 0.0, 0.0, 20.0};
372 std::atomic<uint64_t> mMorphStepCount{0};
373 std::atomic<uint64_t> mTotalSteps{0};
375 bool mCpuThreadRunning{
false};
376 std::unique_ptr<std::thread> mMorphingThread;
378 double mMorphInterval{0.02};
381 std::vector<std::function<void(
int index,
void *sender,
void *userData)>>
383 std::vector<void *> mCallbackUdata;
384 std::vector<std::function<void(
int index, std::string name,
void *userData)>>
386 std::vector<void *> mStoreCallbackUdata;
387 std::vector<std::function<void(std::string)>> mPresetsMapCbs;
389 std::map<int, std::string> mPresetsMap;
The PresetHandler class handles sorting and recalling of presets.
void skipParameter(std::string parameterAddr, bool skip=true)
Add or remove a parameter address from group that will be skipped when recalling presets.
void registerPresetMapCallback(PresetMapCallback cb)
Register a callback to be notified when preset map cahges.
ParameterStates loadPresetValues(std::string name)
load preset into parameter states data structure without setting values
std::string getRootPath()
Base path without appending sub directory.
void storePreset(int index, std::string name="", bool overwrite=true)
Store preset at index. The name argument specifies the preset name.
std::string recallPreset(int index)
Recall a preset by index number.
void registerPresetCallback(std::function< void(int index, void *sender, void *userData)> cb, void *userData=nullptr)
Register a callback to be notified when a preset is loaded.
std::string getCurrentPath()
Path including subdirectory if any.
std::string recallPresetSynchronous(int index)
recall immediately (not using the morph thread)
void useCallbacks(bool use)
useCallbacks determines whether to call the internal callbacks
PresetHandler(std::string rootDirectory, bool verbose=false)
PresetHandler contructor.
bool savePresetValues(const ParameterStates &values, std::string presetName, bool overwrite=true)
save list of parameter states into text preset file
void setInterpolatedPreset(int index1, int index2, double factor)
Set parameters to values interpolated between two presets.
void storePreset(std::string name)
Stores preset.
void registerStoreCallback(std::function< void(int index, std::string name, void *userData)> cb, void *userData=nullptr)
Register a callback to be notified when a preset is stored.
static int asciiToPresetIndex(int ascii, int offset=0)
Map QWERTY ascii keys to presets 0-49.
void setInterpolatedValues(ParameterStates &startValues, ParameterStates &endValues, double factor=1.0)
Interpolate between start and end values according to factor.
PresetHandler(TimeMasterMode timeMasterMode=TimeMasterMode::TIME_MASTER_CPU, std::string rootDirectory="presets", bool verbose=false)
Constructor with option to set time master mode.
void registerMorphTimeCallback(Parameter::ParameterChangeCallback cb)
Register a callback to be notified when morph time parameter is changed.
void recallPreset(std::string name)
Recall a preset by name.