2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
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.
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.
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.
20 #include <vamp-hostsdk/PluginLoader.h>
22 #include "pbd/compose.h"
23 #include "pbd/error.h"
24 #include "pbd/failed_constructor.h"
26 #include "ardour/lua_api.h"
27 #include "ardour/luaproc.h"
28 #include "ardour/luascripting.h"
29 #include "ardour/plugin.h"
30 #include "ardour/plugin_insert.h"
31 #include "ardour/plugin_manager.h"
32 #include "ardour/readable.h"
34 #include "LuaBridge/LuaBridge.h"
38 using namespace ARDOUR;
43 ARDOUR::LuaAPI::datatype_ctor_null (lua_State *L)
45 DataType dt (DataType::NIL);
46 luabridge::Stack <DataType>::push (L, dt);
51 ARDOUR::LuaAPI::datatype_ctor_audio (lua_State *L)
53 DataType dt (DataType::AUDIO);
54 // NB luabridge will copy construct the object and manage lifetime.
55 luabridge::Stack <DataType>::push (L, dt);
60 ARDOUR::LuaAPI::datatype_ctor_midi (lua_State *L)
62 DataType dt (DataType::MIDI);
63 luabridge::Stack <DataType>::push (L, dt);
67 boost::shared_ptr<Processor>
68 ARDOUR::LuaAPI::nil_processor ()
70 return boost::shared_ptr<Processor> ();
73 boost::shared_ptr<Processor>
74 ARDOUR::LuaAPI::new_luaproc (Session *s, const string& name)
77 return boost::shared_ptr<Processor> ();
81 ARDOUR::LuaScriptList & _scripts (LuaScripting::instance ().scripts (LuaScriptInfo::DSP));
82 for (LuaScriptList::const_iterator i = _scripts.begin (); i != _scripts.end (); ++i) {
83 if (name == (*i)->name) {
90 warning << _("Script with given name was not found\n");
91 return boost::shared_ptr<Processor> ();
96 LuaPluginInfoPtr lpi (new LuaPluginInfo (spi));
99 warning << _("Failed to instantiate Lua Processor\n");
100 return boost::shared_ptr<Processor> ();
103 return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
107 ARDOUR::LuaAPI::new_plugin_info (const string& name, ARDOUR::PluginType type)
109 PluginManager& manager = PluginManager::instance ();
110 PluginInfoList all_plugs;
111 all_plugs.insert (all_plugs.end (), manager.ladspa_plugin_info ().begin (), manager.ladspa_plugin_info ().end ());
112 all_plugs.insert (all_plugs.end (), manager.lua_plugin_info ().begin (), manager.lua_plugin_info ().end ());
113 #ifdef WINDOWS_VST_SUPPORT
114 all_plugs.insert (all_plugs.end (), manager.windows_vst_plugin_info ().begin (), manager.windows_vst_plugin_info ().end ());
117 all_plugs.insert (all_plugs.end (), manager.lxvst_plugin_info ().begin (), manager.lxvst_plugin_info ().end ());
119 #ifdef AUDIOUNIT_SUPPORT
120 all_plugs.insert (all_plugs.end (), manager.au_plugin_info ().begin (), manager.au_plugin_info ().end ());
123 all_plugs.insert (all_plugs.end (), manager.lv2_plugin_info ().begin (), manager.lv2_plugin_info ().end ());
126 for (PluginInfoList::const_iterator i = all_plugs.begin (); i != all_plugs.end (); ++i) {
127 if (((*i)->name == name || (*i)->unique_id == name) && (*i)->type == type) {
131 return PluginInfoPtr ();
134 boost::shared_ptr<Processor>
135 ARDOUR::LuaAPI::new_plugin (Session *s, const string& name, ARDOUR::PluginType type, const string& preset)
138 return boost::shared_ptr<Processor> ();
141 PluginInfoPtr pip = new_plugin_info (name, type);
144 return boost::shared_ptr<Processor> ();
147 PluginPtr p = pip->load (*s);
149 return boost::shared_ptr<Processor> ();
152 if (!preset.empty ()) {
153 const Plugin::PresetRecord *pr = p->preset_by_label (preset);
155 p->load_preset (*pr);
159 return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
163 ARDOUR::LuaAPI::set_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, float val)
165 boost::shared_ptr<Plugin> plugin = pi->plugin ();
166 if (!plugin) { return false; }
169 uint32_t controlid = plugin->nth_parameter (which, ok);
170 if (!ok) { return false; }
171 if (!plugin->parameter_is_input (controlid)) { return false; }
173 ParameterDescriptor pd;
174 if (plugin->get_parameter_descriptor (controlid, pd) != 0) { return false; }
175 if (val < pd.lower || val > pd.upper) { return false; }
177 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter (PluginAutomation, 0, controlid));
178 c->set_value (val, PBD::Controllable::NoGroup);
183 ARDOUR::LuaAPI::get_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, bool &ok)
186 boost::shared_ptr<Plugin> plugin = pi->plugin ();
187 if (!plugin) { return 0; }
188 uint32_t controlid = plugin->nth_parameter (which, ok);
189 if (!ok) { return 0; }
190 return plugin->get_parameter ( controlid );
194 ARDOUR::LuaAPI::set_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, float val)
196 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
197 if (!pi) { return false; }
198 return set_plugin_insert_param (pi, which, val);
202 ARDOUR::LuaAPI::get_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, bool &ok)
205 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
206 if (!pi) { return false; }
207 return get_plugin_insert_param (pi, which, ok);
211 ARDOUR::LuaAPI::reset_processor_to_default ( boost::shared_ptr<Processor> proc )
213 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
215 pi->reset_parameters_to_default();
222 ARDOUR::LuaAPI::plugin_automation (lua_State *L)
224 typedef boost::shared_ptr<Processor> T;
226 int top = lua_gettop (L);
228 return luaL_argerror (L, 1, "invalid number of arguments, :plugin_automation (plugin, parameter_number)");
230 T* const p = luabridge::Userdata::get<T> (L, 1, false);
231 uint32_t which = luabridge::Stack<uint32_t>::get (L, 2);
233 return luaL_error (L, "Invalid pointer to Ardour:Processor");
235 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (*p);
237 return luaL_error (L, "Given Processor is not a Plugin Insert");
239 boost::shared_ptr<Plugin> plugin = pi->plugin ();
241 return luaL_error (L, "Given Processor is not a Plugin");
245 uint32_t controlid = plugin->nth_parameter (which, ok);
247 return luaL_error (L, "Invalid Parameter");
249 if (!plugin->parameter_is_input (controlid)) {
250 return luaL_error (L, "Given Parameter is not an input");
253 ParameterDescriptor pd;
254 if (plugin->get_parameter_descriptor (controlid, pd) != 0) {
255 return luaL_error (L, "Cannot describe parameter");
258 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter (PluginAutomation, 0, controlid));
260 luabridge::Stack<boost::shared_ptr<AutomationList> >::push (L, c->alist ());
261 luabridge::Stack<boost::shared_ptr<Evoral::ControlList> >::push (L, c->list ());
262 luabridge::Stack<ParameterDescriptor>::push (L, pd);
267 ARDOUR::LuaAPI::sample_to_timecode (lua_State *L)
269 int top = lua_gettop (L);
271 return luaL_argerror (L, 1, "invalid number of arguments sample_to_timecode (TimecodeFormat, sample_rate, sample)");
273 typedef Timecode::TimecodeFormat T;
274 T tf = luabridge::Stack<T>::get (L, 1);
275 double sample_rate = luabridge::Stack<double>::get (L, 2);
276 int64_t sample = luabridge::Stack<int64_t>::get (L, 3);
278 Timecode::Time timecode;
280 Timecode::sample_to_timecode (
281 sample, timecode, false, false,
282 Timecode::timecode_to_frames_per_second (tf),
283 Timecode::timecode_has_drop_frames (tf),
287 luabridge::Stack<uint32_t>::push (L, timecode.hours);
288 luabridge::Stack<uint32_t>::push (L, timecode.minutes);
289 luabridge::Stack<uint32_t>::push (L, timecode.seconds);
290 luabridge::Stack<uint32_t>::push (L, timecode.frames);
295 ARDOUR::LuaAPI::timecode_to_sample (lua_State *L)
297 int top = lua_gettop (L);
299 return luaL_argerror (L, 1, "invalid number of arguments sample_to_timecode (TimecodeFormat, sample_rate, hh, mm, ss, ff)");
301 typedef Timecode::TimecodeFormat T;
302 T tf = luabridge::Stack<T>::get (L, 1);
303 double sample_rate = luabridge::Stack<double>::get (L, 2);
304 int hh = luabridge::Stack<int>::get (L, 3);
305 int mm = luabridge::Stack<int>::get (L, 4);
306 int ss = luabridge::Stack<int>::get (L, 5);
307 int ff = luabridge::Stack<int>::get (L, 6);
309 Timecode::Time timecode;
310 timecode.negative = false;
312 timecode.minutes = mm;
313 timecode.seconds = ss;
314 timecode.frames = ff;
315 timecode.subframes = 0;
316 timecode.rate = Timecode::timecode_to_frames_per_second (tf);
317 timecode.drop = Timecode::timecode_has_drop_frames (tf);
321 Timecode::timecode_to_sample (
322 timecode, sample, false, false,
323 sample_rate, 0, false, 0);
325 luabridge::Stack<int64_t>::push (L, sample);
330 ARDOUR::LuaAPI::sample_to_timecode_lua (lua_State *L)
332 int top = lua_gettop (L);
334 return luaL_argerror (L, 1, "invalid number of arguments sample_to_timecode (sample)");
336 Session const* const s = luabridge::Userdata::get <Session> (L, 1, true);
337 int64_t sample = luabridge::Stack<int64_t>::get (L, 2);
339 Timecode::Time timecode;
341 Timecode::sample_to_timecode (
342 sample, timecode, false, false,
343 s->timecode_frames_per_second (),
344 s->timecode_drop_frames (),
348 luabridge::Stack<uint32_t>::push (L, timecode.hours);
349 luabridge::Stack<uint32_t>::push (L, timecode.minutes);
350 luabridge::Stack<uint32_t>::push (L, timecode.seconds);
351 luabridge::Stack<uint32_t>::push (L, timecode.frames);
355 ARDOUR::LuaAPI::timecode_to_sample_lua (lua_State *L)
357 int top = lua_gettop (L);
359 return luaL_argerror (L, 1, "invalid number of arguments sample_to_timecode (hh, mm, ss, ff)");
361 Session const* const s = luabridge::Userdata::get <Session> (L, 1, true);
362 int hh = luabridge::Stack<int>::get (L, 2);
363 int mm = luabridge::Stack<int>::get (L, 3);
364 int ss = luabridge::Stack<int>::get (L, 4);
365 int ff = luabridge::Stack<int>::get (L, 5);
367 Timecode::Time timecode;
368 timecode.negative = false;
370 timecode.minutes = mm;
371 timecode.seconds = ss;
372 timecode.frames = ff;
373 timecode.subframes = 0;
374 timecode.rate = s->timecode_frames_per_second ();
375 timecode.drop = s->timecode_drop_frames ();
379 Timecode::timecode_to_sample (
380 timecode, sample, false, false,
384 luabridge::Stack<int64_t>::push (L, sample);
389 ARDOUR::LuaOSC::Address::send (lua_State *L)
391 Address * const luaosc = luabridge::Userdata::get <Address> (L, 1, false);
393 return luaL_error (L, "Invalid pointer to OSC.Address");
395 if (!luaosc->_addr) {
396 return luaL_error (L, "Invalid Destination Address");
399 int top = lua_gettop (L);
401 return luaL_argerror (L, 1, "invalid number of arguments, :send (path, type, ...)");
404 const char* path = luaL_checkstring (L, 2);
405 const char* type = luaL_checkstring (L, 3);
406 assert (path && type);
408 if ((int) strlen (type) != top - 3) {
409 return luaL_argerror (L, 3, "type description does not match arguments");
412 lo_message msg = lo_message_new ();
414 for (int i = 4; i <= top; ++i) {
415 char t = type[i - 4];
416 int lt = lua_type (L, i);
420 if (t == LO_STRING) {
421 ok = lo_message_add_string (msg, luaL_checkstring (L, i));
422 } else if (t == LO_CHAR) {
423 char c = luaL_checkstring (L, i) [0];
424 ok = lo_message_add_char (msg, c);
428 if (t == LO_TRUE || t == LO_FALSE) {
429 if (lua_toboolean (L, i)) {
430 ok = lo_message_add_true (msg);
432 ok = lo_message_add_false (msg);
438 ok = lo_message_add_int32 (msg, (int32_t) luaL_checkinteger (L, i));
440 else if (t == LO_FLOAT) {
441 ok = lo_message_add_float (msg, (float) luaL_checknumber (L, i));
443 else if (t == LO_DOUBLE) {
444 ok = lo_message_add_double (msg, (double) luaL_checknumber (L, i));
446 else if (t == LO_INT64) {
447 ok = lo_message_add_double (msg, (int64_t) luaL_checknumber (L, i));
454 return luaL_argerror (L, i, "type description does not match parameter");
458 int rv = lo_send_message (luaosc->_addr, path, msg);
459 lo_message_free (msg);
460 luabridge::Stack<bool>::push (L, (rv == 0));
464 static double hue2rgb (const double p, const double q, double t) {
465 if (t < 0.0) t += 1.0;
466 if (t > 1.0) t -= 1.0;
467 if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t;
468 if (t < 1.0 / 2.0) return q;
469 if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
474 ARDOUR::LuaAPI::hsla_to_rgba (lua_State *L)
476 int top = lua_gettop (L);
478 return luaL_argerror (L, 1, "invalid number of arguments, :hsla_to_rgba (h, s, l [,a])");
480 double h = luabridge::Stack<double>::get (L, 1);
481 double s = luabridge::Stack<double>::get (L, 2);
482 double l = luabridge::Stack<double>::get (L, 3);
485 a = luabridge::Stack<double>::get (L, 4);
488 // we can't use ArdourCanvas::hsva_to_color here
489 // besides we want HSL not HSV and without intermediate
490 // color_to_rgba (rgba_to_color ())
492 const double cq = l < 0.5 ? l * (1 + s) : l + s - l * s;
493 const double cp = 2.f * l - cq;
494 r = hue2rgb (cp, cq, h + 1.0 / 3.0);
495 g = hue2rgb (cp, cq, h);
496 b = hue2rgb (cp, cq, h - 1.0 / 3.0);
498 luabridge::Stack<double>::push (L, r);
499 luabridge::Stack<double>::push (L, g);
500 luabridge::Stack<double>::push (L, b);
501 luabridge::Stack<double>::push (L, a);
506 ARDOUR::LuaAPI::build_filename (lua_State *L)
508 std::vector<std::string> elem;
509 int top = lua_gettop (L);
511 return luaL_argerror (L, 1, "invalid number of arguments, build_filename (path, ...)");
513 for (int i = 1; i <= top; ++i) {
514 int lt = lua_type (L, i);
515 if (lt != LUA_TSTRING) {
516 return luaL_argerror (L, i, "invalid argument type, expected string");
518 elem.push_back (luaL_checkstring (L, i));
521 luabridge::Stack<std::string>::push (L, Glib::build_filename (elem));
525 luabridge::LuaRef::Proxy&
526 luabridge::LuaRef::Proxy::clone_instance (const void* classkey, void* p) {
527 lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_tableRef);
528 lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_keyRef);
530 luabridge::UserdataPtr::push_raw (m_L, p, classkey);
532 lua_rawset (m_L, -3);
537 LuaTableRef::LuaTableRef () {}
538 LuaTableRef::~LuaTableRef () {}
541 LuaTableRef::get (lua_State* L)
543 luabridge::LuaRef rv (luabridge::newTable (L));
544 for (std::vector<LuaTableEntry>::const_iterator i = _data.begin (); i != _data.end (); ++i) {
545 switch ((*i).keytype) {
547 assign (&rv, i->k_s, *i);
550 assign (&rv, i->k_n, *i);
554 luabridge::push (L, rv);
559 LuaTableRef::set (lua_State* L)
561 if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); }
564 lua_pushvalue (L, -1);
566 while (lua_next (L, -2)) {
567 lua_pushvalue (L, -2);
569 LuaTableEntry s (lua_type (L, -1), lua_type (L, -2));
570 switch (lua_type (L, -1)) {
572 s.k_s = luabridge::Stack<std::string>::get (L, -1);
576 s.k_n = luabridge::Stack<unsigned int>::get (L, -1);
584 switch (lua_type (L, -2)) {
586 s.s = luabridge::Stack<std::string>::get (L, -2);
589 s.b = lua_toboolean (L, -2);
592 s.n = lua_tonumber (L, -2);
597 lua_getmetatable (L, -2);
598 lua_rawgetp (L, -1, luabridge::getIdentityKey ());
599 if (lua_isboolean (L, -1)) {
601 const void* key = lua_topointer (L, -1);
603 void const* classkey = findclasskey (L, key);
608 s.p = luabridge::Userdata::get_ptr (L, -2);
617 // invalid userdata -- fall through
620 case LUA_TFUNCTION: // no support -- we could... string.format("%q", string.dump(value, true))
621 case LUA_TTABLE: // no nested tables, sorry.
622 case LUA_TNIL: // fallthrough
636 LuaTableRef::findclasskey (lua_State *L, const void* key)
638 lua_pushvalue (L, LUA_REGISTRYINDEX);
640 while (lua_next (L, -2)) {
641 lua_pushvalue (L, -2);
642 if (lua_topointer (L, -2) == key) {
643 void* rv = lua_touserdata (L, -1);
654 void LuaTableRef::assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s)
656 switch (s.valuetype) {
667 (*rv)[key].clone_instance (s.c, s.p);
675 std::vector<std::string>
676 LuaAPI::Vamp::list_plugins ()
678 using namespace ::Vamp::HostExt;
679 PluginLoader* loader (PluginLoader::getInstance());
680 return loader->listPlugins ();
683 LuaAPI::Vamp::Vamp (const std::string& key, float sample_rate)
685 , _sample_rate (sample_rate)
688 , _initialized (false)
690 using namespace ::Vamp::HostExt;
692 PluginLoader* loader (PluginLoader::getInstance());
693 _plugin = loader->loadPlugin (key, _sample_rate, PluginLoader::ADAPT_ALL_SAFE);
696 PBD::error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
697 throw failed_constructor ();
700 size_t bs = _plugin->getPreferredBlockSize ();
701 size_t ss = _plugin->getPreferredStepSize ();
703 if (bs > 0 && ss > 0 && bs <= 8192 && ss <= 8192) {
709 LuaAPI::Vamp::~Vamp ()
715 LuaAPI::Vamp::reset ()
717 _initialized = false;
724 LuaAPI::Vamp::initialize ()
726 if (!_plugin || _plugin->getMinChannelCount() > 1) {
729 if (!_plugin->initialise (1, _stepsize, _bufsize)) {
737 LuaAPI::Vamp::analyze (boost::shared_ptr<ARDOUR::Readable> r, uint32_t channel, luabridge::LuaRef cb)
740 if (!initialize ()) {
744 assert (_initialized);
746 ::Vamp::Plugin::FeatureSet features;
747 float* data = new float[_bufsize];
748 float* bufs[1] = { data };
750 framecnt_t len = r->readable_length();
755 framecnt_t to_read = std::min ((len - pos), _bufsize);
756 if (r->read (data, pos, to_read, channel) != to_read) {
760 if (to_read != _bufsize) {
761 memset (data + to_read, 0, (_bufsize - to_read) * sizeof (float));
764 features = _plugin->process (bufs, ::Vamp::RealTime::fromSeconds ((double) pos / _sample_rate));
766 if (cb.type () == LUA_TFUNCTION) {
770 pos += std::min (_stepsize, to_read);
781 ::Vamp::Plugin::FeatureSet
782 LuaAPI::Vamp::process (const std::vector<float*>& d, ::Vamp::RealTime rt)
784 if (!_plugin || d.size() == 0) {
785 return ::Vamp::Plugin::FeatureSet ();
787 const float* const* bufs = &d[0];
788 return _plugin->process (bufs, rt);
791 boost::shared_ptr<Evoral::Note<Evoral::Beats> >
792 LuaAPI::new_noteptr (uint8_t chan, Evoral::Beats beat_time, Evoral::Beats length, uint8_t note, uint8_t velocity)
794 return boost::shared_ptr<Evoral::Note<Evoral::Beats> > (new Evoral::Note<Evoral::Beats>(chan, beat_time, length, note, velocity));