Add convenience Lua bindings to access plugin controls
[ardour.git] / libs / ardour / lua_api.cc
index 176a62005245e86f4360a2eb2f2e4402c332dade..0ca51ad2ad0198eef1c7c825c9a92b6932f15b1f 100644 (file)
 #include "ardour/plugin_insert.h"
 #include "ardour/plugin_manager.h"
 
-#include "i18n.h"
+#include "LuaBridge/LuaBridge.h"
+
+#include "pbd/i18n.h"
 
 using namespace ARDOUR;
 using namespace PBD;
 using namespace std;
 
+int
+ARDOUR::LuaAPI::datatype_ctor_null (lua_State *L)
+{
+       DataType dt (DataType::NIL);
+       luabridge::Stack <DataType>::push (L, dt);
+       return 1;
+}
+
+int
+ARDOUR::LuaAPI::datatype_ctor_audio (lua_State *L)
+{
+       DataType dt (DataType::AUDIO);
+       // NB luabridge will copy construct the object and manage lifetime.
+       luabridge::Stack <DataType>::push (L, dt);
+       return 1;
+}
+
+int
+ARDOUR::LuaAPI::datatype_ctor_midi (lua_State *L)
+{
+       DataType dt (DataType::MIDI);
+       luabridge::Stack <DataType>::push (L, dt);
+       return 1;
+}
+
+boost::shared_ptr<Processor>
+ARDOUR::LuaAPI::nil_processor ()
+{
+       return boost::shared_ptr<Processor> ();
+}
+
 boost::shared_ptr<Processor>
 ARDOUR::LuaAPI::new_luaproc (Session *s, const string& name)
 {
@@ -43,9 +76,9 @@ ARDOUR::LuaAPI::new_luaproc (Session *s, const string& name)
 
        LuaScriptInfoPtr spi;
        ARDOUR::LuaScriptList & _scripts (LuaScripting::instance ().scripts (LuaScriptInfo::DSP));
-       for (LuaScriptList::const_iterator s = _scripts.begin(); s != _scripts.end(); ++s) {
-               if (name == (*s)->name) {
-                       spi = *s;
+       for (LuaScriptList::const_iterator i = _scripts.begin(); i != _scripts.end(); ++i) {
+               if (name == (*i)->name) {
+                       spi = *i;
                        break;
                }
        }
@@ -67,17 +100,13 @@ ARDOUR::LuaAPI::new_luaproc (Session *s, const string& name)
        return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
 }
 
-
-boost::shared_ptr<Processor>
-ARDOUR::LuaAPI::new_plugin (Session *s, const string& name, ARDOUR::PluginType type, const string& preset)
+PluginInfoPtr
+ARDOUR::LuaAPI::new_plugin_info (const string& name, ARDOUR::PluginType type)
 {
-       if (!s) {
-               return boost::shared_ptr<Processor> ();
-       }
-
        PluginManager& manager = PluginManager::instance();
        PluginInfoList all_plugs;
        all_plugs.insert(all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
+       all_plugs.insert(all_plugs.end(), manager.lua_plugin_info().begin(), manager.lua_plugin_info().end());
 #ifdef WINDOWS_VST_SUPPORT
        all_plugs.insert(all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
 #endif
@@ -91,13 +120,23 @@ ARDOUR::LuaAPI::new_plugin (Session *s, const string& name, ARDOUR::PluginType t
        all_plugs.insert(all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
 #endif
 
-       PluginInfoPtr pip;
        for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
                if (((*i)->name == name || (*i)->unique_id == name) && (*i)->type == type) {
-                       pip = *i;
-                       break;
+                       return *i;
                }
        }
+       return PluginInfoPtr ();
+}
+
+boost::shared_ptr<Processor>
+ARDOUR::LuaAPI::new_plugin (Session *s, const string& name, ARDOUR::PluginType type, const string& preset)
+{
+       if (!s) {
+               return boost::shared_ptr<Processor> ();
+       }
+
+       PluginInfoPtr pip = new_plugin_info (name, type);
+
        if (!pip) {
                return boost::shared_ptr<Processor> ();
        }
@@ -117,6 +156,98 @@ ARDOUR::LuaAPI::new_plugin (Session *s, const string& name, ARDOUR::PluginType t
        return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
 }
 
+bool
+ARDOUR::LuaAPI::set_plugin_input_parameter_value_named(boost::shared_ptr<Processor> proc, const string& name, float val)
+{
+       boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
+       if (!pi) { return false; }
+       boost::shared_ptr<Plugin> plugin = pi->plugin();
+       if (!plugin) { return false; }
+
+       bool test=false;
+       uint32_t controlid=0;
+       ParameterDescriptor pd;
+
+       for (uint32_t param_index = 0; param_index < plugin->parameter_count(); ++param_index)
+       {
+               controlid = plugin->nth_parameter (param_index, test);
+               if (!test)
+               {
+                       break;
+               }
+               else
+               {
+                       //skip output ports
+                       if (!plugin->parameter_is_input (controlid)) { continue; }
+                       string param_name=pi->describe_parameter(Evoral::Parameter(PluginAutomation, 0, controlid));
+                       if (param_name.compare(name) != 0) { continue; }
+                       if (plugin->get_parameter_descriptor (controlid, pd) != 0) { continue; }
+                       if (val < pd.lower || val > pd.upper) { break; }
+                       boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
+                       c->set_value (val, PBD::Controllable::NoGroup);
+                       return true;
+               }
+       }
+       return false;
+}
+
+float
+ARDOUR::LuaAPI::get_plugin_parameter_value_named(boost::shared_ptr<Processor> proc, int type, const string& name, bool &ok)
+{
+       ok=false;
+       boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
+       if (!pi) { return 0; }
+       boost::shared_ptr<Plugin> plugin = pi->plugin();
+       if (!plugin) { return 0; }
+
+       bool test=false;
+       uint32_t controlid=0;
+       ParameterDescriptor pd;
+
+       for (uint32_t param_index = 0; param_index < plugin->parameter_count(); ++param_index)
+       {
+               controlid = plugin->nth_parameter (param_index, test);
+               if (!test)
+               {
+                       return 0;
+               }
+               else
+               {
+                       if(type==0) //input
+                       {
+                               //skip output ports
+                               if (!plugin->parameter_is_input (controlid)) { continue; }
+                       }
+                       else if(type==1) //output
+                       {
+                               //skip input ports
+                               if (plugin->parameter_is_input (controlid)) { continue; }
+                       }
+                       else
+                       {
+                               return 0;
+                       }
+                       string param_name=pi->describe_parameter(Evoral::Parameter(PluginAutomation, 0, controlid));
+                       if (param_name.compare(name) != 0) { continue; }
+                       ok=true;
+                       return plugin->get_parameter ( controlid );
+               }
+       }
+       return 0;
+}
+
+float
+ARDOUR::LuaAPI::get_plugin_input_parameter_value_named(boost::shared_ptr<Processor> proc, const string& name, bool &ok)
+{
+       return get_plugin_parameter_value_named(proc,0,name,ok);
+}
+
+float
+ARDOUR::LuaAPI::get_plugin_output_parameter_value_named(boost::shared_ptr<Processor> proc, const string& name, bool &ok)
+{
+       return get_plugin_parameter_value_named(proc,1,name,ok);
+}
+
 bool
 ARDOUR::LuaAPI::set_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, float val)
 {
@@ -137,6 +268,17 @@ ARDOUR::LuaAPI::set_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uin
        return true;
 }
 
+float
+ARDOUR::LuaAPI::get_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, bool &ok)
+{
+       ok=false;
+       boost::shared_ptr<Plugin> plugin = pi->plugin();
+       if (!plugin) { return 0; }
+       uint32_t controlid = plugin->nth_parameter (which, ok);
+       if (!ok) { return 0; }
+       return plugin->get_parameter ( controlid );
+}
+
 bool
 ARDOUR::LuaAPI::set_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, float val)
 {
@@ -145,10 +287,64 @@ ARDOUR::LuaAPI::set_processor_param (boost::shared_ptr<Processor> proc, uint32_t
        return set_plugin_insert_param (pi, which, val);
 }
 
+float
+ARDOUR::LuaAPI::get_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, bool &ok)
+{
+       ok=false;
+       boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
+       if (!pi) { return false; }
+       return get_plugin_insert_param (pi, which, ok);
+}
+
+int
+ARDOUR::LuaAPI::plugin_automation (lua_State *L)
+{
+       typedef boost::shared_ptr<Processor> T;
+
+       int top = lua_gettop(L);
+       if (top < 2) {
+               return luaL_argerror (L, 1, "invalid number of arguments, :plugin_automation (plugin, parameter_number)");
+       }
+       T* const p = luabridge::Userdata::get<T>(L, 1, false);
+       uint32_t which = luabridge::Stack<uint32_t>::get (L, 2);
+       if (!p) {
+               return luaL_error (L, "Invalid pointer to Ardour:Processor");
+       }
+       boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (*p);
+       if (!pi) {
+               return luaL_error (L, "Given Processor is not a Plugin Insert");
+       }
+       boost::shared_ptr<Plugin> plugin = pi->plugin();
+       if (!plugin) {
+               return luaL_error (L, "Given Processor is not a Plugin");
+       }
+
+       bool ok=false;
+       uint32_t controlid = plugin->nth_parameter (which, ok);
+       if (!ok) {
+               return luaL_error (L, "Invalid Parameter");
+       }
+       if (!plugin->parameter_is_input (controlid)) {
+               return luaL_error (L, "Given Parameter is not an input");
+       }
+
+       ParameterDescriptor pd;
+       if (plugin->get_parameter_descriptor (controlid, pd) != 0) {
+               return luaL_error (L, "Cannot describe parameter");
+       }
+
+       boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
+
+       luabridge::Stack<boost::shared_ptr<AutomationList> >::push (L, c->alist());
+       luabridge::Stack<boost::shared_ptr<Evoral::ControlList> >::push (L, c->list());
+       luabridge::Stack<ParameterDescriptor>::push (L, pd);
+       return 3;
+}
+
 int
-ARDOUR::LuaAPI::LuaOSCAddress::send (lua_State *L)
+ARDOUR::LuaOSC::Address::send (lua_State *L)
 {
-       LuaOSCAddress * const luaosc = luabridge::Userdata::get <LuaOSCAddress> (L, 1, false);
+       Address * const luaosc = luabridge::Userdata::get <Address> (L, 1, false);
        if (!luaosc) {
                return luaL_error (L, "Invalid pointer to OSC.Address");
        }
@@ -158,7 +354,7 @@ ARDOUR::LuaAPI::LuaOSCAddress::send (lua_State *L)
 
        int top = lua_gettop(L);
        if (top < 3) {
-    return luaL_argerror (L, 1, "invalid number of arguments, :send (path, type, ...)");
+               return luaL_argerror (L, 1, "invalid number of arguments, :send (path, type, ...)");
        }
 
        const char* path = luaL_checkstring (L, 2);
@@ -166,7 +362,7 @@ ARDOUR::LuaAPI::LuaOSCAddress::send (lua_State *L)
        assert (path && type);
 
        if ((int) strlen(type) != top - 3) {
-    return luaL_argerror (L, 3, "type description does not match arguments");
+               return luaL_argerror (L, 3, "type description does not match arguments");
        }
 
        lo_message msg = lo_message_new ();
@@ -179,7 +375,7 @@ ARDOUR::LuaAPI::LuaOSCAddress::send (lua_State *L)
                        case LUA_TSTRING:
                                if (t == LO_STRING) {
                                        ok = lo_message_add_string (msg, luaL_checkstring(L, i));
-                               } else if (t ==  LO_CHAR) {
+                               } else if (t == LO_CHAR) {
                                        char c = luaL_checkstring (L, i) [0];
                                        ok = lo_message_add_char (msg, c);
                                }
@@ -217,6 +413,217 @@ ARDOUR::LuaAPI::LuaOSCAddress::send (lua_State *L)
 
        int rv = lo_send_message (luaosc->_addr, path, msg);
        lo_message_free (msg);
-       luabridge::Stack<int>::push (L, rv);
+       luabridge::Stack<bool>::push (L, (rv == 0));
+       return 1;
+}
+
+static double hue2rgb (const double p, const double q, double t) {
+       if (t < 0.0) t += 1.0;
+       if (t > 1.0) t -= 1.0;
+       if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t;
+       if (t < 1.0 / 2.0) return q;
+       if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
+       return p;
+}
+
+int
+ARDOUR::LuaAPI::hsla_to_rgba (lua_State *L)
+{
+       int top = lua_gettop(L);
+       if (top < 3) {
+               return luaL_argerror (L, 1, "invalid number of arguments, :hsla_to_rgba (h, s, l [,a])");
+       }
+       double h = luabridge::Stack<double>::get (L, 1);
+       double s = luabridge::Stack<double>::get (L, 2);
+       double l = luabridge::Stack<double>::get (L, 3);
+       double a = 1.0;
+       if (top > 3) {
+               a = luabridge::Stack<double>::get (L, 4);
+       }
+
+       // we can't use ArdourCanvas::hsva_to_color here
+       // besides we want HSL not HSV and without intermediate
+       // color_to_rgba (rgba_to_color ())
+       double r,g,b;
+       const double cq = l < 0.5 ? l * (1 + s) : l + s - l * s;
+       const double cp = 2.f * l - cq;
+       r = hue2rgb (cp, cq, h + 1.0 / 3.0);
+       g = hue2rgb (cp, cq, h);
+       b = hue2rgb (cp, cq, h - 1.0 / 3.0);
+
+       luabridge::Stack<double>::push (L, r);
+       luabridge::Stack<double>::push (L, g);
+       luabridge::Stack<double>::push (L, b);
+       luabridge::Stack<double>::push (L, a);
+       return 4;
+}
+
+int
+ARDOUR::LuaAPI::build_filename (lua_State *L)
+{
+       std::vector<std::string> elem;
+       int top = lua_gettop(L);
+       if (top < 1) {
+               return luaL_argerror (L, 1, "invalid number of arguments, build_filename (path, ...)");
+       }
+       for (int i = 1; i <= top; ++i) {
+               int lt = lua_type (L, i);
+               if (lt != LUA_TSTRING) {
+                       return luaL_argerror (L, i, "invalid argument type, expected string");
+               }
+               elem.push_back (luaL_checkstring(L, i));
+       }
+
+       luabridge::Stack<std::string>::push (L, Glib::build_filename (elem));
+       return 1;
+}
+
+luabridge::LuaRef::Proxy&
+luabridge::LuaRef::Proxy::clone_instance (const void* classkey, void* p) {
+       lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_tableRef);
+       lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_keyRef);
+
+       luabridge::UserdataPtr::push_raw (m_L, p, classkey);
+
+       lua_rawset (m_L, -3);
+       lua_pop (m_L, 1);
+       return *this;
+}
+
+LuaTableRef::LuaTableRef () {}
+LuaTableRef::~LuaTableRef () {}
+
+int
+LuaTableRef::get (lua_State* L)
+{
+       luabridge::LuaRef rv (luabridge::newTable (L));
+       for (std::vector<LuaTableEntry>::const_iterator i = _data.begin (); i != _data.end (); ++i) {
+               switch ((*i).keytype) {
+                       case LUA_TSTRING:
+                               assign(&rv, i->k_s, *i);
+                               break;
+                       case LUA_TNUMBER:
+                               assign(&rv, i->k_n, *i);
+                               break;
+               }
+       }
+       luabridge::push (L, rv);
        return 1;
 }
+
+int
+LuaTableRef::set (lua_State* L)
+{
+       if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); }
+       _data.clear ();
+
+       lua_pushvalue (L, -1);
+       lua_pushnil (L);
+       while (lua_next (L, -2)) {
+               lua_pushvalue (L, -2);
+
+               LuaTableEntry s (lua_type(L, -1), lua_type(L, -2));
+               switch (lua_type(L, -1)) {
+                       case LUA_TSTRING:
+                               s.k_s = luabridge::Stack<std::string>::get (L, -1);
+                               break;
+                               ;
+                       case LUA_TNUMBER:
+                               s.k_n = luabridge::Stack<unsigned int>::get (L, -1);
+                               break;
+                       default:
+                               // invalid key
+                               lua_pop (L, 2);
+                               continue;
+               }
+
+               switch(lua_type(L, -2)) {
+                       case LUA_TSTRING:
+                               s.s = luabridge::Stack<std::string>::get (L, -2);
+                               break;
+                       case LUA_TBOOLEAN:
+                               s.b = lua_toboolean (L, -2);
+                               break;
+                       case LUA_TNUMBER:
+                               s.n = lua_tonumber (L, -2);
+                               break;
+                       case LUA_TUSERDATA:
+                               {
+                                       bool ok = false;
+                                       lua_getmetatable (L, -2);
+                                       lua_rawgetp (L, -1, luabridge::getIdentityKey ());
+                                       if (lua_isboolean (L, -1)) {
+                                               lua_pop (L, 1);
+                                               const void* key = lua_topointer (L, -1);
+                                               lua_pop (L, 1);
+                                               void const* classkey = findclasskey (L, key);
+
+                                               if (classkey) {
+                                                       ok = true;
+                                                       s.c = classkey;
+                                                       s.p = luabridge::Userdata::get_ptr (L, -2);
+                                               }
+                                       } else {
+                                               lua_pop (L, 2);
+                                       }
+
+                                       if (ok) {
+                                               break;
+                                       }
+                                       // invalid userdata -- fall through
+                               }
+                               // no break
+                       case LUA_TFUNCTION: // no support -- we could... string.format("%q", string.dump(value, true))
+                       case LUA_TTABLE: // no nested tables, sorry.
+                       case LUA_TNIL: // fallthrough
+                       default:
+                               // invalid value
+                               lua_pop (L, 2);
+                               continue;
+               }
+
+               _data.push_back(s);
+               lua_pop (L, 2);
+       }
+       return 0;
+}
+
+void*
+LuaTableRef::findclasskey (lua_State *L, const void* key)
+{
+       lua_pushvalue(L, LUA_REGISTRYINDEX);
+       lua_pushnil (L);
+       while (lua_next (L, -2)) {
+               lua_pushvalue (L, -2);
+               if (lua_topointer(L, -2) == key) {
+                       void* rv = lua_touserdata (L, -1);
+                       lua_pop (L, 4);
+                       return rv;
+               }
+               lua_pop (L, 2);
+       }
+       lua_pop (L, 1);
+       return NULL;
+}
+
+template<typename T>
+void LuaTableRef::assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s)
+{
+       switch (s.valuetype) {
+               case LUA_TSTRING:
+                       (*rv)[key] = s.s;
+                       break;
+               case LUA_TBOOLEAN:
+                       (*rv)[key] = s.b;
+                       break;
+               case LUA_TNUMBER:
+                       (*rv)[key] = s.n;
+                       break;
+               case LUA_TUSERDATA:
+                       (*rv)[key].clone_instance (s.c, s.p);
+                       break;
+               default:
+                       assert (0);
+                       break;
+       }
+}