Flush processor (re-activate) on route active change
[ardour.git] / libs / ardour / luaproc.cc
index 7b676167de79e931fe3446cb8ea5c71930121f54..5b829a742b39f7301fe9c089785f87dff910c34f 100644 (file)
@@ -61,6 +61,7 @@ LuaProc::LuaProc (AudioEngine& engine,
        , _designated_bypass_port (UINT32_MAX)
        , _control_data (0)
        , _shadow_data (0)
+       , _configured (false)
        , _has_midi_input (false)
        , _has_midi_output (false)
 {
@@ -86,11 +87,13 @@ LuaProc::LuaProc (const LuaProc &other)
 #endif
        , _lua_dsp (0)
        , _script (other.script ())
+       , _origin (other._origin)
        , _lua_does_channelmapping (false)
        , _lua_has_inline_display (false)
        , _designated_bypass_port (UINT32_MAX)
        , _control_data (0)
        , _shadow_data (0)
+       , _configured (false)
        , _has_midi_input (false)
        , _has_midi_output (false)
 {
@@ -142,10 +145,13 @@ LuaProc::init ()
 
        luabridge::getGlobalNamespace (L)
                .beginNamespace ("Ardour")
-               .beginClass <LuaProc> ("LuaProc")
+               .deriveClass <LuaProc, PBD::StatefulDestructible> ("LuaProc")
                .addFunction ("queue_draw", &LuaProc::queue_draw)
                .addFunction ("shmem", &LuaProc::instance_shm)
                .addFunction ("table", &LuaProc::instance_ref)
+               .addFunction ("route", &LuaProc::route)
+               .addFunction ("unique_id", &LuaProc::unique_id)
+               .addFunction ("name", &LuaProc::name)
                .endClass ()
                .endNamespace ();
 
@@ -165,6 +171,15 @@ LuaProc::init ()
        lua.do_command ("function ardour () end");
 }
 
+boost::weak_ptr<Route>
+LuaProc::route () const
+{
+       if (!_owner) {
+               return boost::weak_ptr<Route>();
+       }
+       return static_cast<Route*>(_owner)->weakroute ();
+}
+
 void
 LuaProc::lua_print (std::string s) {
        std::cout <<"LuaProc: " << s << "\n";
@@ -526,7 +541,7 @@ LuaProc::configure_io (ChanCount in, ChanCount out)
        _info->n_outputs = _selected_out;
 
        // configure the DSP if needed
-       if (in != _configured_in || out != _configured_out) {
+       if (in != _configured_in || out != _configured_out || !_configured) {
                lua_State* L = lua.getState ();
                luabridge::LuaRef lua_dsp_configure = luabridge::getGlobal (L, "dsp_configure");
                if (lua_dsp_configure.type () == LUA_TFUNCTION) {
@@ -563,6 +578,7 @@ LuaProc::configure_io (ChanCount in, ChanCount out)
                                        _info->n_inputs = lin;
                                        _info->n_outputs = lout;
                                }
+                               _configured = true;
                        } catch (luabridge::LuaException const& e) {
                                PBD::error << "LuaException: " << e.what () << "\n";
 #ifndef NDEBUG
@@ -655,7 +671,7 @@ LuaProc::connect_and_run (BufferSet& bufs,
                                if (valid) {
                                        for (MidiBuffer::iterator m = bufs.get_midi(idx).begin();
                                                        m != bufs.get_midi(idx).end(); ++m, ++e) {
-                                               const Evoral::MIDIEvent<framepos_t> ev(*m, false);
+                                               const Evoral::Event<framepos_t> ev(*m, false);
                                                luabridge::LuaRef lua_midi_data (luabridge::newTable (L));
                                                const uint8_t* data = ev.buffer();
                                                for (uint32_t i = 0; i < ev.size(); ++i) {
@@ -751,6 +767,7 @@ LuaProc::add_state (XMLNode* root) const
        g_free (b64);
        XMLNode* script_node = new XMLNode (X_("script"));
        script_node->add_property (X_("lua"), LUA_VERSION);
+       script_node->add_property (X_("origin"), _origin);
        script_node->add_content (b64s);
        root->add_child_nocopy (*script_node);
 
@@ -775,6 +792,10 @@ LuaProc::set_script_from_state (const XMLNode& node)
        }
 
        if ((child = node.child (X_("script"))) != 0) {
+               XMLProperty const* prop;
+               if ((prop = node.property ("origin")) != 0) {
+                       _origin = prop->value();
+               }
                for (XMLNodeList::const_iterator n = child->children ().begin (); n != child->children ().end (); ++n) {
                        if (!(*n)->is_content ()) { continue; }
                        gsize size;
@@ -1017,6 +1038,7 @@ LuaProc::setup_lua_inline_gui (LuaState *lua_gui)
        LuaBindings::stddef (LG);
        LuaBindings::common (LG);
        LuaBindings::dsp (LG);
+       LuaBindings::osc (LG);
 
        lua_gui->Print.connect (sigc::mem_fun (*this, &LuaProc::lua_print));
        lua_gui->do_command ("function ardour () end");
@@ -1112,7 +1134,10 @@ LuaProc::load_preset (PresetRecord r)
                                assert (index);
                                assert (value);
                                LocaleGuard lg;
-                               set_parameter (atoi (index->value().c_str()), atof (value->value().c_str ()));
+                               const uint32_t p = atoi (index->value().c_str());
+                               const float v = atof (value->value().c_str ());
+                               set_parameter (p, v);
+                               PresetPortSetValue (p, v); /* EMIT SIGNAL */
                        }
                }
                return Plugin::load_preset(r);
@@ -1128,6 +1153,9 @@ LuaProc::do_save_preset (std::string name) {
                return "";
        }
 
+       // prevent dups -- just in case
+       t->root()->remove_nodes_and_delete (X_("label"), name);
+
        std::string uri (preset_name_to_uri (name));
 
        XMLNode* p = new XMLNode (X_("Preset"));
@@ -1223,7 +1251,9 @@ LuaPluginInfo::load (Session& session)
        }
 
        try {
-               PluginPtr plugin (new LuaProc (session.engine (), session, script));
+               LuaProc* lp = new LuaProc (session.engine (), session, script);
+               lp->set_origin (path);
+               PluginPtr plugin (lp);
                return plugin;
        } catch (failed_constructor& err) {
                ;