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 /* leave the insert iterator invalid, so that we will do the lookup
550 of where it should be in a "lazy" way - deferring it until
551 we actually add the first point (which may never happen).
554 unlocked_invalidate_insert_iterator ();
558 ControlList::add (double when, double value, bool erase_since_last_add)
560 /* this is for making changes from some kind of user interface or
561 control surface (GUI, MIDI, OSC etc)
564 if (!clamp_value (when, value)) {
568 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 add %2 at %3 w/erase = %4\n", this, value, when, erase_since_last_add));
571 Glib::Mutex::Lock lm (_lock);
572 ControlEvent cp (when, 0.0f);
573 iterator insertion_point;
575 if (_events.empty()) {
577 /* as long as the point we're adding is not at zero,
578 * add an "anchor" point there.
582 _events.insert (_events.end(), new ControlEvent (0, _default_value));
583 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added default value %2 at zero\n", this, _default_value));
587 if (new_write_pass) {
589 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 new write pass, insert pos = %2\n", this, insert_position));
591 /* The first addition of a new control event during a
594 * We need to add a new point at insert_position
595 * corresponding the value there.
598 /* the insert_iterator is not set, figure out where
602 ControlEvent cp (insert_position, 0.0);
603 insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
604 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 looked up insert iterator for new write pass\n", this));
606 double eval_value = unlocked_eval (insert_position);
608 if (insert_iterator == _events.end()) {
609 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at end, adding eval-value there %2\n", this, eval_value));
610 _events.push_back (new ControlEvent (insert_position, eval_value));
611 /* leave insert iterator at the end */
613 } else if ((*insert_iterator)->when == when) {
615 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at existing point, setting eval-value there %2\n", this, eval_value));
617 /* insert_iterator points to a control event
618 already at the insert position, so there is
623 advance insert_iterator so that the "real"
624 insert occurs in the right place, since it
625 points to the control event just inserted.
631 /* insert a new control event at the right spot
634 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert eval-value %2 at iterator\n", this, eval_value));
636 insert_iterator = _events.insert (insert_iterator, new ControlEvent (insert_position, eval_value));
638 /* advance insert_iterator so that the "real"
639 * insert occurs in the right place, since it
640 * points to the control event just inserted.
646 /* don't do this again till the next write pass */
648 new_write_pass = false;
649 did_write_during_pass = true;
651 } else if (insert_iterator == _events.end() || when > (*insert_iterator)->when) {
653 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 need to discover insert iterator (@end ? %2)\n",
654 this, (insert_iterator == _events.end())));
656 /* this means that we either *know* we want to insert
657 * at the end, or that we don't know where to insert.
659 * so ... lets perform some quick checks before we
660 * go doing binary search to figure out where to
664 if (_events.back()->when == when) {
666 /* we need to modify the final point, so
667 make insert_iterator point to it.
670 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 modify final value\n", this));
672 insert_iterator = _events.end();
675 } else if (_events.back()->when < when) {
677 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 plan to append to list\n", this));
679 if (erase_since_last_add) {
680 /* remove the final point, because
681 we're adding one beyond it.
683 delete _events.back();
687 /* leaving this here will force an append */
689 insert_iterator = _events.end();
693 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase %2 from existing iterator (@end ? %3\n",
694 this, erase_since_last_add,
695 (insert_iterator == _events.end())));
697 while (insert_iterator != _events.end()) {
698 if ((*insert_iterator)->when < when) {
699 if (erase_since_last_add) {
700 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase existing @ %2\n", this, (*insert_iterator)));
701 delete *insert_iterator;
702 insert_iterator = _events.erase (insert_iterator);
705 } else if ((*insert_iterator)->when >= when) {
713 /* OK, now we're really ready to add a new point
716 if (insert_iterator == _events.end()) {
717 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 appending new point at end\n", this));
718 _events.push_back (new ControlEvent (when, value));
719 /* leave insert_iterator as it was: at the end */
721 } else if ((*insert_iterator)->when == when) {
722 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 reset existing point to new value %2\n", this, value));
723 /* only one point allowed per time point, so just
724 * reset the value here.
726 (*insert_iterator)->value = value;
727 /* insert iterator now points past the control event we just
728 * modified. the next insert needs to be after this,
733 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert new point at %2 at iterator at %3\n", this, when, (*insert_iterator)->when));
734 _events.insert (insert_iterator, new ControlEvent (when, value));
735 /* leave insert iterator where it was, since it points
736 * to the next control event AFTER the one we just inserted.
744 maybe_signal_changed ();
748 ControlList::erase (iterator i)
751 Glib::Mutex::Lock lm (_lock);
752 if (insert_iterator == i) {
753 unlocked_invalidate_insert_iterator ();
758 maybe_signal_changed ();
762 ControlList::erase (iterator start, iterator end)
765 Glib::Mutex::Lock lm (_lock);
766 _events.erase (start, end);
767 unlocked_invalidate_insert_iterator ();
770 maybe_signal_changed ();
773 /** Erase the first event which matches the given time and value */
775 ControlList::erase (double when, double value)
778 Glib::Mutex::Lock lm (_lock);
780 iterator i = begin ();
781 while (i != end() && ((*i)->when != when || (*i)->value != value)) {
787 if (insert_iterator == i) {
788 unlocked_invalidate_insert_iterator ();
795 maybe_signal_changed ();
799 ControlList::erase_range (double start, double endt)
804 Glib::Mutex::Lock lm (_lock);
805 erased = erase_range_internal (start, endt, _events);
814 maybe_signal_changed ();
819 ControlList::erase_range_internal (double start, double endt, EventList & events)
822 ControlEvent cp (start, 0.0f);
826 if ((s = lower_bound (events.begin(), events.end(), &cp, time_comparator)) != events.end()) {
828 e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
831 unlocked_invalidate_insert_iterator ();
840 ControlList::slide (iterator before, double distance)
843 Glib::Mutex::Lock lm (_lock);
845 if (before == _events.end()) {
849 while (before != _events.end()) {
850 (*before)->when += distance;
857 maybe_signal_changed ();
861 ControlList::shift (double pos, double frames)
864 Glib::Mutex::Lock lm (_lock);
866 for (iterator i = _events.begin(); i != _events.end(); ++i) {
867 if ((*i)->when >= pos) {
868 (*i)->when += frames;
875 maybe_signal_changed ();
879 ControlList::modify (iterator iter, double when, double val)
881 /* note: we assume higher level logic is in place to avoid this
882 reordering the time-order of control events in the list. ie. all
883 points after *iter are later than when.
887 Glib::Mutex::Lock lm (_lock);
889 (*iter)->when = when;
890 (*iter)->value = val;
892 if (std::isnan (val)) {
897 _events.sort (event_time_less_than);
898 unlocked_invalidate_insert_iterator ();
900 _sort_pending = true;
906 maybe_signal_changed ();
909 std::pair<ControlList::iterator,ControlList::iterator>
910 ControlList::control_points_adjacent (double xval)
912 Glib::Mutex::Lock lm (_lock);
914 ControlEvent cp (xval, 0.0f);
915 std::pair<iterator,iterator> ret;
917 ret.first = _events.end();
918 ret.second = _events.end();
920 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
922 if (ret.first == _events.end()) {
923 if ((*i)->when >= xval) {
924 if (i != _events.begin()) {
933 if ((*i)->when > xval) {
943 ControlList::freeze ()
958 Glib::Mutex::Lock lm (_lock);
961 _events.sort (event_time_less_than);
962 unlocked_invalidate_insert_iterator ();
963 _sort_pending = false;
969 ControlList::mark_dirty () const
971 _lookup_cache.left = -1;
972 _search_cache.left = -1;
975 _curve->mark_dirty();
978 Dirty (); /* EMIT SIGNAL */
982 ControlList::truncate_end (double last_coordinate)
985 Glib::Mutex::Lock lm (_lock);
986 ControlEvent cp (last_coordinate, 0);
987 ControlList::reverse_iterator i;
990 if (_events.empty()) {
994 if (last_coordinate == _events.back()->when) {
998 if (last_coordinate > _events.back()->when) {
1003 iterator foo = _events.begin();
1006 if (foo == _events.end()) {
1008 } else if (++foo == _events.end()) {
1011 lessthantwo = false;
1015 /* less than 2 points: add a new point */
1016 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
1019 /* more than 2 points: check to see if the last 2 values
1020 are equal. if so, just move the position of the
1021 last point. otherwise, add a new point.
1024 iterator penultimate = _events.end();
1025 --penultimate; /* points at last point */
1026 --penultimate; /* points at the penultimate point */
1028 if (_events.back()->value == (*penultimate)->value) {
1029 _events.back()->when = last_coordinate;
1031 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
1037 /* shortening end */
1039 last_val = unlocked_eval (last_coordinate);
1040 last_val = max ((double) _min_yval, last_val);
1041 last_val = min ((double) _max_yval, last_val);
1043 i = _events.rbegin();
1045 /* make i point to the last control point */
1049 /* now go backwards, removing control points that are
1050 beyond the new last coordinate.
1053 // FIXME: SLOW! (size() == O(n))
1055 uint32_t sz = _events.size();
1057 while (i != _events.rend() && sz > 2) {
1058 ControlList::reverse_iterator tmp;
1063 if ((*i)->when < last_coordinate) {
1067 _events.erase (i.base());
1073 _events.back()->when = last_coordinate;
1074 _events.back()->value = last_val;
1077 unlocked_invalidate_insert_iterator ();
1081 maybe_signal_changed ();
1085 ControlList::truncate_start (double overall_length)
1088 Glib::Mutex::Lock lm (_lock);
1090 double first_legal_value;
1091 double first_legal_coordinate;
1093 assert(!_events.empty());
1095 if (overall_length == _events.back()->when) {
1096 /* no change in overall length */
1100 if (overall_length > _events.back()->when) {
1102 /* growing at front: duplicate first point. shift all others */
1104 double shift = overall_length - _events.back()->when;
1107 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
1108 (*i)->when += shift;
1113 /* less than 2 points: add a new point */
1114 _events.push_front (new ControlEvent (0, _events.front()->value));
1118 /* more than 2 points: check to see if the first 2 values
1119 are equal. if so, just move the position of the
1120 first point. otherwise, add a new point.
1123 iterator second = _events.begin();
1124 ++second; /* points at the second point */
1126 if (_events.front()->value == (*second)->value) {
1127 /* first segment is flat, just move start point back to zero */
1128 _events.front()->when = 0;
1130 /* leave non-flat segment in place, add a new leading point. */
1131 _events.push_front (new ControlEvent (0, _events.front()->value));
1137 /* shrinking at front */
1139 first_legal_coordinate = _events.back()->when - overall_length;
1140 first_legal_value = unlocked_eval (first_legal_coordinate);
1141 first_legal_value = max (_min_yval, first_legal_value);
1142 first_legal_value = min (_max_yval, first_legal_value);
1144 /* remove all events earlier than the new "front" */
1146 i = _events.begin();
1148 while (i != _events.end() && !_events.empty()) {
1149 ControlList::iterator tmp;
1154 if ((*i)->when > first_legal_coordinate) {
1164 /* shift all remaining points left to keep their same
1168 for (i = _events.begin(); i != _events.end(); ++i) {
1169 (*i)->when -= first_legal_coordinate;
1172 /* add a new point for the interpolated new value */
1174 _events.push_front (new ControlEvent (0, first_legal_value));
1177 unlocked_invalidate_insert_iterator ();
1181 maybe_signal_changed ();
1185 ControlList::unlocked_eval (double x) const
1187 pair<EventList::iterator,EventList::iterator> range;
1193 const_iterator length_check_iter = _events.begin();
1194 for (npoints = 0; npoints < 4; ++npoints, ++length_check_iter) {
1195 if (length_check_iter == _events.end()) {
1202 return _default_value;
1205 return _events.front()->value;
1208 if (x >= _events.back()->when) {
1209 return _events.back()->value;
1210 } else if (x <= _events.front()->when) {
1211 return _events.front()->value;
1214 lpos = _events.front()->when;
1215 lval = _events.front()->value;
1216 upos = _events.back()->when;
1217 uval = _events.back()->value;
1219 if (_interpolation == Discrete) {
1223 /* linear interpolation betweeen the two points */
1224 fraction = (double) (x - lpos) / (double) (upos - lpos);
1225 return lval + (fraction * (uval - lval));
1228 if (x >= _events.back()->when) {
1229 return _events.back()->value;
1230 } else if (x <= _events.front()->when) {
1231 return _events.front()->value;
1234 return multipoint_eval (x);
1237 /*NOTREACHED*/ /* stupid gcc */
1238 return _default_value;
1242 ControlList::multipoint_eval (double x) const
1248 /* "Stepped" lookup (no interpolation) */
1249 /* FIXME: no cache. significant? */
1250 if (_interpolation == Discrete) {
1251 const ControlEvent cp (x, 0);
1252 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
1254 // shouldn't have made it to multipoint_eval
1255 assert(i != _events.end());
1257 if (i == _events.begin() || (*i)->when == x)
1260 return (*(--i))->value;
1263 /* Only do the range lookup if x is in a different range than last time
1264 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
1265 if ((_lookup_cache.left < 0) ||
1266 ((_lookup_cache.left > x) ||
1267 (_lookup_cache.range.first == _events.end()) ||
1268 ((*_lookup_cache.range.second)->when < x))) {
1270 const ControlEvent cp (x, 0);
1272 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
1275 pair<const_iterator,const_iterator> range = _lookup_cache.range;
1277 if (range.first == range.second) {
1279 /* x does not exist within the list as a control point */
1281 _lookup_cache.left = x;
1283 if (range.first != _events.begin()) {
1285 lpos = (*range.first)->when;
1286 lval = (*range.first)->value;
1288 /* we're before the first point */
1289 // return _default_value;
1290 return _events.front()->value;
1293 if (range.second == _events.end()) {
1294 /* we're after the last point */
1295 return _events.back()->value;
1298 upos = (*range.second)->when;
1299 uval = (*range.second)->value;
1301 /* linear interpolation betweeen the two points
1305 fraction = (double) (x - lpos) / (double) (upos - lpos);
1306 return lval + (fraction * (uval - lval));
1310 /* x is a control point in the data */
1311 _lookup_cache.left = -1;
1312 return (*range.first)->value;
1316 ControlList::build_search_cache_if_necessary (double start) const
1318 /* Only do the range lookup if x is in a different range than last time
1319 * this was called (or if the search cache has been marked "dirty" (left<0) */
1320 if (!_events.empty() && ((_search_cache.left < 0) || (_search_cache.left > start))) {
1322 const ControlEvent start_point (start, 0);
1324 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1325 // << start << ".." << end << ")" << endl;
1327 _search_cache.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1328 _search_cache.left = start;
1332 /** Get the earliest event after \a start using the current interpolation style.
1334 * If an event is found, \a x and \a y are set to its coordinates.
1336 * \param inclusive Include events with timestamp exactly equal to \a start
1337 * \return true if event is found (and \a x and \a y are valid).
1340 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
1342 // FIXME: It would be nice if this was unnecessary..
1343 Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
1348 return rt_safe_earliest_event_unlocked (start, x, y, inclusive);
1352 /** Get the earliest event after \a start using the current interpolation style.
1354 * If an event is found, \a x and \a y are set to its coordinates.
1356 * \param inclusive Include events with timestamp exactly equal to \a start
1357 * \return true if event is found (and \a x and \a y are valid).
1360 ControlList::rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool inclusive) const
1362 if (_interpolation == Discrete) {
1363 return rt_safe_earliest_event_discrete_unlocked(start, x, y, inclusive);
1365 return rt_safe_earliest_event_linear_unlocked(start, x, y, inclusive);
1370 /** Get the earliest event after \a start without interpolation.
1372 * If an event is found, \a x and \a y are set to its coordinates.
1374 * \param inclusive Include events with timestamp exactly equal to \a start
1375 * \return true if event is found (and \a x and \a y are valid).
1378 ControlList::rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const
1380 build_search_cache_if_necessary (start);
1382 if (_search_cache.first != _events.end()) {
1383 const ControlEvent* const first = *_search_cache.first;
1385 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1387 /* Earliest points is in range, return it */
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.first;
1405 /* No points in range */
1411 /** Get the earliest time the line crosses an integer (Linear interpolation).
1413 * If an event is found, \a x and \a y are set to its coordinates.
1415 * \param inclusive Include events with timestamp exactly equal to \a start
1416 * \return true if event is found (and \a x and \a y are valid).
1419 ControlList::rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const
1421 // cout << "earliest_event(start: " << start << ", x: " << x << ", y: " << y << ", inclusive: " << inclusive << ")" << endl;
1423 const_iterator length_check_iter = _events.begin();
1424 if (_events.empty()) { // 0 events
1426 } else if (_events.end() == ++length_check_iter) { // 1 event
1427 return rt_safe_earliest_event_discrete_unlocked (start, x, y, inclusive);
1430 // Hack to avoid infinitely repeating the same event
1431 build_search_cache_if_necessary (start);
1433 if (_search_cache.first != _events.end()) {
1435 const ControlEvent* first = NULL;
1436 const ControlEvent* next = NULL;
1438 /* Step is after first */
1439 if (_search_cache.first == _events.begin() || (*_search_cache.first)->when <= start) {
1440 first = *_search_cache.first;
1441 ++_search_cache.first;
1442 if (_search_cache.first == _events.end()) {
1445 next = *_search_cache.first;
1447 /* Step is before first */
1449 const_iterator prev = _search_cache.first;
1452 next = *_search_cache.first;
1455 if (inclusive && first->when == start) {
1458 /* Move left of cache to this point
1459 * (Optimize for immediate call this cycle within range) */
1460 _search_cache.left = x;
1461 //++_search_cache.range.first;
1466 if (fabs(first->value - next->value) <= 1) {
1467 if (next->when > start) {
1470 /* Move left of cache to this point
1471 * (Optimize for immediate call this cycle within range) */
1472 _search_cache.left = x;
1473 //++_search_cache.range.first;
1474 assert(inclusive ? x >= start : x > start);
1481 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1482 //cerr << "start y: " << start_y << endl;
1484 //y = first->value + (slope * fabs(start - first->when));
1487 if (first->value < next->value) // ramping up
1489 else // ramping down
1492 x = first->when + (y - first->value) / (double)slope;
1494 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1496 if (first->value < next->value) // ramping up
1498 else // ramping down
1501 x = first->when + (y - first->value) / (double)slope;
1504 /*cerr << first->value << " @ " << first->when << " ... "
1505 << next->value << " @ " << next->when
1506 << " = " << y << " @ " << x << endl;*/
1508 assert( (y >= first->value && y <= next->value)
1509 || (y <= first->value && y >= next->value) );
1512 const bool past_start = (inclusive ? x >= start : x > start);
1514 /* Move left of cache to this point
1515 * (Optimize for immediate call this cycle within range) */
1516 _search_cache.left = x;
1517 assert(inclusive ? x >= start : x > start);
1523 /* No points in the future, so no steps (towards them) in the future */
1530 /** @param start Start position in model coordinates.
1531 * @param end End position in model coordinates.
1532 * @param op 0 = cut, 1 = copy, 2 = clear.
1534 boost::shared_ptr<ControlList>
1535 ControlList::cut_copy_clear (double start, double end, int op)
1537 boost::shared_ptr<ControlList> nal = create (_parameter);
1539 ControlEvent cp (start, 0.0);
1542 Glib::Mutex::Lock lm (_lock);
1544 /* first, determine s & e, two iterators that define the range of points
1545 affected by this operation
1548 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1552 /* and the last that is at or after `end' */
1554 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1557 /* if "start" isn't the location of an existing point,
1558 evaluate the curve to get a value for the start. Add a point to
1559 both the existing event list, and if its not a "clear" operation,
1560 to the copy ("nal") as well.
1562 Note that the time positions of the points in each list are different
1563 because we want the copy ("nal") to have a zero time reference.
1567 /* before we begin any cut/clear operations, get the value of the curve
1571 double end_value = unlocked_eval (end);
1573 if ((*s)->when != start) {
1575 double val = unlocked_eval (start);
1577 if (op == 0) { // cut
1578 if (start > _events.front()->when) {
1579 _events.insert (s, (new ControlEvent (start, val)));
1583 if (op != 2) { // ! clear
1584 nal->_events.push_back (new ControlEvent (0, val));
1588 for (iterator x = s; x != e; ) {
1590 /* adjust new points to be relative to start, which
1591 has been set to zero.
1595 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1599 x = _events.erase (x);
1605 if (e == _events.end() || (*e)->when != end) {
1607 /* only add a boundary point if there is a point after "end"
1610 if (op == 0 && (e != _events.end() && end < (*e)->when)) { // cut
1611 _events.insert (e, new ControlEvent (end, end_value));
1614 if (op != 2 && (e != _events.end() && end < (*e)->when)) { // cut/copy
1615 nal->_events.push_back (new ControlEvent (end - start, end_value));
1619 unlocked_invalidate_insert_iterator ();
1624 maybe_signal_changed ();
1631 boost::shared_ptr<ControlList>
1632 ControlList::cut (double start, double end)
1634 return cut_copy_clear (start, end, 0);
1637 boost::shared_ptr<ControlList>
1638 ControlList::copy (double start, double end)
1640 return cut_copy_clear (start, end, 1);
1644 ControlList::clear (double start, double end)
1646 cut_copy_clear (start, end, 2);
1649 /** @param pos Position in model coordinates */
1651 ControlList::paste (ControlList& alist, double pos, float /*times*/)
1653 if (alist._events.empty()) {
1658 Glib::Mutex::Lock lm (_lock);
1662 ControlEvent cp (pos, 0.0);
1664 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1666 for (iterator i = alist.begin();i != alist.end(); ++i) {
1667 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1668 end = (*i)->when + pos;
1672 /* move all points after the insertion along the timeline by
1676 while (where != _events.end()) {
1678 if ((*where)->when <= end) {
1681 _events.erase(where);
1689 unlocked_invalidate_insert_iterator ();
1693 maybe_signal_changed ();
1697 /** Move automation around according to a list of region movements.
1698 * @param return true if anything was changed, otherwise false (ie nothing needed changing)
1701 ControlList::move_ranges (const list< RangeMove<double> >& movements)
1703 typedef list< RangeMove<double> > RangeMoveList;
1706 Glib::Mutex::Lock lm (_lock);
1708 /* a copy of the events list before we started moving stuff around */
1709 EventList old_events = _events;
1711 /* clear the source and destination ranges in the new list */
1712 bool things_erased = false;
1713 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1715 if (erase_range_internal (i->from, i->from + i->length, _events)) {
1716 things_erased = true;
1719 if (erase_range_internal (i->to, i->to + i->length, _events)) {
1720 things_erased = true;
1724 /* if nothing was erased, there is nothing to do */
1725 if (!things_erased) {
1729 /* copy the events into the new list */
1730 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1731 iterator j = old_events.begin ();
1732 const double limit = i->from + i->length;
1733 const double dx = i->to - i->from;
1734 while (j != old_events.end () && (*j)->when <= limit) {
1735 if ((*j)->when >= i->from) {
1736 ControlEvent* ev = new ControlEvent (**j);
1738 _events.push_back (ev);
1745 _events.sort (event_time_less_than);
1746 unlocked_invalidate_insert_iterator ();
1748 _sort_pending = true;
1754 maybe_signal_changed ();
1759 ControlList::set_interpolation (InterpolationStyle s)
1761 if (_interpolation == s) {
1766 InterpolationChanged (s); /* EMIT SIGNAL */
1770 ControlList::set_thinning_factor (double v)
1772 _thinning_factor = v;
1776 ControlList::operator!= (ControlList const & other) const
1778 if (_events.size() != other._events.size()) {
1782 EventList::const_iterator i = _events.begin ();
1783 EventList::const_iterator j = other._events.begin ();
1785 while (i != _events.end() && (*i)->when == (*j)->when && (*i)->value == (*j)->value) {
1790 if (i != _events.end ()) {
1795 _parameter != other._parameter ||
1796 _interpolation != other._interpolation ||
1797 _min_yval != other._min_yval ||
1798 _max_yval != other._max_yval ||
1799 _default_value != other._default_value
1803 } // namespace Evoral