Mixbus specific Pin Mapping tweaks
[ardour.git] / libs / ardour / session_state.cc
index bde7208867ee9401dba352cefc6136e454a90268..95eb36c07f4ec6fb617d12f20b214a8fac01f438 100644 (file)
 
 #include "control_protocol/control_protocol.h"
 
+#include "LuaBridge/LuaBridge.h"
+
 #include "i18n.h"
 #include <locale.h>
 
@@ -780,6 +782,10 @@ Session::save_state (string snapshot_name, bool pending, bool switch_to_snapshot
                return 1;
        }
 
+#ifndef NDEBUG
+       const int64_t save_start_time = g_get_monotonic_time();
+#endif
+
        /* tell sources we're saving first, in case they write out to a new file
         * which should be saved with the state rather than the old one */
        for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
@@ -877,6 +883,10 @@ Session::save_state (string snapshot_name, bool pending, bool switch_to_snapshot
                StateSaved (snapshot_name); /* EMIT SIGNAL */
        }
 
+#ifndef NDEBUG
+       const int64_t elapsed_time_us = g_get_monotonic_time() - save_start_time;
+       cerr << "saved state in " << fixed << setprecision (1) << elapsed_time_us / 1000. << " ms\n";
+#endif
        return 0;
 }
 
@@ -1036,7 +1046,7 @@ Session::state (bool full_state)
        if (full_state) {
 
                node->add_property ("name", _name);
-               snprintf (buf, sizeof (buf), "%" PRId64, _nominal_frame_rate);
+               snprintf (buf, sizeof (buf), "%" PRId64, _base_frame_rate);
                node->add_property ("sample-rate", buf);
 
                if (session_dirs.size() > 1) {
@@ -1074,6 +1084,9 @@ Session::state (bool full_state)
        snprintf (buf, sizeof (buf), "%" PRIu64, ID::counter());
        node->add_property ("id-counter", buf);
 
+       snprintf (buf, sizeof (buf), "%u", name_id_counter ());
+       node->add_property ("name-counter", buf);
+
        /* save the event ID counter */
 
        snprintf (buf, sizeof (buf), "%d", Evoral::event_id_counter());
@@ -1249,6 +1262,26 @@ Session::state (bool full_state)
                node->add_child_copy (*_extra_xml);
        }
 
+       {
+               Glib::Threads::Mutex::Lock lm (lua_lock);
+               std::string saved;
+               {
+                       luabridge::LuaRef savedstate ((*_lua_save)());
+                       saved = savedstate.cast<std::string>();
+               }
+               lua.collect_garbage ();
+               lm.release ();
+
+               gchar* b64 = g_base64_encode ((const guchar*)saved.c_str (), saved.size ());
+               std::string b64s (b64);
+               g_free (b64);
+
+               XMLNode* script_node = new XMLNode (X_("Script"));
+               script_node->add_property (X_("lua"), LUA_VERSION);
+               script_node->add_content (b64s);
+               node->add_child_nocopy (*script_node);
+       }
+
        return *node;
 }
 
@@ -1280,10 +1313,12 @@ Session::set_state (const XMLNode& node, int version)
 
        if ((prop = node.property (X_("sample-rate"))) != 0) {
 
-               _nominal_frame_rate = atoi (prop->value());
+               _base_frame_rate = atoi (prop->value());
+               _nominal_frame_rate = _base_frame_rate;
 
-               if (_nominal_frame_rate != _current_frame_rate) {
-                        boost::optional<int> r = AskAboutSampleRateMismatch (_nominal_frame_rate, _current_frame_rate);
+               assert (AudioEngine::instance()->running ());
+               if (_base_frame_rate != AudioEngine::instance()->sample_rate ()) {
+                       boost::optional<int> r = AskAboutSampleRateMismatch (_base_frame_rate, _current_frame_rate);
                        if (r.get_value_or (0)) {
                                goto out;
                        }
@@ -1306,10 +1341,13 @@ Session::set_state (const XMLNode& node, int version)
                ID::init_counter (now);
        }
 
-        if ((prop = node.property (X_("event-counter"))) != 0) {
-                Evoral::init_event_id_counter (atoi (prop->value()));
-        }
+       if ((prop = node.property (X_("name-counter"))) != 0) {
+               init_name_id_counter (atoi (prop->value()));
+       }
 
+       if ((prop = node.property (X_("event-counter"))) != 0) {
+               Evoral::init_event_id_counter (atoi (prop->value()));
+       }
 
        if ((child = find_named_node (node, "MIDIPorts")) != 0) {
                _midi_ports->set_midi_port_states (child->children());
@@ -1459,6 +1497,21 @@ Session::set_state (const XMLNode& node, int version)
                ControlProtocolManager::instance().set_state (*child, version);
        }
 
+       if ((child = find_named_node (node, "Script"))) {
+               for (XMLNodeList::const_iterator n = child->children ().begin (); n != child->children ().end (); ++n) {
+                       if (!(*n)->is_content ()) { continue; }
+                       gsize size;
+                       guchar* buf = g_base64_decode ((*n)->content ().c_str (), &size);
+                       try {
+                               Glib::Threads::Mutex::Lock lm (lua_lock);
+                               (*_lua_load)(std::string ((const char*)buf, size));
+                       } catch (luabridge::LuaException const& e) {
+                               cerr << "LuaException:" << e.what () << endl;
+                       }
+                       g_free (buf);
+               }
+       }
+
        update_route_record_state ();
 
        /* here beginneth the second phase ... */
@@ -2569,6 +2622,15 @@ Session::add_command (Command* const cmd)
                            cmd->name ()));
        _current_trans->add_command (cmd);
 }
+
+PBD::StatefulDiffCommand*
+Session::add_stateful_diff_command (boost::shared_ptr<PBD::StatefulDestructible> sfd)
+{
+       PBD::StatefulDiffCommand* cmd = new PBD::StatefulDiffCommand (sfd);
+       add_command (cmd);
+       return cmd;
+}
+
 void
 Session::begin_reversible_command (const string& name)
 {
@@ -3339,6 +3401,10 @@ Session::controllable_by_descriptor (const ControllableDescriptor& desc)
        case ControllableDescriptor::RemoteControlID:
                r = route_by_remote_id (desc.rid());
                break;
+
+       case ControllableDescriptor::SelectionCount:
+               r = route_by_selected_count (desc.selection_id());
+               break;
        }
 
        if (!r) {
@@ -3373,22 +3439,16 @@ Session::controllable_by_descriptor (const ControllableDescriptor& desc)
        }
 
        case ControllableDescriptor::PanDirection:
-        {
-                c = r->pannable()->pan_azimuth_control;
+               c = r->pan_azimuth_control();
                break;
-        }
 
        case ControllableDescriptor::PanWidth:
-        {
-                c = r->pannable()->pan_width_control;
+               c = r->pan_width_control();
                break;
-        }
 
        case ControllableDescriptor::PanElevation:
-        {
-                c = r->pannable()->pan_elevation_control;
+               c = r->pan_elevation_control();
                break;
-        }
 
        case ControllableDescriptor::Balance:
                /* XXX simple pan control */
@@ -3418,26 +3478,12 @@ Session::controllable_by_descriptor (const ControllableDescriptor& desc)
                break;
        }
 
-       case ControllableDescriptor::SendGain:
-       {
+       case ControllableDescriptor::SendGain: {
                uint32_t send = desc.target (0);
-
-               /* revert to zero-based counting */
-
                if (send > 0) {
                        --send;
                }
-
-               boost::shared_ptr<Processor> p = r->nth_send (send);
-
-               if (p) {
-                       boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
-                       boost::shared_ptr<Amp> a = s->amp();
-
-                       if (a) {
-                               c = s->amp()->gain_control();
-                       }
-               }
+               c = r->send_level_controllable (send);
                break;
        }
 
@@ -3740,7 +3786,7 @@ Session::config_changed (std::string p, bool ours)
        } else if (p == "click-gain") {
 
                if (_click_gain) {
-                       _click_gain->set_gain (Config->get_click_gain(), this);
+                       _click_gain->gain_control()->set_value (Config->get_click_gain(), Controllable::NoGroup);
                }
 
        } else if (p == "send-mtc") {