Implement Lua DSP processor/plugin
[ardour.git] / libs / ardour / luaproc.cc
1 /*
2     Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3     Copyright (C) 2006 Paul Davis
4
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)
8     any later version.
9
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
13     for more details.
14
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.
18 */
19
20 #include <glib.h>
21 #include "pbd/gstdio_compat.h"
22
23 #include "pbd/pthread_utils.h"
24
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"
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 LuaProc::LuaProc (AudioEngine& engine,
40                   Session& session,
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))
45         , _lua_dsp (0)
46         , _lua_params (0)
47         , _script (script)
48         , _lua_does_channelmapping (false)
49         , _control_data (0)
50         , _shadow_data (0)
51         , _has_midi_input (false)
52         , _has_midi_output (false)
53 {
54         init ();
55
56         /* when loading a session, or pasing a processor,
57          * the script is set during set_state();
58          */
59         if (!_script.empty () && load_script ()) {
60                 throw failed_constructor ();
61         }
62 }
63
64 LuaProc::LuaProc (const LuaProc &other)
65         : Plugin (other)
66         , _mempool ("LuaProc", 1048576) // 1 MB is plenty. (64K would be enough)
67         , lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool))
68         , _lua_dsp (0)
69         , _lua_params (0)
70         , _script (other.script ())
71         , _lua_does_channelmapping (false)
72         , _control_data (0)
73         , _shadow_data (0)
74         , _has_midi_input (false)
75         , _has_midi_output (false)
76 {
77         init ();
78
79         if (load_script ()) {
80                 throw failed_constructor ();
81         }
82
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];
86         }
87 }
88
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",
93                                 _info->name.c_str (),
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",
97                                 _info->name.c_str (),
98                                 0.0001f * _stats_avg[1] / (float) _stats_cnt,
99                                 0.0001f * _stats_max[1]);
100         }
101 #endif
102         lua.do_command ("collectgarbage();");
103         delete (_lua_dsp);
104         delete (_lua_params);
105         delete [] _control_data;
106         delete [] _shadow_data;
107 }
108
109 void
110 LuaProc::init ()
111 {
112 #ifdef WITH_LUAPROC_STATS
113         _stats_avg[0] = _stats_avg[1] = _stats_max[0] = _stats_max[1] = _stats_cnt = 0;
114 #endif
115
116 #ifndef NDEBUG
117         lua.Print.connect (sigc::mem_fun (*this, &LuaProc::lua_print));
118 #endif
119         // register session object
120         lua_State* L = lua.getState ();
121         LuaBindings::stddef (L);
122         LuaBindings::common (L);
123         LuaBindings::dsp (L);
124
125         // add session to global lua namespace
126         luabridge::push <Session *> (L, &_session);
127         lua_setglobal (L, "Session");
128
129         // sandbox
130         lua.do_command ("io = nil os = nil loadfile = nil require = nil dofile = nil package = nil debug = nil");
131 #if 0
132         lua.do_command ("for n in pairs(_G) do print(n) end print ('----')"); // print global env
133 #endif
134         lua.do_command ("function ardour () end");
135 }
136
137 void
138 LuaProc::lua_print (std::string s) {
139         std::cout <<"LuaProc: " << s << "\n";
140 }
141
142 bool
143 LuaProc::load_script ()
144 {
145         assert (!_lua_dsp); // don't allow to re-initialize
146
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 }, .. }
154
155         try {
156                 LuaScriptInfoPtr lsi = LuaScripting::script_info (_script);
157                 LuaPluginInfoPtr lpi (new LuaPluginInfo (lsi));
158                 assert (lpi);
159                 set_info (lpi);
160                 _mempool.set_name ("LuaProc: " + lsi->name);
161                 _docs = lsi->description;
162         } catch (failed_constructor& err) {
163                 return true;
164         }
165
166         lua_State* L = lua.getState ();
167         lua.do_command (_script);
168
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");
172
173         if ((lua_dsp_run.type () != LUA_TFUNCTION) == (lua_dsp_map.type () != LUA_TFUNCTION)) {
174                 return true;
175         }
176
177         if (lua_dsp_run.type () == LUA_TFUNCTION) {
178                 _lua_dsp = new luabridge::LuaRef (lua_dsp_run);
179         }
180         else if (lua_dsp_map.type () == LUA_TFUNCTION) {
181                 _lua_dsp = new luabridge::LuaRef (lua_dsp_map);
182                 _lua_does_channelmapping = true;
183         }
184         else {
185                 assert (0);
186         }
187
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) {
191                 try {
192                         lua_dsp_init (_session.nominal_frame_rate ());
193                 } catch (luabridge::LuaException const& e) {
194                         ;
195                 }
196         }
197
198         luabridge::LuaRef lua_dsp_midi_in = luabridge::getGlobal (L, "dsp_midi_input");
199         if (lua_dsp_midi_in.type () == LUA_TFUNCTION) {
200                 try {
201                         _has_midi_input = lua_dsp_midi_in ();
202                 } catch (luabridge::LuaException const& e) {
203                         ;
204                 }
205         }
206
207         _ctrl_params.clear ();
208
209         luabridge::LuaRef lua_params = luabridge::getGlobal (L, "dsp_params");
210         if (lua_params.isFunction ()) {
211
212                 // call function // add try {} catch (luabridge::LuaException const& e)
213                 luabridge::LuaRef params = lua_params ();
214
215                 if (params.isTable ()) {
216
217                         _lua_params = new luabridge::LuaRef (params);
218
219                         for (luabridge::Iterator i (params); !i.isNil (); ++i) {
220                                 // required fields
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; }
227
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> ()));
232                                 }
233                                 else if (type == "output") {
234                                         _ctrl_params.push_back (std::make_pair (true, i.key ().cast<int> ()));
235                                 } else {
236                                         return false;
237                                 }
238                                 assert (i.key ().cast<int> () == (int) _ctrl_params.size ());
239                         }
240                 }
241         }
242
243         _control_data = new float[parameter_count ()];
244         _shadow_data  = new float[parameter_count ()];
245
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);
249                 }
250         }
251
252         // expose ctrl-ports to global lua namespace
253         luabridge::push <float *> (L, _control_data);
254         lua_setglobal (L, "CtrlPorts");
255
256         return false; // no error
257 }
258
259 bool
260 LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out)
261 {
262         if (in.n_midi() > 0 && !_has_midi_input) {
263                 return false;
264         }
265
266         lua_State* L = lua.getState ();
267         luabridge::LuaRef ioconfig = luabridge::getGlobal (L, "dsp_ioconfig");
268         if (!ioconfig.isFunction ()) {
269                 return false;
270         }
271
272         luabridge::LuaRef table = luabridge::getGlobal (L, "table"); //lua std lib
273         luabridge::LuaRef tablesort = table["sort"];
274         assert (tablesort.isFunction ());
275
276         luabridge::LuaRef *_iotable = NULL; // can't use reference :(
277         try {
278                 luabridge::LuaRef iotable = ioconfig ();
279                 tablesort (iotable);
280                 if (iotable.isTable ()) {
281                         _iotable = new luabridge::LuaRef (iotable);
282                 }
283         } catch (luabridge::LuaException const& e) {
284                 return false;
285         }
286
287         if (!_iotable) {
288                 return false;
289         }
290
291         // now we can reference it.
292         luabridge::LuaRef iotable (*_iotable);
293         delete _iotable;
294
295         if ((iotable).length () < 1) {
296                 return false;
297         }
298
299         int32_t audio_in = in.n_audio ();
300         int32_t audio_out;
301
302         if (in.n_midi() > 0 && audio_in == 0) {
303                 audio_out = 2; // prefer stereo version if available.
304         } else {
305                 audio_out = audio_in;
306         }
307
308         for (luabridge::Iterator i (iotable); !i.isNil (); ++i) {
309                 assert (i.value ().type () == LUA_TTABLE);
310                 luabridge::LuaRef io (i.value ());
311
312                 int possible_in = io["audio_in"];
313                 int possible_out = io["audio_out"];
314
315                 // exact match
316                 if ((possible_in == audio_in) && (possible_out == audio_out)) {
317                         out.set (DataType::MIDI, 0);
318                         out.set (DataType::AUDIO, audio_out);
319                         return true;
320                 }
321         }
322
323         /* now allow potentially "imprecise" matches */
324         audio_out = -1;
325         bool found = false;
326
327         for (luabridge::Iterator i (iotable); !i.isNil (); ++i) {
328                 assert (i.value ().type () == LUA_TTABLE);
329                 luabridge::LuaRef io (i.value ());
330
331                 int possible_in = io["audio_in"];
332                 int possible_out = io["audio_out"];
333
334                 if (possible_out == 0) {
335                         continue;
336                 }
337                 if (possible_in == 0) {
338                         /* no inputs, generators & instruments */
339                         if (possible_out == -1) {
340                                 /* any configuration possible, provide stereo output */
341                                 audio_out = 2;
342                                 found = true;
343                         } else if (possible_out == -2) {
344                                 /* invalid, should be (0, -1) */
345                                 audio_out = 2;
346                                 found = true;
347                         } else if (possible_out < -2) {
348                                 /* variable number of outputs. -> whatever */
349                                 audio_out = 2;
350                                 found = true;
351                         } else {
352                                 /* exact number of outputs */
353                                 audio_out = possible_out;
354                                 found = true;
355                         }
356                 }
357
358                 if (possible_in == -1) {
359                         /* wildcard for input */
360                         if (possible_out == -1) {
361                                 /* out much match in */
362                                 audio_out = audio_in;
363                                 found = true;
364                         } else if (possible_out == -2) {
365                                 /* any configuration possible, pick matching */
366                                 audio_out = audio_in;
367                                 found = true;
368                         } else if (possible_out < -2) {
369                                 /* explicitly variable number of outputs, pick maximum */
370                                 audio_out = -possible_out;
371                                 found = true;
372                         } else {
373                                 /* exact number of outputs */
374                                 audio_out = possible_out;
375                                 found = true;
376                         }
377                 }
378
379                 if (possible_in == -2) {
380
381                         if (possible_out == -1) {
382                                 /* any configuration possible, pick matching */
383                                 audio_out = audio_in;
384                                 found = true;
385                         } else if (possible_out == -2) {
386                                 /* invalid. interpret as (-1, -1) */
387                                 audio_out = audio_in;
388                                 found = true;
389                         } else if (possible_out < -2) {
390                                 /* explicitly variable number of outputs, pick maximum */
391                                 audio_out = -possible_out;
392                                 found = true;
393                         } else {
394                                 /* exact number of outputs */
395                                 audio_out = possible_out;
396                                 found = true;
397                         }
398                 }
399
400                 if (possible_in < -2) {
401                         /* explicit variable number of inputs */
402                         if (audio_in > -possible_in) {
403                                 /* request is too large */
404                         }
405                         if (possible_out == -1) {
406                                 /* any output configuration possible, provide stereo out */
407                                 audio_out = 2;
408                                 found = true;
409                         } else if (possible_out == -2) {
410                                 /* invalid. interpret as (<-2, -1) */
411                                 audio_out = 2;
412                                 found = true;
413                         } else if (possible_out < -2) {
414                                 /* explicitly variable number of outputs, pick stereo */
415                                 audio_out = 2;
416                                 found = true;
417                         } else {
418                                 /* exact number of outputs */
419                                 audio_out = possible_out;
420                                 found = true;
421                         }
422                 }
423
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 */
428                                 audio_out = 2;
429                                 found = true;
430                         } else if (possible_out == -2) {
431                                 /* invalid. interpret as (>0, -1) */
432                                 audio_out = 2;
433                                 found = true;
434                         } else if (possible_out < -2) {
435                                 /* explicitly variable number of outputs, pick maximum */
436                                 audio_out = -possible_out;
437                                 found = true;
438                         } else {
439                                 /* exact number of outputs */
440                                 audio_out = possible_out;
441                                 found = true;
442                         }
443                 }
444
445                 if (found) {
446                         break;
447                 }
448         }
449
450         if (!found) {
451                 return false;
452         }
453
454         out.set (DataType::MIDI, 0);
455         out.set (DataType::AUDIO, audio_out);
456         return true;
457 }
458
459 bool
460 LuaProc::configure_io (ChanCount in, ChanCount out)
461 {
462         _configured_in = in;
463         _configured_out = out;
464
465         _configured_in.set (DataType::MIDI, _has_midi_input ? 1 : 0);
466         _configured_out.set (DataType::MIDI, _has_midi_output ? 1 : 0);
467
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) {
472                 try {
473                         lua_dsp_configure (&in, &out);
474                 } catch (luabridge::LuaException const& e) {
475                         ;
476                 }
477         }
478
479         _info->n_inputs = _configured_in;
480         _info->n_outputs = _configured_out;
481         return true;
482 }
483
484 int
485 LuaProc::connect_and_run (BufferSet& bufs,
486                 ChanMapping in, ChanMapping out,
487                 pframes_t nframes, framecnt_t offset)
488 {
489         if (!_lua_dsp) {
490                 return 0;
491         }
492
493         Plugin::connect_and_run (bufs, in, out, nframes, offset);
494
495         // This is needed for ARDOUR::Session requests :(
496         if (! SessionEvent::has_per_thread_pool ()) {
497                 char name[64];
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);
502         }
503
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];
508                 }
509         }
510
511 #ifdef WITH_LUAPROC_STATS
512         int64_t t0 = g_get_monotonic_time ();
513 #endif
514
515         try {
516                 if (_lua_does_channelmapping) {
517                         // run the DSP function
518                         (*_lua_dsp)(&bufs, in, out, nframes, offset);
519                 } else {
520                         // map buffers
521                         BufferSet& silent_bufs  = _session.get_silent_buffers (ChanCount (DataType::AUDIO, 1));
522                         BufferSet& scratch_bufs = _session.get_scratch_buffers (ChanCount (DataType::AUDIO, 1));
523
524                         lua_State* L = lua.getState ();
525                         luabridge::LuaRef in_map (luabridge::newTable (L));
526                         luabridge::LuaRef out_map (luabridge::newTable (L));
527
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 ();
531
532                         for (uint32_t ap = 0; ap < audio_in; ++ap) {
533                                 bool valid;
534                                 const uint32_t buf_index = in.get(DataType::AUDIO, ap, &valid);
535                                 if (valid) {
536                                         in_map[ap + 1] = bufs.get_audio (buf_index).data (offset);
537                                 } else {
538                                         in_map[ap + 1] = silent_bufs.get_audio (0).data (offset);
539                                 }
540                         }
541                         for (uint32_t ap = 0; ap < audio_out; ++ap) {
542                                 bool valid;
543                                 const uint32_t buf_index = out.get(DataType::AUDIO, ap, &valid);
544                                 if (valid) {
545                                         out_map[ap + 1] = bufs.get_audio (buf_index).data (offset);
546                                 } else {
547                                         out_map[ap + 1] = scratch_bufs.get_audio (0).data (offset);
548                                 }
549                         }
550
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) {
554                                 bool valid;
555                                 const uint32_t idx = in.get(DataType::MIDI, mp, &valid);
556                                 if (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];
564                                                 }
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;
569                                         }
570                                 }
571                         }
572
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");
577                         }
578
579
580                         // run the DSP function
581                         (*_lua_dsp)(in_map, out_map, nframes);
582                 }
583         } catch (luabridge::LuaException const& e) {
584 #ifndef NDEBUG
585                 printf ("LuaException: %s\n", e.what ());
586 #endif
587                 return -1;
588         }
589 #ifdef WITH_LUAPROC_STATS
590         int64_t t1 = g_get_monotonic_time ();
591 #endif
592         lua.collect_garbage (); // rt-safe, slight *regular* performance overhead
593 #ifdef WITH_LUAPROC_STATS
594         ++_stats_cnt;
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;
602 #endif
603         return 0;
604 }
605
606
607 void
608 LuaProc::add_state (XMLNode* root) const
609 {
610         gchar* b64 = g_base64_encode ((const guchar*)_script.c_str (), _script.size ());
611         std::string b64s (b64);
612         g_free (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);
617 }
618
619 int
620 LuaProc::set_script_from_state (const XMLNode& node)
621 {
622         XMLNode* child;
623         if (node.name () != state_node_name ()) {
624                 return -1;
625         }
626
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; }
630                         gsize size;
631                         guchar* buf = g_base64_decode ((*n)->content ().c_str (), &size);
632                         _script = std::string ((const char*)buf, size);
633                         g_free (buf);
634                         if (load_script ()) {
635                                 PBD::error << _("Failed to load Lua script from session state.") << endmsg;
636 #ifndef NDEBUG
637                                 std::cerr << "Failed Lua Script: " << _script << std::endl;
638 #endif
639                                 _script = "";
640                         }
641                         break;
642                 }
643         }
644         if (_script.empty ()) {
645                 PBD::error << _("Session State for LuaProcessor did not include a Lua script.") << endmsg;
646                 return -1;
647         }
648         if (!_lua_dsp) {
649                 PBD::error << _("Invalid/incompatible Lua script found for LuaProcessor.") << endmsg;
650                 return -1;
651         }
652         return 0;
653 }
654
655 int
656 LuaProc::set_state (const XMLNode& node, int version)
657 {
658         if (_script.empty ()) {
659                 if (set_script_from_state (node)) {
660                         return -1;
661                 }
662         }
663         return Plugin::set_state (node, version);
664 }
665
666 uint32_t
667 LuaProc::parameter_count () const
668 {
669         return _ctrl_params.size ();
670 }
671
672 float
673 LuaProc::default_value (uint32_t port)
674 {
675         if (_ctrl_params[port].first) {
676                 assert (0);
677                 return 0;
678         }
679         int lp = _ctrl_params[port].second;
680         luabridge::LuaRef lr = (*_lua_params)[lp];
681         return (lr["default"]).cast<float> ();
682 }
683
684 void
685 LuaProc::set_parameter (uint32_t port, float val)
686 {
687         assert (port < parameter_count ());
688         if (get_parameter (port) == val) {
689                 return;
690         }
691         _shadow_data[port] = val;
692         Plugin::set_parameter (port, val);
693 }
694
695 float
696 LuaProc::get_parameter (uint32_t port) const
697 {
698         if (parameter_is_input (port)) {
699                 return _shadow_data[port];
700         } else {
701                 return _control_data[port];
702         }
703 }
704
705 int
706 LuaProc::get_parameter_descriptor (uint32_t port, ParameterDescriptor& desc) const
707 {
708         assert (port <= parameter_count ());
709         int lp = _ctrl_params[port].second;
710
711         luabridge::LuaRef lr = (*_lua_params)[lp];
712         desc.lower  = (lr["min"]).cast<float> ();
713         desc.upper  = (lr["max"]).cast<float> ();
714
715         if (_ctrl_params[port].first) {
716                 desc.normal = desc.lower; // output-port, no default
717         } else {
718                 desc.normal = (lr["default"]).cast<float> ();
719         }
720
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> ();
726
727         // TODO check if assignments make sense, e.g
728         assert (!(desc.toggled && desc.logarithmic));
729
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; }
735         }
736
737         desc.label = (lr["name"]).cast<std::string> ();
738         desc.scale_points = get_scale_points (port);
739         desc.update_steps ();
740         return 0;
741 }
742
743 std::string
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> ();
751         }
752         return "";
753 }
754
755 uint32_t
756 LuaProc::nth_parameter (uint32_t port, bool& ok) const
757 {
758         if (port < _ctrl_params.size ()) {
759                 ok = true;
760                 return port;
761         }
762         ok = false;
763         return 0;
764 }
765
766 bool
767 LuaProc::parameter_is_input (uint32_t port) const
768 {
769         assert (port < _ctrl_params.size ());
770         return (!_ctrl_params[port].first);
771 }
772
773 bool
774 LuaProc::parameter_is_output (uint32_t port) const
775 {
776         assert (port < _ctrl_params.size ());
777         return (_ctrl_params[port].first);
778 }
779
780 std::set<Evoral::Parameter>
781 LuaProc::automatable () const
782 {
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));
787                 }
788         }
789         return automatables;
790 }
791
792 std::string
793 LuaProc::describe_parameter (Evoral::Parameter param)
794 {
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> ();
799         } else {
800                 return "??";
801         }
802 }
803
804 void
805 LuaProc::print_parameter (uint32_t param, char* buf, uint32_t len) const
806 {
807         if (buf && len) {
808                 if (param < parameter_count ()) {
809                         snprintf (buf, len, "%.3f", get_parameter (param));
810                 } else {
811                         strcat (buf, "0");
812                 }
813         }
814 }
815
816 boost::shared_ptr<ScalePoints>
817 LuaProc::get_scale_points (uint32_t port) const
818 {
819         int lp = _ctrl_params[port].second;
820         luabridge::LuaRef lr = (*_lua_params)[lp];
821
822         if (!lr["scalepoints"].isTable()) {
823                 return boost::shared_ptr<ScalePoints> ();
824         }
825
826         int cnt = 0;
827         boost::shared_ptr<ScalePoints> rv = boost::shared_ptr<ScalePoints>(new ScalePoints());
828         luabridge::LuaRef scalepoints (lr["scalepoints"]);
829
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> ()));
835                 ++cnt;
836         }
837
838         if (rv->size() > 0) {
839                 return rv;
840         }
841         return boost::shared_ptr<ScalePoints> ();
842 }
843
844
845 ////////////////////////////////////////////////////////////////////////////////
846 #include <glibmm/miscutils.h>
847 #include <glibmm/fileutils.h>
848
849 LuaPluginInfo::LuaPluginInfo (LuaScriptInfoPtr lsi) {
850         if (lsi->type != LuaScriptInfo::DSP) {
851                 throw failed_constructor ();
852         }
853
854         path = lsi->path;
855         name = lsi->name;
856         creator = lsi->author;
857         category = lsi->category;
858         unique_id = "luascript"; // the interpreter is not unique.
859
860         n_inputs.set (DataType::AUDIO, 1);
861         n_outputs.set (DataType::AUDIO, 1);
862         type = Lua;
863 }
864
865 PluginPtr
866 LuaPluginInfo::load (Session& session)
867 {
868         std::string script = "";
869         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
870                 return PluginPtr ();
871         }
872
873         try {
874                 script = Glib::file_get_contents (path);
875         } catch (Glib::FileError err) {
876                 return PluginPtr ();
877         }
878
879         if (script.empty ()) {
880                 return PluginPtr ();
881         }
882
883         try {
884                 PluginPtr plugin (new LuaProc (session.engine (), session, script));
885                 return plugin;
886         } catch (failed_constructor& err) {
887                 ;
888         }
889         return PluginPtr ();
890 }
891
892 std::vector<Plugin::PresetRecord>
893 LuaPluginInfo::get_presets (bool /*user_only*/) const
894 {
895         std::vector<Plugin::PresetRecord> p;
896         return p;
897 }