Skip to content

Commit

Permalink
- added Engine.compressJSON / Engine.uncompressJSON()
Browse files Browse the repository at this point in the history
- added UserPresetHandler.getSecondsSinceLastPresetSave()
  • Loading branch information
christoph-hart committed Aug 30, 2023
1 parent 3427902 commit 57a5eba
Show file tree
Hide file tree
Showing 9 changed files with 5,308 additions and 5,225 deletions.
9 changes: 9 additions & 0 deletions hi_core/hi_core/MainController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,15 @@ bool MainController::UserPresetHandler::saveStateManager(ValueTree& newPreset, c
return processStateManager(true, newPreset, id);
}

double MainController::UserPresetHandler::getSecondsSinceLastPresetLoad() const
{
auto now = Time::getMillisecondCounter();

auto delta = now - timeOfLastPresetLoad;

return (double)delta / 1000.0;
}

bool MainController::UserPresetHandler::processStateManager(bool shouldSave, ValueTree& presetRoot, const Identifier& stateId)
{
for (int i = 0; i < stateManagers.size(); i++)
Expand Down
10 changes: 8 additions & 2 deletions hi_core/hi_core/MainController.h
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ class MainController: public GlobalScriptCompileBroadcaster,

void setUseCustomDataModel(bool shouldUseCustomModel, bool usePersistentObject);


double getSecondsSinceLastPresetLoad() const;

LambdaBroadcaster<bool> deferredAutomationListener;

Expand Down Expand Up @@ -981,6 +981,8 @@ class MainController: public GlobalScriptCompileBroadcaster,
MainController* mc;
bool useUndoForPresetLoads = false;



struct CustomStateManager : public UserPresetStateManager
{
CustomStateManager(UserPresetHandler& parent_);
Expand Down Expand Up @@ -1008,8 +1010,12 @@ class MainController: public GlobalScriptCompileBroadcaster,

CustomAutomationData::List customAutomationData;



private:


uint32 timeOfLastPresetLoad = 0;

bool processStateManager(bool shouldSave, ValueTree& presetRoot, const Identifier& stateId);

JUCE_DECLARE_WEAK_REFERENCEABLE(UserPresetHandler);
Expand Down
6 changes: 4 additions & 2 deletions hi_core/hi_core/UserPresetHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ float MainController::UserPresetHandler::CustomAutomationData::ProcessorConnecti
MainController::UserPresetHandler::UserPresetHandler(MainController* mc_) :
mc(mc_)
{

timeOfLastPresetLoad = Time::getMillisecondCounter();
}

void MainController::UserPresetHandler::loadUserPreset(const ValueTree& v, bool useUndoManagerIfEnabled)
Expand Down Expand Up @@ -523,7 +523,9 @@ void MainController::UserPresetHandler::loadUserPresetInternal()

{
LockHelpers::freeToGo(mc);


timeOfLastPresetLoad = Time::getMillisecondCounter();

ValueTree userPresetToLoad = pendingPreset;

#if USE_BACKEND
Expand Down
8 changes: 8 additions & 0 deletions hi_scripting/scripting/api/ScriptExpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct ScriptUserPresetHandler::Wrapper
API_VOID_METHOD_WRAPPER_3(ScriptUserPresetHandler, updateAutomationValues);
API_METHOD_WRAPPER_1(ScriptUserPresetHandler, getAutomationIndex);
API_METHOD_WRAPPER_2(ScriptUserPresetHandler, setAutomationValue);
API_METHOD_WRAPPER_0(ScriptUserPresetHandler, getSecondsSinceLastPresetLoad);
API_VOID_METHOD_WRAPPER_1(ScriptUserPresetHandler, updateSaveInPresetComponents);
API_VOID_METHOD_WRAPPER_0(ScriptUserPresetHandler, updateConnectedComponentsFromModuleState);
API_VOID_METHOD_WRAPPER_1(ScriptUserPresetHandler, setUseUndoForPresetLoading);
Expand Down Expand Up @@ -87,6 +88,7 @@ ScriptUserPresetHandler::ScriptUserPresetHandler(ProcessorWithScriptingContent*
ADD_API_METHOD_1(setUseUndoForPresetLoading);
ADD_API_METHOD_0(createObjectForSaveInPresetComponents);
ADD_API_METHOD_0(createObjectForAutomationValues);
ADD_API_METHOD_0(getSecondsSinceLastPresetLoad);
ADD_API_METHOD_0(runTest);

}
Expand Down Expand Up @@ -506,6 +508,12 @@ void ScriptUserPresetHandler::updateAutomationValues(var data, bool sendMessage,
}
}

double ScriptUserPresetHandler::getSecondsSinceLastPresetLoad()
{
auto& uph = getMainController()->getUserPresetHandler();
return uph.getSecondsSinceLastPresetLoad();
}

juce::var ScriptUserPresetHandler::createObjectForAutomationValues()
{
auto& uph = getMainController()->getUserPresetHandler();
Expand Down
3 changes: 3 additions & 0 deletions hi_scripting/scripting/api/ScriptExpansion.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class ScriptUserPresetHandler : public ConstScriptingObject,
/** Updates the given automation values and optionally sends out a message. */
void updateAutomationValues(var data, bool sendMessage, bool useUndoManager);

/** Returns the amount of seconds since the last preset has been loaded. */
double getSecondsSinceLastPresetLoad();

/** Creates an object containing the values for every automation ID. */
var createObjectForAutomationValues();

Expand Down
37 changes: 37 additions & 0 deletions hi_scripting/scripting/api/ScriptingApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,8 @@ struct ScriptingApi::Engine::Wrapper
API_METHOD_WRAPPER_0(Engine, getPlayHead);
API_VOID_METHOD_WRAPPER_2(Engine, dumpAsJSON);
API_METHOD_WRAPPER_1(Engine, loadFromJSON);
API_METHOD_WRAPPER_1(Engine, compressJSON);
API_METHOD_WRAPPER_1(Engine, uncompressJSON);
API_VOID_METHOD_WRAPPER_1(Engine, setCompileProgress);
API_METHOD_WRAPPER_2(Engine, matchesRegex);
API_METHOD_WRAPPER_2(Engine, getRegexMatches);
Expand Down Expand Up @@ -1118,6 +1120,8 @@ parentMidiProcessor(dynamic_cast<ScriptBaseMidiProcessor*>(p))
ADD_API_METHOD_1(decodeBase64ValueTree);
ADD_API_METHOD_2(renderAudio);
ADD_API_METHOD_3(playBuffer);
ADD_API_METHOD_1(compressJSON);
ADD_API_METHOD_1(uncompressJSON);
}


Expand Down Expand Up @@ -3177,6 +3181,39 @@ void ScriptingApi::Engine::dumpAsJSON(var object, String fileName)

}

String ScriptingApi::Engine::compressJSON(var object)
{
auto x = JSON::toString(object, true);

zstd::ZDefaultCompressor comp;

MemoryBlock data;
comp.compress(x, data);

return data.toBase64Encoding();
}

var ScriptingApi::Engine::uncompressJSON(const String& b64)
{
MemoryBlock mb;
mb.fromBase64Encoding(b64);

String json;

zstd::ZDefaultCompressor comp;
comp.expand(mb, json);

var value;
auto r = JSON::parse(json, value);

if(!r.wasOk())
{
reportScriptError(r.getErrorMessage());
}

return value;
}

var ScriptingApi::Engine::loadFromJSON(String fileName)
{
File f;
Expand Down
6 changes: 6 additions & 0 deletions hi_scripting/scripting/api/ScriptingApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,12 @@ class ScriptingApi
/** Exports an object as JSON. */
void dumpAsJSON(var object, String fileName);

/** Compresses a JSON object as Base64 string using zstd. */
String compressJSON(var object);

/** Expands a compressed JSON object. */
var uncompressJSON(const String& b64);

/** Imports a JSON file as object. */
var loadFromJSON(String fileName);

Expand Down
Loading

0 comments on commit 57a5eba

Please sign in to comment.