1 /* This file is part of Evoral.
2 * Copyright (C) 2008 David Robillard <http://drobilla.net>
3 * Copyright (C) 2000-2008 Paul Davis
5 * Evoral is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
10 * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "evoral/ControlList.hpp"
24 #include "evoral/Curve.hpp"
26 #include "pbd/compose.h"
33 inline bool event_time_less_than (ControlEvent* a, ControlEvent* b)
35 return a->when < b->when;
38 /* this has no units but corresponds to the area of a rectangle
39 computed between three points in the list. If the area is
40 large, it indicates significant non-linearity between the
43 during automation recording we thin the recorded points
44 using this value. if a point is sufficiently co-linear
45 with its neighbours (as defined by the area of the rectangle
46 formed by three of them), we will not include it in the
47 ControlList. a smaller value will exclude less points,
48 a larger value will exclude more points, so it effectively
49 measures the amount of thinning to be done.
52 double ControlList::_thinning_factor = 20.0;
54 ControlList::ControlList (const Parameter& id)
56 , _interpolation(Linear)
60 _changed_when_thawed = false;
64 _lookup_cache.left = -1;
65 _lookup_cache.range.first = _events.end();
66 _search_cache.left = -1;
67 _search_cache.first = _events.end();
68 _sort_pending = false;
69 new_write_pass = true;
70 did_write_during_pass = false;
72 insert_iterator = _events.end();
75 ControlList::ControlList (const ControlList& other)
76 : _parameter(other._parameter)
77 , _interpolation(Linear)
81 _changed_when_thawed = false;
82 _min_yval = other._min_yval;
83 _max_yval = other._max_yval;
84 _default_value = other._default_value;
85 _lookup_cache.range.first = _events.end();
86 _search_cache.first = _events.end();
87 _sort_pending = false;
88 new_write_pass = true;
89 did_write_during_pass = false;
91 insert_iterator = _events.end();
98 ControlList::ControlList (const ControlList& other, double start, double end)
99 : _parameter(other._parameter)
100 , _interpolation(Linear)
104 _changed_when_thawed = false;
105 _min_yval = other._min_yval;
106 _max_yval = other._max_yval;
107 _default_value = other._default_value;
108 _lookup_cache.range.first = _events.end();
109 _search_cache.first = _events.end();
110 _sort_pending = false;
112 /* now grab the relevant points, and shift them back if necessary */
114 boost::shared_ptr<ControlList> section = const_cast<ControlList*>(&other)->copy (start, end);
116 if (!section->empty()) {
117 copy_events (*(section.get()));
120 new_write_pass = false;
121 did_write_during_pass = false;
122 insert_position = -1;
123 insert_iterator = _events.end();
128 ControlList::~ControlList()
130 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
137 boost::shared_ptr<ControlList>
138 ControlList::create(Parameter id)
140 return boost::shared_ptr<ControlList>(new ControlList(id));
144 ControlList::operator== (const ControlList& other)
146 return _events == other._events;
150 ControlList::operator= (const ControlList& other)
152 if (this != &other) {
154 _min_yval = other._min_yval;
155 _max_yval = other._max_yval;
156 _default_value = other._default_value;
165 ControlList::copy_events (const ControlList& other)
168 Glib::Mutex::Lock lm (_lock);
170 for (const_iterator i = other.begin(); i != other.end(); ++i) {
171 _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
173 unlocked_invalidate_insert_iterator ();
176 maybe_signal_changed ();
180 ControlList::create_curve()
182 _curve = new Curve(*this);
186 ControlList::destroy_curve()
193 ControlList::maybe_signal_changed ()
198 _changed_when_thawed = true;
203 ControlList::clear ()
206 Glib::Mutex::Lock lm (_lock);
208 unlocked_invalidate_insert_iterator ();
212 maybe_signal_changed ();
216 ControlList::x_scale (double factor)
218 Glib::Mutex::Lock lm (_lock);
223 ControlList::extend_to (double when)
225 Glib::Mutex::Lock lm (_lock);
226 if (_events.empty() || _events.back()->when == when) {
229 double factor = when / _events.back()->when;
235 ControlList::_x_scale (double factor)
237 for (iterator i = _events.begin(); i != _events.end(); ++i) {
238 (*i)->when *= factor;
245 ControlList::write_pass_finished (double when)
247 if (did_write_during_pass) {
250 new_write_pass = true;
251 did_write_during_pass = false;
254 struct ControlEventTimeComparator {
255 bool operator() (ControlEvent* a, ControlEvent* b) {
256 return a->when < b->when;
263 ControlList::merge_nascent (double when)
266 Glib::Mutex::Lock lm (_lock);
268 if (nascent.empty()) {
272 bool was_empty = _events.empty();
274 for (list<NascentInfo*>::iterator n = nascent.begin(); n != nascent.end(); ++n) {
276 NascentInfo* ninfo = *n;
277 EventList& nascent_events (ninfo->events);
278 bool need_adjacent_start_clamp;
279 bool need_adjacent_end_clamp;
280 EventList::iterator at;
282 if (nascent_events.empty()) {
287 nascent_events.sort (ControlEventTimeComparator ());
289 if (ninfo->start_time < 0.0) {
290 ninfo->start_time = nascent_events.front()->when;
293 if (ninfo->end_time < 0.0) {
294 ninfo->end_time = when;
297 if (_events.empty()) {
299 /* add an initial point just before
300 the nascent data, unless nascent_events
301 contains a point at zero or one
304 if (ninfo->start_time > 0) {
305 nascent_events.insert (nascent_events.begin(), new ControlEvent (ninfo->start_time - 1, _default_value));
308 /* add closing "clamp" point before we insert */
310 nascent_events.insert (nascent_events.end(), new ControlEvent (ninfo->end_time + 1, _default_value));
312 /* insert - front or back doesn't matter since
316 _events.insert (_events.begin(), nascent_events.begin(), nascent_events.end());
318 } else if (ninfo->end_time < _events.front()->when) {
320 /* all points in nascent are before the first existing point */
322 if (ninfo->start_time > (_events.front()->when + 1)) {
323 nascent_events.insert (nascent_events.begin(), new ControlEvent (ninfo->start_time - 1, _default_value));
326 /* add closing "clamp" point before we insert */
328 nascent_events.insert (nascent_events.end(), new ControlEvent (ninfo->end_time + 1, _default_value));
330 /* insert at front */
332 _events.insert (_events.begin(), nascent_events.begin(), nascent_events.end());
334 /* now add another default control point right
335 after the inserted nascent data
338 } else if (ninfo->start_time > _events.back()->when) {
340 /* all points in nascent are after the last existing point */
342 if (ninfo->start_time > (_events.back()->when + 1)) {
343 nascent_events.insert (nascent_events.begin(), new ControlEvent (ninfo->start_time - 1, _default_value));
346 /* add closing "clamp" point before we insert */
348 nascent_events.insert (nascent_events.end(), new ControlEvent (ninfo->end_time + 1, _default_value));
352 _events.insert (_events.end(), nascent_events.begin(), nascent_events.end());
356 /* find the range that overlaps with nascent events,
357 and insert the contents of nascent events.
361 iterator range_begin = _events.end();
362 iterator range_end = _events.end();
363 double end_value = unlocked_eval (ninfo->end_time);
364 double start_value = unlocked_eval (ninfo->start_time - 1);
366 need_adjacent_end_clamp = true;
367 need_adjacent_start_clamp = true;
369 for (i = _events.begin(); i != _events.end(); ++i) {
371 if ((*i)->when == ninfo->start_time) {
372 /* existing point at same time, remove it
373 and the consider the next point instead.
375 i = _events.erase (i);
377 if (i == _events.end()) {
381 if (range_begin == _events.end()) {
383 need_adjacent_start_clamp = false;
385 need_adjacent_end_clamp = false;
388 if ((*i)->when > ninfo->end_time) {
393 } else if ((*i)->when > ninfo->start_time) {
395 if (range_begin == _events.end()) {
399 if ((*i)->when > ninfo->end_time) {
407 range_begin is the first event on our list after the first nascent event
408 range_end is the first event on our list after the last nascent event
410 range_begin may be equal to _events.end() if the last event on our list
411 was at the same time as the first nascent event.
414 if (range_begin != _events.begin()) {
415 /* clamp point before */
416 if (need_adjacent_start_clamp) {
417 _events.insert (range_begin, new ControlEvent (ninfo->start_time, start_value));
421 _events.insert (range_begin, nascent_events.begin(), nascent_events.end());
423 if (range_end != _events.end()) {
424 /* clamp point after */
425 if (need_adjacent_end_clamp) {
426 _events.insert (range_begin, new ControlEvent (ninfo->end_time, end_value));
430 _events.erase (range_begin, range_end);
436 if (was_empty && !_events.empty()) {
437 if (_events.front()->when != 0) {
438 _events.insert (_events.begin(), new ControlEvent (0, _default_value));
445 nascent.push_back (new NascentInfo ());
449 maybe_signal_changed ();
456 bool changed = false;
459 Glib::Mutex::Lock lm (_lock);
461 ControlEvent* prevprev = 0;
462 ControlEvent* cur = 0;
463 ControlEvent* prev = 0;
467 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 thin from %2 events\n", this, _events.size()));
469 for (iterator i = _events.begin(); i != _events.end(); ++i) {
476 /* compute the area of the triangle formed by 3 points
479 double area = fabs ((prevprev->when * (prev->value - cur->value)) +
480 (prev->when * (cur->value - prevprev->value)) +
481 (cur->when * (prevprev->value - prev->value)));
483 if (area < _thinning_factor) {
484 iterator tmp = pprev;
486 /* pprev will change to current
487 i is incremented to the next event
503 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 thin => %2 events\n", this, _events.size()));
506 unlocked_invalidate_insert_iterator ();
512 maybe_signal_changed ();
517 ControlList::fast_simple_add (double when, double value)
519 Glib::Mutex::Lock lm (_lock);
520 /* to be used only for loading pre-sorted data from saved state */
521 _events.insert (_events.end(), new ControlEvent (when, value));
522 assert(_events.back());
528 ControlList::invalidate_insert_iterator ()
530 Glib::Mutex::Lock lm (_lock);
531 unlocked_invalidate_insert_iterator ();
535 ControlList::unlocked_invalidate_insert_iterator ()
537 insert_iterator = _events.end();
541 ControlList::start_write_pass (double when)
543 Glib::Mutex::Lock lm (_lock);
545 new_write_pass = true;
546 did_write_during_pass = false;
547 insert_position = when;
549 ControlEvent cp (when, 0.0);
550 insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
552 if ((*insert_iterator)->when != when) {
553 /* doesn't point at a control point at precisely this time,
554 so reset it to the end and we'll find where to insert
555 if/when a new control event is added.
557 unlocked_invalidate_insert_iterator ();
562 ControlList::add (double when, double value, bool erase_since_last_add)
564 /* this is for making changes from some kind of user interface or
565 control surface (GUI, MIDI, OSC etc)
568 if (!clamp_value (when, value)) {
572 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 add %2 at %3 w/erase = %4\n", this, value, when, erase_since_last_add));
575 Glib::Mutex::Lock lm (_lock);
576 ControlEvent cp (when, 0.0f);
577 iterator insertion_point;
579 if (_events.empty()) {
581 /* as long as the point we're adding is not at zero,
582 * add an "anchor" point there.
586 _events.insert (_events.end(), new ControlEvent (0, _default_value));
587 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added default value %2 at zero\n", this, _default_value));
591 if (new_write_pass) {
593 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 new write pass, insert pos = %2, iter @ end ? %3\n",
594 this, insert_position, (insert_iterator == _events.end())));
596 /* The first addition of a new control event during a
599 * We need to add a new point at insert_position
600 * corresponding the value there.
603 if (insert_iterator == _events.end()) {
604 /* the insert_iterator is not set, figure out where
608 ControlEvent cp (insert_position, 0.0);
609 insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
610 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 looked up insert iterator for new write pass\n", this));
613 double eval_value = unlocked_eval (insert_position);
615 if (insert_iterator == _events.end()) {
616 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at end, adding eval-value there %2\n", this, eval_value));
618 _events.push_back (new ControlEvent (insert_position, eval_value));
619 /* leave insert iterator at the end */
621 } else if ((*insert_iterator)->when == when) {
623 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at existing point, setting eval-value there %2\n", this, eval_value));
625 /* insert_iterator points to a control event
626 already at the insert position, so there is
631 advance insert_iterator so that the "real"
632 insert occurs in the right place, since it
633 points to the control event just inserted.
639 /* insert a new control event at the right spot
642 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert eval-value %2 at iterator\n", this, eval_value));
644 insert_iterator = _events.insert (insert_iterator, new ControlEvent (insert_position, eval_value));
646 /* advance insert_iterator so that the "real"
647 * insert occurs in the right place, since it
648 * points to the control event just inserted.
654 /* don't do this again till the next write pass */
656 new_write_pass = false;
657 did_write_during_pass = true;
659 } else if (insert_iterator == _events.end() || when > (*insert_iterator)->when) {
661 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 need to discover insert iterator (@end ? %2)\n",
662 this, (insert_iterator == _events.end())));
664 /* this means that we either *know* we want to insert
665 * at the end, or that we don't know where to insert.
667 * so ... lets perform some quick checks before we
668 * go doing binary search to figure out where to
672 if (_events.back()->when == when) {
674 /* we need to modify the final point, so
675 make insert_iterator point to it.
678 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 modify final value\n", this));
680 insert_iterator = _events.end();
683 } else if (_events.back()->when < when) {
685 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 plan to append to list\n", this));
687 if (erase_since_last_add) {
688 /* remove the final point, because
689 we're adding one beyond it.
691 delete _events.back();
695 /* leaving this here will force an append */
697 insert_iterator = _events.end();
701 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 find based on lower bound, erase = %2\n", this, erase_since_last_add));
703 /* the new point is somewhere within the list,
704 * so figure out where to insert
707 ControlEvent cp (when, 0.0);
708 insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
710 while (insert_iterator != _events.end()) {
711 if ((*insert_iterator)->when < when) {
712 if (erase_since_last_add) {
713 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase existing @ %2\n", this, (*insert_iterator)));
714 delete *insert_iterator;
715 insert_iterator = _events.erase (insert_iterator);
718 } else if ((*insert_iterator)->when >= when) {
726 /* OK, now we're really ready to add a new point
729 if (insert_iterator == _events.end()) {
730 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 appending new point at end\n", this));
731 _events.push_back (new ControlEvent (when, value));
732 /* leave insert_iterator as it was: at the end */
734 } else if ((*insert_iterator)->when == when) {
735 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 reset existing point to new value %2\n", this, value));
736 /* only one point allowed per time point, so just
737 * reset the value here.
739 (*insert_iterator)->value = value;
740 /* insert iterator now points past the control event we just
741 * modified. the next insert needs to be after this,
746 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert new point at %2 at iterator at %3\n", this, when, (*insert_iterator)->when));
747 _events.insert (insert_iterator, new ControlEvent (when, value));
748 /* leave insert iterator where it was, since it points
749 * to the next control event AFTER the one we just inserted.
757 maybe_signal_changed ();
761 ControlList::erase (iterator i)
764 Glib::Mutex::Lock lm (_lock);
765 if (insert_iterator == i) {
766 unlocked_invalidate_insert_iterator ();
771 maybe_signal_changed ();
775 ControlList::erase (iterator start, iterator end)
778 Glib::Mutex::Lock lm (_lock);
779 _events.erase (start, end);
780 unlocked_invalidate_insert_iterator ();
783 maybe_signal_changed ();
786 /** Erase the first event which matches the given time and value */
788 ControlList::erase (double when, double value)
791 Glib::Mutex::Lock lm (_lock);
793 iterator i = begin ();
794 while (i != end() && ((*i)->when != when || (*i)->value != value)) {
800 if (insert_iterator == i) {
801 unlocked_invalidate_insert_iterator ();
808 maybe_signal_changed ();
812 ControlList::erase_range (double start, double endt)
817 Glib::Mutex::Lock lm (_lock);
818 erased = erase_range_internal (start, endt, _events);
827 maybe_signal_changed ();
832 ControlList::erase_range_internal (double start, double endt, EventList & events)
835 ControlEvent cp (start, 0.0f);
839 if ((s = lower_bound (events.begin(), events.end(), &cp, time_comparator)) != events.end()) {
841 e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
844 unlocked_invalidate_insert_iterator ();
853 ControlList::slide (iterator before, double distance)
856 Glib::Mutex::Lock lm (_lock);
858 if (before == _events.end()) {
862 while (before != _events.end()) {
863 (*before)->when += distance;
870 maybe_signal_changed ();
874 ControlList::shift (double pos, double frames)
877 Glib::Mutex::Lock lm (_lock);
879 for (iterator i = _events.begin(); i != _events.end(); ++i) {
880 if ((*i)->when >= pos) {
881 (*i)->when += frames;
888 maybe_signal_changed ();
892 ControlList::modify (iterator iter, double when, double val)
894 /* note: we assume higher level logic is in place to avoid this
895 reordering the time-order of control events in the list. ie. all
896 points after *iter are later than when.
900 Glib::Mutex::Lock lm (_lock);
902 (*iter)->when = when;
903 (*iter)->value = val;
905 if (std::isnan (val)) {
910 _events.sort (event_time_less_than);
911 unlocked_invalidate_insert_iterator ();
913 _sort_pending = true;
919 maybe_signal_changed ();
922 std::pair<ControlList::iterator,ControlList::iterator>
923 ControlList::control_points_adjacent (double xval)
925 Glib::Mutex::Lock lm (_lock);
927 ControlEvent cp (xval, 0.0f);
928 std::pair<iterator,iterator> ret;
930 ret.first = _events.end();
931 ret.second = _events.end();
933 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
935 if (ret.first == _events.end()) {
936 if ((*i)->when >= xval) {
937 if (i != _events.begin()) {
946 if ((*i)->when > xval) {
956 ControlList::freeze ()
971 Glib::Mutex::Lock lm (_lock);
974 _events.sort (event_time_less_than);
975 unlocked_invalidate_insert_iterator ();
976 _sort_pending = false;
982 ControlList::mark_dirty () const
984 _lookup_cache.left = -1;
985 _search_cache.left = -1;
988 _curve->mark_dirty();
991 Dirty (); /* EMIT SIGNAL */
995 ControlList::truncate_end (double last_coordinate)
998 Glib::Mutex::Lock lm (_lock);
999 ControlEvent cp (last_coordinate, 0);
1000 ControlList::reverse_iterator i;
1003 if (_events.empty()) {
1007 if (last_coordinate == _events.back()->when) {
1011 if (last_coordinate > _events.back()->when) {
1016 iterator foo = _events.begin();
1019 if (foo == _events.end()) {
1021 } else if (++foo == _events.end()) {
1024 lessthantwo = false;
1028 /* less than 2 points: add a new point */
1029 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
1032 /* more than 2 points: check to see if the last 2 values
1033 are equal. if so, just move the position of the
1034 last point. otherwise, add a new point.
1037 iterator penultimate = _events.end();
1038 --penultimate; /* points at last point */
1039 --penultimate; /* points at the penultimate point */
1041 if (_events.back()->value == (*penultimate)->value) {
1042 _events.back()->when = last_coordinate;
1044 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
1050 /* shortening end */
1052 last_val = unlocked_eval (last_coordinate);
1053 last_val = max ((double) _min_yval, last_val);
1054 last_val = min ((double) _max_yval, last_val);
1056 i = _events.rbegin();
1058 /* make i point to the last control point */
1062 /* now go backwards, removing control points that are
1063 beyond the new last coordinate.
1066 // FIXME: SLOW! (size() == O(n))
1068 uint32_t sz = _events.size();
1070 while (i != _events.rend() && sz > 2) {
1071 ControlList::reverse_iterator tmp;
1076 if ((*i)->when < last_coordinate) {
1080 _events.erase (i.base());
1086 _events.back()->when = last_coordinate;
1087 _events.back()->value = last_val;
1090 unlocked_invalidate_insert_iterator ();
1094 maybe_signal_changed ();
1098 ControlList::truncate_start (double overall_length)
1101 Glib::Mutex::Lock lm (_lock);
1103 double first_legal_value;
1104 double first_legal_coordinate;
1106 assert(!_events.empty());
1108 if (overall_length == _events.back()->when) {
1109 /* no change in overall length */
1113 if (overall_length > _events.back()->when) {
1115 /* growing at front: duplicate first point. shift all others */
1117 double shift = overall_length - _events.back()->when;
1120 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
1121 (*i)->when += shift;
1126 /* less than 2 points: add a new point */
1127 _events.push_front (new ControlEvent (0, _events.front()->value));
1131 /* more than 2 points: check to see if the first 2 values
1132 are equal. if so, just move the position of the
1133 first point. otherwise, add a new point.
1136 iterator second = _events.begin();
1137 ++second; /* points at the second point */
1139 if (_events.front()->value == (*second)->value) {
1140 /* first segment is flat, just move start point back to zero */
1141 _events.front()->when = 0;
1143 /* leave non-flat segment in place, add a new leading point. */
1144 _events.push_front (new ControlEvent (0, _events.front()->value));
1150 /* shrinking at front */
1152 first_legal_coordinate = _events.back()->when - overall_length;
1153 first_legal_value = unlocked_eval (first_legal_coordinate);
1154 first_legal_value = max (_min_yval, first_legal_value);
1155 first_legal_value = min (_max_yval, first_legal_value);
1157 /* remove all events earlier than the new "front" */
1159 i = _events.begin();
1161 while (i != _events.end() && !_events.empty()) {
1162 ControlList::iterator tmp;
1167 if ((*i)->when > first_legal_coordinate) {
1177 /* shift all remaining points left to keep their same
1181 for (i = _events.begin(); i != _events.end(); ++i) {
1182 (*i)->when -= first_legal_coordinate;
1185 /* add a new point for the interpolated new value */
1187 _events.push_front (new ControlEvent (0, first_legal_value));
1190 unlocked_invalidate_insert_iterator ();
1194 maybe_signal_changed ();
1198 ControlList::unlocked_eval (double x) const
1200 pair<EventList::iterator,EventList::iterator> range;
1206 const_iterator length_check_iter = _events.begin();
1207 for (npoints = 0; npoints < 4; ++npoints, ++length_check_iter) {
1208 if (length_check_iter == _events.end()) {
1215 return _default_value;
1218 return _events.front()->value;
1221 if (x >= _events.back()->when) {
1222 return _events.back()->value;
1223 } else if (x <= _events.front()->when) {
1224 return _events.front()->value;
1227 lpos = _events.front()->when;
1228 lval = _events.front()->value;
1229 upos = _events.back()->when;
1230 uval = _events.back()->value;
1232 if (_interpolation == Discrete) {
1236 /* linear interpolation betweeen the two points */
1237 fraction = (double) (x - lpos) / (double) (upos - lpos);
1238 return lval + (fraction * (uval - lval));
1241 if (x >= _events.back()->when) {
1242 return _events.back()->value;
1243 } else if (x <= _events.front()->when) {
1244 return _events.front()->value;
1247 return multipoint_eval (x);
1250 /*NOTREACHED*/ /* stupid gcc */
1251 return _default_value;
1255 ControlList::multipoint_eval (double x) const
1261 /* "Stepped" lookup (no interpolation) */
1262 /* FIXME: no cache. significant? */
1263 if (_interpolation == Discrete) {
1264 const ControlEvent cp (x, 0);
1265 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
1267 // shouldn't have made it to multipoint_eval
1268 assert(i != _events.end());
1270 if (i == _events.begin() || (*i)->when == x)
1273 return (*(--i))->value;
1276 /* Only do the range lookup if x is in a different range than last time
1277 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
1278 if ((_lookup_cache.left < 0) ||
1279 ((_lookup_cache.left > x) ||
1280 (_lookup_cache.range.first == _events.end()) ||
1281 ((*_lookup_cache.range.second)->when < x))) {
1283 const ControlEvent cp (x, 0);
1285 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
1288 pair<const_iterator,const_iterator> range = _lookup_cache.range;
1290 if (range.first == range.second) {
1292 /* x does not exist within the list as a control point */
1294 _lookup_cache.left = x;
1296 if (range.first != _events.begin()) {
1298 lpos = (*range.first)->when;
1299 lval = (*range.first)->value;
1301 /* we're before the first point */
1302 // return _default_value;
1303 return _events.front()->value;
1306 if (range.second == _events.end()) {
1307 /* we're after the last point */
1308 return _events.back()->value;
1311 upos = (*range.second)->when;
1312 uval = (*range.second)->value;
1314 /* linear interpolation betweeen the two points
1318 fraction = (double) (x - lpos) / (double) (upos - lpos);
1319 return lval + (fraction * (uval - lval));
1323 /* x is a control point in the data */
1324 _lookup_cache.left = -1;
1325 return (*range.first)->value;
1329 ControlList::build_search_cache_if_necessary (double start) const
1331 /* Only do the range lookup if x is in a different range than last time
1332 * this was called (or if the search cache has been marked "dirty" (left<0) */
1333 if (!_events.empty() && ((_search_cache.left < 0) || (_search_cache.left > start))) {
1335 const ControlEvent start_point (start, 0);
1337 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1338 // << start << ".." << end << ")" << endl;
1340 _search_cache.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1341 _search_cache.left = start;
1345 /** Get the earliest event after \a start using the current interpolation style.
1347 * If an event is found, \a x and \a y are set to its coordinates.
1349 * \param inclusive Include events with timestamp exactly equal to \a start
1350 * \return true if event is found (and \a x and \a y are valid).
1353 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
1355 // FIXME: It would be nice if this was unnecessary..
1356 Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
1361 return rt_safe_earliest_event_unlocked (start, x, y, inclusive);
1365 /** Get the earliest event after \a start using the current interpolation style.
1367 * If an event is found, \a x and \a y are set to its coordinates.
1369 * \param inclusive Include events with timestamp exactly equal to \a start
1370 * \return true if event is found (and \a x and \a y are valid).
1373 ControlList::rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool inclusive) const
1375 if (_interpolation == Discrete) {
1376 return rt_safe_earliest_event_discrete_unlocked(start, x, y, inclusive);
1378 return rt_safe_earliest_event_linear_unlocked(start, x, y, inclusive);
1383 /** Get the earliest event after \a start without interpolation.
1385 * If an event is found, \a x and \a y are set to its coordinates.
1387 * \param inclusive Include events with timestamp exactly equal to \a start
1388 * \return true if event is found (and \a x and \a y are valid).
1391 ControlList::rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const
1393 build_search_cache_if_necessary (start);
1395 if (_search_cache.first != _events.end()) {
1396 const ControlEvent* const first = *_search_cache.first;
1398 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1400 /* Earliest points is in range, return it */
1406 /* Move left of cache to this point
1407 * (Optimize for immediate call this cycle within range) */
1408 _search_cache.left = x;
1409 ++_search_cache.first;
1418 /* No points in range */
1424 /** Get the earliest time the line crosses an integer (Linear interpolation).
1426 * If an event is found, \a x and \a y are set to its coordinates.
1428 * \param inclusive Include events with timestamp exactly equal to \a start
1429 * \return true if event is found (and \a x and \a y are valid).
1432 ControlList::rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const
1434 // cout << "earliest_event(start: " << start << ", x: " << x << ", y: " << y << ", inclusive: " << inclusive << ")" << endl;
1436 const_iterator length_check_iter = _events.begin();
1437 if (_events.empty()) { // 0 events
1439 } else if (_events.end() == ++length_check_iter) { // 1 event
1440 return rt_safe_earliest_event_discrete_unlocked (start, x, y, inclusive);
1443 // Hack to avoid infinitely repeating the same event
1444 build_search_cache_if_necessary (start);
1446 if (_search_cache.first != _events.end()) {
1448 const ControlEvent* first = NULL;
1449 const ControlEvent* next = NULL;
1451 /* Step is after first */
1452 if (_search_cache.first == _events.begin() || (*_search_cache.first)->when <= start) {
1453 first = *_search_cache.first;
1454 ++_search_cache.first;
1455 if (_search_cache.first == _events.end()) {
1458 next = *_search_cache.first;
1460 /* Step is before first */
1462 const_iterator prev = _search_cache.first;
1465 next = *_search_cache.first;
1468 if (inclusive && first->when == start) {
1471 /* Move left of cache to this point
1472 * (Optimize for immediate call this cycle within range) */
1473 _search_cache.left = x;
1474 //++_search_cache.range.first;
1479 if (fabs(first->value - next->value) <= 1) {
1480 if (next->when > start) {
1483 /* Move left of cache to this point
1484 * (Optimize for immediate call this cycle within range) */
1485 _search_cache.left = x;
1486 //++_search_cache.range.first;
1487 assert(inclusive ? x >= start : x > start);
1494 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1495 //cerr << "start y: " << start_y << endl;
1497 //y = first->value + (slope * fabs(start - first->when));
1500 if (first->value < next->value) // ramping up
1502 else // ramping down
1505 x = first->when + (y - first->value) / (double)slope;
1507 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1509 if (first->value < next->value) // ramping up
1511 else // ramping down
1514 x = first->when + (y - first->value) / (double)slope;
1517 /*cerr << first->value << " @ " << first->when << " ... "
1518 << next->value << " @ " << next->when
1519 << " = " << y << " @ " << x << endl;*/
1521 assert( (y >= first->value && y <= next->value)
1522 || (y <= first->value && y >= next->value) );
1525 const bool past_start = (inclusive ? x >= start : x > start);
1527 /* Move left of cache to this point
1528 * (Optimize for immediate call this cycle within range) */
1529 _search_cache.left = x;
1530 assert(inclusive ? x >= start : x > start);
1536 /* No points in the future, so no steps (towards them) in the future */
1543 /** @param start Start position in model coordinates.
1544 * @param end End position in model coordinates.
1545 * @param op 0 = cut, 1 = copy, 2 = clear.
1547 boost::shared_ptr<ControlList>
1548 ControlList::cut_copy_clear (double start, double end, int op)
1550 boost::shared_ptr<ControlList> nal = create (_parameter);
1552 ControlEvent cp (start, 0.0);
1555 Glib::Mutex::Lock lm (_lock);
1557 /* first, determine s & e, two iterators that define the range of points
1558 affected by this operation
1561 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1565 /* and the last that is at or after `end' */
1567 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1570 /* if "start" isn't the location of an existing point,
1571 evaluate the curve to get a value for the start. Add a point to
1572 both the existing event list, and if its not a "clear" operation,
1573 to the copy ("nal") as well.
1575 Note that the time positions of the points in each list are different
1576 because we want the copy ("nal") to have a zero time reference.
1580 /* before we begin any cut/clear operations, get the value of the curve
1584 double end_value = unlocked_eval (end);
1586 if ((*s)->when != start) {
1588 double val = unlocked_eval (start);
1590 if (op == 0) { // cut
1591 if (start > _events.front()->when) {
1592 _events.insert (s, (new ControlEvent (start, val)));
1596 if (op != 2) { // ! clear
1597 nal->_events.push_back (new ControlEvent (0, val));
1601 for (iterator x = s; x != e; ) {
1603 /* adjust new points to be relative to start, which
1604 has been set to zero.
1608 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1612 x = _events.erase (x);
1618 if (e == _events.end() || (*e)->when != end) {
1620 /* only add a boundary point if there is a point after "end"
1623 if (op == 0 && (e != _events.end() && end < (*e)->when)) { // cut
1624 _events.insert (e, new ControlEvent (end, end_value));
1627 if (op != 2 && (e != _events.end() && end < (*e)->when)) { // cut/copy
1628 nal->_events.push_back (new ControlEvent (end - start, end_value));
1632 unlocked_invalidate_insert_iterator ();
1637 maybe_signal_changed ();
1644 boost::shared_ptr<ControlList>
1645 ControlList::cut (double start, double end)
1647 return cut_copy_clear (start, end, 0);
1650 boost::shared_ptr<ControlList>
1651 ControlList::copy (double start, double end)
1653 return cut_copy_clear (start, end, 1);
1657 ControlList::clear (double start, double end)
1659 cut_copy_clear (start, end, 2);
1662 /** @param pos Position in model coordinates */
1664 ControlList::paste (ControlList& alist, double pos, float /*times*/)
1666 if (alist._events.empty()) {
1671 Glib::Mutex::Lock lm (_lock);
1675 ControlEvent cp (pos, 0.0);
1677 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1679 for (iterator i = alist.begin();i != alist.end(); ++i) {
1680 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1681 end = (*i)->when + pos;
1685 /* move all points after the insertion along the timeline by
1689 while (where != _events.end()) {
1691 if ((*where)->when <= end) {
1694 _events.erase(where);
1702 unlocked_invalidate_insert_iterator ();
1706 maybe_signal_changed ();
1710 /** Move automation around according to a list of region movements.
1711 * @param return true if anything was changed, otherwise false (ie nothing needed changing)
1714 ControlList::move_ranges (const list< RangeMove<double> >& movements)
1716 typedef list< RangeMove<double> > RangeMoveList;
1719 Glib::Mutex::Lock lm (_lock);
1721 /* a copy of the events list before we started moving stuff around */
1722 EventList old_events = _events;
1724 /* clear the source and destination ranges in the new list */
1725 bool things_erased = false;
1726 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1728 if (erase_range_internal (i->from, i->from + i->length, _events)) {
1729 things_erased = true;
1732 if (erase_range_internal (i->to, i->to + i->length, _events)) {
1733 things_erased = true;
1737 /* if nothing was erased, there is nothing to do */
1738 if (!things_erased) {
1742 /* copy the events into the new list */
1743 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1744 iterator j = old_events.begin ();
1745 const double limit = i->from + i->length;
1746 const double dx = i->to - i->from;
1747 while (j != old_events.end () && (*j)->when <= limit) {
1748 if ((*j)->when >= i->from) {
1749 ControlEvent* ev = new ControlEvent (**j);
1751 _events.push_back (ev);
1758 _events.sort (event_time_less_than);
1759 unlocked_invalidate_insert_iterator ();
1761 _sort_pending = true;
1767 maybe_signal_changed ();
1772 ControlList::set_interpolation (InterpolationStyle s)
1774 if (_interpolation == s) {
1779 InterpolationChanged (s); /* EMIT SIGNAL */
1783 ControlList::set_thinning_factor (double v)
1785 _thinning_factor = v;
1789 ControlList::operator!= (ControlList const & other) const
1791 if (_events.size() != other._events.size()) {
1795 EventList::const_iterator i = _events.begin ();
1796 EventList::const_iterator j = other._events.begin ();
1798 while (i != _events.end() && (*i)->when == (*j)->when && (*i)->value == (*j)->value) {
1803 if (i != _events.end ()) {
1808 _parameter != other._parameter ||
1809 _interpolation != other._interpolation ||
1810 _min_yval != other._min_yval ||
1811 _max_yval != other._max_yval ||
1812 _default_value != other._default_value
1816 } // namespace Evoral