decrease LTC flywheel time and adjust DLL settings
[ardour.git] / libs / evoral / src / ControlList.cpp
index 9ae269453d7678399f952c9a26adbf1c69bd1933..d2ef1fe246f2ef0887a117a16d8c5af4bd6f9dc3 100644 (file)
@@ -67,9 +67,10 @@ ControlList::ControlList (const Parameter& id)
        _search_cache.first = _events.end();
        _sort_pending = false;
        new_write_pass = true;
+       _in_write_pass = false;
        did_write_during_pass = false;
        insert_position = -1;
-       insert_iterator = _events.end();
+       most_recent_insert_iterator = _events.end();
 }
 
 ControlList::ControlList (const ControlList& other)
@@ -86,9 +87,10 @@ ControlList::ControlList (const ControlList& other)
        _search_cache.first = _events.end();
        _sort_pending = false;
        new_write_pass = true;
+       _in_write_pass = false;
        did_write_during_pass = false;
        insert_position = -1;
-       insert_iterator = _events.end();
+       most_recent_insert_iterator = _events.end();
 
        copy_events (other);
 
@@ -118,9 +120,10 @@ ControlList::ControlList (const ControlList& other, double start, double end)
        }
 
        new_write_pass = false;
+       _in_write_pass = false;
        did_write_during_pass = false;
        insert_position = -1;
-       insert_iterator = _events.end();
+       most_recent_insert_iterator = _events.end();
 
        mark_dirty ();
 }
@@ -165,7 +168,7 @@ void
 ControlList::copy_events (const ControlList& other)
 {
        {
-               Glib::Mutex::Lock lm (_lock);
+               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));
@@ -203,7 +206,7 @@ void
 ControlList::clear ()
 {
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
                _events.clear ();
                unlocked_invalidate_insert_iterator ();
                mark_dirty ();
@@ -215,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;
        }
@@ -241,222 +244,19 @@ ControlList::_x_scale (double factor)
        mark_dirty ();
 }
 
-void
-ControlList::write_pass_finished (double when)
-{
-       if (did_write_during_pass) {
-               thin ();
-       }
-       new_write_pass = true;
-       did_write_during_pass = false;
-}
-
 struct ControlEventTimeComparator {
        bool operator() (ControlEvent* a, ControlEvent* b) {
                return a->when < b->when;
        }
 };
 
-#if 0
-
-void
-ControlList::merge_nascent (double when)
-{
-       {
-               Glib::Mutex::Lock lm (_lock);
-
-               if (nascent.empty()) {
-                       return;
-               }
-
-               bool was_empty = _events.empty();
-                       
-               for (list<NascentInfo*>::iterator n = nascent.begin(); n != nascent.end(); ++n) {
-
-                       NascentInfo* ninfo = *n;
-                       EventList& nascent_events (ninfo->events);
-                       bool need_adjacent_start_clamp;
-                       bool need_adjacent_end_clamp;
-                       EventList::iterator at;
-
-                       if (nascent_events.empty()) {
-                               delete ninfo;
-                               continue;
-                       }
-
-                       nascent_events.sort (ControlEventTimeComparator ());
-
-                       if (ninfo->start_time < 0.0) {
-                               ninfo->start_time = nascent_events.front()->when;
-                       }
-
-                       if (ninfo->end_time < 0.0) {
-                               ninfo->end_time = when;
-                       }
-
-                       if (_events.empty()) {
-
-                               /* add an initial point just before
-                                  the nascent data, unless nascent_events
-                                  contains a point at zero or one
-                               */
-
-                               if (ninfo->start_time > 0) {
-                                       nascent_events.insert (nascent_events.begin(), new ControlEvent (ninfo->start_time - 1, _default_value));
-                               }
-
-                               /* add closing "clamp" point before we insert */
-
-                               nascent_events.insert (nascent_events.end(), new ControlEvent (ninfo->end_time + 1, _default_value));
-
-                               /* insert - front or back doesn't matter since
-                                * _events is empty
-                                */
-
-                               _events.insert (_events.begin(), nascent_events.begin(), nascent_events.end());
-
-                       } else if (ninfo->end_time < _events.front()->when) {
-                               
-                               /* all points in nascent are before the first existing point */
-
-                               if (ninfo->start_time > (_events.front()->when + 1)) {
-                                       nascent_events.insert (nascent_events.begin(), new ControlEvent (ninfo->start_time - 1, _default_value));
-                               }
-
-                               /* add closing "clamp" point before we insert */
-
-                               nascent_events.insert (nascent_events.end(), new ControlEvent (ninfo->end_time + 1, _default_value));
-
-                               /* insert at front */
-
-                               _events.insert (_events.begin(), nascent_events.begin(), nascent_events.end());
-                               
-                               /* now add another default control point right
-                                  after the inserted nascent data 
-                               */
-
-                       } else if (ninfo->start_time > _events.back()->when) {
-
-                               /* all points in nascent are after the last existing point */
-
-                               if (ninfo->start_time > (_events.back()->when + 1)) {
-                                       nascent_events.insert (nascent_events.begin(), new ControlEvent (ninfo->start_time - 1, _default_value));
-                               }
-
-                               /* add closing "clamp" point before we insert */
-
-                               nascent_events.insert (nascent_events.end(), new ControlEvent (ninfo->end_time + 1, _default_value));
-
-                               /* insert */
-
-                               _events.insert (_events.end(), nascent_events.begin(), nascent_events.end());
-
-                       } else {
-
-                               /* find the range that overlaps with nascent events,
-                                  and insert the contents of nascent events.
-                               */
-
-                               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);
-
-                               need_adjacent_end_clamp = true;
-                               need_adjacent_start_clamp = true;
-
-                               for (i = _events.begin(); i != _events.end(); ++i) {
-
-                                       if ((*i)->when == ninfo->start_time) {
-                                               /* existing point at same time, remove it
-                                                  and the consider the next point instead.
-                                               */
-                                               i = _events.erase (i);
-
-                                               if (i == _events.end()) {
-                                                       break;
-                                               }
-
-                                               if (range_begin == _events.end()) {
-                                                       range_begin = i;
-                                                       need_adjacent_start_clamp = false;
-                                               } else {
-                                                       need_adjacent_end_clamp = false;
-                                               }
-
-                                               if ((*i)->when > ninfo->end_time) {
-                                                       range_end = i;
-                                                       break;
-                                               }
-
-                                       } else if ((*i)->when > ninfo->start_time) {
-
-                                               if (range_begin == _events.end()) {
-                                                       range_begin = i;
-                                               }
-
-                                               if ((*i)->when > ninfo->end_time) {
-                                                       range_end = i;
-                                                       break;
-                                               }
-                                       }
-                               }
-
-                               /* 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
-
-                                  range_begin may be equal to _events.end() if the last event on our list
-                                  was at the same time as the first nascent event.
-                               */
-
-                               if (range_begin != _events.begin()) {
-                                       /* clamp point before */
-                                       if (need_adjacent_start_clamp) {
-                                               _events.insert (range_begin, new ControlEvent (ninfo->start_time, start_value));
-                                       }
-                               }
-
-                               _events.insert (range_begin, nascent_events.begin(), nascent_events.end());
-
-                               if (range_end != _events.end()) {
-                                       /* clamp point after */
-                                       if (need_adjacent_end_clamp) {
-                                               _events.insert (range_begin, new ControlEvent (ninfo->end_time, end_value));
-                                       }
-                               }
-
-                               _events.erase (range_begin, range_end);
-                       }
-
-                       delete ninfo;
-               }
-
-               if (was_empty && !_events.empty()) {
-                       if (_events.front()->when != 0) {
-                               _events.insert (_events.begin(), new ControlEvent (0, _default_value));
-                       }
-               }
-
-               nascent.clear ();
-
-               if (writing()) {
-                       nascent.push_back (new NascentInfo ());
-               }
-       }
-
-       maybe_signal_changed ();
-}
-#endif
-
 void
 ControlList::thin ()
 {
        bool changed = false;
 
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
                
                ControlEvent* prevprev = 0;
                ControlEvent* cur = 0;
@@ -516,7 +316,7 @@ ControlList::thin ()
 void
 ControlList::fast_simple_add (double when, double value)
 {
-       Glib::Mutex::Lock lm (_lock);
+       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());
@@ -527,39 +327,58 @@ ControlList::fast_simple_add (double when, double value)
 void
 ControlList::invalidate_insert_iterator ()
 {
-       Glib::Mutex::Lock lm (_lock);
+       Glib::Threads::Mutex::Lock lm (_lock);
        unlocked_invalidate_insert_iterator ();
 }
 
 void
 ControlList::unlocked_invalidate_insert_iterator ()
 {
-       insert_iterator = _events.end();
+       most_recent_insert_iterator = _events.end();
 }
 
 void
 ControlList::start_write_pass (double when)
 {
-       Glib::Mutex::Lock lm (_lock);
+       Glib::Threads::Mutex::Lock lm (_lock);
 
        new_write_pass = true;
        did_write_during_pass = false;
        insert_position = when;
 
-       ControlEvent cp (when, 0.0);
-       insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
+       /* 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).
+       */
 
-       if ((*insert_iterator)->when != when) {
-               /* doesn't point at a control point at precisely this time,
-                  so reset it to the end and we'll find where to insert
-                  if/when a new control event is added.
-               */
-               unlocked_invalidate_insert_iterator ();
+       unlocked_invalidate_insert_iterator ();
+}
+
+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;
 }
 
 void
-ControlList::add (double when, double value, bool erase_since_last_add)
+ControlList::set_in_write_pass (bool yn)
+{
+       _in_write_pass = yn;
+}
+
+bool
+ControlList::in_write_pass () const
+{
+       return _in_write_pass;
+}
+
+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)
@@ -569,10 +388,11 @@ ControlList::add (double when, double value, bool erase_since_last_add)
                return;
        }
 
-       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 add %2 at %3 w/erase = %4\n", this, value, when, erase_since_last_add));
+       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())));
 
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
                ControlEvent cp (when, 0.0f);
                iterator insertion_point;
 
@@ -588,10 +408,9 @@ ControlList::add (double when, double value, bool erase_since_last_add)
                        }
                }
 
-               if (new_write_pass) {
+               if (_in_write_pass && new_write_pass) {
 
-                       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 new write pass, insert pos = %2, iter @ end ? %3\n",
-                                                                        this, insert_position, (insert_iterator == _events.end())));
+                       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.
@@ -600,55 +419,54 @@ ControlList::add (double when, double value, bool erase_since_last_add)
                         * corresponding the value there. 
                         */
 
-                       if (insert_iterator == _events.end()) {
-                               /* the insert_iterator is not set, figure out where
-                                * it needs to be.
-                                */
-
-                               ControlEvent cp (insert_position, 0.0);
-                               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));
-                       }
+                       /* 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));
 
                        double eval_value = unlocked_eval (insert_position);
                        
-                       if (insert_iterator == _events.end()) {
-                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at end, adding eval-value there %2\n", this, eval_value));
+                       if (most_recent_insert_iterator == _events.end()) {
 
+                               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 */
 
-                       } else if ((*insert_iterator)->when == when) {
+                       } else if ((*most_recent_insert_iterator)->when == when) {
 
                                DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at existing point, setting eval-value there %2\n", this, eval_value));
 
-                               /* insert_iterator points to a control event
+                               /* most_recent_insert_iterator points to a control event
                                   already at the insert position, so there is
                                   nothing to do.
 
                                   ... except ... 
 
-                                  advance insert_iterator so that the "real"
+                                  advance most_recent_insert_iterator so that the "real"
                                   insert occurs in the right place, since it 
                                   points to the control event just inserted.
                                 */
 
-                               ++insert_iterator;
+                               ++most_recent_insert_iterator;
                        } else {
 
                                /* insert a new control event at the right spot
                                 */
 
-                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert eval-value %2 at iterator\n", this, eval_value));
+                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert eval-value %2 just before iterator @ %3\n", 
+                                                                                this, eval_value, (*most_recent_insert_iterator)->when));
 
-                               insert_iterator = _events.insert (insert_iterator, new ControlEvent (insert_position, eval_value));
+                               most_recent_insert_iterator = _events.insert (most_recent_insert_iterator, new ControlEvent (insert_position, eval_value));
 
-                               /* advance insert_iterator so that the "real"
+                               /* advance most_recent_insert_iterator so that the "real"
                                 * insert occurs in the right place, since it 
                                 * points to the control event just inserted.
                                 */
 
-                               ++insert_iterator;
+                               ++most_recent_insert_iterator;
                        }
 
                        /* don't do this again till the next write pass */
@@ -656,10 +474,18 @@ ControlList::add (double when, double value, bool erase_since_last_add)
                        new_write_pass = false;
                        did_write_during_pass = true;
 
-               } else if (insert_iterator == _events.end() || when > (*insert_iterator)->when) {
+               } else if (most_recent_insert_iterator == _events.end() || when > (*most_recent_insert_iterator)->when) {
+
+                       /* 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, (insert_iterator == _events.end())));
+                                                                        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.
@@ -672,19 +498,23 @@ ControlList::add (double when, double value, bool erase_since_last_add)
                        if (_events.back()->when == when) {
 
                                /* we need to modify the final point, so 
-                                  make insert_iterator point to it.
+                                  make most_recent_insert_iterator point to it.
                                */
 
                                DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 modify final value\n", this));
                                
-                               insert_iterator = _events.end();
-                               --insert_iterator;
+                               most_recent_insert_iterator = _events.end();
+                               --most_recent_insert_iterator;
 
                        } else if (_events.back()->when < when) {
 
+                               /* the new point is beyond the end of the
+                                * current list
+                                */
+
                                DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 plan to append to list\n", this));
 
-                               if (erase_since_last_add) {
+                               if (_in_write_pass) {
                                        /* remove the final point, because
                                           we're adding one beyond it.
                                        */
@@ -694,31 +524,34 @@ ControlList::add (double when, double value, bool erase_since_last_add)
                                
                                /* leaving this here will force an append */
 
-                               insert_iterator = _events.end();
+                               most_recent_insert_iterator = _events.end();
 
                        } else {
 
-                               DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 find based on lower bound, erase = %2\n", this, erase_since_last_add));
-
-                               /* the new point is somewhere within the list,
-                                * so figure out where to insert 
-                                */
-
-                               ControlEvent cp (when, 0.0);
-                               insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
-
-                               while (insert_iterator != _events.end()) {
-                                       if ((*insert_iterator)->when < when) {
-                                               if (erase_since_last_add) {
-                                                       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase existing @ %2\n", this, (*insert_iterator)));
-                                                       delete *insert_iterator;
-                                                       insert_iterator = _events.erase (insert_iterator);
-                                                       continue;
+                               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;
                                                }
-                                       } else if ((*insert_iterator)->when >= when) {
-                                               break;
+                                               ++most_recent_insert_iterator;
                                        }
-                                       ++insert_iterator;
+                               } else {
+                                       
+                                       /* not in a write pass: figure out the iterator we should insert in front of */
+
+                                       ControlEvent cp (when, 0.0f);
+                                       most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
                                }
                        }
                } 
@@ -726,30 +559,91 @@ ControlList::add (double when, double value, bool erase_since_last_add)
                /* OK, now we're really ready to add a new point
                 */
 
-               if (insert_iterator == _events.end()) {
+               if (most_recent_insert_iterator == _events.end()) {
                        DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 appending new point at end\n", this));
-                       _events.push_back (new ControlEvent (when, value));
-                       /* leave insert_iterator as it was: at the end */
+                       
+                       bool done = false;
+
+                       /* check if would just be adding to a straight line,
+                        * and don't add another point if so
+                        */
+
+                       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;
+                                               }
+                                       }
+                               }
+                       }
+
+                       if (!done) {
+                               _events.push_back (new ControlEvent (when, value));
+                       }
 
-               } else if ((*insert_iterator)->when == when) {
+                       if (!_in_write_pass) {
+                               most_recent_insert_iterator = _events.end();
+                               --most_recent_insert_iterator;
+                       }
+
+               } 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));
+
                        /* only one point allowed per time point, so just
                         * reset the value here.
                         */
-                       (*insert_iterator)->value = value;
-                       /* insert iterator now points past the control event we just
-                        * modified. the next insert needs to be after this,
-                        * so.. 
-                        */
-                       ++insert_iterator;
+
+                       (*most_recent_insert_iterator)->value = value;
+
                } else {
-                       DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert new point at %2 at iterator at %3\n", this, when, (*insert_iterator)->when));
-                       _events.insert (insert_iterator, new ControlEvent (when, value));
-                       /* leave insert iterator where it was, since it points
-                        * to the next control event AFTER the one we just inserted.
+                       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 (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 (!_in_write_pass) {
+                                       most_recent_insert_iterator = x;
+                               }
+                       }
+               }
 
                mark_dirty ();
        }
@@ -761,8 +655,8 @@ void
 ControlList::erase (iterator i)
 {
        {
-               Glib::Mutex::Lock lm (_lock);
-               if (insert_iterator == i) {
+               Glib::Threads::Mutex::Lock lm (_lock);
+               if (most_recent_insert_iterator == i) {
                        unlocked_invalidate_insert_iterator ();
                }
                _events.erase (i);
@@ -775,7 +669,7 @@ 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 ();
@@ -788,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)) {
@@ -797,7 +691,7 @@ ControlList::erase (double when, double value)
 
                if (i != end ()) {
                        _events.erase (i);
-                       if (insert_iterator == i) {
+                       if (most_recent_insert_iterator == i) {
                                unlocked_invalidate_insert_iterator ();
                        }
                }
@@ -814,7 +708,7 @@ 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) {
@@ -853,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;
@@ -874,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) {
@@ -897,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;
@@ -922,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;
@@ -968,7 +862,7 @@ ControlList::thaw ()
        }
 
        {
-               Glib::Mutex::Lock lm (_lock);
+               Glib::Threads::Mutex::Lock lm (_lock);
 
                if (_sort_pending) {
                        _events.sort (event_time_less_than);
@@ -995,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;
@@ -1098,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;
@@ -1353,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;
        }
@@ -1552,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
@@ -1668,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;
@@ -1716,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;