Less code.
[ardour.git] / libs / ardour / automatable.cc
index 40b6eb2a07a04715c489f3bfe83383887d74db37..03e5ad97c31e7d511222931fb86e24a3085652d5 100644 (file)
 #include <inttypes.h>
 #include <cstdio>
 #include <errno.h>
+
+#include <glibmm/miscutils.h>
+
 #include "pbd/error.h"
 #include "pbd/enumwriter.h"
+#include "pbd/stacktrace.h"
 
 #include "midi++/names.h"
 
@@ -41,7 +45,7 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-nframes_t Automatable::_automation_interval = 0;
+framecnt_t Automatable::_automation_interval = 0;
 
 Automatable::Automatable(Session& session)
        : _a_session(session)
@@ -98,7 +102,7 @@ Automatable::load_automation (const string& path)
 {
        string fullpath;
 
-       if (path[0] == '/') { // legacy
+       if (Glib::path_is_absolute (path)) { // legacy
                fullpath = path;
        } else {
                fullpath = _a_session.automation_dir();
@@ -147,19 +151,17 @@ Automatable::add_control(boost::shared_ptr<Evoral::Control> ac)
 {
        Evoral::Parameter param = ac->parameter();
 
-       ControlSet::add_control(ac);
-       _can_automate_list.insert(param);
-       auto_state_changed(param); // sync everything up
+       boost::shared_ptr<AutomationList> al = boost::dynamic_pointer_cast<AutomationList> (ac->list ());
+       assert (al);
+       
+       al->automation_state_changed.connect_same_thread (
+               _list_connections, boost::bind (&Automatable::automation_list_automation_state_changed, this, ac->parameter(), _1)
+               );
 
-       /* connect to automation_state_changed so that we can emit a signal when one of our controls'
-          automation state changes
-       */
-       boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl> (ac);
-       if (c) {
-               c->alist()->automation_state_changed.connect_same_thread (
-                       _control_connections, boost::bind (&Automatable::automation_state_changed, this, c->parameter())
-                       );
-       }
+       ControlSet::add_control (ac);
+       _can_automate_list.insert (param);
+
+       automation_list_automation_state_changed (param, al->automation_state ()); // sync everything up
 }
 
 void
@@ -184,7 +186,7 @@ Automatable::describe_parameter (Evoral::Parameter param)
                /* ID's are zero-based, present them as 1-based */
                return (string_compose(_("Pan %1"), param.id() + 1));
        } else if (param.type() == MidiCCAutomation) {
-               return string_compose("%2 [%3]",
+               return string_compose("%1: %2 [%3]",
                                param.id() + 1, midi_name(param.id()), int(param.channel()) + 1);
        } else if (param.type() == MidiPgmChangeAutomation) {
                return string_compose("Program [%1]", int(param.channel()) + 1);
@@ -222,7 +224,7 @@ Automatable::mark_automation_visible (Evoral::Parameter what, bool yn)
  * pass that type and it will be used for the untyped AutomationList found.
  */
 int
-Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy_param)
+Automatable::set_automation_xml_state (const XMLNode& node, Evoral::Parameter legacy_param)
 {
        Glib::Mutex::Lock lm (control_lock());
 
@@ -251,7 +253,7 @@ Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy
                        if (param.type() == NullAutomation) {
                                warning << "Automation has null type" << endl;
                                continue;
-                       }
+                        }
 
                        boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
 
@@ -270,7 +272,7 @@ Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy
                        }
 
                } else {
-                       error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
+                       error << "Expected AutomationList node, got '" << (*niter)->name() << "'" << endmsg;
                }
        }
 
@@ -280,7 +282,7 @@ Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy
 }
 
 XMLNode&
-Automatable::get_automation_state ()
+Automatable::get_automation_xml_state ()
 {
        Glib::Mutex::Lock lm (control_lock());
        XMLNode* node = new XMLNode (X_("Automation"));
@@ -315,21 +317,16 @@ Automatable::set_parameter_automation_state (Evoral::Parameter param, AutoState
 }
 
 AutoState
-Automatable::get_parameter_automation_state (Evoral::Parameter param, bool lock)
+Automatable::get_parameter_automation_state (Evoral::Parameter param)
 {
        AutoState result = Off;
 
-       if (lock)
-               control_lock().lock();
-
        boost::shared_ptr<Evoral::Control> c = control(param);
        boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
 
-       if (c)
+       if (c) {
                result = l->automation_state();
-
-       if (lock)
-               control_lock().unlock();
+       }
 
        return result;
 }
@@ -390,15 +387,15 @@ Automatable::protect_automation ()
 }
 
 void
-Automatable::automation_snapshot (nframes_t now, bool force)
+Automatable::automation_snapshot (framepos_t now, bool force)
 {
        if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
 
                for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
                        boost::shared_ptr<AutomationControl> c
                                        = boost::dynamic_pointer_cast<AutomationControl>(i->second);
-                       if (c->automation_write()) {
-                               c->list()->rt_add (now, i->second->user_float());
+                       if (_a_session.transport_rolling() && c->automation_write()) {
+                               c->list()->rt_add (now, i->second->user_double());
                        }
                }
 
@@ -407,20 +404,28 @@ Automatable::automation_snapshot (nframes_t now, bool force)
 }
 
 void
-Automatable::transport_stopped (sframes_t now)
+Automatable::transport_stopped (framepos_t now)
 {
        for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
 
                boost::shared_ptr<AutomationControl> c
                                = boost::dynamic_pointer_cast<AutomationControl>(li->second);
-               boost::shared_ptr<AutomationList> l
+                if (c) {
+                        boost::shared_ptr<AutomationList> l
                                = boost::dynamic_pointer_cast<AutomationList>(c->list());
-
-               c->list()->reposition_for_rt_add (now);
-
-               if (c->automation_state() != Off) {
-                       c->set_value(c->list()->eval(now));
-               }
+                        
+                        if (l) {
+                                l->write_pass_finished (now);
+                                
+                                if (l->automation_playback()) {
+                                        c->set_value(c->list()->eval(now));
+                                }
+                                
+                                if (l->automation_state() == Write) {
+                                        l->set_automation_state (Touch);
+                                }
+                        }
+                }
        }
 }
 
@@ -451,9 +456,10 @@ Automatable::control_factory(const Evoral::Parameter& param)
                        warning << "GainAutomation for non-Amp" << endl;
                }
        } else if (param.type() == PanAutomation) {
-               Panner* me = dynamic_cast<Panner*>(this);
-               if (me) {
-                       control = new Panner::PanControllable(me->session(), X_("panner"), *me, param);
+               Panner* panner = dynamic_cast<Panner*>(this);
+               if (panner) {
+                        StreamPanner& sp (panner->streampanner (param.channel()));
+                       control = new StreamPanner::PanControllable (_a_session, X_("direction"), &sp, param);
                } else {
                        warning << "PanAutomation for non-Panner" << endl;
                }
@@ -479,16 +485,9 @@ Automatable::automation_control (const Evoral::Parameter& id) const
        return boost::dynamic_pointer_cast<const AutomationControl>(Evoral::ControlSet::control(id));
 }
 
-void
-Automatable::automation_state_changed (Evoral::Parameter const & p)
-{
-       AutomationStateChanged (p); /* EMIT SIGNAL */
-}
-
 void
 Automatable::clear_controls ()
 {
        _control_connections.drop_connections ();
        ControlSet::clear_controls ();
 }
-