Add option to limit automatable control parmaters
[ardour.git] / libs / ardour / luaproc.cc
index 9122fb32fbdb9e5b1d71218dcd102a68a9201c8b..bf93e1df676adfe690d31b3590a2b6e1679c1579 100644 (file)
@@ -54,10 +54,12 @@ LuaProc::LuaProc (AudioEngine& engine,
        , lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool))
 #endif
        , _lua_dsp (0)
+       , _lua_latency (0)
        , _script (script)
        , _lua_does_channelmapping (false)
        , _lua_has_inline_display (false)
        , _designated_bypass_port (UINT32_MAX)
+       , _signal_latency (0)
        , _control_data (0)
        , _shadow_data (0)
        , _configured (false)
@@ -85,11 +87,13 @@ LuaProc::LuaProc (const LuaProc &other)
        , lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool))
 #endif
        , _lua_dsp (0)
+       , _lua_latency (0)
        , _script (other.script ())
        , _origin (other._origin)
        , _lua_does_channelmapping (false)
        , _lua_has_inline_display (false)
        , _designated_bypass_port (UINT32_MAX)
+       , _signal_latency (0)
        , _control_data (0)
        , _shadow_data (0)
        , _configured (false)
@@ -111,18 +115,21 @@ LuaProc::LuaProc (const LuaProc &other)
 LuaProc::~LuaProc () {
 #ifdef WITH_LUAPROC_STATS
        if (_info && _stats_cnt > 0) {
-               printf ("LuaProc: '%s' run()  avg: %.3f  max: %.3f [ms]\n",
+               printf ("LuaProc: '%s' run()  avg: %.3f  max: %.3f [ms] p: %.1f\n",
                                _info->name.c_str (),
                                0.0001f * _stats_avg[0] / (float) _stats_cnt,
-                               0.0001f * _stats_max[0]);
-               printf ("LuaProc: '%s' gc()   avg: %.3f  max: %.3f [ms]\n",
+                               0.0001f * _stats_max[0],
+                               _stats_max[0] * (float)_stats_cnt / _stats_avg[0]);
+               printf ("LuaProc: '%s' gc()   avg: %.3f  max: %.3f [ms] p: %.1f\n",
                                _info->name.c_str (),
                                0.0001f * _stats_avg[1] / (float) _stats_cnt,
-                               0.0001f * _stats_max[1]);
+                               0.0001f * _stats_max[1],
+                               _stats_max[1] * (float)_stats_cnt / _stats_avg[1]);
        }
 #endif
        lua.do_command ("collectgarbage();");
        delete (_lua_dsp);
+       delete (_lua_latency);
        delete [] _control_data;
        delete [] _shadow_data;
 }
@@ -131,13 +138,14 @@ void
 LuaProc::init ()
 {
 #ifdef WITH_LUAPROC_STATS
-       _stats_avg[0] = _stats_avg[1] = _stats_max[0] = _stats_max[1] = _stats_cnt = 0;
+       _stats_avg[0] = _stats_avg[1] = _stats_max[0] = _stats_max[1] = 0;
+       _stats_cnt = -25;
 #endif
 
-       lua.tweak_rt_gc ();
        lua.Print.connect (sigc::mem_fun (*this, &LuaProc::lua_print));
        // register session object
        lua_State* L = lua.getState ();
+       lua_mlock (L, 1);
        LuaBindings::stddef (L);
        LuaBindings::common (L);
        LuaBindings::dsp (L);
@@ -153,6 +161,7 @@ LuaProc::init ()
                .addFunction ("name", &LuaProc::name)
                .endClass ()
                .endNamespace ();
+       lua_mlock (L, 0);
 
        // add session to global lua namespace
        luabridge::push <Session *> (L, &_session);
@@ -232,6 +241,11 @@ LuaProc::load_script ()
                assert (0);
        }
 
+       luabridge::LuaRef lua_dsp_latency = luabridge::getGlobal (L, "dsp_latency");
+       if (lua_dsp_latency.type () == LUA_TFUNCTION) {
+               _lua_latency = new luabridge::LuaRef (lua_dsp_latency);
+       }
+
        // initialize the DSP if needed
        luabridge::LuaRef lua_dsp_init = luabridge::getGlobal (L, "dsp_init");
        if (lua_dsp_init.type () == LUA_TFUNCTION) {
@@ -731,6 +745,11 @@ LuaProc::connect_and_run (BufferSet& bufs,
                                }
                        }
                }
+
+               if (_lua_latency) {
+                       _signal_latency = (*_lua_latency)();
+               }
+
        } catch (luabridge::LuaException const& e) {
                PBD::error << "LuaException: " << e.what () << "\n";
 #ifndef NDEBUG
@@ -744,16 +763,17 @@ LuaProc::connect_and_run (BufferSet& bufs,
        int64_t t1 = g_get_monotonic_time ();
 #endif
 
-       lua.collect_garbage_step (100 /*kB*/);
+       lua.collect_garbage_step ();
 #ifdef WITH_LUAPROC_STATS
-       ++_stats_cnt;
-       int64_t t2 = g_get_monotonic_time ();
-       int64_t ela0 = t1 - t0;
-       int64_t ela1 = t2 - t1;
-       if (ela0 > _stats_max[0]) _stats_max[0] = ela0;
-       if (ela1 > _stats_max[1]) _stats_max[1] = ela1;
-       _stats_avg[0] += ela0;
-       _stats_avg[1] += ela1;
+       if (++_stats_cnt > 0) {
+               int64_t t2 = g_get_monotonic_time ();
+               int64_t ela0 = t1 - t0;
+               int64_t ela1 = t2 - t1;
+               if (ela0 > _stats_max[0]) _stats_max[0] = ela0;
+               if (ela1 > _stats_max[1]) _stats_max[1] = ela1;
+               _stats_avg[0] += ela0;
+               _stats_avg[1] += ela1;
+       }
 #endif
        return 0;
 }
@@ -1226,7 +1246,6 @@ LuaPluginInfo::LuaPluginInfo (LuaScriptInfoPtr lsi) {
        n_outputs.set (DataType::AUDIO, 1);
        type = Lua;
 
-       _is_instrument = category == "Instrument";
 }
 
 PluginPtr
@@ -1239,7 +1258,7 @@ LuaPluginInfo::load (Session& session)
 
        try {
                script = Glib::file_get_contents (path);
-       } catch (Glib::FileError err) {
+       } catch (Glib::FileError const& err) {
                return PluginPtr ();
        }