2 Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3 Copyright (C) 2006 Paul Davis
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "pbd/gstdio_compat.h"
23 #include "pbd/pthread_utils.h"
25 #include "ardour/audio_buffer.h"
26 #include "ardour/buffer_set.h"
27 #include "ardour/luabindings.h"
28 #include "ardour/luaproc.h"
29 #include "ardour/luascripting.h"
30 #include "ardour/midi_buffer.h"
31 #include "ardour/plugin.h"
32 #include "ardour/session.h"
36 using namespace ARDOUR;
39 LuaProc::LuaProc (AudioEngine& engine,
41 const std::string &script)
42 : Plugin (engine, session)
43 , _mempool ("LuaProc", 1048576) // 1 MB is plenty. (64K would be enough)
44 , lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool))
48 , _lua_does_channelmapping (false)
51 , _has_midi_input (false)
52 , _has_midi_output (false)
56 /* when loading a session, or pasing a processor,
57 * the script is set during set_state();
59 if (!_script.empty () && load_script ()) {
60 throw failed_constructor ();
64 LuaProc::LuaProc (const LuaProc &other)
66 , _mempool ("LuaProc", 1048576) // 1 MB is plenty. (64K would be enough)
67 , lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool))
70 , _script (other.script ())
71 , _lua_does_channelmapping (false)
74 , _has_midi_input (false)
75 , _has_midi_output (false)
80 throw failed_constructor ();
83 for (uint32_t i = 0; i < parameter_count (); ++i) {
84 _control_data[i] = other._shadow_data[i];
85 _shadow_data[i] = other._shadow_data[i];
89 LuaProc::~LuaProc () {
90 #ifdef WITH_LUAPROC_STATS
91 if (_info && _stats_cnt > 0) {
92 printf ("LuaProc: '%s' run() avg: %.3f max: %.3f [ms]\n",
94 0.0001f * _stats_avg[0] / (float) _stats_cnt,
95 0.0001f * _stats_max[0]);
96 printf ("LuaProc: '%s' gc() avg: %.3f max: %.3f [ms]\n",
98 0.0001f * _stats_avg[1] / (float) _stats_cnt,
99 0.0001f * _stats_max[1]);
102 lua.do_command ("collectgarbage();");
104 delete (_lua_params);
105 delete [] _control_data;
106 delete [] _shadow_data;
112 #ifdef WITH_LUAPROC_STATS
113 _stats_avg[0] = _stats_avg[1] = _stats_max[0] = _stats_max[1] = _stats_cnt = 0;
117 lua.Print.connect (sigc::mem_fun (*this, &LuaProc::lua_print));
119 // register session object
120 lua_State* L = lua.getState ();
121 LuaBindings::stddef (L);
122 LuaBindings::common (L);
123 LuaBindings::dsp (L);
125 // add session to global lua namespace
126 luabridge::push <Session *> (L, &_session);
127 lua_setglobal (L, "Session");
130 lua.do_command ("io = nil os = nil loadfile = nil require = nil dofile = nil package = nil debug = nil");
132 lua.do_command ("for n in pairs(_G) do print(n) end print ('----')"); // print global env
134 lua.do_command ("function ardour () end");
138 LuaProc::lua_print (std::string s) {
139 std::cout <<"LuaProc: " << s << "\n";
143 LuaProc::load_script ()
145 assert (!_lua_dsp); // don't allow to re-initialize
147 // TODO: refine APIs; function arguments..
148 // - perform channel-map in ardour (silent/scratch buffers) ?
149 // - control-port API (explicit get/set functions ??)
150 // - latency reporting (global var? ctrl-port? set-function ?)
151 // - MIDI -> sparse table of events
152 // { [sample] => { Event }, .. }
153 // or { { sample, Event }, .. }
156 LuaScriptInfoPtr lsi = LuaScripting::script_info (_script);
157 LuaPluginInfoPtr lpi (new LuaPluginInfo (lsi));
160 _mempool.set_name ("LuaProc: " + lsi->name);
161 _docs = lsi->description;
162 } catch (failed_constructor& err) {
166 lua_State* L = lua.getState ();
167 lua.do_command (_script);
169 // check if script has a DSP callback
170 luabridge::LuaRef lua_dsp_run = luabridge::getGlobal (L, "dsp_run");
171 luabridge::LuaRef lua_dsp_map = luabridge::getGlobal (L, "dsp_runmap");
173 if ((lua_dsp_run.type () != LUA_TFUNCTION) == (lua_dsp_map.type () != LUA_TFUNCTION)) {
177 if (lua_dsp_run.type () == LUA_TFUNCTION) {
178 _lua_dsp = new luabridge::LuaRef (lua_dsp_run);
180 else if (lua_dsp_map.type () == LUA_TFUNCTION) {
181 _lua_dsp = new luabridge::LuaRef (lua_dsp_map);
182 _lua_does_channelmapping = true;
188 // initialize the DSP if needed
189 luabridge::LuaRef lua_dsp_init = luabridge::getGlobal (L, "dsp_init");
190 if (lua_dsp_init.type () == LUA_TFUNCTION) {
192 lua_dsp_init (_session.nominal_frame_rate ());
193 } catch (luabridge::LuaException const& e) {
198 luabridge::LuaRef lua_dsp_midi_in = luabridge::getGlobal (L, "dsp_midi_input");
199 if (lua_dsp_midi_in.type () == LUA_TFUNCTION) {
201 _has_midi_input = lua_dsp_midi_in ();
202 } catch (luabridge::LuaException const& e) {
207 _ctrl_params.clear ();
209 luabridge::LuaRef lua_params = luabridge::getGlobal (L, "dsp_params");
210 if (lua_params.isFunction ()) {
212 // call function // add try {} catch (luabridge::LuaException const& e)
213 luabridge::LuaRef params = lua_params ();
215 if (params.isTable ()) {
217 _lua_params = new luabridge::LuaRef (params);
219 for (luabridge::Iterator i (params); !i.isNil (); ++i) {
221 if (!i.key ().isNumber ()) { return false; }
222 if (!i.value ().isTable ()) { return false; }
223 if (!i.value ()["type"].isString ()) { return false; }
224 if (!i.value ()["name"].isString ()) { return false; }
225 if (!i.value ()["min"].isNumber ()) { return false; }
226 if (!i.value ()["max"].isNumber ()) { return false; }
228 std::string type = i.value ()["type"].cast<std::string> ();
229 if (type == "input") {
230 if (!i.value ()["default"].isNumber ()) { return false; }
231 _ctrl_params.push_back (std::make_pair (false, i.key ().cast<int> ()));
233 else if (type == "output") {
234 _ctrl_params.push_back (std::make_pair (true, i.key ().cast<int> ()));
238 assert (i.key ().cast<int> () == (int) _ctrl_params.size ());
243 _control_data = new float[parameter_count ()];
244 _shadow_data = new float[parameter_count ()];
246 for (uint32_t i = 0; i < parameter_count (); ++i) {
247 if (parameter_is_input (i)) {
248 _control_data[i] = _shadow_data[i] = default_value (i);
252 // expose ctrl-ports to global lua namespace
253 luabridge::push <float *> (L, _control_data);
254 lua_setglobal (L, "CtrlPorts");
256 return false; // no error
260 LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out)
262 if (in.n_midi() > 0 && !_has_midi_input) {
266 lua_State* L = lua.getState ();
267 luabridge::LuaRef ioconfig = luabridge::getGlobal (L, "dsp_ioconfig");
268 if (!ioconfig.isFunction ()) {
272 luabridge::LuaRef table = luabridge::getGlobal (L, "table"); //lua std lib
273 luabridge::LuaRef tablesort = table["sort"];
274 assert (tablesort.isFunction ());
276 luabridge::LuaRef *_iotable = NULL; // can't use reference :(
278 luabridge::LuaRef iotable = ioconfig ();
280 if (iotable.isTable ()) {
281 _iotable = new luabridge::LuaRef (iotable);
283 } catch (luabridge::LuaException const& e) {
291 // now we can reference it.
292 luabridge::LuaRef iotable (*_iotable);
295 if ((iotable).length () < 1) {
299 int32_t audio_in = in.n_audio ();
302 if (in.n_midi() > 0 && audio_in == 0) {
303 audio_out = 2; // prefer stereo version if available.
305 audio_out = audio_in;
308 for (luabridge::Iterator i (iotable); !i.isNil (); ++i) {
309 assert (i.value ().type () == LUA_TTABLE);
310 luabridge::LuaRef io (i.value ());
312 int possible_in = io["audio_in"];
313 int possible_out = io["audio_out"];
316 if ((possible_in == audio_in) && (possible_out == audio_out)) {
317 out.set (DataType::MIDI, 0);
318 out.set (DataType::AUDIO, audio_out);
323 /* now allow potentially "imprecise" matches */
327 for (luabridge::Iterator i (iotable); !i.isNil (); ++i) {
328 assert (i.value ().type () == LUA_TTABLE);
329 luabridge::LuaRef io (i.value ());
331 int possible_in = io["audio_in"];
332 int possible_out = io["audio_out"];
334 if (possible_out == 0) {
337 if (possible_in == 0) {
338 /* no inputs, generators & instruments */
339 if (possible_out == -1) {
340 /* any configuration possible, provide stereo output */
343 } else if (possible_out == -2) {
344 /* invalid, should be (0, -1) */
347 } else if (possible_out < -2) {
348 /* variable number of outputs. -> whatever */
352 /* exact number of outputs */
353 audio_out = possible_out;
358 if (possible_in == -1) {
359 /* wildcard for input */
360 if (possible_out == -1) {
361 /* out much match in */
362 audio_out = audio_in;
364 } else if (possible_out == -2) {
365 /* any configuration possible, pick matching */
366 audio_out = audio_in;
368 } else if (possible_out < -2) {
369 /* explicitly variable number of outputs, pick maximum */
370 audio_out = -possible_out;
373 /* exact number of outputs */
374 audio_out = possible_out;
379 if (possible_in == -2) {
381 if (possible_out == -1) {
382 /* any configuration possible, pick matching */
383 audio_out = audio_in;
385 } else if (possible_out == -2) {
386 /* invalid. interpret as (-1, -1) */
387 audio_out = audio_in;
389 } else if (possible_out < -2) {
390 /* explicitly variable number of outputs, pick maximum */
391 audio_out = -possible_out;
394 /* exact number of outputs */
395 audio_out = possible_out;
400 if (possible_in < -2) {
401 /* explicit variable number of inputs */
402 if (audio_in > -possible_in) {
403 /* request is too large */
405 if (possible_out == -1) {
406 /* any output configuration possible, provide stereo out */
409 } else if (possible_out == -2) {
410 /* invalid. interpret as (<-2, -1) */
413 } else if (possible_out < -2) {
414 /* explicitly variable number of outputs, pick stereo */
418 /* exact number of outputs */
419 audio_out = possible_out;
424 if (possible_in && (possible_in == audio_in)) {
425 /* exact number of inputs ... must match obviously */
426 if (possible_out == -1) {
427 /* any output configuration possible, provide stereo output */
430 } else if (possible_out == -2) {
431 /* invalid. interpret as (>0, -1) */
434 } else if (possible_out < -2) {
435 /* explicitly variable number of outputs, pick maximum */
436 audio_out = -possible_out;
439 /* exact number of outputs */
440 audio_out = possible_out;
454 out.set (DataType::MIDI, 0);
455 out.set (DataType::AUDIO, audio_out);
460 LuaProc::configure_io (ChanCount in, ChanCount out)
463 _configured_out = out;
465 _configured_in.set (DataType::MIDI, _has_midi_input ? 1 : 0);
466 _configured_out.set (DataType::MIDI, _has_midi_output ? 1 : 0);
468 // configure the DSP if needed
469 lua_State* L = lua.getState ();
470 luabridge::LuaRef lua_dsp_configure = luabridge::getGlobal (L, "dsp_configure");
471 if (lua_dsp_configure.type () == LUA_TFUNCTION) {
473 lua_dsp_configure (&in, &out);
474 } catch (luabridge::LuaException const& e) {
479 _info->n_inputs = _configured_in;
480 _info->n_outputs = _configured_out;
485 LuaProc::connect_and_run (BufferSet& bufs,
486 ChanMapping in, ChanMapping out,
487 pframes_t nframes, framecnt_t offset)
493 Plugin::connect_and_run (bufs, in, out, nframes, offset);
495 // This is needed for ARDOUR::Session requests :(
496 if (! SessionEvent::has_per_thread_pool ()) {
498 snprintf (name, 64, "Proc-%p", this);
499 pthread_set_name (name);
500 SessionEvent::create_per_thread_pool (name, 64);
501 PBD::notify_event_loops_about_thread_creation (pthread_self(), name, 64);
504 uint32_t const n = parameter_count ();
505 for (uint32_t i = 0; i < n; ++i) {
506 if (parameter_is_control (i) && parameter_is_input (i)) {
507 _control_data[i] = _shadow_data[i];
511 #ifdef WITH_LUAPROC_STATS
512 int64_t t0 = g_get_monotonic_time ();
516 if (_lua_does_channelmapping) {
517 // run the DSP function
518 (*_lua_dsp)(&bufs, in, out, nframes, offset);
521 BufferSet& silent_bufs = _session.get_silent_buffers (ChanCount (DataType::AUDIO, 1));
522 BufferSet& scratch_bufs = _session.get_scratch_buffers (ChanCount (DataType::AUDIO, 1));
524 lua_State* L = lua.getState ();
525 luabridge::LuaRef in_map (luabridge::newTable (L));
526 luabridge::LuaRef out_map (luabridge::newTable (L));
528 const uint32_t audio_in = _configured_in.n_audio ();
529 const uint32_t audio_out = _configured_out.n_audio ();
530 const uint32_t midi_in = _configured_in.n_midi ();
532 for (uint32_t ap = 0; ap < audio_in; ++ap) {
534 const uint32_t buf_index = in.get(DataType::AUDIO, ap, &valid);
536 in_map[ap + 1] = bufs.get_audio (buf_index).data (offset);
538 in_map[ap + 1] = silent_bufs.get_audio (0).data (offset);
541 for (uint32_t ap = 0; ap < audio_out; ++ap) {
543 const uint32_t buf_index = out.get(DataType::AUDIO, ap, &valid);
545 out_map[ap + 1] = bufs.get_audio (buf_index).data (offset);
547 out_map[ap + 1] = scratch_bufs.get_audio (0).data (offset);
551 luabridge::LuaRef lua_midi_tbl (luabridge::newTable (L));
552 int e = 1; // > 1 port, we merge events (unsorted)
553 for (uint32_t mp = 0; mp < midi_in; ++mp) {
555 const uint32_t idx = in.get(DataType::MIDI, mp, &valid);
557 for (MidiBuffer::iterator m = bufs.get_midi(idx).begin();
558 m != bufs.get_midi(idx).end(); ++m, ++e) {
559 const Evoral::MIDIEvent<framepos_t> ev(*m, false);
560 luabridge::LuaRef lua_midi_data (luabridge::newTable (L));
561 const uint8_t* data = ev.buffer();
562 for (uint32_t i = 0; i < ev.size(); ++i) {
563 lua_midi_data [i + 1] = data[i];
565 luabridge::LuaRef lua_midi_event (luabridge::newTable (L));
566 lua_midi_event["time"] = 1 + (*m).time();
567 lua_midi_event["data"] = lua_midi_data;
568 lua_midi_tbl[e] = lua_midi_event;
573 if (_has_midi_input) {
574 // XXX TODO This needs a better solution than global namespace
575 luabridge::push (L, lua_midi_tbl);
576 lua_setglobal (L, "mididata");
580 // run the DSP function
581 (*_lua_dsp)(in_map, out_map, nframes);
583 } catch (luabridge::LuaException const& e) {
585 printf ("LuaException: %s\n", e.what ());
589 #ifdef WITH_LUAPROC_STATS
590 int64_t t1 = g_get_monotonic_time ();
592 lua.collect_garbage (); // rt-safe, slight *regular* performance overhead
593 #ifdef WITH_LUAPROC_STATS
595 int64_t t2 = g_get_monotonic_time ();
596 int64_t ela0 = t1 - t0;
597 int64_t ela1 = t2 - t1;
598 if (ela0 > _stats_max[0]) _stats_max[0] = ela0;
599 if (ela1 > _stats_max[1]) _stats_max[1] = ela1;
600 _stats_avg[0] += ela0;
601 _stats_avg[1] += ela1;
608 LuaProc::add_state (XMLNode* root) const
610 gchar* b64 = g_base64_encode ((const guchar*)_script.c_str (), _script.size ());
611 std::string b64s (b64);
613 XMLNode* script_node = new XMLNode (X_("script"));
614 script_node->add_property (X_("lua"), LUA_VERSION);
615 script_node->add_content (b64s);
616 root->add_child_nocopy (*script_node);
620 LuaProc::set_script_from_state (const XMLNode& node)
623 if (node.name () != state_node_name ()) {
627 if ((child = node.child (X_("script"))) != 0) {
628 for (XMLNodeList::const_iterator n = child->children ().begin (); n != child->children ().end (); ++n) {
629 if (!(*n)->is_content ()) { continue; }
631 guchar* buf = g_base64_decode ((*n)->content ().c_str (), &size);
632 _script = std::string ((const char*)buf, size);
634 if (load_script ()) {
635 PBD::error << _("Failed to load Lua script from session state.") << endmsg;
637 std::cerr << "Failed Lua Script: " << _script << std::endl;
644 if (_script.empty ()) {
645 PBD::error << _("Session State for LuaProcessor did not include a Lua script.") << endmsg;
649 PBD::error << _("Invalid/incompatible Lua script found for LuaProcessor.") << endmsg;
656 LuaProc::set_state (const XMLNode& node, int version)
658 if (_script.empty ()) {
659 if (set_script_from_state (node)) {
663 return Plugin::set_state (node, version);
667 LuaProc::parameter_count () const
669 return _ctrl_params.size ();
673 LuaProc::default_value (uint32_t port)
675 if (_ctrl_params[port].first) {
679 int lp = _ctrl_params[port].second;
680 luabridge::LuaRef lr = (*_lua_params)[lp];
681 return (lr["default"]).cast<float> ();
685 LuaProc::set_parameter (uint32_t port, float val)
687 assert (port < parameter_count ());
688 if (get_parameter (port) == val) {
691 _shadow_data[port] = val;
692 Plugin::set_parameter (port, val);
696 LuaProc::get_parameter (uint32_t port) const
698 if (parameter_is_input (port)) {
699 return _shadow_data[port];
701 return _control_data[port];
706 LuaProc::get_parameter_descriptor (uint32_t port, ParameterDescriptor& desc) const
708 assert (port <= parameter_count ());
709 int lp = _ctrl_params[port].second;
711 luabridge::LuaRef lr = (*_lua_params)[lp];
712 desc.lower = (lr["min"]).cast<float> ();
713 desc.upper = (lr["max"]).cast<float> ();
715 if (_ctrl_params[port].first) {
716 desc.normal = desc.lower; // output-port, no default
718 desc.normal = (lr["default"]).cast<float> ();
721 desc.toggled = lr["toggled"].isBoolean () && (lr["toggled"]).cast<bool> ();
722 desc.logarithmic = lr["logarithmic"].isBoolean () && (lr["logarithmic"]).cast<bool> ();
723 desc.integer_step = lr["integer"].isBoolean () && (lr["integer"]).cast<bool> ();
724 desc.sr_dependent = lr["ratemult"].isBoolean () && (lr["ratemult"]).cast<bool> ();
725 desc.enumeration = lr["enum"].isBoolean () && (lr["enum"]).cast<bool> ();
727 // TODO check if assignments make sense, e.g
728 assert (!(desc.toggled && desc.logarithmic));
730 if (lr["unit"].isString ()) {
731 std::string unit = lr["unit"].cast<std::string> ();
732 if (unit == "dB") { desc.unit = ParameterDescriptor::DB; }
733 else if (unit == "Hz") { desc.unit = ParameterDescriptor::HZ; }
734 else if (unit == "Midi Note") { desc.unit = ParameterDescriptor::MIDI_NOTE; }
737 desc.label = (lr["name"]).cast<std::string> ();
738 desc.scale_points = get_scale_points (port);
739 desc.update_steps ();
744 LuaProc::get_parameter_docs (uint32_t port) const {
745 assert (port <= parameter_count ());
746 int lp = _ctrl_params[port].second;
747 luabridge::LuaRef lr = (*_lua_params)[lp];
748 luabridge::LuaRef doc = lr["doc"];
749 if (doc.isString ()) {
750 return doc.cast<std::string> ();
756 LuaProc::nth_parameter (uint32_t port, bool& ok) const
758 if (port < _ctrl_params.size ()) {
767 LuaProc::parameter_is_input (uint32_t port) const
769 assert (port < _ctrl_params.size ());
770 return (!_ctrl_params[port].first);
774 LuaProc::parameter_is_output (uint32_t port) const
776 assert (port < _ctrl_params.size ());
777 return (_ctrl_params[port].first);
780 std::set<Evoral::Parameter>
781 LuaProc::automatable () const
783 std::set<Evoral::Parameter> automatables;
784 for (uint32_t i = 0; i < _ctrl_params.size (); ++i) {
785 if (parameter_is_input (i)) {
786 automatables.insert (automatables.end (), Evoral::Parameter (PluginAutomation, 0, i));
793 LuaProc::describe_parameter (Evoral::Parameter param)
795 if (param.type () == PluginAutomation && param.id () < parameter_count ()) {
796 int lp = _ctrl_params[param.id ()].second;
797 luabridge::LuaRef lr = (*_lua_params)[lp];
798 return (lr["name"]).cast<std::string> ();
805 LuaProc::print_parameter (uint32_t param, char* buf, uint32_t len) const
808 if (param < parameter_count ()) {
809 snprintf (buf, len, "%.3f", get_parameter (param));
816 boost::shared_ptr<ScalePoints>
817 LuaProc::get_scale_points (uint32_t port) const
819 int lp = _ctrl_params[port].second;
820 luabridge::LuaRef lr = (*_lua_params)[lp];
822 if (!lr["scalepoints"].isTable()) {
823 return boost::shared_ptr<ScalePoints> ();
827 boost::shared_ptr<ScalePoints> rv = boost::shared_ptr<ScalePoints>(new ScalePoints());
828 luabridge::LuaRef scalepoints (lr["scalepoints"]);
830 for (luabridge::Iterator i (scalepoints); !i.isNil (); ++i) {
831 if (!i.key ().isString ()) { continue; }
832 if (!i.value ().isNumber ()) { continue; }
833 rv->insert(make_pair(i.key ().cast<std::string> (),
834 i.value ().cast<float> ()));
838 if (rv->size() > 0) {
841 return boost::shared_ptr<ScalePoints> ();
845 ////////////////////////////////////////////////////////////////////////////////
846 #include <glibmm/miscutils.h>
847 #include <glibmm/fileutils.h>
849 LuaPluginInfo::LuaPluginInfo (LuaScriptInfoPtr lsi) {
850 if (lsi->type != LuaScriptInfo::DSP) {
851 throw failed_constructor ();
856 creator = lsi->author;
857 category = lsi->category;
858 unique_id = "luascript"; // the interpreter is not unique.
860 n_inputs.set (DataType::AUDIO, 1);
861 n_outputs.set (DataType::AUDIO, 1);
866 LuaPluginInfo::load (Session& session)
868 std::string script = "";
869 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
874 script = Glib::file_get_contents (path);
875 } catch (Glib::FileError err) {
879 if (script.empty ()) {
884 PluginPtr plugin (new LuaProc (session.engine (), session, script));
886 } catch (failed_constructor& err) {
892 std::vector<Plugin::PresetRecord>
893 LuaPluginInfo::get_presets (bool /*user_only*/) const
895 std::vector<Plugin::PresetRecord> p;