allow to get custom/product/version independent cach dir
[ardour.git] / libs / ardour / lua_api.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
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.
8  *
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.
13  *
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.
17  *
18  */
19 #include <cstring>
20
21 #include "pbd/error.h"
22 #include "pbd/compose.h"
23
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"
30
31 #include "LuaBridge/LuaBridge.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace ARDOUR;
36 using namespace PBD;
37 using namespace std;
38
39 int
40 ARDOUR::LuaAPI::datatype_ctor_null (lua_State *L)
41 {
42         DataType dt (DataType::NIL);
43         luabridge::Stack <DataType>::push (L, dt);
44         return 1;
45 }
46
47 int
48 ARDOUR::LuaAPI::datatype_ctor_audio (lua_State *L)
49 {
50         DataType dt (DataType::AUDIO);
51         // NB luabridge will copy construct the object and manage lifetime.
52         luabridge::Stack <DataType>::push (L, dt);
53         return 1;
54 }
55
56 int
57 ARDOUR::LuaAPI::datatype_ctor_midi (lua_State *L)
58 {
59         DataType dt (DataType::MIDI);
60         luabridge::Stack <DataType>::push (L, dt);
61         return 1;
62 }
63
64 boost::shared_ptr<Processor>
65 ARDOUR::LuaAPI::nil_processor ()
66 {
67         return boost::shared_ptr<Processor> ();
68 }
69
70 boost::shared_ptr<Processor>
71 ARDOUR::LuaAPI::new_luaproc (Session *s, const string& name)
72 {
73         if (!s) {
74                 return boost::shared_ptr<Processor> ();
75         }
76
77         LuaScriptInfoPtr spi;
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) {
81                         spi = *i;
82                         break;
83                 }
84         }
85
86         if (!spi) {
87                 warning << _("Script with given name was not found\n");
88                 return boost::shared_ptr<Processor> ();
89         }
90
91         PluginPtr p;
92         try {
93                 LuaPluginInfoPtr lpi (new LuaPluginInfo (spi));
94                 p = (lpi->load (*s));
95         } catch (...) {
96                 warning << _("Failed to instantiate Lua Processor\n");
97                 return boost::shared_ptr<Processor> ();
98         }
99
100         return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
101 }
102
103 PluginInfoPtr
104 ARDOUR::LuaAPI::new_plugin_info (const string& name, ARDOUR::PluginType type)
105 {
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 ());
112 #endif
113 #ifdef LXVST_SUPPORT
114         all_plugs.insert (all_plugs.end (), manager.lxvst_plugin_info ().begin (), manager.lxvst_plugin_info ().end ());
115 #endif
116 #ifdef AUDIOUNIT_SUPPORT
117         all_plugs.insert (all_plugs.end (), manager.au_plugin_info ().begin (), manager.au_plugin_info ().end ());
118 #endif
119 #ifdef LV2_SUPPORT
120         all_plugs.insert (all_plugs.end (), manager.lv2_plugin_info ().begin (), manager.lv2_plugin_info ().end ());
121 #endif
122
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) {
125                         return *i;
126                 }
127         }
128         return PluginInfoPtr ();
129 }
130
131 boost::shared_ptr<Processor>
132 ARDOUR::LuaAPI::new_plugin (Session *s, const string& name, ARDOUR::PluginType type, const string& preset)
133 {
134         if (!s) {
135                 return boost::shared_ptr<Processor> ();
136         }
137
138         PluginInfoPtr pip = new_plugin_info (name, type);
139
140         if (!pip) {
141                 return boost::shared_ptr<Processor> ();
142         }
143
144         PluginPtr p = pip->load (*s);
145         if (!p) {
146                 return boost::shared_ptr<Processor> ();
147         }
148
149         if (!preset.empty ()) {
150                 const Plugin::PresetRecord *pr = p->preset_by_label (preset);
151                 if (pr) {
152                         p->load_preset (*pr);
153                 }
154         }
155
156         return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
157 }
158
159 bool
160 ARDOUR::LuaAPI::set_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, float val)
161 {
162         boost::shared_ptr<Plugin> plugin = pi->plugin ();
163         if (!plugin) { return false; }
164
165         bool ok=false;
166         uint32_t controlid = plugin->nth_parameter (which, ok);
167         if (!ok) { return false; }
168         if (!plugin->parameter_is_input (controlid)) { return false; }
169
170         ParameterDescriptor pd;
171         if (plugin->get_parameter_descriptor (controlid, pd) != 0) { return false; }
172         if (val < pd.lower || val > pd.upper) { return false; }
173
174         boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter (PluginAutomation, 0, controlid));
175         c->set_value (val, PBD::Controllable::NoGroup);
176         return true;
177 }
178
179 float
180 ARDOUR::LuaAPI::get_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, bool &ok)
181 {
182         ok=false;
183         boost::shared_ptr<Plugin> plugin = pi->plugin ();
184         if (!plugin) { return 0; }
185         uint32_t controlid = plugin->nth_parameter (which, ok);
186         if (!ok) { return 0; }
187         return plugin->get_parameter ( controlid );
188 }
189
190 bool
191 ARDOUR::LuaAPI::set_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, float val)
192 {
193         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
194         if (!pi) { return false; }
195         return set_plugin_insert_param (pi, which, val);
196 }
197
198 float
199 ARDOUR::LuaAPI::get_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, bool &ok)
200 {
201         ok=false;
202         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
203         if (!pi) { return false; }
204         return get_plugin_insert_param (pi, which, ok);
205 }
206
207 int
208 ARDOUR::LuaAPI::plugin_automation (lua_State *L)
209 {
210         typedef boost::shared_ptr<Processor> T;
211
212         int top = lua_gettop (L);
213         if (top < 2) {
214                 return luaL_argerror (L, 1, "invalid number of arguments, :plugin_automation (plugin, parameter_number)");
215         }
216         T* const p = luabridge::Userdata::get<T> (L, 1, false);
217         uint32_t which = luabridge::Stack<uint32_t>::get (L, 2);
218         if (!p) {
219                 return luaL_error (L, "Invalid pointer to Ardour:Processor");
220         }
221         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (*p);
222         if (!pi) {
223                 return luaL_error (L, "Given Processor is not a Plugin Insert");
224         }
225         boost::shared_ptr<Plugin> plugin = pi->plugin ();
226         if (!plugin) {
227                 return luaL_error (L, "Given Processor is not a Plugin");
228         }
229
230         bool ok=false;
231         uint32_t controlid = plugin->nth_parameter (which, ok);
232         if (!ok) {
233                 return luaL_error (L, "Invalid Parameter");
234         }
235         if (!plugin->parameter_is_input (controlid)) {
236                 return luaL_error (L, "Given Parameter is not an input");
237         }
238
239         ParameterDescriptor pd;
240         if (plugin->get_parameter_descriptor (controlid, pd) != 0) {
241                 return luaL_error (L, "Cannot describe parameter");
242         }
243
244         boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter (PluginAutomation, 0, controlid));
245
246         luabridge::Stack<boost::shared_ptr<AutomationList> >::push (L, c->alist ());
247         luabridge::Stack<boost::shared_ptr<Evoral::ControlList> >::push (L, c->list ());
248         luabridge::Stack<ParameterDescriptor>::push (L, pd);
249         return 3;
250 }
251
252 int
253 ARDOUR::LuaOSC::Address::send (lua_State *L)
254 {
255         Address * const luaosc = luabridge::Userdata::get <Address> (L, 1, false);
256         if (!luaosc) {
257                 return luaL_error (L, "Invalid pointer to OSC.Address");
258         }
259         if (!luaosc->_addr) {
260                 return luaL_error (L, "Invalid Destination Address");
261         }
262
263         int top = lua_gettop (L);
264         if (top < 3) {
265                 return luaL_argerror (L, 1, "invalid number of arguments, :send (path, type, ...)");
266         }
267
268         const char* path = luaL_checkstring (L, 2);
269         const char* type = luaL_checkstring (L, 3);
270         assert (path && type);
271
272         if ((int) strlen (type) != top - 3) {
273                 return luaL_argerror (L, 3, "type description does not match arguments");
274         }
275
276         lo_message msg = lo_message_new ();
277
278         for (int i = 4; i <= top; ++i) {
279                 char t = type[i - 4];
280                 int lt = lua_type (L, i);
281                 int ok = -1;
282                 switch (lt) {
283                         case LUA_TSTRING:
284                                 if (t == LO_STRING) {
285                                         ok = lo_message_add_string (msg, luaL_checkstring (L, i));
286                                 } else if (t == LO_CHAR) {
287                                         char c = luaL_checkstring (L, i) [0];
288                                         ok = lo_message_add_char (msg, c);
289                                 }
290                                 break;
291                         case LUA_TBOOLEAN:
292                                 if (t == LO_TRUE || t == LO_FALSE) {
293                                         if (lua_toboolean (L, i)) {
294                                                 ok = lo_message_add_true (msg);
295                                         } else {
296                                                 ok = lo_message_add_false (msg);
297                                         }
298                                 }
299                                 break;
300                         case LUA_TNUMBER:
301                                 if (t == LO_INT32) {
302                                         ok = lo_message_add_int32 (msg, (int32_t) luaL_checkinteger (L, i));
303                                 }
304                                 else if (t == LO_FLOAT) {
305                                         ok = lo_message_add_float (msg, (float) luaL_checknumber (L, i));
306                                 }
307                                 else if (t == LO_DOUBLE) {
308                                         ok = lo_message_add_double (msg, (double) luaL_checknumber (L, i));
309                                 }
310                                 else if (t == LO_INT64) {
311                                         ok = lo_message_add_double (msg, (int64_t) luaL_checknumber (L, i));
312                                 }
313                                 break;
314                         default:
315                                 break;
316                 }
317                 if (ok != 0) {
318                         return luaL_argerror (L, i, "type description does not match parameter");
319                 }
320         }
321
322         int rv = lo_send_message (luaosc->_addr, path, msg);
323         lo_message_free (msg);
324         luabridge::Stack<bool>::push (L, (rv == 0));
325         return 1;
326 }
327
328 static double hue2rgb (const double p, const double q, double t) {
329         if (t < 0.0) t += 1.0;
330         if (t > 1.0) t -= 1.0;
331         if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t;
332         if (t < 1.0 / 2.0) return q;
333         if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
334         return p;
335 }
336
337 int
338 ARDOUR::LuaAPI::hsla_to_rgba (lua_State *L)
339 {
340         int top = lua_gettop (L);
341         if (top < 3) {
342                 return luaL_argerror (L, 1, "invalid number of arguments, :hsla_to_rgba (h, s, l [,a])");
343         }
344         double h = luabridge::Stack<double>::get (L, 1);
345         double s = luabridge::Stack<double>::get (L, 2);
346         double l = luabridge::Stack<double>::get (L, 3);
347         double a = 1.0;
348         if (top > 3) {
349                 a = luabridge::Stack<double>::get (L, 4);
350         }
351
352         // we can't use ArdourCanvas::hsva_to_color here
353         // besides we want HSL not HSV and without intermediate
354         // color_to_rgba (rgba_to_color ())
355         double r, g, b;
356         const double cq = l < 0.5 ? l * (1 + s) : l + s - l * s;
357         const double cp = 2.f * l - cq;
358         r = hue2rgb (cp, cq, h + 1.0 / 3.0);
359         g = hue2rgb (cp, cq, h);
360         b = hue2rgb (cp, cq, h - 1.0 / 3.0);
361
362         luabridge::Stack<double>::push (L, r);
363         luabridge::Stack<double>::push (L, g);
364         luabridge::Stack<double>::push (L, b);
365         luabridge::Stack<double>::push (L, a);
366         return 4;
367 }
368
369 int
370 ARDOUR::LuaAPI::build_filename (lua_State *L)
371 {
372         std::vector<std::string> elem;
373         int top = lua_gettop (L);
374         if (top < 1) {
375                 return luaL_argerror (L, 1, "invalid number of arguments, build_filename (path, ...)");
376         }
377         for (int i = 1; i <= top; ++i) {
378                 int lt = lua_type (L, i);
379                 if (lt != LUA_TSTRING) {
380                         return luaL_argerror (L, i, "invalid argument type, expected string");
381                 }
382                 elem.push_back (luaL_checkstring (L, i));
383         }
384
385         luabridge::Stack<std::string>::push (L, Glib::build_filename (elem));
386         return 1;
387 }
388
389 luabridge::LuaRef::Proxy&
390 luabridge::LuaRef::Proxy::clone_instance (const void* classkey, void* p) {
391         lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_tableRef);
392         lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_keyRef);
393
394         luabridge::UserdataPtr::push_raw (m_L, p, classkey);
395
396         lua_rawset (m_L, -3);
397         lua_pop (m_L, 1);
398         return *this;
399 }
400
401 LuaTableRef::LuaTableRef () {}
402 LuaTableRef::~LuaTableRef () {}
403
404 int
405 LuaTableRef::get (lua_State* L)
406 {
407         luabridge::LuaRef rv (luabridge::newTable (L));
408         for (std::vector<LuaTableEntry>::const_iterator i = _data.begin (); i != _data.end (); ++i) {
409                 switch ((*i).keytype) {
410                         case LUA_TSTRING:
411                                 assign (&rv, i->k_s, *i);
412                                 break;
413                         case LUA_TNUMBER:
414                                 assign (&rv, i->k_n, *i);
415                                 break;
416                 }
417         }
418         luabridge::push (L, rv);
419         return 1;
420 }
421
422 int
423 LuaTableRef::set (lua_State* L)
424 {
425         if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); }
426         _data.clear ();
427
428         lua_pushvalue (L, -1);
429         lua_pushnil (L);
430         while (lua_next (L, -2)) {
431                 lua_pushvalue (L, -2);
432
433                 LuaTableEntry s (lua_type (L, -1), lua_type (L, -2));
434                 switch (lua_type (L, -1)) {
435                         case LUA_TSTRING:
436                                 s.k_s = luabridge::Stack<std::string>::get (L, -1);
437                                 break;
438                                 ;
439                         case LUA_TNUMBER:
440                                 s.k_n = luabridge::Stack<unsigned int>::get (L, -1);
441                                 break;
442                         default:
443                                 // invalid key
444                                 lua_pop (L, 2);
445                                 continue;
446                 }
447
448                 switch (lua_type (L, -2)) {
449                         case LUA_TSTRING:
450                                 s.s = luabridge::Stack<std::string>::get (L, -2);
451                                 break;
452                         case LUA_TBOOLEAN:
453                                 s.b = lua_toboolean (L, -2);
454                                 break;
455                         case LUA_TNUMBER:
456                                 s.n = lua_tonumber (L, -2);
457                                 break;
458                         case LUA_TUSERDATA:
459                                 {
460                                         bool ok = false;
461                                         lua_getmetatable (L, -2);
462                                         lua_rawgetp (L, -1, luabridge::getIdentityKey ());
463                                         if (lua_isboolean (L, -1)) {
464                                                 lua_pop (L, 1);
465                                                 const void* key = lua_topointer (L, -1);
466                                                 lua_pop (L, 1);
467                                                 void const* classkey = findclasskey (L, key);
468
469                                                 if (classkey) {
470                                                         ok = true;
471                                                         s.c = classkey;
472                                                         s.p = luabridge::Userdata::get_ptr (L, -2);
473                                                 }
474                                         } else {
475                                                 lua_pop (L, 2);
476                                         }
477
478                                         if (ok) {
479                                                 break;
480                                         }
481                                         // invalid userdata -- fall through
482                                 }
483                                 // no break
484                         case LUA_TFUNCTION: // no support -- we could... string.format("%q", string.dump(value, true))
485                         case LUA_TTABLE: // no nested tables, sorry.
486                         case LUA_TNIL: // fallthrough
487                         default:
488                                 // invalid value
489                                 lua_pop (L, 2);
490                                 continue;
491                 }
492
493                 _data.push_back (s);
494                 lua_pop (L, 2);
495         }
496         return 0;
497 }
498
499 void*
500 LuaTableRef::findclasskey (lua_State *L, const void* key)
501 {
502         lua_pushvalue (L, LUA_REGISTRYINDEX);
503         lua_pushnil (L);
504         while (lua_next (L, -2)) {
505                 lua_pushvalue (L, -2);
506                 if (lua_topointer (L, -2) == key) {
507                         void* rv = lua_touserdata (L, -1);
508                         lua_pop (L, 4);
509                         return rv;
510                 }
511                 lua_pop (L, 2);
512         }
513         lua_pop (L, 1);
514         return NULL;
515 }
516
517 template<typename T>
518 void LuaTableRef::assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s)
519 {
520         switch (s.valuetype) {
521                 case LUA_TSTRING:
522                         (*rv)[key] = s.s;
523                         break;
524                 case LUA_TBOOLEAN:
525                         (*rv)[key] = s.b;
526                         break;
527                 case LUA_TNUMBER:
528                         (*rv)[key] = s.n;
529                         break;
530                 case LUA_TUSERDATA:
531                         (*rv)[key].clone_instance (s.c, s.p);
532                         break;
533                 default:
534                         assert (0);
535                         break;
536         }
537 }