From 6d49e245035fe3b078b5e58825d81f20bce9511d Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Fri, 29 Apr 2016 18:34:27 +0200 Subject: implement lua plugin presets --- libs/ardour/ardour/luaproc.h | 14 ++-- libs/ardour/luaproc.cc | 164 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 171 insertions(+), 7 deletions(-) (limited to 'libs/ardour') diff --git a/libs/ardour/ardour/luaproc.h b/libs/ardour/ardour/luaproc.h index d7c0aea5c5..67c4432087 100644 --- a/libs/ardour/ardour/luaproc.h +++ b/libs/ardour/ardour/luaproc.h @@ -92,7 +92,10 @@ public: int set_state (const XMLNode&, int version); int set_script_from_state (const XMLNode&); - bool load_preset (PresetRecord) { return false; } + bool load_preset (PresetRecord); + std::string do_save_preset (std::string); + void do_remove_preset (std::string); + bool has_editor() const { return false; } bool can_support_io_configuration (const ChanCount& in, ChanCount& out, ChanCount* imprecise); @@ -101,14 +104,11 @@ public: ChanCount output_streams() const { return _configured_out; } ChanCount input_streams() const { return _configured_in; } - std::string do_save_preset (std::string) { return ""; } - void do_remove_preset (std::string) { } - bool has_inline_display () { return _lua_has_inline_display; } void setup_lua_inline_gui (LuaState *lua_gui); private: - void find_presets () { } + void find_presets (); /* END Plugin interface */ protected: @@ -131,6 +131,10 @@ private: bool load_script (); void lua_print (std::string s); + std::string preset_name_to_uri (const std::string&) const; + std::string presets_file () const; + XMLTree* presets_tree () const; + boost::shared_ptr parse_scale_points (luabridge::LuaRef*); std::vector > _ctrl_params; diff --git a/libs/ardour/luaproc.cc b/libs/ardour/luaproc.cc index 6b6f383301..af9e8503da 100644 --- a/libs/ardour/luaproc.cc +++ b/libs/ardour/luaproc.cc @@ -18,12 +18,16 @@ */ #include +#include +#include + #include "pbd/gstdio_compat.h" #include "pbd/pthread_utils.h" #include "ardour/audio_buffer.h" #include "ardour/buffer_set.h" +#include "ardour/filesystem_paths.h" #include "ardour/luabindings.h" #include "ardour/luaproc.h" #include "ardour/luascripting.h" @@ -1002,10 +1006,152 @@ LuaProc::setup_lua_inline_gui (LuaState *lua_gui) luabridge::push (LG, _shadow_data); lua_setglobal (LG, "CtrlPorts"); } +//////////////////////////////////////////////////////////////////////////////// + +#include "ardour/search_paths.h" +#include "sha1.c" + +std::string +LuaProc::preset_name_to_uri (const std::string& name) const +{ + std::string uri ("urn:lua:"); + char hash[41]; + Sha1Digest s; + sha1_init (&s); + sha1_write (&s, (const uint8_t *) name.c_str(), name.size ()); + sha1_write (&s, (const uint8_t *) _script.c_str(), _script.size ()); + sha1_result_hash (&s, hash); + return uri + hash; +} + +std::string +LuaProc::presets_file () const +{ + return string_compose ("lua-%1", _info->unique_id); +} + +XMLTree* +LuaProc::presets_tree () const +{ + XMLTree* t = new XMLTree; + std::string p = Glib::build_filename (ARDOUR::user_config_directory (), "presets"); + + if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) { + if (g_mkdir_with_parents (p.c_str(), 0755) != 0) { + error << _("Unable to create LuaProc presets directory") << endmsg; + }; + } + + p = Glib::build_filename (p, presets_file ()); + + if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) { + t->set_root (new XMLNode (X_("LuaPresets"))); + return t; + } + + t->set_filename (p); + if (!t->read ()) { + delete t; + return 0; + } + return t; +} + +bool +LuaProc::load_preset (PresetRecord r) +{ + boost::shared_ptr t (presets_tree ()); + if (t == 0) { + return false; + } + + XMLNode* root = t->root (); + for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) { + XMLProperty* label = (*i)->property (X_("label")); + assert (label); + if (label->value() != r.label) { + continue; + } + + for (XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) { + if ((*j)->name() == X_("Parameter")) { + XMLProperty* index = (*j)->property (X_("index")); + XMLProperty* value = (*j)->property (X_("value")); + assert (index); + assert (value); + set_parameter (atoi (index->value().c_str()), atof (value->value().c_str ())); + } + } + return true; + } + return false; +} + +std::string +LuaProc::do_save_preset (std::string name) { + + boost::shared_ptr t (presets_tree ()); + if (t == 0) { + return ""; + } + + std::string uri (preset_name_to_uri (name)); + + XMLNode* p = new XMLNode (X_("Preset")); + p->add_property (X_("uri"), uri); + p->add_property (X_("label"), name); + + for (uint32_t i = 0; i < parameter_count(); ++i) { + if (parameter_is_input (i)) { + XMLNode* c = new XMLNode (X_("Parameter")); + c->add_property (X_("index"), string_compose ("%1", i)); + c->add_property (X_("value"), string_compose ("%1", get_parameter (i))); + p->add_child_nocopy (*c); + } + } + t->root()->add_child_nocopy (*p); + + std::string f = Glib::build_filename (ARDOUR::user_config_directory (), "presets"); + f = Glib::build_filename (f, presets_file ()); + + t->write (f); + return uri; +} + +void +LuaProc::do_remove_preset (std::string name) +{ + boost::shared_ptr t (presets_tree ()); + if (t == 0) { + return; + } + t->root()->remove_nodes_and_delete (X_("label"), name); + std::string f = Glib::build_filename (ARDOUR::user_config_directory (), "presets"); + f = Glib::build_filename (f, presets_file ()); + t->write (f); +} + +void +LuaProc::find_presets () +{ + boost::shared_ptr t (presets_tree ()); + if (t) { + XMLNode* root = t->root (); + for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) { + + XMLProperty* uri = (*i)->property (X_("uri")); + XMLProperty* label = (*i)->property (X_("label")); + + assert (uri); + assert (label); + + PresetRecord r (uri->value(), label->value(), true); + _presets.insert (make_pair (r.uri, r)); + } + } +} //////////////////////////////////////////////////////////////////////////////// -#include -#include LuaPluginInfo::LuaPluginInfo (LuaScriptInfoPtr lsi) { if (lsi->type != LuaScriptInfo::DSP) { @@ -1054,5 +1200,19 @@ std::vector LuaPluginInfo::get_presets (bool /*user_only*/) const { std::vector p; + XMLTree* t = new XMLTree; + std::string pf = Glib::build_filename (ARDOUR::user_config_directory (), "presets", string_compose ("lua-%1", unique_id)); + if (Glib::file_test (pf, Glib::FILE_TEST_EXISTS)) { + t->set_filename (pf); + if (t->read ()) { + XMLNode* root = t->root (); + for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) { + XMLProperty* uri = (*i)->property (X_("uri")); + XMLProperty* label = (*i)->property (X_("label")); + p.push_back (Plugin::PresetRecord (uri->value(), label->value(), true)); + } + } + } + delete t; return p; } -- cgit v1.2.3