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;
63 _default_value = id.normal();
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, bool add_point, double when)
373 if (yn && add_point) {
374 add_guard_point (when);
379 ControlList::add_guard_point (double when)
381 ControlEvent cp (when, 0.0);
382 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
384 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 ADD GUARD POINT @ %2looked up insert iterator for new write pass\n", this, when));
386 double eval_value = unlocked_eval (insert_position);
388 if (most_recent_insert_iterator == _events.end()) {
390 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at end, adding eval-value there %2\n", this, eval_value));
391 _events.push_back (new ControlEvent (when, eval_value));
392 /* leave insert iterator at the end */
394 } else if ((*most_recent_insert_iterator)->when == when) {
396 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at existing point, setting eval-value there %2\n", this, eval_value));
398 /* most_recent_insert_iterator points to a control event
399 already at the insert position, so there is
404 advance most_recent_insert_iterator so that the "real"
405 insert occurs in the right place, since it
406 points to the control event just inserted.
409 ++most_recent_insert_iterator;
412 /* insert a new control event at the right spot
415 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert eval-value %2 just before iterator @ %3\n",
416 this, eval_value, (*most_recent_insert_iterator)->when));
418 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator, new ControlEvent (when, eval_value));
420 /* advance most_recent_insert_iterator so that the "real"
421 * insert occurs in the right place, since it
422 * points to the control event just inserted.
425 ++most_recent_insert_iterator;
428 /* don't do this again till the next write pass */
430 new_write_pass = false;
434 ControlList::in_write_pass () const
436 return _in_write_pass;
440 ControlList::add (double when, double value)
442 /* this is for making changes from some kind of user interface or
443 control surface (GUI, MIDI, OSC etc)
446 if (!clamp_value (when, value)) {
450 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 add %2 at %3 w/erase = %4 at end ? %5\n",
451 this, value, when, _in_write_pass, (most_recent_insert_iterator == _events.end())));
453 Glib::Threads::Mutex::Lock lm (_lock);
454 ControlEvent cp (when, 0.0f);
455 iterator insertion_point;
457 if (_events.empty()) {
459 /* as long as the point we're adding is not at zero,
460 * add an "anchor" point there.
464 _events.insert (_events.end(), new ControlEvent (0, _default_value));
465 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added default value %2 at zero\n", this, _default_value));
469 if (_in_write_pass && new_write_pass) {
471 add_guard_point (insert_position);
472 did_write_during_pass = true;
474 } else if (most_recent_insert_iterator == _events.end() || when > (*most_recent_insert_iterator)->when) {
476 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 %2 erase from existing iterator (@end ? %3)\n",
477 this, (_in_write_pass ? "DO" : "DON'T"),
478 (most_recent_insert_iterator == _events.end())));
480 if (_in_write_pass) {
481 while (most_recent_insert_iterator != _events.end()) {
482 if ((*most_recent_insert_iterator)->when < when) {
483 if (_in_write_pass) {
484 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase existing @ %2\n", this, (*most_recent_insert_iterator)));
485 delete *most_recent_insert_iterator;
486 most_recent_insert_iterator = _events.erase (most_recent_insert_iterator);
489 } else if ((*most_recent_insert_iterator)->when >= when) {
492 ++most_recent_insert_iterator;
495 if (most_recent_insert_iterator != _events.end()) {
496 if ((*most_recent_insert_iterator)->when - when > 64) {
497 /* next control point is some
498 * distance from where our new
499 * point is going to go - add a
500 * new point to avoid changing
501 * the shape of the line too
502 * much. the insert iterator needs
503 * to point to the new control
504 * point so that our insert
505 * will happen correctly.
507 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator,
508 new ControlEvent (when+1, (*most_recent_insert_iterator)->value));
509 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added post-erase guard point @ %2 = %3\n",
511 (*most_recent_insert_iterator)->value));
517 /* not in a write pass: figure out the iterator we should insert in front of */
519 DEBUG_TRACE (DEBUG::ControlList, string_compose ("compute MRI for position %1\n", when));
520 ControlEvent cp (when, 0.0f);
521 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
526 /* not in a write pass, adding a point within existing
527 * data: figure out the iterator we should insert in
531 DEBUG_TRACE (DEBUG::ControlList, string_compose ("compute(b) MRI for position %1\n", when));
532 ControlEvent cp (when, 0.0f);
533 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
536 /* OK, now we're really ready to add a new point
539 if (most_recent_insert_iterator == _events.end()) {
540 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 appending new point at end\n", this));
544 /* check if would just be adding to a straight line,
545 * and don't add another point if so
548 if (!_events.empty()) { // avoid O(N) _events.size() here
549 if (_events.back()->value == value) {
550 EventList::iterator b = _events.end();
551 --b; // final point, which we know exists
552 if (b != _events.begin()) { // step back again, but check first that it is legal
553 --b; // penultimate-point
554 if ((*b)->value == value) {
555 /* there are at least two points with the exact same value ...
556 * straight line - just move the final
557 * point to the new time
559 _events.back()->when = when;
561 DEBUG_TRACE (DEBUG::ControlList, string_compose ("final value of %1 moved to %2\n", value, when));
568 _events.push_back (new ControlEvent (when, value));
569 DEBUG_TRACE (DEBUG::ControlList, string_compose ("\tactually appended, size now %1\n", _events.size()));
572 if (!_in_write_pass) {
573 most_recent_insert_iterator = _events.end();
574 --most_recent_insert_iterator;
577 } else if ((*most_recent_insert_iterator)->when == when) {
579 if ((*most_recent_insert_iterator)->value != value) {
580 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 reset existing point to new value %2\n", this, value));
582 /* only one point allowed per time point, so just
583 * reset the value here.
586 (*most_recent_insert_iterator)->value = value;
588 /* if we modified the final value, then its as
589 * if we inserted a new point as far as the
590 * next addition, so make sure we know that.
593 if (_in_write_pass && _events.back()->when == when) {
594 most_recent_insert_iterator = _events.end();
598 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 same time %2, same value value %3\n", this, when, value));
602 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert new point at %2 at iterator at %3\n", this, when, (*most_recent_insert_iterator)->when));
606 /* check if would just be adding to a straight line,
607 * and don't add another point if so
610 if (most_recent_insert_iterator != _events.begin()) {
611 EventList::iterator b = most_recent_insert_iterator;
612 --b; // prior point, which we know exists
613 if ((*b)->value == value) { // same value as the point we plan to insert
614 if (b != _events.begin()) { // step back again, which may not be possible
615 EventList::iterator bb = b;
616 --bb; // next-to-prior-point
617 if ((*bb)->value == value) {
618 /* straight line - just move the prior
619 * point to the new time
623 if (!_in_write_pass) {
624 most_recent_insert_iterator = b;
627 DEBUG_TRACE (DEBUG::ControlList, string_compose ("final value of %1 moved to %2\n", value, when));
634 if (most_recent_insert_iterator != _events.end()) {
635 if ((*most_recent_insert_iterator)->when - when > 64) {
636 /* next control point is some
637 * distance from where our new
638 * point is going to go - add a
639 * new point to avoid changing
640 * the shape of the line too
641 * much. the insert iterator needs
642 * to point to the new control
643 * point so that our insert
644 * will happen correctly.
646 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator,
647 new ControlEvent (when+1, (*most_recent_insert_iterator)->value));
648 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added insert-post-erase guard point @ %2 = %3\n",
650 (*most_recent_insert_iterator)->value));
655 EventList::iterator x = _events.insert (most_recent_insert_iterator, new ControlEvent (when, value));
656 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 inserted new value before MRI, size now %2\n", this, _events.size()));
658 if (!_in_write_pass) {
659 most_recent_insert_iterator = x;
667 maybe_signal_changed ();
671 ControlList::erase (iterator i)
674 Glib::Threads::Mutex::Lock lm (_lock);
675 if (most_recent_insert_iterator == i) {
676 unlocked_invalidate_insert_iterator ();
681 maybe_signal_changed ();
685 ControlList::erase (iterator start, iterator end)
688 Glib::Threads::Mutex::Lock lm (_lock);
689 _events.erase (start, end);
690 unlocked_invalidate_insert_iterator ();
693 maybe_signal_changed ();
696 /** Erase the first event which matches the given time and value */
698 ControlList::erase (double when, double value)
701 Glib::Threads::Mutex::Lock lm (_lock);
703 iterator i = begin ();
704 while (i != end() && ((*i)->when != when || (*i)->value != value)) {
710 if (most_recent_insert_iterator == i) {
711 unlocked_invalidate_insert_iterator ();
718 maybe_signal_changed ();
722 ControlList::erase_range (double start, double endt)
727 Glib::Threads::Mutex::Lock lm (_lock);
728 erased = erase_range_internal (start, endt, _events);
737 maybe_signal_changed ();
742 ControlList::erase_range_internal (double start, double endt, EventList & events)
745 ControlEvent cp (start, 0.0f);
749 if ((s = lower_bound (events.begin(), events.end(), &cp, time_comparator)) != events.end()) {
751 e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
754 unlocked_invalidate_insert_iterator ();
763 ControlList::slide (iterator before, double distance)
766 Glib::Threads::Mutex::Lock lm (_lock);
768 if (before == _events.end()) {
772 while (before != _events.end()) {
773 (*before)->when += distance;
780 maybe_signal_changed ();
784 ControlList::shift (double pos, double frames)
787 Glib::Threads::Mutex::Lock lm (_lock);
789 for (iterator i = _events.begin(); i != _events.end(); ++i) {
790 if ((*i)->when >= pos) {
791 (*i)->when += frames;
798 maybe_signal_changed ();
802 ControlList::modify (iterator iter, double when, double val)
804 /* note: we assume higher level logic is in place to avoid this
805 reordering the time-order of control events in the list. ie. all
806 points after *iter are later than when.
810 Glib::Threads::Mutex::Lock lm (_lock);
812 (*iter)->when = when;
813 (*iter)->value = val;
820 _events.sort (event_time_less_than);
821 unlocked_invalidate_insert_iterator ();
823 _sort_pending = true;
829 maybe_signal_changed ();
832 std::pair<ControlList::iterator,ControlList::iterator>
833 ControlList::control_points_adjacent (double xval)
835 Glib::Threads::Mutex::Lock lm (_lock);
837 ControlEvent cp (xval, 0.0f);
838 std::pair<iterator,iterator> ret;
840 ret.first = _events.end();
841 ret.second = _events.end();
843 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
845 if (ret.first == _events.end()) {
846 if ((*i)->when >= xval) {
847 if (i != _events.begin()) {
856 if ((*i)->when > xval) {
866 ControlList::freeze ()
881 Glib::Threads::Mutex::Lock lm (_lock);
884 _events.sort (event_time_less_than);
885 unlocked_invalidate_insert_iterator ();
886 _sort_pending = false;
892 ControlList::mark_dirty () const
894 _lookup_cache.left = -1;
895 _search_cache.left = -1;
898 _curve->mark_dirty();
901 Dirty (); /* EMIT SIGNAL */
905 ControlList::truncate_end (double last_coordinate)
908 Glib::Threads::Mutex::Lock lm (_lock);
909 ControlEvent cp (last_coordinate, 0);
910 ControlList::reverse_iterator i;
913 if (_events.empty()) {
917 if (last_coordinate == _events.back()->when) {
921 if (last_coordinate > _events.back()->when) {
926 iterator foo = _events.begin();
929 if (foo == _events.end()) {
931 } else if (++foo == _events.end()) {
938 /* less than 2 points: add a new point */
939 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
942 /* more than 2 points: check to see if the last 2 values
943 are equal. if so, just move the position of the
944 last point. otherwise, add a new point.
947 iterator penultimate = _events.end();
948 --penultimate; /* points at last point */
949 --penultimate; /* points at the penultimate point */
951 if (_events.back()->value == (*penultimate)->value) {
952 _events.back()->when = last_coordinate;
954 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
962 last_val = unlocked_eval (last_coordinate);
963 last_val = max ((double) _min_yval, last_val);
964 last_val = min ((double) _max_yval, last_val);
966 i = _events.rbegin();
968 /* make i point to the last control point */
972 /* now go backwards, removing control points that are
973 beyond the new last coordinate.
976 // FIXME: SLOW! (size() == O(n))
978 uint32_t sz = _events.size();
980 while (i != _events.rend() && sz > 2) {
981 ControlList::reverse_iterator tmp;
986 if ((*i)->when < last_coordinate) {
990 _events.erase (i.base());
996 _events.back()->when = last_coordinate;
997 _events.back()->value = last_val;
1000 unlocked_invalidate_insert_iterator ();
1004 maybe_signal_changed ();
1008 ControlList::truncate_start (double overall_length)
1011 Glib::Threads::Mutex::Lock lm (_lock);
1013 double first_legal_value;
1014 double first_legal_coordinate;
1016 assert(!_events.empty());
1018 if (overall_length == _events.back()->when) {
1019 /* no change in overall length */
1023 if (overall_length > _events.back()->when) {
1025 /* growing at front: duplicate first point. shift all others */
1027 double shift = overall_length - _events.back()->when;
1030 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
1031 (*i)->when += shift;
1036 /* less than 2 points: add a new point */
1037 _events.push_front (new ControlEvent (0, _events.front()->value));
1041 /* more than 2 points: check to see if the first 2 values
1042 are equal. if so, just move the position of the
1043 first point. otherwise, add a new point.
1046 iterator second = _events.begin();
1047 ++second; /* points at the second point */
1049 if (_events.front()->value == (*second)->value) {
1050 /* first segment is flat, just move start point back to zero */
1051 _events.front()->when = 0;
1053 /* leave non-flat segment in place, add a new leading point. */
1054 _events.push_front (new ControlEvent (0, _events.front()->value));
1060 /* shrinking at front */
1062 first_legal_coordinate = _events.back()->when - overall_length;
1063 first_legal_value = unlocked_eval (first_legal_coordinate);
1064 first_legal_value = max (_min_yval, first_legal_value);
1065 first_legal_value = min (_max_yval, first_legal_value);
1067 /* remove all events earlier than the new "front" */
1069 i = _events.begin();
1071 while (i != _events.end() && !_events.empty()) {
1072 ControlList::iterator tmp;
1077 if ((*i)->when > first_legal_coordinate) {
1087 /* shift all remaining points left to keep their same
1091 for (i = _events.begin(); i != _events.end(); ++i) {
1092 (*i)->when -= first_legal_coordinate;
1095 /* add a new point for the interpolated new value */
1097 _events.push_front (new ControlEvent (0, first_legal_value));
1100 unlocked_invalidate_insert_iterator ();
1104 maybe_signal_changed ();
1108 ControlList::unlocked_eval (double x) const
1110 pair<EventList::iterator,EventList::iterator> range;
1116 const_iterator length_check_iter = _events.begin();
1117 for (npoints = 0; npoints < 4; ++npoints, ++length_check_iter) {
1118 if (length_check_iter == _events.end()) {
1125 return _default_value;
1128 return _events.front()->value;
1131 if (x >= _events.back()->when) {
1132 return _events.back()->value;
1133 } else if (x <= _events.front()->when) {
1134 return _events.front()->value;
1137 lpos = _events.front()->when;
1138 lval = _events.front()->value;
1139 upos = _events.back()->when;
1140 uval = _events.back()->value;
1142 if (_interpolation == Discrete) {
1146 /* linear interpolation betweeen the two points */
1147 fraction = (double) (x - lpos) / (double) (upos - lpos);
1148 return lval + (fraction * (uval - lval));
1151 if (x >= _events.back()->when) {
1152 return _events.back()->value;
1153 } else if (x <= _events.front()->when) {
1154 return _events.front()->value;
1157 return multipoint_eval (x);
1160 /*NOTREACHED*/ /* stupid gcc */
1161 return _default_value;
1165 ControlList::multipoint_eval (double x) const
1171 /* "Stepped" lookup (no interpolation) */
1172 /* FIXME: no cache. significant? */
1173 if (_interpolation == Discrete) {
1174 const ControlEvent cp (x, 0);
1175 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
1177 // shouldn't have made it to multipoint_eval
1178 assert(i != _events.end());
1180 if (i == _events.begin() || (*i)->when == x)
1183 return (*(--i))->value;
1186 /* Only do the range lookup if x is in a different range than last time
1187 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
1188 if ((_lookup_cache.left < 0) ||
1189 ((_lookup_cache.left > x) ||
1190 (_lookup_cache.range.first == _events.end()) ||
1191 ((*_lookup_cache.range.second)->when < x))) {
1193 const ControlEvent cp (x, 0);
1195 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
1198 pair<const_iterator,const_iterator> range = _lookup_cache.range;
1200 if (range.first == range.second) {
1202 /* x does not exist within the list as a control point */
1204 _lookup_cache.left = x;
1206 if (range.first != _events.begin()) {
1208 lpos = (*range.first)->when;
1209 lval = (*range.first)->value;
1211 /* we're before the first point */
1212 // return _default_value;
1213 return _events.front()->value;
1216 if (range.second == _events.end()) {
1217 /* we're after the last point */
1218 return _events.back()->value;
1221 upos = (*range.second)->when;
1222 uval = (*range.second)->value;
1224 /* linear interpolation betweeen the two points
1228 fraction = (double) (x - lpos) / (double) (upos - lpos);
1229 return lval + (fraction * (uval - lval));
1233 /* x is a control point in the data */
1234 _lookup_cache.left = -1;
1235 return (*range.first)->value;
1239 ControlList::build_search_cache_if_necessary (double start) const
1241 /* Only do the range lookup if x is in a different range than last time
1242 * this was called (or if the search cache has been marked "dirty" (left<0) */
1243 if (!_events.empty() && ((_search_cache.left < 0) || (_search_cache.left > start))) {
1245 const ControlEvent start_point (start, 0);
1247 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1248 // << start << ".." << end << ")" << endl;
1250 _search_cache.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1251 _search_cache.left = start;
1255 /** Get the earliest event after \a start using the current interpolation style.
1257 * If an event is found, \a x and \a y are set to its coordinates.
1259 * \param inclusive Include events with timestamp exactly equal to \a start
1260 * \return true if event is found (and \a x and \a y are valid).
1263 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
1265 // FIXME: It would be nice if this was unnecessary..
1266 Glib::Threads::Mutex::Lock lm(_lock, Glib::Threads::TRY_LOCK);
1271 return rt_safe_earliest_event_unlocked (start, x, y, inclusive);
1275 /** Get the earliest event after \a start using the current interpolation style.
1277 * If an event is found, \a x and \a y are set to its coordinates.
1279 * \param inclusive Include events with timestamp exactly equal to \a start
1280 * \return true if event is found (and \a x and \a y are valid).
1283 ControlList::rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool inclusive) const
1285 if (_interpolation == Discrete) {
1286 return rt_safe_earliest_event_discrete_unlocked(start, x, y, inclusive);
1288 return rt_safe_earliest_event_linear_unlocked(start, x, y, inclusive);
1293 /** Get the earliest event after \a start without interpolation.
1295 * If an event is found, \a x and \a y are set to its coordinates.
1297 * \param inclusive Include events with timestamp exactly equal to \a start
1298 * \return true if event is found (and \a x and \a y are valid).
1301 ControlList::rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const
1303 build_search_cache_if_necessary (start);
1305 if (_search_cache.first != _events.end()) {
1306 const ControlEvent* const first = *_search_cache.first;
1308 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1310 /* Earliest points is in range, return it */
1316 /* Move left of cache to this point
1317 * (Optimize for immediate call this cycle within range) */
1318 _search_cache.left = x;
1319 ++_search_cache.first;
1328 /* No points in range */
1334 /** Get the earliest time the line crosses an integer (Linear interpolation).
1336 * If an event is found, \a x and \a y are set to its coordinates.
1338 * \param inclusive Include events with timestamp exactly equal to \a start
1339 * \return true if event is found (and \a x and \a y are valid).
1342 ControlList::rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const
1344 // cout << "earliest_event(start: " << start << ", x: " << x << ", y: " << y << ", inclusive: " << inclusive << ")" << endl;
1346 const_iterator length_check_iter = _events.begin();
1347 if (_events.empty()) { // 0 events
1349 } else if (_events.end() == ++length_check_iter) { // 1 event
1350 return rt_safe_earliest_event_discrete_unlocked (start, x, y, inclusive);
1353 // Hack to avoid infinitely repeating the same event
1354 build_search_cache_if_necessary (start);
1356 if (_search_cache.first != _events.end()) {
1358 const ControlEvent* first = NULL;
1359 const ControlEvent* next = NULL;
1361 /* Step is after first */
1362 if (_search_cache.first == _events.begin() || (*_search_cache.first)->when <= start) {
1363 first = *_search_cache.first;
1364 ++_search_cache.first;
1365 if (_search_cache.first == _events.end()) {
1368 next = *_search_cache.first;
1370 /* Step is before first */
1372 const_iterator prev = _search_cache.first;
1375 next = *_search_cache.first;
1378 if (inclusive && first->when == start) {
1381 /* Move left of cache to this point
1382 * (Optimize for immediate call this cycle within range) */
1383 _search_cache.left = x;
1384 //++_search_cache.range.first;
1389 if (fabs(first->value - next->value) <= 1) {
1390 if (next->when > start) {
1393 /* Move left of cache to this point
1394 * (Optimize for immediate call this cycle within range) */
1395 _search_cache.left = x;
1396 //++_search_cache.range.first;
1397 assert(inclusive ? x >= start : x > start);
1404 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1405 //cerr << "start y: " << start_y << endl;
1407 //y = first->value + (slope * fabs(start - first->when));
1410 if (first->value < next->value) // ramping up
1412 else // ramping down
1415 x = first->when + (y - first->value) / (double)slope;
1417 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1419 if (first->value < next->value) // ramping up
1421 else // ramping down
1424 x = first->when + (y - first->value) / (double)slope;
1427 /*cerr << first->value << " @ " << first->when << " ... "
1428 << next->value << " @ " << next->when
1429 << " = " << y << " @ " << x << endl;*/
1431 assert( (y >= first->value && y <= next->value)
1432 || (y <= first->value && y >= next->value) );
1435 const bool past_start = (inclusive ? x >= start : x > start);
1437 /* Move left of cache to this point
1438 * (Optimize for immediate call this cycle within range) */
1439 _search_cache.left = x;
1440 assert(inclusive ? x >= start : x > start);
1446 /* No points in the future, so no steps (towards them) in the future */
1453 /** @param start Start position in model coordinates.
1454 * @param end End position in model coordinates.
1455 * @param op 0 = cut, 1 = copy, 2 = clear.
1457 boost::shared_ptr<ControlList>
1458 ControlList::cut_copy_clear (double start, double end, int op)
1460 boost::shared_ptr<ControlList> nal = create (_parameter);
1462 ControlEvent cp (start, 0.0);
1465 Glib::Threads::Mutex::Lock lm (_lock);
1467 /* first, determine s & e, two iterators that define the range of points
1468 affected by this operation
1471 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1475 /* and the last that is at or after `end' */
1477 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1480 /* if "start" isn't the location of an existing point,
1481 evaluate the curve to get a value for the start. Add a point to
1482 both the existing event list, and if its not a "clear" operation,
1483 to the copy ("nal") as well.
1485 Note that the time positions of the points in each list are different
1486 because we want the copy ("nal") to have a zero time reference.
1490 /* before we begin any cut/clear operations, get the value of the curve
1494 double end_value = unlocked_eval (end);
1496 if ((*s)->when != start) {
1498 double val = unlocked_eval (start);
1500 if (op == 0) { // cut
1501 if (start > _events.front()->when) {
1502 _events.insert (s, (new ControlEvent (start, val)));
1506 if (op != 2) { // ! clear
1507 nal->_events.push_back (new ControlEvent (0, val));
1511 for (iterator x = s; x != e; ) {
1513 /* adjust new points to be relative to start, which
1514 has been set to zero.
1518 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1522 x = _events.erase (x);
1528 if (e == _events.end() || (*e)->when != end) {
1530 /* only add a boundary point if there is a point after "end"
1533 if (op == 0 && (e != _events.end() && end < (*e)->when)) { // cut
1534 _events.insert (e, new ControlEvent (end, end_value));
1537 if (op != 2 && (e != _events.end() && end < (*e)->when)) { // cut/copy
1538 nal->_events.push_back (new ControlEvent (end - start, end_value));
1542 unlocked_invalidate_insert_iterator ();
1547 maybe_signal_changed ();
1554 boost::shared_ptr<ControlList>
1555 ControlList::cut (double start, double end)
1557 return cut_copy_clear (start, end, 0);
1560 boost::shared_ptr<ControlList>
1561 ControlList::copy (double start, double end)
1563 return cut_copy_clear (start, end, 1);
1567 ControlList::clear (double start, double end)
1569 cut_copy_clear (start, end, 2);
1572 /** @param pos Position in model coordinates */
1574 ControlList::paste (ControlList& alist, double pos, float /*times*/)
1576 if (alist._events.empty()) {
1581 Glib::Threads::Mutex::Lock lm (_lock);
1585 ControlEvent cp (pos, 0.0);
1587 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1589 for (iterator i = alist.begin();i != alist.end(); ++i) {
1590 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1591 end = (*i)->when + pos;
1595 /* move all points after the insertion along the timeline by
1599 while (where != _events.end()) {
1601 if ((*where)->when <= end) {
1604 _events.erase(where);
1612 unlocked_invalidate_insert_iterator ();
1616 maybe_signal_changed ();
1620 /** Move automation around according to a list of region movements.
1621 * @param return true if anything was changed, otherwise false (ie nothing needed changing)
1624 ControlList::move_ranges (const list< RangeMove<double> >& movements)
1626 typedef list< RangeMove<double> > RangeMoveList;
1629 Glib::Threads::Mutex::Lock lm (_lock);
1631 /* a copy of the events list before we started moving stuff around */
1632 EventList old_events = _events;
1634 /* clear the source and destination ranges in the new list */
1635 bool things_erased = false;
1636 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1638 if (erase_range_internal (i->from, i->from + i->length, _events)) {
1639 things_erased = true;
1642 if (erase_range_internal (i->to, i->to + i->length, _events)) {
1643 things_erased = true;
1647 /* if nothing was erased, there is nothing to do */
1648 if (!things_erased) {
1652 /* copy the events into the new list */
1653 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1654 iterator j = old_events.begin ();
1655 const double limit = i->from + i->length;
1656 const double dx = i->to - i->from;
1657 while (j != old_events.end () && (*j)->when <= limit) {
1658 if ((*j)->when >= i->from) {
1659 ControlEvent* ev = new ControlEvent (**j);
1661 _events.push_back (ev);
1668 _events.sort (event_time_less_than);
1669 unlocked_invalidate_insert_iterator ();
1671 _sort_pending = true;
1677 maybe_signal_changed ();
1682 ControlList::set_interpolation (InterpolationStyle s)
1684 if (_interpolation == s) {
1689 InterpolationChanged (s); /* EMIT SIGNAL */
1693 ControlList::set_thinning_factor (double v)
1695 _thinning_factor = v;
1699 ControlList::operator!= (ControlList const & other) const
1701 if (_events.size() != other._events.size()) {
1705 EventList::const_iterator i = _events.begin ();
1706 EventList::const_iterator j = other._events.begin ();
1708 while (i != _events.end() && (*i)->when == (*j)->when && (*i)->value == (*j)->value) {
1713 if (i != _events.end ()) {
1718 _parameter != other._parameter ||
1719 _interpolation != other._interpolation ||
1720 _min_yval != other._min_yval ||
1721 _max_yval != other._max_yval ||
1722 _default_value != other._default_value
1727 ControlList::dump (ostream& o)
1729 /* NOT LOCKED ... for debugging only */
1731 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
1732 o << (*x)->value << " @ " << (*x)->when << endl;
1736 } // namespace Evoral