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