2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "pbd/error.h"
22 #include "pbd/compose.h"
24 #include "ardour/lua_api.h"
25 #include "ardour/luaproc.h"
26 #include "ardour/luascripting.h"
27 #include "ardour/plugin.h"
28 #include "ardour/plugin_insert.h"
29 #include "ardour/plugin_manager.h"
31 #include "LuaBridge/LuaBridge.h"
35 using namespace ARDOUR;
40 ARDOUR::LuaAPI::datatype_ctor_null (lua_State *L)
42 DataType dt (DataType::NIL);
43 luabridge::Stack <DataType>::push (L, dt);
48 ARDOUR::LuaAPI::datatype_ctor_audio (lua_State *L)
50 DataType dt (DataType::AUDIO);
51 // NB luabridge will copy construct the object and manage lifetime.
52 luabridge::Stack <DataType>::push (L, dt);
57 ARDOUR::LuaAPI::datatype_ctor_midi (lua_State *L)
59 DataType dt (DataType::MIDI);
60 luabridge::Stack <DataType>::push (L, dt);
64 boost::shared_ptr<Processor>
65 ARDOUR::LuaAPI::nil_processor ()
67 return boost::shared_ptr<Processor> ();
70 boost::shared_ptr<Processor>
71 ARDOUR::LuaAPI::new_luaproc (Session *s, const string& name)
74 return boost::shared_ptr<Processor> ();
78 ARDOUR::LuaScriptList & _scripts (LuaScripting::instance ().scripts (LuaScriptInfo::DSP));
79 for (LuaScriptList::const_iterator i = _scripts.begin(); i != _scripts.end(); ++i) {
80 if (name == (*i)->name) {
87 warning << _("Script with given name was not found\n");
88 return boost::shared_ptr<Processor> ();
93 LuaPluginInfoPtr lpi (new LuaPluginInfo(spi));
96 warning << _("Failed to instantiate Lua Processor\n");
97 return boost::shared_ptr<Processor> ();
100 return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
104 ARDOUR::LuaAPI::new_plugin_info (const string& name, ARDOUR::PluginType type)
106 PluginManager& manager = PluginManager::instance();
107 PluginInfoList all_plugs;
108 all_plugs.insert(all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
109 all_plugs.insert(all_plugs.end(), manager.lua_plugin_info().begin(), manager.lua_plugin_info().end());
110 #ifdef WINDOWS_VST_SUPPORT
111 all_plugs.insert(all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
114 all_plugs.insert(all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
116 #ifdef AUDIOUNIT_SUPPORT
117 all_plugs.insert(all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
120 all_plugs.insert(all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
123 for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
124 if (((*i)->name == name || (*i)->unique_id == name) && (*i)->type == type) {
128 return PluginInfoPtr ();
131 boost::shared_ptr<Processor>
132 ARDOUR::LuaAPI::new_plugin (Session *s, const string& name, ARDOUR::PluginType type, const string& preset)
135 return boost::shared_ptr<Processor> ();
138 PluginInfoPtr pip = new_plugin_info (name, type);
141 return boost::shared_ptr<Processor> ();
144 PluginPtr p = pip->load (*s);
146 return boost::shared_ptr<Processor> ();
149 if (!preset.empty()) {
150 const Plugin::PresetRecord *pr = p->preset_by_label (preset);
152 p->load_preset (*pr);
156 return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
160 ARDOUR::LuaAPI::set_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, float val)
162 boost::shared_ptr<Plugin> plugin = pi->plugin();
163 if (!plugin) { return false; }
166 uint32_t controlid = plugin->nth_parameter (which, ok);
167 if (!ok) { return false; }
168 if (!plugin->parameter_is_input (controlid)) { return false; }
170 ParameterDescriptor pd;
171 if (plugin->get_parameter_descriptor (controlid, pd) != 0) { return false; }
172 if (val < pd.lower || val > pd.upper) { return false; }
174 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
175 c->set_value (val, PBD::Controllable::NoGroup);
180 ARDOUR::LuaAPI::set_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, float val)
182 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
183 if (!pi) { return false; }
184 return set_plugin_insert_param (pi, which, val);
188 ARDOUR::LuaAPI::plugin_automation (lua_State *L)
190 typedef boost::shared_ptr<Processor> T;
192 int top = lua_gettop(L);
194 return luaL_argerror (L, 1, "invalid number of arguments, :plugin_automation (plugin, parameter_number)");
196 T* const p = luabridge::Userdata::get<T>(L, 1, false);
197 uint32_t which = luabridge::Stack<uint32_t>::get (L, 2);
199 return luaL_error (L, "Invalid pointer to Ardour:Processor");
201 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (*p);
203 return luaL_error (L, "Given Processor is not a Plugin Insert");
205 boost::shared_ptr<Plugin> plugin = pi->plugin();
207 return luaL_error (L, "Given Processor is not a Plugin");
211 uint32_t controlid = plugin->nth_parameter (which, ok);
213 return luaL_error (L, "Invalid Parameter");
215 if (!plugin->parameter_is_input (controlid)) {
216 return luaL_error (L, "Given Parameter is not an input");
219 ParameterDescriptor pd;
220 if (plugin->get_parameter_descriptor (controlid, pd) != 0) {
221 return luaL_error (L, "Cannot describe parameter");
224 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
226 luabridge::Stack<boost::shared_ptr<AutomationList> >::push (L, c->alist());
227 luabridge::Stack<boost::shared_ptr<Evoral::ControlList> >::push (L, c->list());
228 luabridge::Stack<ParameterDescriptor>::push (L, pd);
233 ARDOUR::LuaOSC::Address::send (lua_State *L)
235 Address * const luaosc = luabridge::Userdata::get <Address> (L, 1, false);
237 return luaL_error (L, "Invalid pointer to OSC.Address");
239 if (!luaosc->_addr) {
240 return luaL_error (L, "Invalid Destination Address");
243 int top = lua_gettop(L);
245 return luaL_argerror (L, 1, "invalid number of arguments, :send (path, type, ...)");
248 const char* path = luaL_checkstring (L, 2);
249 const char* type = luaL_checkstring (L, 3);
250 assert (path && type);
252 if ((int) strlen(type) != top - 3) {
253 return luaL_argerror (L, 3, "type description does not match arguments");
256 lo_message msg = lo_message_new ();
258 for (int i = 4; i <= top; ++i) {
259 char t = type[i - 4];
260 int lt = lua_type(L, i);
264 if (t == LO_STRING) {
265 ok = lo_message_add_string (msg, luaL_checkstring(L, i));
266 } else if (t == LO_CHAR) {
267 char c = luaL_checkstring (L, i) [0];
268 ok = lo_message_add_char (msg, c);
272 if (t == LO_TRUE || t == LO_FALSE) {
273 if (lua_toboolean (L, i)) {
274 ok = lo_message_add_true (msg);
276 ok = lo_message_add_false (msg);
282 ok = lo_message_add_int32 (msg, (int32_t) luaL_checkinteger(L, i));
284 else if (t == LO_FLOAT) {
285 ok = lo_message_add_float (msg, (float) luaL_checknumber(L, i));
287 else if (t == LO_DOUBLE) {
288 ok = lo_message_add_double (msg, (double) luaL_checknumber(L, i));
290 else if (t == LO_INT64) {
291 ok = lo_message_add_double (msg, (int64_t) luaL_checknumber(L, i));
298 return luaL_argerror (L, i, "type description does not match parameter");
302 int rv = lo_send_message (luaosc->_addr, path, msg);
303 lo_message_free (msg);
304 luabridge::Stack<bool>::push (L, (rv == 0));
308 static double hue2rgb (const double p, const double q, double t) {
309 if (t < 0.0) t += 1.0;
310 if (t > 1.0) t -= 1.0;
311 if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t;
312 if (t < 1.0 / 2.0) return q;
313 if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
318 ARDOUR::LuaAPI::hsla_to_rgba (lua_State *L)
320 int top = lua_gettop(L);
322 return luaL_argerror (L, 1, "invalid number of arguments, :hsla_to_rgba (h, s, l [,a])");
324 double h = luabridge::Stack<double>::get (L, 1);
325 double s = luabridge::Stack<double>::get (L, 2);
326 double l = luabridge::Stack<double>::get (L, 3);
329 a = luabridge::Stack<double>::get (L, 4);
332 // we can't use ArdourCanvas::hsva_to_color here
333 // besides we want HSL not HSV and without intermediate
334 // color_to_rgba (rgba_to_color ())
336 const double cq = l < 0.5 ? l * (1 + s) : l + s - l * s;
337 const double cp = 2.f * l - cq;
338 r = hue2rgb (cp, cq, h + 1.0 / 3.0);
339 g = hue2rgb (cp, cq, h);
340 b = hue2rgb (cp, cq, h - 1.0 / 3.0);
342 luabridge::Stack<double>::push (L, r);
343 luabridge::Stack<double>::push (L, g);
344 luabridge::Stack<double>::push (L, b);
345 luabridge::Stack<double>::push (L, a);
350 ARDOUR::LuaAPI::build_filename (lua_State *L)
352 std::vector<std::string> elem;
353 int top = lua_gettop(L);
355 return luaL_argerror (L, 1, "invalid number of arguments, build_filename (path, ...)");
357 for (int i = 1; i <= top; ++i) {
358 int lt = lua_type (L, i);
359 if (lt != LUA_TSTRING) {
360 return luaL_argerror (L, i, "invalid argument type, expected string");
362 elem.push_back (luaL_checkstring(L, i));
365 luabridge::Stack<std::string>::push (L, Glib::build_filename (elem));
369 luabridge::LuaRef::Proxy&
370 luabridge::LuaRef::Proxy::clone_instance (const void* classkey, void* p) {
371 lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_tableRef);
372 lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_keyRef);
374 luabridge::UserdataPtr::push_raw (m_L, p, classkey);
376 lua_rawset (m_L, -3);
381 LuaTableRef::LuaTableRef () {}
382 LuaTableRef::~LuaTableRef () {}
385 LuaTableRef::get (lua_State* L)
387 luabridge::LuaRef rv (luabridge::newTable (L));
388 for (std::vector<LuaTableEntry>::const_iterator i = _data.begin (); i != _data.end (); ++i) {
389 switch ((*i).keytype) {
391 assign(&rv, i->k_s, *i);
394 assign(&rv, i->k_n, *i);
398 luabridge::push (L, rv);
403 LuaTableRef::set (lua_State* L)
405 if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); }
408 lua_pushvalue (L, -1);
410 while (lua_next (L, -2)) {
411 lua_pushvalue (L, -2);
413 LuaTableEntry s (lua_type(L, -1), lua_type(L, -2));
414 switch (lua_type(L, -1)) {
416 s.k_s = luabridge::Stack<std::string>::get (L, -1);
420 s.k_n = luabridge::Stack<unsigned int>::get (L, -1);
428 switch(lua_type(L, -2)) {
430 s.s = luabridge::Stack<std::string>::get (L, -2);
433 s.b = lua_toboolean (L, -2);
436 s.n = lua_tonumber (L, -2);
441 lua_getmetatable (L, -2);
442 lua_rawgetp (L, -1, luabridge::getIdentityKey ());
443 if (lua_isboolean (L, -1)) {
445 const void* key = lua_topointer (L, -1);
447 void const* classkey = findclasskey (L, key);
452 s.p = luabridge::Userdata::get_ptr (L, -2);
461 // invalid userdata -- fall through
464 case LUA_TFUNCTION: // no support -- we could... string.format("%q", string.dump(value, true))
465 case LUA_TTABLE: // no nested tables, sorry.
466 case LUA_TNIL: // fallthrough
480 LuaTableRef::findclasskey (lua_State *L, const void* key)
482 lua_pushvalue(L, LUA_REGISTRYINDEX);
484 while (lua_next (L, -2)) {
485 lua_pushvalue (L, -2);
486 if (lua_topointer(L, -2) == key) {
487 void* rv = lua_touserdata (L, -1);
498 void LuaTableRef::assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s)
500 switch (s.valuetype) {
511 (*rv)[key].clone_instance (s.c, s.p);