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