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 _in_write_pass = false;
71 did_write_during_pass = false;
73 most_recent_insert_iterator = _events.end();
76 ControlList::ControlList (const ControlList& other)
77 : _parameter(other._parameter)
78 , _interpolation(Linear)
82 _changed_when_thawed = false;
83 _min_yval = other._min_yval;
84 _max_yval = other._max_yval;
85 _default_value = other._default_value;
86 _lookup_cache.range.first = _events.end();
87 _search_cache.first = _events.end();
88 _sort_pending = false;
89 new_write_pass = true;
90 _in_write_pass = false;
91 did_write_during_pass = false;
93 most_recent_insert_iterator = _events.end();
100 ControlList::ControlList (const ControlList& other, double start, double end)
101 : _parameter(other._parameter)
102 , _interpolation(Linear)
106 _changed_when_thawed = false;
107 _min_yval = other._min_yval;
108 _max_yval = other._max_yval;
109 _default_value = other._default_value;
110 _lookup_cache.range.first = _events.end();
111 _search_cache.first = _events.end();
112 _sort_pending = false;
114 /* now grab the relevant points, and shift them back if necessary */
116 boost::shared_ptr<ControlList> section = const_cast<ControlList*>(&other)->copy (start, end);
118 if (!section->empty()) {
119 copy_events (*(section.get()));
122 new_write_pass = false;
123 _in_write_pass = false;
124 did_write_during_pass = false;
125 insert_position = -1;
126 most_recent_insert_iterator = _events.end();
131 ControlList::~ControlList()
133 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
140 boost::shared_ptr<ControlList>
141 ControlList::create(Parameter id)
143 return boost::shared_ptr<ControlList>(new ControlList(id));
147 ControlList::operator== (const ControlList& other)
149 return _events == other._events;
153 ControlList::operator= (const ControlList& other)
155 if (this != &other) {
157 _min_yval = other._min_yval;
158 _max_yval = other._max_yval;
159 _default_value = other._default_value;
168 ControlList::copy_events (const ControlList& other)
171 Glib::Threads::Mutex::Lock lm (_lock);
173 for (const_iterator i = other.begin(); i != other.end(); ++i) {
174 _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
176 unlocked_invalidate_insert_iterator ();
179 maybe_signal_changed ();
183 ControlList::create_curve()
185 _curve = new Curve(*this);
189 ControlList::destroy_curve()
196 ControlList::maybe_signal_changed ()
201 _changed_when_thawed = true;
206 ControlList::clear ()
209 Glib::Threads::Mutex::Lock lm (_lock);
211 unlocked_invalidate_insert_iterator ();
215 maybe_signal_changed ();
219 ControlList::x_scale (double factor)
221 Glib::Threads::Mutex::Lock lm (_lock);
226 ControlList::extend_to (double when)
228 Glib::Threads::Mutex::Lock lm (_lock);
229 if (_events.empty() || _events.back()->when == when) {
232 double factor = when / _events.back()->when;
238 ControlList::_x_scale (double factor)
240 for (iterator i = _events.begin(); i != _events.end(); ++i) {
241 (*i)->when *= factor;
247 struct ControlEventTimeComparator {
248 bool operator() (ControlEvent* a, ControlEvent* b) {
249 return a->when < b->when;
256 bool changed = false;
259 Glib::Threads::Mutex::Lock lm (_lock);
261 ControlEvent* prevprev = 0;
262 ControlEvent* cur = 0;
263 ControlEvent* prev = 0;
267 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 thin from %2 events\n", this, _events.size()));
269 for (iterator i = _events.begin(); i != _events.end(); ++i) {
276 /* compute the area of the triangle formed by 3 points
279 double area = fabs ((prevprev->when * (prev->value - cur->value)) +
280 (prev->when * (cur->value - prevprev->value)) +
281 (cur->when * (prevprev->value - prev->value)));
283 if (area < _thinning_factor) {
284 iterator tmp = pprev;
286 /* pprev will change to current
287 i is incremented to the next event
303 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 thin => %2 events\n", this, _events.size()));
306 unlocked_invalidate_insert_iterator ();
312 maybe_signal_changed ();
317 ControlList::fast_simple_add (double when, double value)
319 Glib::Threads::Mutex::Lock lm (_lock);
320 /* to be used only for loading pre-sorted data from saved state */
321 _events.insert (_events.end(), new ControlEvent (when, value));
322 assert(_events.back());
328 ControlList::invalidate_insert_iterator ()
330 Glib::Threads::Mutex::Lock lm (_lock);
331 unlocked_invalidate_insert_iterator ();
335 ControlList::unlocked_invalidate_insert_iterator ()
337 most_recent_insert_iterator = _events.end();
341 ControlList::start_write_pass (double when)
343 Glib::Threads::Mutex::Lock lm (_lock);
345 new_write_pass = true;
346 did_write_during_pass = false;
347 insert_position = when;
349 /* leave the insert iterator invalid, so that we will do the lookup
350 of where it should be in a "lazy" way - deferring it until
351 we actually add the first point (which may never happen).
354 unlocked_invalidate_insert_iterator ();
358 ControlList::write_pass_finished (double when)
360 if (did_write_during_pass) {
362 did_write_during_pass = false;
364 new_write_pass = true;
365 _in_write_pass = false;
369 ControlList::set_in_write_pass (bool yn)
375 ControlList::in_write_pass () const
377 return _in_write_pass;
381 ControlList::add (double when, double value)
383 /* this is for making changes from some kind of user interface or
384 control surface (GUI, MIDI, OSC etc)
387 if (!clamp_value (when, value)) {
391 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 add %2 at %3 w/erase = %4 at end ? %5\n",
392 this, value, when, _in_write_pass, (most_recent_insert_iterator == _events.end())));
394 Glib::Threads::Mutex::Lock lm (_lock);
395 ControlEvent cp (when, 0.0f);
396 iterator insertion_point;
398 if (_events.empty()) {
400 /* as long as the point we're adding is not at zero,
401 * add an "anchor" point there.
405 _events.insert (_events.end(), new ControlEvent (0, _default_value));
406 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added default value %2 at zero\n", this, _default_value));
410 if (_in_write_pass && new_write_pass) {
412 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 new write pass, insert pos = %2\n", this, insert_position));
414 /* The first addition of a new control event during a
417 * We need to add a new point at insert_position
418 * corresponding to the (existing, implicit) value there.
421 /* the insert_iterator is not set, figure out where
425 ControlEvent cp (insert_position, 0.0);
426 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
427 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 looked up insert iterator for new write pass\n", this));
429 double eval_value = unlocked_eval (insert_position);
431 if (most_recent_insert_iterator == _events.end()) {
433 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at end, adding eval-value there %2\n", this, eval_value));
434 _events.push_back (new ControlEvent (insert_position, eval_value));
435 /* leave insert iterator at the end */
437 } else if ((*most_recent_insert_iterator)->when == when) {
439 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at existing point, setting eval-value there %2\n", this, eval_value));
441 /* most_recent_insert_iterator points to a control event
442 already at the insert position, so there is
447 advance most_recent_insert_iterator so that the "real"
448 insert occurs in the right place, since it
449 points to the control event just inserted.
452 ++most_recent_insert_iterator;
455 /* insert a new control event at the right spot
458 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert eval-value %2 just before iterator @ %3\n",
459 this, eval_value, (*most_recent_insert_iterator)->when));
461 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator, new ControlEvent (insert_position, eval_value));
463 /* advance most_recent_insert_iterator so that the "real"
464 * insert occurs in the right place, since it
465 * points to the control event just inserted.
468 ++most_recent_insert_iterator;
471 /* don't do this again till the next write pass */
473 new_write_pass = false;
474 did_write_during_pass = true;
476 } else if (most_recent_insert_iterator == _events.end() || when > (*most_recent_insert_iterator)->when) {
478 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 %2 erase from existing iterator (@end ? %3)\n",
479 this, (_in_write_pass ? "DO" : "DON'T"),
480 (most_recent_insert_iterator == _events.end())));
482 if (_in_write_pass) {
483 while (most_recent_insert_iterator != _events.end()) {
484 if ((*most_recent_insert_iterator)->when < when) {
485 if (_in_write_pass) {
486 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase existing @ %2\n", this, (*most_recent_insert_iterator)));
487 delete *most_recent_insert_iterator;
488 most_recent_insert_iterator = _events.erase (most_recent_insert_iterator);
491 } else if ((*most_recent_insert_iterator)->when >= when) {
494 ++most_recent_insert_iterator;
497 if (most_recent_insert_iterator != _events.end()) {
498 if ((*most_recent_insert_iterator)->when - when > 64) {
499 /* next control point is some
500 * distance from where our new
501 * point is going to go - add a
502 * new point to avoid changing
503 * the shape of the line too
504 * much. the insert iterator needs
505 * to point to the new control
506 * point so that our insert
507 * will happen correctly.
509 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator,
510 new ControlEvent (when+1, (*most_recent_insert_iterator)->value));
511 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added post-erase guard point @ %2 = %3\n",
513 (*most_recent_insert_iterator)->value));
519 /* not in a write pass: figure out the iterator we should insert in front of */
521 ControlEvent cp (when, 0.0f);
522 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
526 /* OK, now we're really ready to add a new point
529 if (most_recent_insert_iterator == _events.end()) {
530 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 appending new point at end\n", this));
534 /* check if would just be adding to a straight line,
535 * and don't add another point if so
538 if (!_events.empty()) { // avoid O(N) _events.size() here
539 if (_events.back()->value == value) {
540 EventList::iterator b = _events.end();
541 --b; // final point, which we know exists
542 if (b != _events.begin()) { // step back again, but check first that it is legal
543 --b; // penultimate-point
544 if ((*b)->value == value) {
545 /* there are at least two points with the exact same value ...
546 * straight line - just move the final
547 * point to the new time
549 _events.back()->when = when;
551 DEBUG_TRACE (DEBUG::ControlList, string_compose ("final value of %1 moved to %2\n", value, when));
558 _events.push_back (new ControlEvent (when, value));
559 DEBUG_TRACE (DEBUG::ControlList, string_compose ("\tactually appended, size now %1\n", _events.size()));
562 if (!_in_write_pass) {
563 most_recent_insert_iterator = _events.end();
564 --most_recent_insert_iterator;
567 } else if ((*most_recent_insert_iterator)->when == when) {
569 if ((*most_recent_insert_iterator)->value != value) {
570 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 reset existing point to new value %2\n", this, value));
572 /* only one point allowed per time point, so just
573 * reset the value here.
576 (*most_recent_insert_iterator)->value = value;
578 /* if we modified the final value, then its as
579 * if we inserted a new point as far as the
580 * next addition, so make sure we know that.
583 if (_in_write_pass && _events.back()->when == when) {
584 most_recent_insert_iterator = _events.end();
588 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 same time %2, same value value %3\n", this, when, value));
592 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert new point at %2 at iterator at %3\n", this, when, (*most_recent_insert_iterator)->when));
596 /* check if would just be adding to a straight line,
597 * and don't add another point if so
600 if (most_recent_insert_iterator != _events.begin()) {
601 EventList::iterator b = most_recent_insert_iterator;
602 --b; // prior point, which we know exists
603 if ((*b)->value == value) { // same value as the point we plan to insert
604 if (b != _events.begin()) { // step back again, which may not be possible
605 EventList::iterator bb = b;
606 --bb; // next-to-prior-point
607 if ((*bb)->value == value) {
608 /* straight line - just move the prior
609 * point to the new time
613 if (!_in_write_pass) {
614 most_recent_insert_iterator = b;
617 DEBUG_TRACE (DEBUG::ControlList, string_compose ("final value of %1 moved to %2\n", value, when));
624 if (most_recent_insert_iterator != _events.end()) {
625 if ((*most_recent_insert_iterator)->when - when > 64) {
626 /* next control point is some
627 * distance from where our new
628 * point is going to go - add a
629 * new point to avoid changing
630 * the shape of the line too
631 * much. the insert iterator needs
632 * to point to the new control
633 * point so that our insert
634 * will happen correctly.
636 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator,
637 new ControlEvent (when+1, (*most_recent_insert_iterator)->value));
638 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added insert-post-erase guard point @ %2 = %3\n",
640 (*most_recent_insert_iterator)->value));
645 EventList::iterator x = _events.insert (most_recent_insert_iterator, new ControlEvent (when, value));
646 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 inserted new value before MRI, size now %2\n", this, _events.size()));
648 if (!_in_write_pass) {
649 most_recent_insert_iterator = x;
657 maybe_signal_changed ();
661 ControlList::erase (iterator i)
664 Glib::Threads::Mutex::Lock lm (_lock);
665 if (most_recent_insert_iterator == i) {
666 unlocked_invalidate_insert_iterator ();
671 maybe_signal_changed ();
675 ControlList::erase (iterator start, iterator end)
678 Glib::Threads::Mutex::Lock lm (_lock);
679 _events.erase (start, end);
680 unlocked_invalidate_insert_iterator ();
683 maybe_signal_changed ();
686 /** Erase the first event which matches the given time and value */
688 ControlList::erase (double when, double value)
691 Glib::Threads::Mutex::Lock lm (_lock);
693 iterator i = begin ();
694 while (i != end() && ((*i)->when != when || (*i)->value != value)) {
700 if (most_recent_insert_iterator == i) {
701 unlocked_invalidate_insert_iterator ();
708 maybe_signal_changed ();
712 ControlList::erase_range (double start, double endt)
717 Glib::Threads::Mutex::Lock lm (_lock);
718 erased = erase_range_internal (start, endt, _events);
727 maybe_signal_changed ();
732 ControlList::erase_range_internal (double start, double endt, EventList & events)
735 ControlEvent cp (start, 0.0f);
739 if ((s = lower_bound (events.begin(), events.end(), &cp, time_comparator)) != events.end()) {
741 e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
744 unlocked_invalidate_insert_iterator ();
753 ControlList::slide (iterator before, double distance)
756 Glib::Threads::Mutex::Lock lm (_lock);
758 if (before == _events.end()) {
762 while (before != _events.end()) {
763 (*before)->when += distance;
770 maybe_signal_changed ();
774 ControlList::shift (double pos, double frames)
777 Glib::Threads::Mutex::Lock lm (_lock);
779 for (iterator i = _events.begin(); i != _events.end(); ++i) {
780 if ((*i)->when >= pos) {
781 (*i)->when += frames;
788 maybe_signal_changed ();
792 ControlList::modify (iterator iter, double when, double val)
794 /* note: we assume higher level logic is in place to avoid this
795 reordering the time-order of control events in the list. ie. all
796 points after *iter are later than when.
800 Glib::Threads::Mutex::Lock lm (_lock);
802 (*iter)->when = when;
803 (*iter)->value = val;
805 if (std::isnan (val)) {
810 _events.sort (event_time_less_than);
811 unlocked_invalidate_insert_iterator ();
813 _sort_pending = true;
819 maybe_signal_changed ();
822 std::pair<ControlList::iterator,ControlList::iterator>
823 ControlList::control_points_adjacent (double xval)
825 Glib::Threads::Mutex::Lock lm (_lock);
827 ControlEvent cp (xval, 0.0f);
828 std::pair<iterator,iterator> ret;
830 ret.first = _events.end();
831 ret.second = _events.end();
833 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
835 if (ret.first == _events.end()) {
836 if ((*i)->when >= xval) {
837 if (i != _events.begin()) {
846 if ((*i)->when > xval) {
856 ControlList::freeze ()
871 Glib::Threads::Mutex::Lock lm (_lock);
874 _events.sort (event_time_less_than);
875 unlocked_invalidate_insert_iterator ();
876 _sort_pending = false;
882 ControlList::mark_dirty () const
884 _lookup_cache.left = -1;
885 _search_cache.left = -1;
888 _curve->mark_dirty();
891 Dirty (); /* EMIT SIGNAL */
895 ControlList::truncate_end (double last_coordinate)
898 Glib::Threads::Mutex::Lock lm (_lock);
899 ControlEvent cp (last_coordinate, 0);
900 ControlList::reverse_iterator i;
903 if (_events.empty()) {
907 if (last_coordinate == _events.back()->when) {
911 if (last_coordinate > _events.back()->when) {
916 iterator foo = _events.begin();
919 if (foo == _events.end()) {
921 } else if (++foo == _events.end()) {
928 /* less than 2 points: add a new point */
929 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
932 /* more than 2 points: check to see if the last 2 values
933 are equal. if so, just move the position of the
934 last point. otherwise, add a new point.
937 iterator penultimate = _events.end();
938 --penultimate; /* points at last point */
939 --penultimate; /* points at the penultimate point */
941 if (_events.back()->value == (*penultimate)->value) {
942 _events.back()->when = last_coordinate;
944 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
952 last_val = unlocked_eval (last_coordinate);
953 last_val = max ((double) _min_yval, last_val);
954 last_val = min ((double) _max_yval, last_val);
956 i = _events.rbegin();
958 /* make i point to the last control point */
962 /* now go backwards, removing control points that are
963 beyond the new last coordinate.
966 // FIXME: SLOW! (size() == O(n))
968 uint32_t sz = _events.size();
970 while (i != _events.rend() && sz > 2) {
971 ControlList::reverse_iterator tmp;
976 if ((*i)->when < last_coordinate) {
980 _events.erase (i.base());
986 _events.back()->when = last_coordinate;
987 _events.back()->value = last_val;
990 unlocked_invalidate_insert_iterator ();
994 maybe_signal_changed ();
998 ControlList::truncate_start (double overall_length)
1001 Glib::Threads::Mutex::Lock lm (_lock);
1003 double first_legal_value;
1004 double first_legal_coordinate;
1006 assert(!_events.empty());
1008 if (overall_length == _events.back()->when) {
1009 /* no change in overall length */
1013 if (overall_length > _events.back()->when) {
1015 /* growing at front: duplicate first point. shift all others */
1017 double shift = overall_length - _events.back()->when;
1020 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
1021 (*i)->when += shift;
1026 /* less than 2 points: add a new point */
1027 _events.push_front (new ControlEvent (0, _events.front()->value));
1031 /* more than 2 points: check to see if the first 2 values
1032 are equal. if so, just move the position of the
1033 first point. otherwise, add a new point.
1036 iterator second = _events.begin();
1037 ++second; /* points at the second point */
1039 if (_events.front()->value == (*second)->value) {
1040 /* first segment is flat, just move start point back to zero */
1041 _events.front()->when = 0;
1043 /* leave non-flat segment in place, add a new leading point. */
1044 _events.push_front (new ControlEvent (0, _events.front()->value));
1050 /* shrinking at front */
1052 first_legal_coordinate = _events.back()->when - overall_length;
1053 first_legal_value = unlocked_eval (first_legal_coordinate);
1054 first_legal_value = max (_min_yval, first_legal_value);
1055 first_legal_value = min (_max_yval, first_legal_value);
1057 /* remove all events earlier than the new "front" */
1059 i = _events.begin();
1061 while (i != _events.end() && !_events.empty()) {
1062 ControlList::iterator tmp;
1067 if ((*i)->when > first_legal_coordinate) {
1077 /* shift all remaining points left to keep their same
1081 for (i = _events.begin(); i != _events.end(); ++i) {
1082 (*i)->when -= first_legal_coordinate;
1085 /* add a new point for the interpolated new value */
1087 _events.push_front (new ControlEvent (0, first_legal_value));
1090 unlocked_invalidate_insert_iterator ();
1094 maybe_signal_changed ();
1098 ControlList::unlocked_eval (double x) const
1100 pair<EventList::iterator,EventList::iterator> range;
1106 const_iterator length_check_iter = _events.begin();
1107 for (npoints = 0; npoints < 4; ++npoints, ++length_check_iter) {
1108 if (length_check_iter == _events.end()) {
1115 return _default_value;
1118 return _events.front()->value;
1121 if (x >= _events.back()->when) {
1122 return _events.back()->value;
1123 } else if (x <= _events.front()->when) {
1124 return _events.front()->value;
1127 lpos = _events.front()->when;
1128 lval = _events.front()->value;
1129 upos = _events.back()->when;
1130 uval = _events.back()->value;
1132 if (_interpolation == Discrete) {
1136 /* linear interpolation betweeen the two points */
1137 fraction = (double) (x - lpos) / (double) (upos - lpos);
1138 return lval + (fraction * (uval - lval));
1141 if (x >= _events.back()->when) {
1142 return _events.back()->value;
1143 } else if (x <= _events.front()->when) {
1144 return _events.front()->value;
1147 return multipoint_eval (x);
1150 /*NOTREACHED*/ /* stupid gcc */
1151 return _default_value;
1155 ControlList::multipoint_eval (double x) const
1161 /* "Stepped" lookup (no interpolation) */
1162 /* FIXME: no cache. significant? */
1163 if (_interpolation == Discrete) {
1164 const ControlEvent cp (x, 0);
1165 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
1167 // shouldn't have made it to multipoint_eval
1168 assert(i != _events.end());
1170 if (i == _events.begin() || (*i)->when == x)
1173 return (*(--i))->value;
1176 /* Only do the range lookup if x is in a different range than last time
1177 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
1178 if ((_lookup_cache.left < 0) ||
1179 ((_lookup_cache.left > x) ||
1180 (_lookup_cache.range.first == _events.end()) ||
1181 ((*_lookup_cache.range.second)->when < x))) {
1183 const ControlEvent cp (x, 0);
1185 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
1188 pair<const_iterator,const_iterator> range = _lookup_cache.range;
1190 if (range.first == range.second) {
1192 /* x does not exist within the list as a control point */
1194 _lookup_cache.left = x;
1196 if (range.first != _events.begin()) {
1198 lpos = (*range.first)->when;
1199 lval = (*range.first)->value;
1201 /* we're before the first point */
1202 // return _default_value;
1203 return _events.front()->value;
1206 if (range.second == _events.end()) {
1207 /* we're after the last point */
1208 return _events.back()->value;
1211 upos = (*range.second)->when;
1212 uval = (*range.second)->value;
1214 /* linear interpolation betweeen the two points
1218 fraction = (double) (x - lpos) / (double) (upos - lpos);
1219 return lval + (fraction * (uval - lval));
1223 /* x is a control point in the data */
1224 _lookup_cache.left = -1;
1225 return (*range.first)->value;
1229 ControlList::build_search_cache_if_necessary (double start) const
1231 /* Only do the range lookup if x is in a different range than last time
1232 * this was called (or if the search cache has been marked "dirty" (left<0) */
1233 if (!_events.empty() && ((_search_cache.left < 0) || (_search_cache.left > start))) {
1235 const ControlEvent start_point (start, 0);
1237 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1238 // << start << ".." << end << ")" << endl;
1240 _search_cache.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1241 _search_cache.left = start;
1245 /** Get the earliest event after \a start using the current interpolation style.
1247 * If an event is found, \a x and \a y are set to its coordinates.
1249 * \param inclusive Include events with timestamp exactly equal to \a start
1250 * \return true if event is found (and \a x and \a y are valid).
1253 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
1255 // FIXME: It would be nice if this was unnecessary..
1256 Glib::Threads::Mutex::Lock lm(_lock, Glib::Threads::TRY_LOCK);
1261 return rt_safe_earliest_event_unlocked (start, x, y, inclusive);
1265 /** Get the earliest event after \a start using the current interpolation style.
1267 * If an event is found, \a x and \a y are set to its coordinates.
1269 * \param inclusive Include events with timestamp exactly equal to \a start
1270 * \return true if event is found (and \a x and \a y are valid).
1273 ControlList::rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool inclusive) const
1275 if (_interpolation == Discrete) {
1276 return rt_safe_earliest_event_discrete_unlocked(start, x, y, inclusive);
1278 return rt_safe_earliest_event_linear_unlocked(start, x, y, inclusive);
1283 /** Get the earliest event after \a start without interpolation.
1285 * If an event is found, \a x and \a y are set to its coordinates.
1287 * \param inclusive Include events with timestamp exactly equal to \a start
1288 * \return true if event is found (and \a x and \a y are valid).
1291 ControlList::rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const
1293 build_search_cache_if_necessary (start);
1295 if (_search_cache.first != _events.end()) {
1296 const ControlEvent* const first = *_search_cache.first;
1298 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1300 /* Earliest points is in range, return it */
1306 /* Move left of cache to this point
1307 * (Optimize for immediate call this cycle within range) */
1308 _search_cache.left = x;
1309 ++_search_cache.first;
1318 /* No points in range */
1324 /** Get the earliest time the line crosses an integer (Linear interpolation).
1326 * If an event is found, \a x and \a y are set to its coordinates.
1328 * \param inclusive Include events with timestamp exactly equal to \a start
1329 * \return true if event is found (and \a x and \a y are valid).
1332 ControlList::rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const
1334 // cout << "earliest_event(start: " << start << ", x: " << x << ", y: " << y << ", inclusive: " << inclusive << ")" << endl;
1336 const_iterator length_check_iter = _events.begin();
1337 if (_events.empty()) { // 0 events
1339 } else if (_events.end() == ++length_check_iter) { // 1 event
1340 return rt_safe_earliest_event_discrete_unlocked (start, x, y, inclusive);
1343 // Hack to avoid infinitely repeating the same event
1344 build_search_cache_if_necessary (start);
1346 if (_search_cache.first != _events.end()) {
1348 const ControlEvent* first = NULL;
1349 const ControlEvent* next = NULL;
1351 /* Step is after first */
1352 if (_search_cache.first == _events.begin() || (*_search_cache.first)->when <= start) {
1353 first = *_search_cache.first;
1354 ++_search_cache.first;
1355 if (_search_cache.first == _events.end()) {
1358 next = *_search_cache.first;
1360 /* Step is before first */
1362 const_iterator prev = _search_cache.first;
1365 next = *_search_cache.first;
1368 if (inclusive && first->when == start) {
1371 /* Move left of cache to this point
1372 * (Optimize for immediate call this cycle within range) */
1373 _search_cache.left = x;
1374 //++_search_cache.range.first;
1379 if (fabs(first->value - next->value) <= 1) {
1380 if (next->when > start) {
1383 /* Move left of cache to this point
1384 * (Optimize for immediate call this cycle within range) */
1385 _search_cache.left = x;
1386 //++_search_cache.range.first;
1387 assert(inclusive ? x >= start : x > start);
1394 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1395 //cerr << "start y: " << start_y << endl;
1397 //y = first->value + (slope * fabs(start - first->when));
1400 if (first->value < next->value) // ramping up
1402 else // ramping down
1405 x = first->when + (y - first->value) / (double)slope;
1407 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1409 if (first->value < next->value) // ramping up
1411 else // ramping down
1414 x = first->when + (y - first->value) / (double)slope;
1417 /*cerr << first->value << " @ " << first->when << " ... "
1418 << next->value << " @ " << next->when
1419 << " = " << y << " @ " << x << endl;*/
1421 assert( (y >= first->value && y <= next->value)
1422 || (y <= first->value && y >= next->value) );
1425 const bool past_start = (inclusive ? x >= start : x > start);
1427 /* Move left of cache to this point
1428 * (Optimize for immediate call this cycle within range) */
1429 _search_cache.left = x;
1430 assert(inclusive ? x >= start : x > start);
1436 /* No points in the future, so no steps (towards them) in the future */
1443 /** @param start Start position in model coordinates.
1444 * @param end End position in model coordinates.
1445 * @param op 0 = cut, 1 = copy, 2 = clear.
1447 boost::shared_ptr<ControlList>
1448 ControlList::cut_copy_clear (double start, double end, int op)
1450 boost::shared_ptr<ControlList> nal = create (_parameter);
1452 ControlEvent cp (start, 0.0);
1455 Glib::Threads::Mutex::Lock lm (_lock);
1457 /* first, determine s & e, two iterators that define the range of points
1458 affected by this operation
1461 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1465 /* and the last that is at or after `end' */
1467 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1470 /* if "start" isn't the location of an existing point,
1471 evaluate the curve to get a value for the start. Add a point to
1472 both the existing event list, and if its not a "clear" operation,
1473 to the copy ("nal") as well.
1475 Note that the time positions of the points in each list are different
1476 because we want the copy ("nal") to have a zero time reference.
1480 /* before we begin any cut/clear operations, get the value of the curve
1484 double end_value = unlocked_eval (end);
1486 if ((*s)->when != start) {
1488 double val = unlocked_eval (start);
1490 if (op == 0) { // cut
1491 if (start > _events.front()->when) {
1492 _events.insert (s, (new ControlEvent (start, val)));
1496 if (op != 2) { // ! clear
1497 nal->_events.push_back (new ControlEvent (0, val));
1501 for (iterator x = s; x != e; ) {
1503 /* adjust new points to be relative to start, which
1504 has been set to zero.
1508 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1512 x = _events.erase (x);
1518 if (e == _events.end() || (*e)->when != end) {
1520 /* only add a boundary point if there is a point after "end"
1523 if (op == 0 && (e != _events.end() && end < (*e)->when)) { // cut
1524 _events.insert (e, new ControlEvent (end, end_value));
1527 if (op != 2 && (e != _events.end() && end < (*e)->when)) { // cut/copy
1528 nal->_events.push_back (new ControlEvent (end - start, end_value));
1532 unlocked_invalidate_insert_iterator ();
1537 maybe_signal_changed ();
1544 boost::shared_ptr<ControlList>
1545 ControlList::cut (double start, double end)
1547 return cut_copy_clear (start, end, 0);
1550 boost::shared_ptr<ControlList>
1551 ControlList::copy (double start, double end)
1553 return cut_copy_clear (start, end, 1);
1557 ControlList::clear (double start, double end)
1559 cut_copy_clear (start, end, 2);
1562 /** @param pos Position in model coordinates */
1564 ControlList::paste (ControlList& alist, double pos, float /*times*/)
1566 if (alist._events.empty()) {
1571 Glib::Threads::Mutex::Lock lm (_lock);
1575 ControlEvent cp (pos, 0.0);
1577 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1579 for (iterator i = alist.begin();i != alist.end(); ++i) {
1580 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1581 end = (*i)->when + pos;
1585 /* move all points after the insertion along the timeline by
1589 while (where != _events.end()) {
1591 if ((*where)->when <= end) {
1594 _events.erase(where);
1602 unlocked_invalidate_insert_iterator ();
1606 maybe_signal_changed ();
1610 /** Move automation around according to a list of region movements.
1611 * @param return true if anything was changed, otherwise false (ie nothing needed changing)
1614 ControlList::move_ranges (const list< RangeMove<double> >& movements)
1616 typedef list< RangeMove<double> > RangeMoveList;
1619 Glib::Threads::Mutex::Lock lm (_lock);
1621 /* a copy of the events list before we started moving stuff around */
1622 EventList old_events = _events;
1624 /* clear the source and destination ranges in the new list */
1625 bool things_erased = false;
1626 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1628 if (erase_range_internal (i->from, i->from + i->length, _events)) {
1629 things_erased = true;
1632 if (erase_range_internal (i->to, i->to + i->length, _events)) {
1633 things_erased = true;
1637 /* if nothing was erased, there is nothing to do */
1638 if (!things_erased) {
1642 /* copy the events into the new list */
1643 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1644 iterator j = old_events.begin ();
1645 const double limit = i->from + i->length;
1646 const double dx = i->to - i->from;
1647 while (j != old_events.end () && (*j)->when <= limit) {
1648 if ((*j)->when >= i->from) {
1649 ControlEvent* ev = new ControlEvent (**j);
1651 _events.push_back (ev);
1658 _events.sort (event_time_less_than);
1659 unlocked_invalidate_insert_iterator ();
1661 _sort_pending = true;
1667 maybe_signal_changed ();
1672 ControlList::set_interpolation (InterpolationStyle s)
1674 if (_interpolation == s) {
1679 InterpolationChanged (s); /* EMIT SIGNAL */
1683 ControlList::set_thinning_factor (double v)
1685 _thinning_factor = v;
1689 ControlList::operator!= (ControlList const & other) const
1691 if (_events.size() != other._events.size()) {
1695 EventList::const_iterator i = _events.begin ();
1696 EventList::const_iterator j = other._events.begin ();
1698 while (i != _events.end() && (*i)->when == (*j)->when && (*i)->value == (*j)->value) {
1703 if (i != _events.end ()) {
1708 _parameter != other._parameter ||
1709 _interpolation != other._interpolation ||
1710 _min_yval != other._min_yval ||
1711 _max_yval != other._max_yval ||
1712 _default_value != other._default_value
1716 } // namespace Evoral