decrease LTC flywheel time and adjust DLL settings
[ardour.git] / libs / evoral / src / ControlList.cpp
index 75b591d6a3dcbbaabb37cb5349777c778a0dacfb..d2ef1fe246f2ef0887a117a16d8c5af4bd6f9dc3 100644 (file)
 #include "evoral/ControlList.hpp"
 #include "evoral/Curve.hpp"
 
+#include "pbd/compose.h"
+
 using namespace std;
+using namespace PBD;
 
 namespace Evoral {
 
-
 inline bool event_time_less_than (ControlEvent* a, ControlEvent* b)
 {
        return a->when < b->when;
@@ -64,6 +66,11 @@ ControlList::ControlList (const Parameter& id)
        _search_cache.left = -1;
        _search_cache.first = _events.end();
        _sort_pending = false;
+       new_write_pass = true;
+       _in_write_pass = false;
+       did_write_during_pass = false;
+       insert_position = -1;
+       most_recent_insert_iterator = _events.end();
 }
 
 ControlList::ControlList (const ControlList& other)
@@ -79,10 +86,13 @@ ControlList::ControlList (const ControlList& other)
        _lookup_cache.range.first = _events.end();
        _search_cache.first = _events.end();
        _sort_pending = false;
+       new_write_pass = true;
+       _in_write_pass = false;
+       did_write_during_pass = false;
+       insert_position = -1;
+       most_recent_insert_iterator = _events.end();
 
-       for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
-               _events.push_back (new ControlEvent (**i));
-       }
+       copy_events (other);
 
        mark_dirty ();
 }
@@ -106,11 +116,15 @@ ControlList::ControlList (const ControlList& other, double start, double end)
        boost::shared_ptr<ControlList> section = const_cast<ControlList*>(&other)->copy (start, end);
 
        if (!section->empty()) {
-               for (iterator i = section->begin(); i != section->end(); ++i) {
-                       _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
-               }
+               copy_events (*(section.get()));
        }
 
+       new_write_pass = false;
+       _in_write_pass = false;
+       did_write_during_pass = false;
+       insert_position = -1;
+       most_recent_insert_iterator = _events.end();
+
        mark_dirty ();
 }
 
@@ -120,13 +134,6 @@ ControlList::~ControlList()
                delete (*x);
        }
 
-       for (list<NascentInfo*>::iterator n = nascent.begin(); n != nascent.end(); ++n) {
-               for (EventList::iterator x = (*n)->events.begin(); x != (*n)->events.end(); ++x) {
-                       delete *x;
-               }
-               delete (*n);
-       }
-
        delete _curve;
 }
 
@@ -147,23 +154,31 @@ ControlList::operator= (const ControlList& other)
 {
        if (this != &other) {
 
-               _events.clear ();
-
-               for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
-                       _events.push_back (new ControlEvent (**i));
-               }
-
                _min_yval = other._min_yval;
                _max_yval = other._max_yval;
                _default_value = other._default_value;
-
-               mark_dirty ();
-               maybe_signal_changed ();
+               
+               copy_events (other);
        }
 
        return *this;
 }
 
+void
+ControlList::copy_events (const ControlList& other)
+{
+       {
+               Glib::Threads::Mutex::Lock lm (_lock);
+               _events.clear ();
+               for (const_iterator i = other.begin(); i != other.end(); ++i) {
+                       _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
+               }
+               unlocked_invalidate_insert_iterator ();
+               mark_dirty ();
+       }
+       maybe_signal_changed ();
+}
+
 void
 ControlList::create_curve()
 {
@@ -191,8 +206,9 @@ void
 ControlList::clear ()
 {
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
                _events.clear ();
+               unlocked_invalidate_insert_iterator ();
                mark_dirty ();
        }
 
@@ -202,14 +218,14 @@ ControlList::clear ()
 void
 ControlList::x_scale (double factor)
 {
-       Glib::Mutex::Lock lm (_lock);
+       Glib::Threads::Mutex::Lock lm (_lock);
        _x_scale (factor);
 }
 
 bool
 ControlList::extend_to (double when)
 {
-       Glib::Mutex::Lock lm (_lock);
+       Glib::Threads::Mutex::Lock lm (_lock);
        if (_events.empty() || _events.back()->when == when) {
                return false;
        }
@@ -228,13 +244,6 @@ ControlList::_x_scale (double factor)
        mark_dirty ();
 }
 
-void
-ControlList::write_pass_finished (double when)
-{
-       merge_nascent (when);
-}
-
-
 struct ControlEventTimeComparator {
        bool operator() (ControlEvent* a, ControlEvent* b) {
                return a->when < b->when;
@@ -242,269 +251,400 @@ struct ControlEventTimeComparator {
 };
 
 void
-ControlList::merge_nascent (double when)
+ControlList::thin ()
 {
+       bool changed = false;
+
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
+               
+               ControlEvent* prevprev = 0;
+               ControlEvent* cur = 0;
+               ControlEvent* prev = 0;
+               iterator pprev;
+               int counter = 0;
+               
+               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 thin from %2 events\n", this, _events.size()));
+               
+               for (iterator i = _events.begin(); i != _events.end(); ++i) {
+                       
+                       cur = *i;
+                       counter++;
+                       
+                       if (counter > 2) {
+                               
+                               /* compute the area of the triangle formed by 3 points
+                                */
+                               
+                               double area = fabs ((prevprev->when * (prev->value - cur->value)) + 
+                                                   (prev->when * (cur->value - prevprev->value)) + 
+                                                   (cur->when * (prevprev->value - prev->value)));
+                               
+                               if (area < _thinning_factor) {
+                                       iterator tmp = pprev;
+                                       
+                                       /* pprev will change to current
+                                          i is incremented to the next event
+                                          as we loop.
+                                       */
+                                       
+                                       pprev = i;
+                                       _events.erase (tmp);
+                                       changed = true;
+                                       continue;
+                               }
+                       }
+                       
+                       prevprev = prev;
+                       prev = cur;
+                       pprev = i;
+               }
+               
+               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 thin => %2 events\n", this, _events.size()));
 
-               if (nascent.empty()) {
-                       return;
+               if (changed) {
+                       unlocked_invalidate_insert_iterator ();
+                       mark_dirty ();
                }
+       }
 
-               for (list<NascentInfo*>::iterator n = nascent.begin(); n != nascent.end(); ++n) {
+       if (changed) {
+               maybe_signal_changed ();
+       }
+}
 
-                       NascentInfo* ninfo = *n;
-                       EventList& nascent_events (ninfo->events);
-                       bool need_adjacent_start_clamp;
-                       bool need_adjacent_end_clamp;
+void
+ControlList::fast_simple_add (double when, double value)
+{
+       Glib::Threads::Mutex::Lock lm (_lock);
+       /* to be used only for loading pre-sorted data from saved state */
+       _events.insert (_events.end(), new ControlEvent (when, value));
+       assert(_events.back());
 
-                       if (nascent_events.empty()) {
-                               delete ninfo;
-                               continue;
-                       }
+       mark_dirty ();
+}
 
-                       nascent_events.sort (ControlEventTimeComparator ());
+void
+ControlList::invalidate_insert_iterator ()
+{
+       Glib::Threads::Mutex::Lock lm (_lock);
+       unlocked_invalidate_insert_iterator ();
+}
 
-                       if (ninfo->start_time < 0.0) {
-                               ninfo->start_time = nascent_events.front()->when;
-                       }
+void
+ControlList::unlocked_invalidate_insert_iterator ()
+{
+       most_recent_insert_iterator = _events.end();
+}
 
-                       if (ninfo->end_time < 0.0) {
-                               ninfo->end_time = when;
-                       }
+void
+ControlList::start_write_pass (double when)
+{
+       Glib::Threads::Mutex::Lock lm (_lock);
 
-                       bool preexisting = !_events.empty();
+       new_write_pass = true;
+       did_write_during_pass = false;
+       insert_position = when;
 
-                       if (!preexisting) {
+       /* leave the insert iterator invalid, so that we will do the lookup
+          of where it should be in a "lazy" way - deferring it until
+          we actually add the first point (which may never happen).
+       */
 
-                               _events = nascent_events;
+       unlocked_invalidate_insert_iterator ();
+}
 
-                       } else if (ninfo->end_time < _events.front()->when) {
+void
+ControlList::write_pass_finished (double when)
+{
+       if (did_write_during_pass) {
+               thin ();
+               did_write_during_pass = false;
+       }
+       new_write_pass = true;
+       _in_write_pass = false;
+}
 
-                               /* all points in nascent are before the first existing point */
+void
+ControlList::set_in_write_pass (bool yn)
+{
+       _in_write_pass = yn;
+}
 
-                               _events.insert (_events.begin(), nascent_events.begin(), nascent_events.end());
+bool
+ControlList::in_write_pass () const
+{
+       return _in_write_pass;
+}
 
-                       } else if (ninfo->start_time > _events.back()->when) {
+void
+ControlList::add (double when, double value)
+{
+       /* this is for making changes from some kind of user interface or
+          control surface (GUI, MIDI, OSC etc)
+       */
 
-                               /* all points in nascent are after the last existing point */
+       if (!clamp_value (when, value)) {
+               return;
+       }
 
-                               _events.insert (_events.end(), nascent_events.begin(), nascent_events.end());
+       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 add %2 at %3 w/erase = %4 at end ? %5\n", 
+                                                        this, value, when, _in_write_pass, (most_recent_insert_iterator == _events.end())));
 
-                       } else {
+       {
+               Glib::Threads::Mutex::Lock lm (_lock);
+               ControlEvent cp (when, 0.0f);
+               iterator insertion_point;
 
-                               /* find the range that overlaps with nascent events,
-                                  and insert the contents of nascent events.
-                               */
+               if (_events.empty()) {
+                       
+                       /* as long as the point we're adding is not at zero,
+                        * add an "anchor" point there.
+                        */
 
-                               iterator i;
-                               iterator range_begin = _events.end();
-                               iterator range_end = _events.end();
-                               double end_value = unlocked_eval (ninfo->end_time);
-                               double start_value = unlocked_eval (ninfo->start_time - 1);
+                       if (when > 1) {
+                               _events.insert (_events.end(), new ControlEvent (0, _default_value));
+                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added default value %2 at zero\n", this, _default_value));
+                       }
+               }
 
-                               need_adjacent_end_clamp = true;
-                               need_adjacent_start_clamp = true;
+               if (_in_write_pass && new_write_pass) {
 
-                               for (i = _events.begin(); i != _events.end(); ++i) {
+                       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 new write pass, insert pos = %2\n", this, insert_position));
+                       
+                       /* The first addition of a new control event during a
+                        * write pass.
+                        *
+                        * We need to add a new point at insert_position
+                        * corresponding the value there. 
+                        */
 
-                                       if ((*i)->when == ninfo->start_time) {
-                                               /* existing point at same time, remove it
-                                                  and the consider the next point instead.
-                                               */
-                                               i = _events.erase (i);
+                       /* the insert_iterator is not set, figure out where
+                        * it needs to be.
+                        */
+                       
+                       ControlEvent cp (insert_position, 0.0);
+                       most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
+                       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 looked up insert iterator for new write pass\n", this));
 
-                                               if (i == _events.end()) {
-                                                       break;
-                                               }
+                       double eval_value = unlocked_eval (insert_position);
+                       
+                       if (most_recent_insert_iterator == _events.end()) {
 
-                                               if (range_begin == _events.end()) {
-                                                       range_begin = i;
-                                                       need_adjacent_start_clamp = false;
-                                               } else {
-                                                       need_adjacent_end_clamp = false;
-                                               }
+                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at end, adding eval-value there %2\n", this, eval_value));
+                               _events.push_back (new ControlEvent (insert_position, eval_value));
+                               /* leave insert iterator at the end */
 
-                                               if ((*i)->when > ninfo->end_time) {
-                                                       range_end = i;
-                                                       break;
-                                               }
+                       } else if ((*most_recent_insert_iterator)->when == when) {
 
-                                       } else if ((*i)->when > ninfo->start_time) {
+                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at existing point, setting eval-value there %2\n", this, eval_value));
 
-                                               if (range_begin == _events.end()) {
-                                                       range_begin = i;
-                                               }
+                               /* most_recent_insert_iterator points to a control event
+                                  already at the insert position, so there is
+                                  nothing to do.
 
-                                               if ((*i)->when > ninfo->end_time) {
-                                                       range_end = i;
-                                                       break;
-                                               }
-                                       }
-                               }
+                                  ... except ... 
 
-                               /* Now:
-                                  range_begin is the first event on our list after the first nascent event
-                                  range_end   is the first event on our list after the last  nascent event
+                                  advance most_recent_insert_iterator so that the "real"
+                                  insert occurs in the right place, since it 
+                                  points to the control event just inserted.
+                                */
 
-                                  range_begin may be equal to _events.end() iff the last event on our list
-                                  was at the same time as the first nascent event.
-                               */
+                               ++most_recent_insert_iterator;
+                       } else {
 
-                               if (range_begin != _events.begin()) {
-                                       /* clamp point before */
-                                       if (need_adjacent_start_clamp) {
-                                               _events.insert (range_begin, new ControlEvent (ninfo->start_time, start_value));
-                                       }
-                               }
+                               /* insert a new control event at the right spot
+                                */
 
-                               _events.insert (range_begin, nascent_events.begin(), nascent_events.end());
+                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert eval-value %2 just before iterator @ %3\n", 
+                                                                                this, eval_value, (*most_recent_insert_iterator)->when));
 
-                               if (range_end != _events.end()) {
-                                       /* clamp point after */
-                                       if (need_adjacent_end_clamp) {
-                                               _events.insert (range_begin, new ControlEvent (ninfo->end_time, end_value));
-                                       }
-                               }
+                               most_recent_insert_iterator = _events.insert (most_recent_insert_iterator, new ControlEvent (insert_position, eval_value));
 
-                               _events.erase (range_begin, range_end);
+                               /* advance most_recent_insert_iterator so that the "real"
+                                * insert occurs in the right place, since it 
+                                * points to the control event just inserted.
+                                */
+
+                               ++most_recent_insert_iterator;
                        }
 
-                       delete ninfo;
-               }
+                       /* don't do this again till the next write pass */
+                       
+                       new_write_pass = false;
+                       did_write_during_pass = true;
 
-               nascent.clear ();
+               } else if (most_recent_insert_iterator == _events.end() || when > (*most_recent_insert_iterator)->when) {
 
-               if (writing()) {
-                       nascent.push_back (new NascentInfo ());
-               }
-       }
+                       /* this is NOT the first point to be added after the
+                          start of a write pass, and we have a bit of work to
+                          do figuring out where to add the new point, as well
+                          as potentially erasing existing data between the
+                          most recently added point and wherever this one
+                          will end up.
+                       */
+                               
+                       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 need to discover insert iterator (@end ? %2)\n", 
+                                                                        this, (most_recent_insert_iterator == _events.end())));
+
+                       /* this means that we either *know* we want to insert
+                        * at the end, or that we don't know where to insert.
+                        * 
+                        * so ... lets perform some quick checks before we
+                        * go doing binary search to figure out where to
+                        * insert.
+                        */
 
-       maybe_signal_changed ();
-}
+                       if (_events.back()->when == when) {
 
-void
-ControlList::rt_add (double when, double value)
-{
-       // this is for automation recording
+                               /* we need to modify the final point, so 
+                                  make most_recent_insert_iterator point to it.
+                               */
 
-       if (touch_enabled() && !touching()) {
-               return;
-       }
+                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 modify final value\n", this));
+                               
+                               most_recent_insert_iterator = _events.end();
+                               --most_recent_insert_iterator;
 
-       //cerr << "RT: alist " << this << " add " << value << " @ " << when << endl;
+                       } else if (_events.back()->when < when) {
 
-       Glib::Mutex::Lock lm (_lock, Glib::TRY_LOCK);
+                               /* the new point is beyond the end of the
+                                * current list
+                                */
 
-       if (lm.locked()) {
-               assert (!nascent.empty());
-               /* we don't worry about adding events out of time order as we will
-                  sort them in merge_nascent.
-               */
+                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 plan to append to list\n", this));
 
-               EventList& el (nascent.back()->events);
-
-               if (el.size() > 1 && (when >= el.back()->when) && (value == el.back()->value)) {
-                       /* same value, later timestamp, effective slope is
-                        * zero, so just move the last point in nascent to our
-                        * new time position. this avoids storing an unlimited
-                        * number of points to represent a flat line.
-                        */
-                       el.back()->when = when;
-               } else {
-                       nascent.back()->events.push_back (new ControlEvent (when, value));
-               }
-       }
-}
+                               if (_in_write_pass) {
+                                       /* remove the final point, because
+                                          we're adding one beyond it.
+                                       */
+                                       delete _events.back();
+                                       _events.pop_back();
+                               }
+                               
+                               /* leaving this here will force an append */
 
-void
-ControlList::thin ()
-{
-       Glib::Mutex::Lock lm (_lock);
+                               most_recent_insert_iterator = _events.end();
 
-       ControlEvent* prevprev;
-       ControlEvent* cur;
-       ControlEvent* prev;
-       iterator pprev;
-       int counter = 0;
+                       } else {
 
-       for (iterator i = _events.begin(); i != _events.end(); ++i) {
+                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase %2 from existing iterator (@end ? %3\n", 
+                                                                                this, _in_write_pass,
+                                                                                (most_recent_insert_iterator == _events.end())));
+
+                               if (_in_write_pass) {
+                                       while (most_recent_insert_iterator != _events.end()) {
+                                               if ((*most_recent_insert_iterator)->when < when) {
+                                                       if (_in_write_pass) {
+                                                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase existing @ %2\n", this, (*most_recent_insert_iterator)));
+                                                               delete *most_recent_insert_iterator;
+                                                               most_recent_insert_iterator = _events.erase (most_recent_insert_iterator);
+                                                               continue;
+                                                       }
+                                               } else if ((*most_recent_insert_iterator)->when >= when) {
+                                                       break;
+                                               }
+                                               ++most_recent_insert_iterator;
+                                       }
+                               } else {
+                                       
+                                       /* not in a write pass: figure out the iterator we should insert in front of */
 
-               cur = *i;
-               counter++;
+                                       ControlEvent cp (when, 0.0f);
+                                       most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
+                               }
+                       }
+               } 
+               
+               /* OK, now we're really ready to add a new point
+                */
 
-               if (counter > 2) {
+               if (most_recent_insert_iterator == _events.end()) {
+                       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 appending new point at end\n", this));
                        
-                       double area = fabs ((prevprev->when * (prev->value - cur->value)) + 
-                                           (prev->when * (cur->value - prevprev->value)) + 
-                                           (cur->when * (prevprev->value - prev->value)));
-                       
-                       if (area < _thinning_factor) {
-                               iterator tmp = pprev;
-
-                               /* pprev will change to current
-                                  i is incremented to the next event
-                               */
+                       bool done = false;
 
-                               pprev = i;
-                               _events.erase (tmp);
+                       /* check if would just be adding to a straight line,
+                        * and don't add another point if so
+                        */
 
-                               continue;
+                       if (!_events.empty()) { // avoid O(N) _events.size() here
+                               if (_events.back()->value == value) {
+                                       EventList::iterator b = _events.end();
+                                       --b; // final point, which we know exists
+                                       if (b != _events.begin()) { // step back again, but check first that it is legal
+                                               --b; // penultimate-point
+                                               if ((*b)->value == value) {
+                                                       /* there are at least two points with the exact same value ...
+                                                        * straight line - just move the final
+                                                        * point to the new time
+                                                        */
+                                                       _events.back()->when = when;
+                                                       done = true;
+                                               }
+                                       }
+                               }
                        }
-               }
-
-               prevprev = prev;
-               prev = cur;
-               pprev = i;
-       }
-}
 
-void
-ControlList::fast_simple_add (double when, double value)
-{
-       /* to be used only for loading pre-sorted data from saved state */
-       _events.insert (_events.end(), new ControlEvent (when, value));
-       assert(_events.back());
-}
+                       if (!done) {
+                               _events.push_back (new ControlEvent (when, value));
+                       }
 
-void
-ControlList::add (double when, double value)
-{
-       /* this is for making changes from some kind of user interface or
-          control surface (GUI, MIDI, OSC etc)
-       */
+                       if (!_in_write_pass) {
+                               most_recent_insert_iterator = _events.end();
+                               --most_recent_insert_iterator;
+                       }
 
-       if (!clamp_value (when, value)) {
-               return;
-       }
+               } else if ((*most_recent_insert_iterator)->when == when) {
+                       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 reset existing point to new value %2\n", this, value));
 
-       {
-               Glib::Mutex::Lock lm (_lock);
-               ControlEvent cp (when, 0.0f);
-               bool insert = true;
-               iterator insertion_point;
+                       /* only one point allowed per time point, so just
+                        * reset the value here.
+                        */
 
-               for (insertion_point = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); insertion_point != _events.end(); ++insertion_point) {
+                       (*most_recent_insert_iterator)->value = value;
 
-                       /* only one point allowed per time point */
+               } else {
+                       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert new point at %2 at iterator at %3\n", this, when, (*most_recent_insert_iterator)->when));
+                       
+                       bool done = false;
+                       
+                       /* check if would just be adding to a straight line,
+                        * and don't add another point if so
+                        */
 
-                       if ((*insertion_point)->when == when) {
-                               (*insertion_point)->value = value;
-                               insert = false;
-                               break;
+                       if (most_recent_insert_iterator != _events.begin()) {
+                               EventList::iterator b = most_recent_insert_iterator;
+                               --b; // prior point, which we know exists
+                               if ((*b)->value == value) { // same value as the point we plan to insert
+                                       if (b != _events.begin()) { // step back again, which may not be possible
+                                               EventList::iterator bb = b;
+                                               --bb; // next-to-prior-point
+                                               if ((*bb)->value == value) {
+                                                       /* straight line - just move the prior
+                                                        * point to the new time
+                                                        */
+                                                       (*b)->when = when;
+                                                       
+                                                       if (!_in_write_pass) {
+                                                               most_recent_insert_iterator = b;
+                                                       }
+
+                                                       done = true;
+                                               }
+                                       }
+                               }
                        }
+                       
+                       if (!done) {
+                               EventList::iterator x = _events.insert (most_recent_insert_iterator, new ControlEvent (when, value));
 
-                       if ((*insertion_point)->when >= when) {
-                               break;
+                               if (!_in_write_pass) {
+                                       most_recent_insert_iterator = x;
+                               }
                        }
                }
 
-               if (insert) {
-
-                       _events.insert (insertion_point, new ControlEvent (when, value));
-
-               }
-
                mark_dirty ();
        }
 
@@ -515,7 +655,10 @@ void
 ControlList::erase (iterator i)
 {
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
+               if (most_recent_insert_iterator == i) {
+                       unlocked_invalidate_insert_iterator ();
+               }
                _events.erase (i);
                mark_dirty ();
        }
@@ -526,8 +669,9 @@ void
 ControlList::erase (iterator start, iterator end)
 {
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
                _events.erase (start, end);
+               unlocked_invalidate_insert_iterator ();
                mark_dirty ();
        }
        maybe_signal_changed ();
@@ -538,7 +682,7 @@ void
 ControlList::erase (double when, double value)
 {
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
 
                iterator i = begin ();
                while (i != end() && ((*i)->when != when || (*i)->value != value)) {
@@ -547,6 +691,9 @@ ControlList::erase (double when, double value)
 
                if (i != end ()) {
                        _events.erase (i);
+                       if (most_recent_insert_iterator == i) {
+                               unlocked_invalidate_insert_iterator ();
+                       }
                }
 
                mark_dirty ();
@@ -555,44 +702,13 @@ ControlList::erase (double when, double value)
        maybe_signal_changed ();
 }
 
-void
-ControlList::reset_range (double start, double endt)
-{
-       bool reset = false;
-
-       {
-               Glib::Mutex::Lock lm (_lock);
-               ControlEvent cp (start, 0.0f);
-               iterator s;
-               iterator e;
-
-               if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) != _events.end()) {
-
-                       cp.when = endt;
-                       e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
-
-                       for (iterator i = s; i != e; ++i) {
-                               (*i)->value = _default_value;
-                       }
-
-                       reset = true;
-
-                       mark_dirty ();
-               }
-       }
-
-       if (reset) {
-               maybe_signal_changed ();
-       }
-}
-
 void
 ControlList::erase_range (double start, double endt)
 {
        bool erased = false;
 
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
                erased = erase_range_internal (start, endt, _events);
 
                if (erased) {
@@ -619,6 +735,7 @@ ControlList::erase_range_internal (double start, double endt, EventList & events
                e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
                events.erase (s, e);
                if (s != e) {
+                       unlocked_invalidate_insert_iterator ();
                        erased = true;
                }
        }
@@ -630,7 +747,7 @@ void
 ControlList::slide (iterator before, double distance)
 {
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
 
                if (before == _events.end()) {
                        return;
@@ -651,7 +768,7 @@ void
 ControlList::shift (double pos, double frames)
 {
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
 
                for (iterator i = _events.begin(); i != _events.end(); ++i) {
                        if ((*i)->when >= pos) {
@@ -674,7 +791,7 @@ ControlList::modify (iterator iter, double when, double val)
        */
 
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
 
                (*iter)->when = when;
                (*iter)->value = val;
@@ -685,6 +802,7 @@ ControlList::modify (iterator iter, double when, double val)
 
                if (!_frozen) {
                        _events.sort (event_time_less_than);
+                       unlocked_invalidate_insert_iterator ();
                } else {
                        _sort_pending = true;
                }
@@ -698,7 +816,7 @@ ControlList::modify (iterator iter, double when, double val)
 std::pair<ControlList::iterator,ControlList::iterator>
 ControlList::control_points_adjacent (double xval)
 {
-       Glib::Mutex::Lock lm (_lock);
+       Glib::Threads::Mutex::Lock lm (_lock);
        iterator i;
        ControlEvent cp (xval, 0.0f);
        std::pair<iterator,iterator> ret;
@@ -744,10 +862,11 @@ ControlList::thaw ()
        }
 
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
 
                if (_sort_pending) {
                        _events.sort (event_time_less_than);
+                       unlocked_invalidate_insert_iterator ();
                        _sort_pending = false;
                }
        }
@@ -770,7 +889,7 @@ void
 ControlList::truncate_end (double last_coordinate)
 {
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
                ControlEvent cp (last_coordinate, 0);
                ControlList::reverse_iterator i;
                double last_val;
@@ -861,7 +980,8 @@ ControlList::truncate_end (double last_coordinate)
                        _events.back()->when = last_coordinate;
                        _events.back()->value = last_val;
                }
-
+               
+               unlocked_invalidate_insert_iterator ();
                mark_dirty();
        }
 
@@ -872,7 +992,7 @@ void
 ControlList::truncate_start (double overall_length)
 {
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
                iterator i;
                double first_legal_value;
                double first_legal_coordinate;
@@ -961,6 +1081,7 @@ ControlList::truncate_start (double overall_length)
                        _events.push_front (new ControlEvent (0, first_legal_value));
                }
 
+               unlocked_invalidate_insert_iterator ();
                mark_dirty();
        }
 
@@ -1126,7 +1247,7 @@ bool
 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
 {
        // FIXME: It would be nice if this was unnecessary..
-       Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
+       Glib::Threads::Mutex::Lock lm(_lock, Glib::Threads::TRY_LOCK);
        if (!lm.locked()) {
                return false;
        }
@@ -1325,7 +1446,7 @@ ControlList::cut_copy_clear (double start, double end, int op)
        ControlEvent cp (start, 0.0);
 
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
 
                /* first, determine s & e, two iterators that define the range of points
                   affected by this operation
@@ -1402,6 +1523,7 @@ ControlList::cut_copy_clear (double start, double end, int op)
                        }
                }
 
+               unlocked_invalidate_insert_iterator ();
                mark_dirty ();
        }
 
@@ -1440,7 +1562,7 @@ ControlList::paste (ControlList& alist, double pos, float /*times*/)
        }
 
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
                iterator where;
                iterator prev;
                double end = 0;
@@ -1471,6 +1593,7 @@ ControlList::paste (ControlList& alist, double pos, float /*times*/)
                        }
                }
 
+               unlocked_invalidate_insert_iterator ();
                mark_dirty ();
        }
 
@@ -1487,7 +1610,7 @@ ControlList::move_ranges (const list< RangeMove<double> >& movements)
        typedef list< RangeMove<double> > RangeMoveList;
 
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
 
                /* a copy of the events list before we started moving stuff around */
                EventList old_events = _events;
@@ -1527,6 +1650,7 @@ ControlList::move_ranges (const list< RangeMove<double> >& movements)
 
                if (!_frozen) {
                        _events.sort (event_time_less_than);
+                       unlocked_invalidate_insert_iterator ();
                } else {
                        _sort_pending = true;
                }
@@ -1555,5 +1679,33 @@ ControlList::set_thinning_factor (double v)
        _thinning_factor = v;
 }
 
+bool
+ControlList::operator!= (ControlList const & other) const
+{
+       if (_events.size() != other._events.size()) {
+               return true;
+       }
+
+       EventList::const_iterator i = _events.begin ();
+       EventList::const_iterator j = other._events.begin ();
+
+       while (i != _events.end() && (*i)->when == (*j)->when && (*i)->value == (*j)->value) {
+               ++i;
+               ++j;
+       }
+
+       if (i != _events.end ()) {
+               return true;
+       }
+       
+       return (
+               _parameter != other._parameter ||
+               _interpolation != other._interpolation ||
+               _min_yval != other._min_yval ||
+               _max_yval != other._max_yval ||
+               _default_value != other._default_value
+               );
+}
+
 } // namespace Evoral