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 ());
125 all_plugs.insert (all_plugs.end (), manager.lua_plugin_info ().begin (), manager.lua_plugin_info ().end ());
127 for (PluginInfoList::const_iterator i = all_plugs.begin (); i != all_plugs.end (); ++i) {
128 if (((*i)->name == name || (*i)->unique_id == name) && (*i)->type == type) {
132 return PluginInfoPtr ();
135 boost::shared_ptr<Processor>
136 ARDOUR::LuaAPI::new_plugin (Session *s, const string& name, ARDOUR::PluginType type, const string& preset)
139 return boost::shared_ptr<Processor> ();
142 PluginInfoPtr pip = new_plugin_info (name, type);
145 return boost::shared_ptr<Processor> ();
148 PluginPtr p = pip->load (*s);
150 return boost::shared_ptr<Processor> ();
153 if (!preset.empty ()) {
154 const Plugin::PresetRecord *pr = p->preset_by_label (preset);
156 p->load_preset (*pr);
160 return boost::shared_ptr<Processor> (new PluginInsert (*s, p));
164 ARDOUR::LuaAPI::set_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, float val)
166 boost::shared_ptr<Plugin> plugin = pi->plugin ();
167 if (!plugin) { return false; }
170 uint32_t controlid = plugin->nth_parameter (which, ok);
171 if (!ok) { return false; }
172 if (!plugin->parameter_is_input (controlid)) { return false; }
174 ParameterDescriptor pd;
175 if (plugin->get_parameter_descriptor (controlid, pd) != 0) { return false; }
176 if (val < pd.lower || val > pd.upper) { return false; }
178 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter (PluginAutomation, 0, controlid));
179 c->set_value (val, PBD::Controllable::NoGroup);
184 ARDOUR::LuaAPI::get_plugin_insert_param (boost::shared_ptr<PluginInsert> pi, uint32_t which, bool &ok)
187 boost::shared_ptr<Plugin> plugin = pi->plugin ();
188 if (!plugin) { return 0; }
189 uint32_t controlid = plugin->nth_parameter (which, ok);
190 if (!ok) { return 0; }
191 return plugin->get_parameter ( controlid );
195 ARDOUR::LuaAPI::set_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, float val)
197 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
198 if (!pi) { return false; }
199 return set_plugin_insert_param (pi, which, val);
203 ARDOUR::LuaAPI::get_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, bool &ok)
206 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
207 if (!pi) { return false; }
208 return get_plugin_insert_param (pi, which, ok);
212 ARDOUR::LuaAPI::reset_processor_to_default ( boost::shared_ptr<Processor> proc )
214 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
216 pi->reset_parameters_to_default();
223 ARDOUR::LuaAPI::plugin_automation (lua_State *L)
225 typedef boost::shared_ptr<Processor> T;
227 int top = lua_gettop (L);
229 return luaL_argerror (L, 1, "invalid number of arguments, :plugin_automation (plugin, parameter_number)");
231 T* const p = luabridge::Userdata::get<T> (L, 1, false);
232 uint32_t which = luabridge::Stack<uint32_t>::get (L, 2);
234 return luaL_error (L, "Invalid pointer to Ardour:Processor");
236 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (*p);
238 return luaL_error (L, "Given Processor is not a Plugin Insert");
240 boost::shared_ptr<Plugin> plugin = pi->plugin ();
242 return luaL_error (L, "Given Processor is not a Plugin");
246 uint32_t controlid = plugin->nth_parameter (which, ok);
248 return luaL_error (L, "Invalid Parameter");
250 if (!plugin->parameter_is_input (controlid)) {
251 return luaL_error (L, "Given Parameter is not an input");
254 ParameterDescriptor pd;
255 if (plugin->get_parameter_descriptor (controlid, pd) != 0) {
256 return luaL_error (L, "Cannot describe parameter");
259 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter (PluginAutomation, 0, controlid));
261 luabridge::Stack<boost::shared_ptr<AutomationList> >::push (L, c->alist ());
262 luabridge::Stack<boost::shared_ptr<Evoral::ControlList> >::push (L, c->list ());
263 luabridge::Stack<ParameterDescriptor>::push (L, pd);
268 ARDOUR::LuaAPI::sample_to_timecode (lua_State *L)
270 int top = lua_gettop (L);
272 return luaL_argerror (L, 1, "invalid number of arguments sample_to_timecode (TimecodeFormat, sample_rate, sample)");
274 typedef Timecode::TimecodeFormat T;
275 T tf = luabridge::Stack<T>::get (L, 1);
276 double sample_rate = luabridge::Stack<double>::get (L, 2);
277 int64_t sample = luabridge::Stack<int64_t>::get (L, 3);
279 Timecode::Time timecode;
281 Timecode::sample_to_timecode (
282 sample, timecode, false, false,
283 Timecode::timecode_to_frames_per_second (tf),
284 Timecode::timecode_has_drop_frames (tf),
288 luabridge::Stack<uint32_t>::push (L, timecode.hours);
289 luabridge::Stack<uint32_t>::push (L, timecode.minutes);
290 luabridge::Stack<uint32_t>::push (L, timecode.seconds);
291 luabridge::Stack<uint32_t>::push (L, timecode.frames);
296 ARDOUR::LuaAPI::timecode_to_sample (lua_State *L)
298 int top = lua_gettop (L);
300 return luaL_argerror (L, 1, "invalid number of arguments sample_to_timecode (TimecodeFormat, sample_rate, hh, mm, ss, ff)");
302 typedef Timecode::TimecodeFormat T;
303 T tf = luabridge::Stack<T>::get (L, 1);
304 double sample_rate = luabridge::Stack<double>::get (L, 2);
305 int hh = luabridge::Stack<int>::get (L, 3);
306 int mm = luabridge::Stack<int>::get (L, 4);
307 int ss = luabridge::Stack<int>::get (L, 5);
308 int ff = luabridge::Stack<int>::get (L, 6);
310 Timecode::Time timecode;
311 timecode.negative = false;
313 timecode.minutes = mm;
314 timecode.seconds = ss;
315 timecode.frames = ff;
316 timecode.subframes = 0;
317 timecode.rate = Timecode::timecode_to_frames_per_second (tf);
318 timecode.drop = Timecode::timecode_has_drop_frames (tf);
322 Timecode::timecode_to_sample (
323 timecode, sample, false, false,
324 sample_rate, 0, false, 0);
326 luabridge::Stack<int64_t>::push (L, sample);
331 ARDOUR::LuaAPI::sample_to_timecode_lua (lua_State *L)
333 int top = lua_gettop (L);
335 return luaL_argerror (L, 1, "invalid number of arguments sample_to_timecode (sample)");
337 Session const* const s = luabridge::Userdata::get <Session> (L, 1, true);
338 int64_t sample = luabridge::Stack<int64_t>::get (L, 2);
340 Timecode::Time timecode;
342 Timecode::sample_to_timecode (
343 sample, timecode, false, false,
344 s->timecode_frames_per_second (),
345 s->timecode_drop_frames (),
349 luabridge::Stack<uint32_t>::push (L, timecode.hours);
350 luabridge::Stack<uint32_t>::push (L, timecode.minutes);
351 luabridge::Stack<uint32_t>::push (L, timecode.seconds);
352 luabridge::Stack<uint32_t>::push (L, timecode.frames);
356 ARDOUR::LuaAPI::timecode_to_sample_lua (lua_State *L)
358 int top = lua_gettop (L);
360 return luaL_argerror (L, 1, "invalid number of arguments sample_to_timecode (hh, mm, ss, ff)");
362 Session const* const s = luabridge::Userdata::get <Session> (L, 1, true);
363 int hh = luabridge::Stack<int>::get (L, 2);
364 int mm = luabridge::Stack<int>::get (L, 3);
365 int ss = luabridge::Stack<int>::get (L, 4);
366 int ff = luabridge::Stack<int>::get (L, 5);
368 Timecode::Time timecode;
369 timecode.negative = false;
371 timecode.minutes = mm;
372 timecode.seconds = ss;
373 timecode.frames = ff;
374 timecode.subframes = 0;
375 timecode.rate = s->timecode_frames_per_second ();
376 timecode.drop = s->timecode_drop_frames ();
380 Timecode::timecode_to_sample (
381 timecode, sample, false, false,
385 luabridge::Stack<int64_t>::push (L, sample);
390 ARDOUR::LuaOSC::Address::send (lua_State *L)
392 Address * const luaosc = luabridge::Userdata::get <Address> (L, 1, false);
394 return luaL_error (L, "Invalid pointer to OSC.Address");
396 if (!luaosc->_addr) {
397 return luaL_error (L, "Invalid Destination Address");
400 int top = lua_gettop (L);
402 return luaL_argerror (L, 1, "invalid number of arguments, :send (path, type, ...)");
405 const char* path = luaL_checkstring (L, 2);
406 const char* type = luaL_checkstring (L, 3);
407 assert (path && type);
409 if ((int) strlen (type) != top - 3) {
410 return luaL_argerror (L, 3, "type description does not match arguments");
413 lo_message msg = lo_message_new ();
415 for (int i = 4; i <= top; ++i) {
416 char t = type[i - 4];
417 int lt = lua_type (L, i);
421 if (t == LO_STRING) {
422 ok = lo_message_add_string (msg, luaL_checkstring (L, i));
423 } else if (t == LO_CHAR) {
424 char c = luaL_checkstring (L, i) [0];
425 ok = lo_message_add_char (msg, c);
429 if (t == LO_TRUE || t == LO_FALSE) {
430 if (lua_toboolean (L, i)) {
431 ok = lo_message_add_true (msg);
433 ok = lo_message_add_false (msg);
439 ok = lo_message_add_int32 (msg, (int32_t) luaL_checkinteger (L, i));
441 else if (t == LO_FLOAT) {
442 ok = lo_message_add_float (msg, (float) luaL_checknumber (L, i));
444 else if (t == LO_DOUBLE) {
445 ok = lo_message_add_double (msg, (double) luaL_checknumber (L, i));
447 else if (t == LO_INT64) {
448 ok = lo_message_add_double (msg, (int64_t) luaL_checknumber (L, i));
455 return luaL_argerror (L, i, "type description does not match parameter");
459 int rv = lo_send_message (luaosc->_addr, path, msg);
460 lo_message_free (msg);
461 luabridge::Stack<bool>::push (L, (rv == 0));
465 static double hue2rgb (const double p, const double q, double t) {
466 if (t < 0.0) t += 1.0;
467 if (t > 1.0) t -= 1.0;
468 if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t;
469 if (t < 1.0 / 2.0) return q;
470 if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
475 ARDOUR::LuaAPI::hsla_to_rgba (lua_State *L)
477 int top = lua_gettop (L);
479 return luaL_argerror (L, 1, "invalid number of arguments, :hsla_to_rgba (h, s, l [,a])");
481 double h = luabridge::Stack<double>::get (L, 1);
482 double s = luabridge::Stack<double>::get (L, 2);
483 double l = luabridge::Stack<double>::get (L, 3);
486 a = luabridge::Stack<double>::get (L, 4);
489 // we can't use ArdourCanvas::hsva_to_color here
490 // besides we want HSL not HSV and without intermediate
491 // color_to_rgba (rgba_to_color ())
493 const double cq = l < 0.5 ? l * (1 + s) : l + s - l * s;
494 const double cp = 2.f * l - cq;
495 r = hue2rgb (cp, cq, h + 1.0 / 3.0);
496 g = hue2rgb (cp, cq, h);
497 b = hue2rgb (cp, cq, h - 1.0 / 3.0);
499 luabridge::Stack<double>::push (L, r);
500 luabridge::Stack<double>::push (L, g);
501 luabridge::Stack<double>::push (L, b);
502 luabridge::Stack<double>::push (L, a);
507 ARDOUR::LuaAPI::color_to_rgba (lua_State *L)
509 int top = lua_gettop (L);
511 return luaL_argerror (L, 1, "invalid number of arguments, color_to_rgba (uint32_t)");
513 uint32_t color = luabridge::Stack<uint32_t>::get (L, 1);
516 /* libardour is no user of libcanvas, otherwise
518 * ArdourCanvas::color_to_rgba (color, r, g, b, a);
520 r = ((color >> 24) & 0xff) / 255.0;
521 g = ((color >> 16) & 0xff) / 255.0;
522 b = ((color >> 8) & 0xff) / 255.0;
523 a = ((color >> 0) & 0xff) / 255.0;
525 luabridge::Stack <double>::push (L, r);
526 luabridge::Stack <double>::push (L, g);
527 luabridge::Stack <double>::push (L, b);
528 luabridge::Stack <double>::push (L, a);
533 ARDOUR::LuaAPI::build_filename (lua_State *L)
535 std::vector<std::string> elem;
536 int top = lua_gettop (L);
538 return luaL_argerror (L, 1, "invalid number of arguments, build_filename (path, ...)");
540 for (int i = 1; i <= top; ++i) {
541 int lt = lua_type (L, i);
542 if (lt != LUA_TSTRING) {
543 return luaL_argerror (L, i, "invalid argument type, expected string");
545 elem.push_back (luaL_checkstring (L, i));
548 luabridge::Stack<std::string>::push (L, Glib::build_filename (elem));
552 luabridge::LuaRef::Proxy&
553 luabridge::LuaRef::Proxy::clone_instance (const void* classkey, void* p) {
554 lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_tableRef);
555 lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_keyRef);
557 luabridge::UserdataPtr::push_raw (m_L, p, classkey);
559 lua_rawset (m_L, -3);
564 LuaTableRef::LuaTableRef () {}
565 LuaTableRef::~LuaTableRef () {}
568 LuaTableRef::get (lua_State* L)
570 luabridge::LuaRef rv (luabridge::newTable (L));
571 for (std::vector<LuaTableEntry>::const_iterator i = _data.begin (); i != _data.end (); ++i) {
572 switch ((*i).keytype) {
574 assign (&rv, i->k_s, *i);
577 assign (&rv, i->k_n, *i);
581 luabridge::push (L, rv);
586 LuaTableRef::set (lua_State* L)
588 if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); }
591 lua_pushvalue (L, -1);
593 while (lua_next (L, -2)) {
594 lua_pushvalue (L, -2);
596 LuaTableEntry s (lua_type (L, -1), lua_type (L, -2));
597 switch (lua_type (L, -1)) {
599 s.k_s = luabridge::Stack<std::string>::get (L, -1);
603 s.k_n = luabridge::Stack<unsigned int>::get (L, -1);
611 switch (lua_type (L, -2)) {
613 s.s = luabridge::Stack<std::string>::get (L, -2);
616 s.b = lua_toboolean (L, -2);
619 s.n = lua_tonumber (L, -2);
624 lua_getmetatable (L, -2);
625 lua_rawgetp (L, -1, luabridge::getIdentityKey ());
626 if (lua_isboolean (L, -1)) {
628 const void* key = lua_topointer (L, -1);
630 void const* classkey = findclasskey (L, key);
635 s.p = luabridge::Userdata::get_ptr (L, -2);
644 // invalid userdata -- fall through
647 case LUA_TFUNCTION: // no support -- we could... string.format("%q", string.dump(value, true))
648 case LUA_TTABLE: // no nested tables, sorry.
649 case LUA_TNIL: // fallthrough
663 LuaTableRef::findclasskey (lua_State *L, const void* key)
665 lua_pushvalue (L, LUA_REGISTRYINDEX);
667 while (lua_next (L, -2)) {
668 lua_pushvalue (L, -2);
669 if (lua_topointer (L, -2) == key) {
670 void* rv = lua_touserdata (L, -1);
681 void LuaTableRef::assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s)
683 switch (s.valuetype) {
694 (*rv)[key].clone_instance (s.c, s.p);
702 std::vector<std::string>
703 LuaAPI::Vamp::list_plugins ()
705 using namespace ::Vamp::HostExt;
706 PluginLoader* loader (PluginLoader::getInstance());
707 return loader->listPlugins ();
710 LuaAPI::Vamp::Vamp (const std::string& key, float sample_rate)
712 , _sample_rate (sample_rate)
715 , _initialized (false)
717 using namespace ::Vamp::HostExt;
719 PluginLoader* loader (PluginLoader::getInstance());
720 _plugin = loader->loadPlugin (key, _sample_rate, PluginLoader::ADAPT_ALL_SAFE);
723 PBD::error << string_compose (_("VAMP Plugin \"%1\" could not be loaded"), key) << endmsg;
724 throw failed_constructor ();
727 size_t bs = _plugin->getPreferredBlockSize ();
728 size_t ss = _plugin->getPreferredStepSize ();
730 if (bs > 0 && ss > 0 && bs <= 8192 && ss <= 8192) {
736 LuaAPI::Vamp::~Vamp ()
742 LuaAPI::Vamp::reset ()
744 _initialized = false;
751 LuaAPI::Vamp::initialize ()
753 if (!_plugin || _plugin->getMinChannelCount() > 1) {
756 if (!_plugin->initialise (1, _stepsize, _bufsize)) {
764 LuaAPI::Vamp::analyze (boost::shared_ptr<ARDOUR::Readable> r, uint32_t channel, luabridge::LuaRef cb)
767 if (!initialize ()) {
771 assert (_initialized);
773 ::Vamp::Plugin::FeatureSet features;
774 float* data = new float[_bufsize];
775 float* bufs[1] = { data };
777 framecnt_t len = r->readable_length();
782 framecnt_t to_read = std::min ((len - pos), _bufsize);
783 if (r->read (data, pos, to_read, channel) != to_read) {
787 if (to_read != _bufsize) {
788 memset (data + to_read, 0, (_bufsize - to_read) * sizeof (float));
791 features = _plugin->process (bufs, ::Vamp::RealTime::fromSeconds ((double) pos / _sample_rate));
793 if (cb.type () == LUA_TFUNCTION) {
797 pos += std::min (_stepsize, to_read);
808 ::Vamp::Plugin::FeatureSet
809 LuaAPI::Vamp::process (const std::vector<float*>& d, ::Vamp::RealTime rt)
811 if (!_plugin || d.size() == 0) {
812 return ::Vamp::Plugin::FeatureSet ();
814 const float* const* bufs = &d[0];
815 return _plugin->process (bufs, rt);
818 boost::shared_ptr<Evoral::Note<Evoral::Beats> >
819 LuaAPI::new_noteptr (uint8_t chan, Evoral::Beats beat_time, Evoral::Beats length, uint8_t note, uint8_t velocity)
821 return boost::shared_ptr<Evoral::Note<Evoral::Beats> > (new Evoral::Note<Evoral::Beats>(chan, beat_time, length, note, velocity));
824 std::list<boost::shared_ptr<Evoral::Note<Evoral::Beats> > >
825 LuaAPI::note_list (boost::shared_ptr<MidiModel> mm)
827 typedef typename boost::shared_ptr<Evoral::Note<Evoral::Beats> > NotePtr;
829 std::list<NotePtr> note_ptr_list;
831 const MidiModel::Notes& notes = mm->notes();
832 for (MidiModel::Notes::const_iterator i = notes.begin(); i != notes.end(); ++i) {
833 note_ptr_list.push_back (*i);
835 return note_ptr_list;