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"
34 #include "LuaBridge/LuaBridge.h"
38 using namespace ARDOUR;
41 LuaProc::LuaProc (AudioEngine& engine,
43 const std::string &script)
44 : Plugin (engine, session)
45 , _mempool ("LuaProc", 1048576) // 1 MB is plenty. (64K would be enough)
46 , lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool))
49 , _lua_does_channelmapping (false)
52 , _has_midi_input (false)
53 , _has_midi_output (false)
57 /* when loading a session, or pasing a processor,
58 * the script is set during set_state();
60 if (!_script.empty () && load_script ()) {
61 throw failed_constructor ();
65 LuaProc::LuaProc (const LuaProc &other)
67 , _mempool ("LuaProc", 1048576) // 1 MB is plenty. (64K would be enough)
68 , 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 [] _control_data;
105 delete [] _shadow_data;
111 #ifdef WITH_LUAPROC_STATS
112 _stats_avg[0] = _stats_avg[1] = _stats_max[0] = _stats_max[1] = _stats_cnt = 0;
116 lua.Print.connect (sigc::mem_fun (*this, &LuaProc::lua_print));
118 // register session object
119 lua_State* L = lua.getState ();
120 LuaBindings::stddef (L);
121 LuaBindings::common (L);
122 LuaBindings::dsp (L);
124 luabridge::getGlobalNamespace (L)
125 .beginNamespace ("Ardour")
126 .beginClass <LuaProc> ("LuaProc")
127 .addFunction ("queue_draw", &LuaProc::queue_draw)
128 .addFunction ("shmem", &LuaProc::instance_shm)
132 // add session to global lua namespace
133 luabridge::push <Session *> (L, &_session);
134 lua_setglobal (L, "Session");
137 luabridge::push <LuaProc *> (L, this);
138 lua_setglobal (L, "self");
141 lua.do_command ("io = nil os = nil loadfile = nil require = nil dofile = nil package = nil debug = nil");
143 lua.do_command ("for n in pairs(_G) do print(n) end print ('----')"); // print global env
145 lua.do_command ("function ardour () end");
149 LuaProc::lua_print (std::string s) {
150 std::cout <<"LuaProc: " << s << "\n";
154 LuaProc::load_script ()
156 assert (!_lua_dsp); // don't allow to re-initialize
158 // TODO: refine APIs; function arguments..
159 // - perform channel-map in ardour (silent/scratch buffers) ?
160 // - control-port API (explicit get/set functions ??)
161 // - latency reporting (global var? ctrl-port? set-function ?)
162 // - MIDI -> sparse table of events
163 // { [sample] => { Event }, .. }
164 // or { { sample, Event }, .. }
167 LuaScriptInfoPtr lsi = LuaScripting::script_info (_script);
168 LuaPluginInfoPtr lpi (new LuaPluginInfo (lsi));
171 _mempool.set_name ("LuaProc: " + lsi->name);
172 _docs = lsi->description;
173 } catch (failed_constructor& err) {
177 lua_State* L = lua.getState ();
178 lua.do_command (_script);
180 // check if script has a DSP callback
181 luabridge::LuaRef lua_dsp_run = luabridge::getGlobal (L, "dsp_run");
182 luabridge::LuaRef lua_dsp_map = luabridge::getGlobal (L, "dsp_runmap");
184 if ((lua_dsp_run.type () != LUA_TFUNCTION) == (lua_dsp_map.type () != LUA_TFUNCTION)) {
188 if (lua_dsp_run.type () == LUA_TFUNCTION) {
189 _lua_dsp = new luabridge::LuaRef (lua_dsp_run);
191 else if (lua_dsp_map.type () == LUA_TFUNCTION) {
192 _lua_dsp = new luabridge::LuaRef (lua_dsp_map);
193 _lua_does_channelmapping = true;
199 // initialize the DSP if needed
200 luabridge::LuaRef lua_dsp_init = luabridge::getGlobal (L, "dsp_init");
201 if (lua_dsp_init.type () == LUA_TFUNCTION) {
203 lua_dsp_init (_session.nominal_frame_rate ());
204 } catch (luabridge::LuaException const& e) {
209 luabridge::LuaRef lua_dsp_midi_in = luabridge::getGlobal (L, "dsp_midi_input");
210 if (lua_dsp_midi_in.type () == LUA_TFUNCTION) {
212 _has_midi_input = lua_dsp_midi_in ();
213 } catch (luabridge::LuaException const& e) {
218 _ctrl_params.clear ();
220 luabridge::LuaRef lua_render = luabridge::getGlobal (L, "render_inline");
221 if (lua_render.isFunction ()) {
222 _lua_has_inline_display = true;
225 luabridge::LuaRef lua_params = luabridge::getGlobal (L, "dsp_params");
226 if (lua_params.isFunction ()) {
228 // call function // add try {} catch (luabridge::LuaException const& e)
229 luabridge::LuaRef params = lua_params ();
231 if (params.isTable ()) {
233 for (luabridge::Iterator i (params); !i.isNil (); ++i) {
235 if (!i.key ().isNumber ()) { return false; }
236 if (!i.value ().isTable ()) { return false; }
237 if (!i.value ()["type"].isString ()) { return false; }
238 if (!i.value ()["name"].isString ()) { return false; }
239 if (!i.value ()["min"].isNumber ()) { return false; }
240 if (!i.value ()["max"].isNumber ()) { return false; }
242 int pn = i.key ().cast<int> ();
243 std::string type = i.value ()["type"].cast<std::string> ();
244 if (type == "input") {
245 if (!i.value ()["default"].isNumber ()) { return false; }
246 _ctrl_params.push_back (std::make_pair (false, pn));
248 else if (type == "output") {
249 _ctrl_params.push_back (std::make_pair (true, pn));
253 assert (pn == (int) _ctrl_params.size ());
255 //_param_desc[pn] = boost::shared_ptr<ParameterDescriptor> (new ParameterDescriptor());
256 luabridge::LuaRef lr = i.value ();
258 if (type == "input") {
259 _param_desc[pn].normal = lr["default"].cast<float> ();
261 _param_desc[pn].normal = lr["min"].cast<float> (); // output-port, no default
263 _param_desc[pn].lower = lr["min"].cast<float> ();
264 _param_desc[pn].upper = lr["max"].cast<float> ();
265 _param_desc[pn].toggled = lr["toggled"].isBoolean () && (lr["toggled"]).cast<bool> ();
266 _param_desc[pn].logarithmic = lr["logarithmic"].isBoolean () && (lr["logarithmic"]).cast<bool> ();
267 _param_desc[pn].integer_step = lr["integer"].isBoolean () && (lr["integer"]).cast<bool> ();
268 _param_desc[pn].sr_dependent = lr["ratemult"].isBoolean () && (lr["ratemult"]).cast<bool> ();
269 _param_desc[pn].enumeration = lr["enum"].isBoolean () && (lr["enum"]).cast<bool> ();
271 if (lr["unit"].isString ()) {
272 std::string unit = lr["unit"].cast<std::string> ();
273 if (unit == "dB") { _param_desc[pn].unit = ParameterDescriptor::DB; }
274 else if (unit == "Hz") { _param_desc[pn].unit = ParameterDescriptor::HZ; }
275 else if (unit == "Midi Note") { _param_desc[pn].unit = ParameterDescriptor::MIDI_NOTE; }
277 _param_desc[pn].label = (lr["name"]).cast<std::string> ();
278 _param_desc[pn].scale_points = parse_scale_points (&lr);
280 luabridge::LuaRef doc = lr["doc"];
281 if (doc.isString ()) {
282 _param_doc[pn] = doc.cast<std::string> ();
286 assert (!(_param_desc[pn].toggled && _param_desc[pn].logarithmic));
291 _control_data = new float[parameter_count ()];
292 _shadow_data = new float[parameter_count ()];
294 for (uint32_t i = 0; i < parameter_count (); ++i) {
295 if (parameter_is_input (i)) {
296 _control_data[i] = _shadow_data[i] = default_value (i);
300 // expose ctrl-ports to global lua namespace
301 luabridge::push <float *> (L, _control_data);
302 lua_setglobal (L, "CtrlPorts");
304 return false; // no error
308 LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out)
310 if (in.n_midi() > 0 && !_has_midi_input) {
314 lua_State* L = lua.getState ();
315 luabridge::LuaRef ioconfig = luabridge::getGlobal (L, "dsp_ioconfig");
316 if (!ioconfig.isFunction ()) {
320 luabridge::LuaRef table = luabridge::getGlobal (L, "table"); //lua std lib
321 luabridge::LuaRef tablesort = table["sort"];
322 assert (tablesort.isFunction ());
324 luabridge::LuaRef *_iotable = NULL; // can't use reference :(
326 luabridge::LuaRef iotable = ioconfig ();
328 if (iotable.isTable ()) {
329 _iotable = new luabridge::LuaRef (iotable);
331 } catch (luabridge::LuaException const& e) {
339 // now we can reference it.
340 luabridge::LuaRef iotable (*_iotable);
343 if ((iotable).length () < 1) {
347 int32_t audio_in = in.n_audio ();
350 if (in.n_midi() > 0 && audio_in == 0) {
351 audio_out = 2; // prefer stereo version if available.
353 audio_out = audio_in;
356 for (luabridge::Iterator i (iotable); !i.isNil (); ++i) {
357 assert (i.value ().type () == LUA_TTABLE);
358 luabridge::LuaRef io (i.value ());
360 int possible_in = io["audio_in"];
361 int possible_out = io["audio_out"];
364 if ((possible_in == audio_in) && (possible_out == audio_out)) {
365 out.set (DataType::MIDI, 0);
366 out.set (DataType::AUDIO, audio_out);
371 /* now allow potentially "imprecise" matches */
375 for (luabridge::Iterator i (iotable); !i.isNil (); ++i) {
376 assert (i.value ().type () == LUA_TTABLE);
377 luabridge::LuaRef io (i.value ());
379 int possible_in = io["audio_in"];
380 int possible_out = io["audio_out"];
382 if (possible_out == 0) {
385 if (possible_in == 0) {
386 /* no inputs, generators & instruments */
387 if (possible_out == -1) {
388 /* any configuration possible, provide stereo output */
391 } else if (possible_out == -2) {
392 /* invalid, should be (0, -1) */
395 } else if (possible_out < -2) {
396 /* variable number of outputs. -> whatever */
400 /* exact number of outputs */
401 audio_out = possible_out;
406 if (possible_in == -1) {
407 /* wildcard for input */
408 if (possible_out == -1) {
409 /* out much match in */
410 audio_out = audio_in;
412 } else if (possible_out == -2) {
413 /* any configuration possible, pick matching */
414 audio_out = audio_in;
416 } else if (possible_out < -2) {
417 /* explicitly variable number of outputs, pick maximum */
418 audio_out = -possible_out;
421 /* exact number of outputs */
422 audio_out = possible_out;
427 if (possible_in == -2) {
429 if (possible_out == -1) {
430 /* any configuration possible, pick matching */
431 audio_out = audio_in;
433 } else if (possible_out == -2) {
434 /* invalid. interpret as (-1, -1) */
435 audio_out = audio_in;
437 } else if (possible_out < -2) {
438 /* explicitly variable number of outputs, pick maximum */
439 audio_out = -possible_out;
442 /* exact number of outputs */
443 audio_out = possible_out;
448 if (possible_in < -2) {
449 /* explicit variable number of inputs */
450 if (audio_in > -possible_in) {
451 /* request is too large */
453 if (possible_out == -1) {
454 /* any output configuration possible, provide stereo out */
457 } else if (possible_out == -2) {
458 /* invalid. interpret as (<-2, -1) */
461 } else if (possible_out < -2) {
462 /* explicitly variable number of outputs, pick stereo */
466 /* exact number of outputs */
467 audio_out = possible_out;
472 if (possible_in && (possible_in == audio_in)) {
473 /* exact number of inputs ... must match obviously */
474 if (possible_out == -1) {
475 /* any output configuration possible, provide stereo output */
478 } else if (possible_out == -2) {
479 /* invalid. interpret as (>0, -1) */
482 } else if (possible_out < -2) {
483 /* explicitly variable number of outputs, pick maximum */
484 audio_out = -possible_out;
487 /* exact number of outputs */
488 audio_out = possible_out;
502 out.set (DataType::MIDI, 0);
503 out.set (DataType::AUDIO, audio_out);
508 LuaProc::configure_io (ChanCount in, ChanCount out)
511 _configured_out = out;
513 _configured_in.set (DataType::MIDI, _has_midi_input ? 1 : 0);
514 _configured_out.set (DataType::MIDI, _has_midi_output ? 1 : 0);
516 // configure the DSP if needed
517 lua_State* L = lua.getState ();
518 luabridge::LuaRef lua_dsp_configure = luabridge::getGlobal (L, "dsp_configure");
519 if (lua_dsp_configure.type () == LUA_TFUNCTION) {
521 lua_dsp_configure (&in, &out);
522 } catch (luabridge::LuaException const& e) {
527 _info->n_inputs = _configured_in;
528 _info->n_outputs = _configured_out;
533 LuaProc::connect_and_run (BufferSet& bufs,
534 ChanMapping in, ChanMapping out,
535 pframes_t nframes, framecnt_t offset)
541 Plugin::connect_and_run (bufs, in, out, nframes, offset);
543 // This is needed for ARDOUR::Session requests :(
544 if (! SessionEvent::has_per_thread_pool ()) {
546 snprintf (name, 64, "Proc-%p", this);
547 pthread_set_name (name);
548 SessionEvent::create_per_thread_pool (name, 64);
549 PBD::notify_event_loops_about_thread_creation (pthread_self(), name, 64);
552 uint32_t const n = parameter_count ();
553 for (uint32_t i = 0; i < n; ++i) {
554 if (parameter_is_control (i) && parameter_is_input (i)) {
555 _control_data[i] = _shadow_data[i];
559 #ifdef WITH_LUAPROC_STATS
560 int64_t t0 = g_get_monotonic_time ();
564 if (_lua_does_channelmapping) {
565 // run the DSP function
566 (*_lua_dsp)(&bufs, in, out, nframes, offset);
569 BufferSet& silent_bufs = _session.get_silent_buffers (ChanCount (DataType::AUDIO, 1));
570 BufferSet& scratch_bufs = _session.get_scratch_buffers (ChanCount (DataType::AUDIO, 1));
572 lua_State* L = lua.getState ();
573 luabridge::LuaRef in_map (luabridge::newTable (L));
574 luabridge::LuaRef out_map (luabridge::newTable (L));
576 const uint32_t audio_in = _configured_in.n_audio ();
577 const uint32_t audio_out = _configured_out.n_audio ();
578 const uint32_t midi_in = _configured_in.n_midi ();
580 for (uint32_t ap = 0; ap < audio_in; ++ap) {
582 const uint32_t buf_index = in.get(DataType::AUDIO, ap, &valid);
584 in_map[ap + 1] = bufs.get_audio (buf_index).data (offset);
586 in_map[ap + 1] = silent_bufs.get_audio (0).data (offset);
589 for (uint32_t ap = 0; ap < audio_out; ++ap) {
591 const uint32_t buf_index = out.get(DataType::AUDIO, ap, &valid);
593 out_map[ap + 1] = bufs.get_audio (buf_index).data (offset);
595 out_map[ap + 1] = scratch_bufs.get_audio (0).data (offset);
599 luabridge::LuaRef lua_midi_tbl (luabridge::newTable (L));
600 int e = 1; // > 1 port, we merge events (unsorted)
601 for (uint32_t mp = 0; mp < midi_in; ++mp) {
603 const uint32_t idx = in.get(DataType::MIDI, mp, &valid);
605 for (MidiBuffer::iterator m = bufs.get_midi(idx).begin();
606 m != bufs.get_midi(idx).end(); ++m, ++e) {
607 const Evoral::MIDIEvent<framepos_t> ev(*m, false);
608 luabridge::LuaRef lua_midi_data (luabridge::newTable (L));
609 const uint8_t* data = ev.buffer();
610 for (uint32_t i = 0; i < ev.size(); ++i) {
611 lua_midi_data [i + 1] = data[i];
613 luabridge::LuaRef lua_midi_event (luabridge::newTable (L));
614 lua_midi_event["time"] = 1 + (*m).time();
615 lua_midi_event["data"] = lua_midi_data;
616 lua_midi_tbl[e] = lua_midi_event;
621 if (_has_midi_input) {
622 // XXX TODO This needs a better solution than global namespace
623 luabridge::push (L, lua_midi_tbl);
624 lua_setglobal (L, "mididata");
628 // run the DSP function
629 (*_lua_dsp)(in_map, out_map, nframes);
631 } catch (luabridge::LuaException const& e) {
633 printf ("LuaException: %s\n", e.what ());
637 #ifdef WITH_LUAPROC_STATS
638 int64_t t1 = g_get_monotonic_time ();
640 lua.collect_garbage (); // rt-safe, slight *regular* performance overhead
641 #ifdef WITH_LUAPROC_STATS
643 int64_t t2 = g_get_monotonic_time ();
644 int64_t ela0 = t1 - t0;
645 int64_t ela1 = t2 - t1;
646 if (ela0 > _stats_max[0]) _stats_max[0] = ela0;
647 if (ela1 > _stats_max[1]) _stats_max[1] = ela1;
648 _stats_avg[0] += ela0;
649 _stats_avg[1] += ela1;
656 LuaProc::add_state (XMLNode* root) const
660 LocaleGuard lg(X_("C"));
662 gchar* b64 = g_base64_encode ((const guchar*)_script.c_str (), _script.size ());
663 std::string b64s (b64);
665 XMLNode* script_node = new XMLNode (X_("script"));
666 script_node->add_property (X_("lua"), LUA_VERSION);
667 script_node->add_content (b64s);
668 root->add_child_nocopy (*script_node);
670 for (uint32_t i = 0; i < parameter_count(); ++i) {
671 if (parameter_is_input(i) && parameter_is_control(i)) {
672 child = new XMLNode("Port");
673 snprintf(buf, sizeof(buf), "%u", i);
674 child->add_property("id", std::string(buf));
675 snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
676 child->add_property("value", std::string(buf));
677 root->add_child_nocopy(*child);
683 LuaProc::set_script_from_state (const XMLNode& node)
686 if (node.name () != state_node_name ()) {
690 if ((child = node.child (X_("script"))) != 0) {
691 for (XMLNodeList::const_iterator n = child->children ().begin (); n != child->children ().end (); ++n) {
692 if (!(*n)->is_content ()) { continue; }
694 guchar* buf = g_base64_decode ((*n)->content ().c_str (), &size);
695 _script = std::string ((const char*)buf, size);
697 if (load_script ()) {
698 PBD::error << _("Failed to load Lua script from session state.") << endmsg;
700 std::cerr << "Failed Lua Script: " << _script << std::endl;
707 if (_script.empty ()) {
708 PBD::error << _("Session State for LuaProcessor did not include a Lua script.") << endmsg;
712 PBD::error << _("Invalid/incompatible Lua script found for LuaProcessor.") << endmsg;
719 LuaProc::set_state (const XMLNode& node, int version)
721 #ifndef NO_PLUGIN_STATE
724 XMLNodeConstIterator iter;
730 LocaleGuard lg (X_("C"));
732 if (_script.empty ()) {
733 if (set_script_from_state (node)) {
738 #ifndef NO_PLUGIN_STATE
739 if (node.name() != state_node_name()) {
740 error << _("Bad node sent to LuaProc::set_state") << endmsg;
744 nodes = node.children ("Port");
745 for (iter = nodes.begin(); iter != nodes.end(); ++iter) {
747 if ((prop = child->property("id")) != 0) {
748 port = prop->value().c_str();
750 warning << _("LuaProc: port has no symbol, ignored") << endmsg;
753 if ((prop = child->property("value")) != 0) {
754 value = prop->value().c_str();
756 warning << _("LuaProc: port has no value, ignored") << endmsg;
759 sscanf (port, "%" PRIu32, &port_id);
760 set_parameter (port_id, atof(value));
764 return Plugin::set_state (node, version);
768 LuaProc::parameter_count () const
770 return _ctrl_params.size ();
774 LuaProc::default_value (uint32_t port)
776 if (_ctrl_params[port].first) {
780 int lp = _ctrl_params[port].second;
781 return _param_desc[lp].normal;
785 LuaProc::set_parameter (uint32_t port, float val)
787 assert (port < parameter_count ());
788 if (get_parameter (port) == val) {
791 _shadow_data[port] = val;
792 Plugin::set_parameter (port, val);
796 LuaProc::get_parameter (uint32_t port) const
798 if (parameter_is_input (port)) {
799 return _shadow_data[port];
801 return _control_data[port];
806 LuaProc::get_parameter_descriptor (uint32_t port, ParameterDescriptor& desc) const
808 assert (port <= parameter_count ());
809 int lp = _ctrl_params[port].second;
810 const ParameterDescriptor& d (_param_desc.find(lp)->second);
812 desc.lower = d.lower;
813 desc.upper = d.upper;
814 desc.normal = d.normal;
815 desc.toggled = d.toggled;
816 desc.logarithmic = d.logarithmic;
817 desc.integer_step = d.integer_step;
818 desc.sr_dependent = d.sr_dependent;
819 desc.enumeration = d.enumeration;
821 desc.label = d.label;
822 desc.scale_points = d.scale_points;
824 desc.update_steps ();
829 LuaProc::get_parameter_docs (uint32_t port) const {
830 assert (port <= parameter_count ());
831 int lp = _ctrl_params[port].second;
832 return _param_doc.find(lp)->second;
836 LuaProc::nth_parameter (uint32_t port, bool& ok) const
838 if (port < _ctrl_params.size ()) {
847 LuaProc::parameter_is_input (uint32_t port) const
849 assert (port < _ctrl_params.size ());
850 return (!_ctrl_params[port].first);
854 LuaProc::parameter_is_output (uint32_t port) const
856 assert (port < _ctrl_params.size ());
857 return (_ctrl_params[port].first);
860 std::set<Evoral::Parameter>
861 LuaProc::automatable () const
863 std::set<Evoral::Parameter> automatables;
864 for (uint32_t i = 0; i < _ctrl_params.size (); ++i) {
865 if (parameter_is_input (i)) {
866 automatables.insert (automatables.end (), Evoral::Parameter (PluginAutomation, 0, i));
873 LuaProc::describe_parameter (Evoral::Parameter param)
875 if (param.type () == PluginAutomation && param.id () < parameter_count ()) {
876 int lp = _ctrl_params[param.id ()].second;
877 return _param_desc[lp].label;
883 LuaProc::print_parameter (uint32_t param, char* buf, uint32_t len) const
886 if (param < parameter_count ()) {
887 snprintf (buf, len, "%.3f", get_parameter (param));
894 boost::shared_ptr<ScalePoints>
895 LuaProc::parse_scale_points (luabridge::LuaRef* lr)
897 if (!(*lr)["scalepoints"].isTable()) {
898 return boost::shared_ptr<ScalePoints> ();
902 boost::shared_ptr<ScalePoints> rv = boost::shared_ptr<ScalePoints>(new ScalePoints());
903 luabridge::LuaRef scalepoints ((*lr)["scalepoints"]);
905 for (luabridge::Iterator i (scalepoints); !i.isNil (); ++i) {
906 if (!i.key ().isString ()) { continue; }
907 if (!i.value ().isNumber ()) { continue; }
908 rv->insert(make_pair(i.key ().cast<std::string> (),
909 i.value ().cast<float> ()));
913 if (rv->size() > 0) {
916 return boost::shared_ptr<ScalePoints> ();
919 boost::shared_ptr<ScalePoints>
920 LuaProc::get_scale_points (uint32_t port) const
922 int lp = _ctrl_params[port].second;
923 return _param_desc.find(lp)->second.scale_points;
927 LuaProc::setup_lua_inline_gui (LuaState *lua_gui)
929 lua_State* LG = lua_gui->getState ();
930 LuaBindings::stddef (LG);
931 LuaBindings::common (LG);
932 LuaBindings::dsp (LG);
935 lua_gui->Print.connect (sigc::mem_fun (*this, &LuaProc::lua_print));
938 lua_gui->do_command ("function ardour () end");
939 lua_gui->do_command (_script);
941 // TODO think: use a weak-pointer here ?
942 // (the GUI itself uses a shared ptr to this plugin, so we should be good)
943 luabridge::getGlobalNamespace (LG)
944 .beginNamespace ("Ardour")
945 .beginClass <LuaProc> ("LuaProc")
946 .addFunction ("shmem", &LuaProc::instance_shm)
950 luabridge::push <LuaProc *> (LG, this);
951 lua_setglobal (LG, "self");
953 luabridge::push <float *> (LG, _shadow_data);
954 lua_setglobal (LG, "CtrlPorts");
957 ////////////////////////////////////////////////////////////////////////////////
958 #include <glibmm/miscutils.h>
959 #include <glibmm/fileutils.h>
961 LuaPluginInfo::LuaPluginInfo (LuaScriptInfoPtr lsi) {
962 if (lsi->type != LuaScriptInfo::DSP) {
963 throw failed_constructor ();
968 creator = lsi->author;
969 category = lsi->category;
970 unique_id = "luascript"; // the interpreter is not unique.
972 n_inputs.set (DataType::AUDIO, 1);
973 n_outputs.set (DataType::AUDIO, 1);
978 LuaPluginInfo::load (Session& session)
980 std::string script = "";
981 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
986 script = Glib::file_get_contents (path);
987 } catch (Glib::FileError err) {
991 if (script.empty ()) {
996 PluginPtr plugin (new LuaProc (session.engine (), session, script));
998 } catch (failed_constructor& err) {
1001 return PluginPtr ();
1004 std::vector<Plugin::PresetRecord>
1005 LuaPluginInfo::get_presets (bool /*user_only*/) const
1007 std::vector<Plugin::PresetRecord> p;