consistent lua binding name (nil is a reserved word in lua)
[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 "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::new_luaproc (Session *s, const string& name)
66 {
67         if (!s) {
68                 return boost::shared_ptr<Processor> ();
69         }
70
71         LuaScriptInfoPtr spi;
72         ARDOUR::LuaScriptList & _scripts (LuaScripting::instance ().scripts (LuaScriptInfo::DSP));
73         for (LuaScriptList::const_iterator i = _scripts.begin(); i != _scripts.end(); ++i) {
74                 if (name == (*i)->name) {
75                         spi = *i;
76                         break;
77                 }
78         }
79
80         if (!spi) {
81                 warning << _("Script with given name was not found\n");
82                 return boost::shared_ptr<Processor> ();
83         }
84
85         PluginPtr p;
86         try {
87                 LuaPluginInfoPtr lpi (new LuaPluginInfo(spi));
88                 p = (lpi->load (*s));
89         } catch (...) {
90                 warning << _("Failed to instantiate Lua Processor\n");
91                 return boost::shared_ptr<Processor> ();
92         }
93
94         return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
95 }
96
97 PluginInfoPtr
98 ARDOUR::LuaAPI::new_plugin_info (const string& name, ARDOUR::PluginType type)
99 {
100         PluginManager& manager = PluginManager::instance();
101         PluginInfoList all_plugs;
102         all_plugs.insert(all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
103 #ifdef WINDOWS_VST_SUPPORT
104         all_plugs.insert(all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
105 #endif
106 #ifdef LXVST_SUPPORT
107         all_plugs.insert(all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
108 #endif
109 #ifdef AUDIOUNIT_SUPPORT
110         all_plugs.insert(all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
111 #endif
112 #ifdef LV2_SUPPORT
113         all_plugs.insert(all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
114 #endif
115
116         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
117                 if (((*i)->name == name || (*i)->unique_id == name) && (*i)->type == type) {
118                         return *i;
119                 }
120         }
121         return PluginInfoPtr ();
122 }
123
124 boost::shared_ptr<Processor>
125 ARDOUR::LuaAPI::new_plugin (Session *s, const string& name, ARDOUR::PluginType type, const string& preset)
126 {
127         if (!s) {
128                 return boost::shared_ptr<Processor> ();
129         }
130
131         PluginInfoPtr pip = new_plugin_info (name, type);
132
133         if (!pip) {
134                 return boost::shared_ptr<Processor> ();
135         }
136
137         PluginPtr p = pip->load (*s);
138         if (!p) {
139                 return boost::shared_ptr<Processor> ();
140         }
141
142         if (!preset.empty()) {
143                 const Plugin::PresetRecord *pr = p->preset_by_label (preset);
144                 if (pr) {
145                         p->load_preset (*pr);
146                 }
147         }
148
149         return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
150 }
151
152 bool
153 ARDOUR::LuaAPI::set_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, float val)
154 {
155         boost::shared_ptr<Plugin> plugin = pi->plugin();
156         if (!plugin) { return false; }
157
158         bool ok=false;
159         uint32_t controlid = plugin->nth_parameter (which, ok);
160         if (!ok) { return false; }
161         if (!plugin->parameter_is_input (controlid)) { return false; }
162
163         ParameterDescriptor pd;
164         if (plugin->get_parameter_descriptor (controlid, pd) != 0) { return false; }
165         if (val < pd.lower || val > pd.upper) { return false; }
166
167         boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
168         c->set_value (val, PBD::Controllable::NoGroup);
169         return true;
170 }
171
172 bool
173 ARDOUR::LuaAPI::set_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, float val)
174 {
175         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
176         if (!pi) { return false; }
177         return set_plugin_insert_param (pi, which, val);
178 }
179
180 int
181 ARDOUR::LuaOSC::Address::send (lua_State *L)
182 {
183         Address * const luaosc = luabridge::Userdata::get <Address> (L, 1, false);
184         if (!luaosc) {
185                 return luaL_error (L, "Invalid pointer to OSC.Address");
186         }
187         if (!luaosc->_addr) {
188                 return luaL_error (L, "Invalid Destination Address");
189         }
190
191         int top = lua_gettop(L);
192         if (top < 3) {
193     return luaL_argerror (L, 1, "invalid number of arguments, :send (path, type, ...)");
194         }
195
196         const char* path = luaL_checkstring (L, 2);
197         const char* type = luaL_checkstring (L, 3);
198         assert (path && type);
199
200         if ((int) strlen(type) != top - 3) {
201     return luaL_argerror (L, 3, "type description does not match arguments");
202         }
203
204         lo_message msg = lo_message_new ();
205
206         for (int i = 4; i <= top; ++i) {
207                 char t = type[i - 4];
208                 int lt = lua_type(L, i);
209                 int ok = -1;
210                 switch(lt) {
211                         case LUA_TSTRING:
212                                 if (t == LO_STRING) {
213                                         ok = lo_message_add_string (msg, luaL_checkstring(L, i));
214                                 } else if (t ==  LO_CHAR) {
215                                         char c = luaL_checkstring (L, i) [0];
216                                         ok = lo_message_add_char (msg, c);
217                                 }
218                                 break;
219                         case LUA_TBOOLEAN:
220                                 if (t == LO_TRUE || t == LO_FALSE) {
221                                         if (lua_toboolean (L, i)) {
222                                                 ok = lo_message_add_true (msg);
223                                         } else {
224                                                 ok = lo_message_add_false (msg);
225                                         }
226                                 }
227                                 break;
228                         case LUA_TNUMBER:
229                                 if (t == LO_INT32) {
230                                         ok = lo_message_add_int32 (msg, (int32_t) luaL_checkinteger(L, i));
231                                 }
232                                 else if (t == LO_FLOAT) {
233                                         ok = lo_message_add_float (msg, (float) luaL_checknumber(L, i));
234                                 }
235                                 else if (t == LO_DOUBLE) {
236                                         ok = lo_message_add_double (msg, (double) luaL_checknumber(L, i));
237                                 }
238                                 else if (t == LO_INT64) {
239                                         ok = lo_message_add_double (msg, (int64_t) luaL_checknumber(L, i));
240                                 }
241                                 break;
242                         default:
243                                 break;
244                 }
245                 if (ok != 0) {
246                         return luaL_argerror (L, i, "type description does not match parameter");
247                 }
248         }
249
250         int rv = lo_send_message (luaosc->_addr, path, msg);
251         lo_message_free (msg);
252         luabridge::Stack<bool>::push (L, (rv == 0));
253         return 1;
254 }