prepare fix for copying plugin state
[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::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 #ifdef WINDOWS_VST_SUPPORT
110         all_plugs.insert(all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
111 #endif
112 #ifdef LXVST_SUPPORT
113         all_plugs.insert(all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
114 #endif
115 #ifdef AUDIOUNIT_SUPPORT
116         all_plugs.insert(all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
117 #endif
118 #ifdef LV2_SUPPORT
119         all_plugs.insert(all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
120 #endif
121
122         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i) {
123                 if (((*i)->name == name || (*i)->unique_id == name) && (*i)->type == type) {
124                         return *i;
125                 }
126         }
127         return PluginInfoPtr ();
128 }
129
130 boost::shared_ptr<Processor>
131 ARDOUR::LuaAPI::new_plugin (Session *s, const string& name, ARDOUR::PluginType type, const string& preset)
132 {
133         if (!s) {
134                 return boost::shared_ptr<Processor> ();
135         }
136
137         PluginInfoPtr pip = new_plugin_info (name, type);
138
139         if (!pip) {
140                 return boost::shared_ptr<Processor> ();
141         }
142
143         PluginPtr p = pip->load (*s);
144         if (!p) {
145                 return boost::shared_ptr<Processor> ();
146         }
147
148         if (!preset.empty()) {
149                 const Plugin::PresetRecord *pr = p->preset_by_label (preset);
150                 if (pr) {
151                         p->load_preset (*pr);
152                 }
153         }
154
155         return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
156 }
157
158 bool
159 ARDOUR::LuaAPI::set_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, float val)
160 {
161         boost::shared_ptr<Plugin> plugin = pi->plugin();
162         if (!plugin) { return false; }
163
164         bool ok=false;
165         uint32_t controlid = plugin->nth_parameter (which, ok);
166         if (!ok) { return false; }
167         if (!plugin->parameter_is_input (controlid)) { return false; }
168
169         ParameterDescriptor pd;
170         if (plugin->get_parameter_descriptor (controlid, pd) != 0) { return false; }
171         if (val < pd.lower || val > pd.upper) { return false; }
172
173         boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
174         c->set_value (val, PBD::Controllable::NoGroup);
175         return true;
176 }
177
178 bool
179 ARDOUR::LuaAPI::set_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, float val)
180 {
181         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
182         if (!pi) { return false; }
183         return set_plugin_insert_param (pi, which, val);
184 }
185
186 int
187 ARDOUR::LuaAPI::plugin_automation (lua_State *L)
188 {
189         typedef boost::shared_ptr<Processor> T;
190
191         int top = lua_gettop(L);
192         if (top < 2) {
193                 return luaL_argerror (L, 1, "invalid number of arguments, :plugin_automation (plugin, parameter_number)");
194         }
195         T* const p = luabridge::Userdata::get<T>(L, 1, false);
196         uint32_t which = luabridge::Stack<uint32_t>::get (L, 2);
197         if (!p) {
198                 return luaL_error (L, "Invalid pointer to Ardour:Processor");
199         }
200         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (*p);
201         if (!pi) {
202                 return luaL_error (L, "Given Processor is not a Plugin Insert");
203         }
204         boost::shared_ptr<Plugin> plugin = pi->plugin();
205         if (!plugin) {
206                 return luaL_error (L, "Given Processor is not a Plugin");
207         }
208
209         bool ok=false;
210         uint32_t controlid = plugin->nth_parameter (which, ok);
211         if (!ok) {
212                 return luaL_error (L, "Invalid Parameter");
213         }
214         if (!plugin->parameter_is_input (controlid)) {
215                 return luaL_error (L, "Given Parameter is not an input");
216         }
217
218         ParameterDescriptor pd;
219         if (plugin->get_parameter_descriptor (controlid, pd) != 0) {
220                 return luaL_error (L, "Cannot describe parameter");
221         }
222
223         boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
224
225         luabridge::Stack<boost::shared_ptr<AutomationList> >::push (L, c->alist());
226         luabridge::Stack<boost::shared_ptr<Evoral::ControlList> >::push (L, c->list());
227         luabridge::Stack<ParameterDescriptor>::push (L, pd);
228         return 3;
229 }
230
231 int
232 ARDOUR::LuaOSC::Address::send (lua_State *L)
233 {
234         Address * const luaosc = luabridge::Userdata::get <Address> (L, 1, false);
235         if (!luaosc) {
236                 return luaL_error (L, "Invalid pointer to OSC.Address");
237         }
238         if (!luaosc->_addr) {
239                 return luaL_error (L, "Invalid Destination Address");
240         }
241
242         int top = lua_gettop(L);
243         if (top < 3) {
244     return luaL_argerror (L, 1, "invalid number of arguments, :send (path, type, ...)");
245         }
246
247         const char* path = luaL_checkstring (L, 2);
248         const char* type = luaL_checkstring (L, 3);
249         assert (path && type);
250
251         if ((int) strlen(type) != top - 3) {
252     return luaL_argerror (L, 3, "type description does not match arguments");
253         }
254
255         lo_message msg = lo_message_new ();
256
257         for (int i = 4; i <= top; ++i) {
258                 char t = type[i - 4];
259                 int lt = lua_type(L, i);
260                 int ok = -1;
261                 switch(lt) {
262                         case LUA_TSTRING:
263                                 if (t == LO_STRING) {
264                                         ok = lo_message_add_string (msg, luaL_checkstring(L, i));
265                                 } else if (t ==  LO_CHAR) {
266                                         char c = luaL_checkstring (L, i) [0];
267                                         ok = lo_message_add_char (msg, c);
268                                 }
269                                 break;
270                         case LUA_TBOOLEAN:
271                                 if (t == LO_TRUE || t == LO_FALSE) {
272                                         if (lua_toboolean (L, i)) {
273                                                 ok = lo_message_add_true (msg);
274                                         } else {
275                                                 ok = lo_message_add_false (msg);
276                                         }
277                                 }
278                                 break;
279                         case LUA_TNUMBER:
280                                 if (t == LO_INT32) {
281                                         ok = lo_message_add_int32 (msg, (int32_t) luaL_checkinteger(L, i));
282                                 }
283                                 else if (t == LO_FLOAT) {
284                                         ok = lo_message_add_float (msg, (float) luaL_checknumber(L, i));
285                                 }
286                                 else if (t == LO_DOUBLE) {
287                                         ok = lo_message_add_double (msg, (double) luaL_checknumber(L, i));
288                                 }
289                                 else if (t == LO_INT64) {
290                                         ok = lo_message_add_double (msg, (int64_t) luaL_checknumber(L, i));
291                                 }
292                                 break;
293                         default:
294                                 break;
295                 }
296                 if (ok != 0) {
297                         return luaL_argerror (L, i, "type description does not match parameter");
298                 }
299         }
300
301         int rv = lo_send_message (luaosc->_addr, path, msg);
302         lo_message_free (msg);
303         luabridge::Stack<bool>::push (L, (rv == 0));
304         return 1;
305 }