add OSC Lua bindings
[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
30 #include "i18n.h"
31
32 using namespace ARDOUR;
33 using namespace PBD;
34 using namespace std;
35
36 boost::shared_ptr<Processor>
37 ARDOUR::LuaAPI::new_luaproc (Session *s, const string& name)
38 {
39         if (!s) {
40                 return boost::shared_ptr<Processor> ();
41         }
42
43         LuaScriptInfoPtr spi;
44         ARDOUR::LuaScriptList & _scripts (LuaScripting::instance ().scripts (LuaScriptInfo::DSP));
45         for (LuaScriptList::const_iterator s = _scripts.begin(); s != _scripts.end(); ++s) {
46                 if (name == (*s)->name) {
47                         spi = *s;
48                         break;
49                 }
50         }
51
52         if (!spi) {
53                 warning << _("Script with given name was not found\n");
54                 return boost::shared_ptr<Processor> ();
55         }
56
57         PluginPtr p;
58         try {
59                 LuaPluginInfoPtr lpi (new LuaPluginInfo(spi));
60                 p = (lpi->load (*s));
61         } catch (...) {
62                 warning << _("Failed to instantiate Lua Processor\n");
63                 return boost::shared_ptr<Processor> ();
64         }
65
66         return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
67 }
68
69 int
70 ARDOUR::LuaAPI::LuaOSCAddress::send (lua_State *L)
71 {
72         LuaOSCAddress * const luaosc = luabridge::Userdata::get <LuaOSCAddress> (L, 1, false);
73         if (!luaosc) {
74                 return luaL_error (L, "Invalid pointer to OSC.Address");
75         }
76         if (!luaosc->_addr) {
77                 return luaL_error (L, "Invalid Destination Address");
78         }
79
80         int top = lua_gettop(L);
81         if (top < 3) {
82     return luaL_argerror (L, 1, "invalid number of arguments, :send (path, type, ...)");
83         }
84
85         const char* path = luaL_checkstring (L, 2);
86         const char* type = luaL_checkstring (L, 3);
87         assert (path && type);
88
89         if ((int) strlen(type) != top - 3) {
90     return luaL_argerror (L, 3, "type description does not match arguments");
91         }
92
93         lo_message msg = lo_message_new ();
94
95         for (int i = 4; i <= top; ++i) {
96                 char t = type[i - 4];
97                 int lt = lua_type(L, i);
98                 int ok = -1;
99                 switch(lt) {
100                         case LUA_TSTRING:
101                                 if (t == LO_STRING) {
102                                         ok = lo_message_add_string (msg, luaL_checkstring(L, i));
103                                 } else if (t ==  LO_CHAR) {
104                                         char c = luaL_checkstring (L, i) [0];
105                                         ok = lo_message_add_char (msg, c);
106                                 }
107                                 break;
108                         case LUA_TBOOLEAN:
109                                 if (t == LO_TRUE || t == LO_FALSE) {
110                                         if (lua_toboolean (L, i)) {
111                                                 ok = lo_message_add_true (msg);
112                                         } else {
113                                                 ok = lo_message_add_false (msg);
114                                         }
115                                 }
116                                 break;
117                         case LUA_TNUMBER:
118                                 if (t == LO_INT32) {
119                                         ok = lo_message_add_int32 (msg, (int32_t) luaL_checkinteger(L, i));
120                                 }
121                                 else if (t == LO_FLOAT) {
122                                         ok = lo_message_add_float (msg, (float) luaL_checknumber(L, i));
123                                 }
124                                 else if (t == LO_DOUBLE) {
125                                         ok = lo_message_add_double (msg, (double) luaL_checknumber(L, i));
126                                 }
127                                 else if (t == LO_INT64) {
128                                         ok = lo_message_add_double (msg, (int64_t) luaL_checknumber(L, i));
129                                 }
130                                 break;
131                         default:
132                                 break;
133                 }
134                 if (ok != 0) {
135                         return luaL_argerror (L, i, "type description does not match parameter");
136                 }
137         }
138
139         int rv = lo_send_message (luaosc->_addr, path, msg);
140         lo_message_free (msg);
141         luabridge::Stack<int>::push (L, rv);
142         return 1;
143 }