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 DEBUG_TRACE (DEBUG::ControlList, string_compose ("%1: setup write pass @ %2\n", this, when));
347 new_write_pass = true;
348 did_write_during_pass = false;
349 insert_position = when;
351 /* leave the insert iterator invalid, so that we will do the lookup
352 of where it should be in a "lazy" way - deferring it until
353 we actually add the first point (which may never happen).
356 unlocked_invalidate_insert_iterator ();
360 ControlList::write_pass_finished (double /*when*/)
362 DEBUG_TRACE (DEBUG::ControlList, "write pass finished\n");
364 if (did_write_during_pass) {
366 did_write_during_pass = false;
368 new_write_pass = true;
369 _in_write_pass = false;
373 ControlList::set_in_write_pass (bool yn, bool add_point, double when)
375 DEBUG_TRACE (DEBUG::ControlList, string_compose ("now in write pass @ %1, add point ? %2\n", when, add_point));
379 if (yn && add_point) {
380 add_guard_point (when);
385 ControlList::add_guard_point (double when)
387 ControlEvent cp (when, 0.0);
388 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
390 double eval_value = unlocked_eval (insert_position);
392 if (most_recent_insert_iterator == _events.end()) {
394 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at end, adding eval-value there %2\n", this, eval_value));
395 _events.push_back (new ControlEvent (when, eval_value));
396 /* leave insert iterator at the end */
398 } else if ((*most_recent_insert_iterator)->when == when) {
400 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at existing point, setting eval-value there %2\n", this, eval_value));
402 /* most_recent_insert_iterator points to a control event
403 already at the insert position, so there is
408 advance most_recent_insert_iterator so that the "real"
409 insert occurs in the right place, since it
410 points to the control event just inserted.
413 ++most_recent_insert_iterator;
416 /* insert a new control event at the right spot
419 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert eval-value %2 just before iterator @ %3\n",
420 this, eval_value, (*most_recent_insert_iterator)->when));
422 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator, new ControlEvent (when, eval_value));
424 /* advance most_recent_insert_iterator so that the "real"
425 * insert occurs in the right place, since it
426 * points to the control event just inserted.
429 ++most_recent_insert_iterator;
432 /* don't do this again till the next write pass */
434 new_write_pass = false;
438 ControlList::in_write_pass () const
440 return _in_write_pass;
444 ControlList::add (double when, double value, bool with_guards)
446 /* this is for making changes from some kind of user interface or
447 control surface (GUI, MIDI, OSC etc)
450 if (!clamp_value (when, value)) {
454 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 add %2 at %3 w/erase = %4 (new ? %6) at end ? %5\n",
455 this, value, when, _in_write_pass, (most_recent_insert_iterator == _events.end()), new_write_pass));
457 Glib::Threads::Mutex::Lock lm (_lock);
458 ControlEvent cp (when, 0.0f);
459 iterator insertion_point;
461 if (_events.empty()) {
463 /* as long as the point we're adding is not at zero,
464 * add an "anchor" point there.
468 _events.insert (_events.end(), new ControlEvent (0, _default_value));
469 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added default value %2 at zero\n", this, _default_value));
473 if (_in_write_pass && new_write_pass) {
476 add_guard_point (insert_position);
477 did_write_during_pass = true;
480 } else if (most_recent_insert_iterator == _events.end() || when > (*most_recent_insert_iterator)->when) {
482 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 %2 erase from existing iterator (@end ? %3)\n",
483 this, (_in_write_pass ? "DO" : "DON'T"),
484 (most_recent_insert_iterator == _events.end())));
486 if (_in_write_pass) {
487 while (most_recent_insert_iterator != _events.end()) {
488 if ((*most_recent_insert_iterator)->when < when) {
489 if (_in_write_pass) {
490 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase existing @ %2\n", this, (*most_recent_insert_iterator)));
491 delete *most_recent_insert_iterator;
492 most_recent_insert_iterator = _events.erase (most_recent_insert_iterator);
495 } else if ((*most_recent_insert_iterator)->when >= when) {
498 ++most_recent_insert_iterator;
501 if (with_guards && most_recent_insert_iterator != _events.end()) {
502 if ((*most_recent_insert_iterator)->when - when > 64) {
503 /* next control point is some
504 * distance from where our new
505 * point is going to go - add a
506 * new point to avoid changing
507 * the shape of the line too
508 * much. the insert iterator needs
509 * to point to the new control
510 * point so that our insert
511 * will happen correctly.
513 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator,
514 new ControlEvent (when+1, (*most_recent_insert_iterator)->value));
515 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added post-erase guard point @ %2 = %3\n",
517 (*most_recent_insert_iterator)->value));
523 /* not in a write pass: figure out the iterator we should insert in front of */
525 DEBUG_TRACE (DEBUG::ControlList, string_compose ("compute MRI for position %1\n", when));
526 ControlEvent cp (when, 0.0f);
527 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
532 /* not in a write pass, adding a point within existing
533 * data: figure out the iterator we should insert in
537 DEBUG_TRACE (DEBUG::ControlList, string_compose ("compute(b) MRI for position %1\n", when));
538 ControlEvent cp (when, 0.0f);
539 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
542 /* OK, now we're really ready to add a new point
545 if (most_recent_insert_iterator == _events.end()) {
546 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 appending new point at end\n", this));
550 /* check if would just be adding to a straight line,
551 * and don't add another point if so
554 if (!_events.empty()) { // avoid O(N) _events.size() here
555 if (_events.back()->value == value) {
556 EventList::iterator b = _events.end();
557 --b; // final point, which we know exists
558 if (b != _events.begin()) { // step back again, but check first that it is legal
559 --b; // penultimate-point
560 if ((*b)->value == value) {
561 /* there are at least two points with the exact same value ...
562 * straight line - just move the final
563 * point to the new time
565 _events.back()->when = when;
567 DEBUG_TRACE (DEBUG::ControlList, string_compose ("final value of %1 moved to %2\n", value, when));
574 _events.push_back (new ControlEvent (when, value));
575 DEBUG_TRACE (DEBUG::ControlList, string_compose ("\tactually appended, size now %1\n", _events.size()));
578 if (!_in_write_pass) {
579 most_recent_insert_iterator = _events.end();
580 --most_recent_insert_iterator;
583 } else if ((*most_recent_insert_iterator)->when == when) {
585 if ((*most_recent_insert_iterator)->value != value) {
586 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 reset existing point to new value %2\n", this, value));
588 /* only one point allowed per time point, so just
589 * reset the value here.
592 (*most_recent_insert_iterator)->value = value;
594 /* if we modified the final value, then its as
595 * if we inserted a new point as far as the
596 * next addition, so make sure we know that.
599 if (_in_write_pass && _events.back()->when == when) {
600 most_recent_insert_iterator = _events.end();
604 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 same time %2, same value value %3\n", this, when, value));
608 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert new point at %2 at iterator at %3\n", this, when, (*most_recent_insert_iterator)->when));
612 /* check if would just be adding to a straight line,
613 * and don't add another point if so
616 if (most_recent_insert_iterator != _events.begin()) {
617 EventList::iterator b = most_recent_insert_iterator;
618 --b; // prior point, which we know exists
619 if ((*b)->value == value) { // same value as the point we plan to insert
620 if (b != _events.begin()) { // step back again, which may not be possible
621 EventList::iterator bb = b;
622 --bb; // next-to-prior-point
623 if ((*bb)->value == value) {
624 /* straight line - just move the prior
625 * point to the new time
629 if (!_in_write_pass) {
630 most_recent_insert_iterator = b;
633 DEBUG_TRACE (DEBUG::ControlList, string_compose ("final value of %1 moved to %2\n", value, when));
640 if (with_guards && most_recent_insert_iterator != _events.end()) {
641 if ((*most_recent_insert_iterator)->when - when > 64) {
642 /* next control point is some
643 * distance from where our new
644 * point is going to go - add a
645 * new point to avoid changing
646 * the shape of the line too
647 * much. the insert iterator needs
648 * to point to the new control
649 * point so that our insert
650 * will happen correctly.
652 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator,
653 new ControlEvent (when+1, (*most_recent_insert_iterator)->value));
654 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added insert-post-erase guard point @ %2 = %3\n",
656 (*most_recent_insert_iterator)->value));
661 EventList::iterator x = _events.insert (most_recent_insert_iterator, new ControlEvent (when, value));
662 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 inserted new value before MRI, size now %2\n", this, _events.size()));
664 if (!_in_write_pass) {
665 most_recent_insert_iterator = x;
673 maybe_signal_changed ();
677 ControlList::erase (iterator i)
680 Glib::Threads::Mutex::Lock lm (_lock);
681 if (most_recent_insert_iterator == i) {
682 unlocked_invalidate_insert_iterator ();
687 maybe_signal_changed ();
691 ControlList::erase (iterator start, iterator end)
694 Glib::Threads::Mutex::Lock lm (_lock);
695 _events.erase (start, end);
696 unlocked_invalidate_insert_iterator ();
699 maybe_signal_changed ();
702 /** Erase the first event which matches the given time and value */
704 ControlList::erase (double when, double value)
707 Glib::Threads::Mutex::Lock lm (_lock);
709 iterator i = begin ();
710 while (i != end() && ((*i)->when != when || (*i)->value != value)) {
716 if (most_recent_insert_iterator == i) {
717 unlocked_invalidate_insert_iterator ();
724 maybe_signal_changed ();
728 ControlList::erase_range (double start, double endt)
733 Glib::Threads::Mutex::Lock lm (_lock);
734 erased = erase_range_internal (start, endt, _events);
743 maybe_signal_changed ();
748 ControlList::erase_range_internal (double start, double endt, EventList & events)
751 ControlEvent cp (start, 0.0f);
755 if ((s = lower_bound (events.begin(), events.end(), &cp, time_comparator)) != events.end()) {
757 e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
760 unlocked_invalidate_insert_iterator ();
769 ControlList::slide (iterator before, double distance)
772 Glib::Threads::Mutex::Lock lm (_lock);
774 if (before == _events.end()) {
778 while (before != _events.end()) {
779 (*before)->when += distance;
786 maybe_signal_changed ();
790 ControlList::shift (double pos, double frames)
793 Glib::Threads::Mutex::Lock lm (_lock);
795 for (iterator i = _events.begin(); i != _events.end(); ++i) {
796 if ((*i)->when >= pos) {
797 (*i)->when += frames;
804 maybe_signal_changed ();
808 ControlList::modify (iterator iter, double when, double val)
810 /* note: we assume higher level logic is in place to avoid this
811 reordering the time-order of control events in the list. ie. all
812 points after *iter are later than when.
816 Glib::Threads::Mutex::Lock lm (_lock);
818 (*iter)->when = when;
819 (*iter)->value = val;
826 _events.sort (event_time_less_than);
827 unlocked_invalidate_insert_iterator ();
829 _sort_pending = true;
835 maybe_signal_changed ();
838 std::pair<ControlList::iterator,ControlList::iterator>
839 ControlList::control_points_adjacent (double xval)
841 Glib::Threads::Mutex::Lock lm (_lock);
843 ControlEvent cp (xval, 0.0f);
844 std::pair<iterator,iterator> ret;
846 ret.first = _events.end();
847 ret.second = _events.end();
849 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
851 if (ret.first == _events.end()) {
852 if ((*i)->when >= xval) {
853 if (i != _events.begin()) {
862 if ((*i)->when > xval) {
872 ControlList::freeze ()
887 Glib::Threads::Mutex::Lock lm (_lock);
890 _events.sort (event_time_less_than);
891 unlocked_invalidate_insert_iterator ();
892 _sort_pending = false;
898 ControlList::mark_dirty () const
900 _lookup_cache.left = -1;
901 _search_cache.left = -1;
904 _curve->mark_dirty();
907 Dirty (); /* EMIT SIGNAL */
911 ControlList::truncate_end (double last_coordinate)
914 Glib::Threads::Mutex::Lock lm (_lock);
915 ControlEvent cp (last_coordinate, 0);
916 ControlList::reverse_iterator i;
919 if (_events.empty()) {
923 if (last_coordinate == _events.back()->when) {
927 if (last_coordinate > _events.back()->when) {
932 iterator foo = _events.begin();
935 if (foo == _events.end()) {
937 } else if (++foo == _events.end()) {
944 /* less than 2 points: add a new point */
945 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
948 /* more than 2 points: check to see if the last 2 values
949 are equal. if so, just move the position of the
950 last point. otherwise, add a new point.
953 iterator penultimate = _events.end();
954 --penultimate; /* points at last point */
955 --penultimate; /* points at the penultimate point */
957 if (_events.back()->value == (*penultimate)->value) {
958 _events.back()->when = last_coordinate;
960 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
968 last_val = unlocked_eval (last_coordinate);
969 last_val = max ((double) _min_yval, last_val);
970 last_val = min ((double) _max_yval, last_val);
972 i = _events.rbegin();
974 /* make i point to the last control point */
978 /* now go backwards, removing control points that are
979 beyond the new last coordinate.
982 // FIXME: SLOW! (size() == O(n))
984 uint32_t sz = _events.size();
986 while (i != _events.rend() && sz > 2) {
987 ControlList::reverse_iterator tmp;
992 if ((*i)->when < last_coordinate) {
996 _events.erase (i.base());
1002 _events.back()->when = last_coordinate;
1003 _events.back()->value = last_val;
1006 unlocked_invalidate_insert_iterator ();
1010 maybe_signal_changed ();
1014 ControlList::truncate_start (double overall_length)
1017 Glib::Threads::Mutex::Lock lm (_lock);
1019 double first_legal_value;
1020 double first_legal_coordinate;
1022 assert(!_events.empty());
1024 if (overall_length == _events.back()->when) {
1025 /* no change in overall length */
1029 if (overall_length > _events.back()->when) {
1031 /* growing at front: duplicate first point. shift all others */
1033 double shift = overall_length - _events.back()->when;
1036 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
1037 (*i)->when += shift;
1042 /* less than 2 points: add a new point */
1043 _events.push_front (new ControlEvent (0, _events.front()->value));
1047 /* more than 2 points: check to see if the first 2 values
1048 are equal. if so, just move the position of the
1049 first point. otherwise, add a new point.
1052 iterator second = _events.begin();
1053 ++second; /* points at the second point */
1055 if (_events.front()->value == (*second)->value) {
1056 /* first segment is flat, just move start point back to zero */
1057 _events.front()->when = 0;
1059 /* leave non-flat segment in place, add a new leading point. */
1060 _events.push_front (new ControlEvent (0, _events.front()->value));
1066 /* shrinking at front */
1068 first_legal_coordinate = _events.back()->when - overall_length;
1069 first_legal_value = unlocked_eval (first_legal_coordinate);
1070 first_legal_value = max (_min_yval, first_legal_value);
1071 first_legal_value = min (_max_yval, first_legal_value);
1073 /* remove all events earlier than the new "front" */
1075 i = _events.begin();
1077 while (i != _events.end() && !_events.empty()) {
1078 ControlList::iterator tmp;
1083 if ((*i)->when > first_legal_coordinate) {
1093 /* shift all remaining points left to keep their same
1097 for (i = _events.begin(); i != _events.end(); ++i) {
1098 (*i)->when -= first_legal_coordinate;
1101 /* add a new point for the interpolated new value */
1103 _events.push_front (new ControlEvent (0, first_legal_value));
1106 unlocked_invalidate_insert_iterator ();
1110 maybe_signal_changed ();
1114 ControlList::unlocked_eval (double x) const
1116 pair<EventList::iterator,EventList::iterator> range;
1122 const_iterator length_check_iter = _events.begin();
1123 for (npoints = 0; npoints < 4; ++npoints, ++length_check_iter) {
1124 if (length_check_iter == _events.end()) {
1131 return _default_value;
1134 return _events.front()->value;
1137 if (x >= _events.back()->when) {
1138 return _events.back()->value;
1139 } else if (x <= _events.front()->when) {
1140 return _events.front()->value;
1143 lpos = _events.front()->when;
1144 lval = _events.front()->value;
1145 upos = _events.back()->when;
1146 uval = _events.back()->value;
1148 if (_interpolation == Discrete) {
1152 /* linear interpolation betweeen the two points */
1153 fraction = (double) (x - lpos) / (double) (upos - lpos);
1154 return lval + (fraction * (uval - lval));
1157 if (x >= _events.back()->when) {
1158 return _events.back()->value;
1159 } else if (x <= _events.front()->when) {
1160 return _events.front()->value;
1163 return multipoint_eval (x);
1166 /*NOTREACHED*/ /* stupid gcc */
1167 return _default_value;
1171 ControlList::multipoint_eval (double x) const
1177 /* "Stepped" lookup (no interpolation) */
1178 /* FIXME: no cache. significant? */
1179 if (_interpolation == Discrete) {
1180 const ControlEvent cp (x, 0);
1181 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
1183 // shouldn't have made it to multipoint_eval
1184 assert(i != _events.end());
1186 if (i == _events.begin() || (*i)->when == x)
1189 return (*(--i))->value;
1192 /* Only do the range lookup if x is in a different range than last time
1193 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
1194 if ((_lookup_cache.left < 0) ||
1195 ((_lookup_cache.left > x) ||
1196 (_lookup_cache.range.first == _events.end()) ||
1197 ((*_lookup_cache.range.second)->when < x))) {
1199 const ControlEvent cp (x, 0);
1201 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
1204 pair<const_iterator,const_iterator> range = _lookup_cache.range;
1206 if (range.first == range.second) {
1208 /* x does not exist within the list as a control point */
1210 _lookup_cache.left = x;
1212 if (range.first != _events.begin()) {
1214 lpos = (*range.first)->when;
1215 lval = (*range.first)->value;
1217 /* we're before the first point */
1218 // return _default_value;
1219 return _events.front()->value;
1222 if (range.second == _events.end()) {
1223 /* we're after the last point */
1224 return _events.back()->value;
1227 upos = (*range.second)->when;
1228 uval = (*range.second)->value;
1230 /* linear interpolation betweeen the two points
1234 fraction = (double) (x - lpos) / (double) (upos - lpos);
1235 return lval + (fraction * (uval - lval));
1239 /* x is a control point in the data */
1240 _lookup_cache.left = -1;
1241 return (*range.first)->value;
1245 ControlList::build_search_cache_if_necessary (double start) const
1247 /* Only do the range lookup if x is in a different range than last time
1248 * this was called (or if the search cache has been marked "dirty" (left<0) */
1249 if (!_events.empty() && ((_search_cache.left < 0) || (_search_cache.left > start))) {
1251 const ControlEvent start_point (start, 0);
1253 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1254 // << start << ".." << end << ")" << endl;
1256 _search_cache.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1257 _search_cache.left = start;
1261 /** Get the earliest event after \a start using the current interpolation style.
1263 * If an event is found, \a x and \a y are set to its coordinates.
1265 * \param inclusive Include events with timestamp exactly equal to \a start
1266 * \return true if event is found (and \a x and \a y are valid).
1269 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
1271 // FIXME: It would be nice if this was unnecessary..
1272 Glib::Threads::Mutex::Lock lm(_lock, Glib::Threads::TRY_LOCK);
1277 return rt_safe_earliest_event_unlocked (start, x, y, inclusive);
1281 /** Get the earliest event after \a start using the current interpolation style.
1283 * If an event is found, \a x and \a y are set to its coordinates.
1285 * \param inclusive Include events with timestamp exactly equal to \a start
1286 * \return true if event is found (and \a x and \a y are valid).
1289 ControlList::rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool inclusive) const
1291 if (_interpolation == Discrete) {
1292 return rt_safe_earliest_event_discrete_unlocked(start, x, y, inclusive);
1294 return rt_safe_earliest_event_linear_unlocked(start, x, y, inclusive);
1299 /** Get the earliest event after \a start without interpolation.
1301 * If an event is found, \a x and \a y are set to its coordinates.
1303 * \param inclusive Include events with timestamp exactly equal to \a start
1304 * \return true if event is found (and \a x and \a y are valid).
1307 ControlList::rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const
1309 build_search_cache_if_necessary (start);
1311 if (_search_cache.first != _events.end()) {
1312 const ControlEvent* const first = *_search_cache.first;
1314 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1316 /* Earliest points is in range, return it */
1322 /* Move left of cache to this point
1323 * (Optimize for immediate call this cycle within range) */
1324 _search_cache.left = x;
1325 ++_search_cache.first;
1334 /* No points in range */
1340 /** Get the earliest time the line crosses an integer (Linear interpolation).
1342 * If an event is found, \a x and \a y are set to its coordinates.
1344 * \param inclusive Include events with timestamp exactly equal to \a start
1345 * \return true if event is found (and \a x and \a y are valid).
1348 ControlList::rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const
1350 // cout << "earliest_event(start: " << start << ", x: " << x << ", y: " << y << ", inclusive: " << inclusive << ")" << endl;
1352 const_iterator length_check_iter = _events.begin();
1353 if (_events.empty()) { // 0 events
1355 } else if (_events.end() == ++length_check_iter) { // 1 event
1356 return rt_safe_earliest_event_discrete_unlocked (start, x, y, inclusive);
1359 // Hack to avoid infinitely repeating the same event
1360 build_search_cache_if_necessary (start);
1362 if (_search_cache.first != _events.end()) {
1364 const ControlEvent* first = NULL;
1365 const ControlEvent* next = NULL;
1367 /* Step is after first */
1368 if (_search_cache.first == _events.begin() || (*_search_cache.first)->when <= start) {
1369 first = *_search_cache.first;
1370 ++_search_cache.first;
1371 if (_search_cache.first == _events.end()) {
1374 next = *_search_cache.first;
1376 /* Step is before first */
1378 const_iterator prev = _search_cache.first;
1381 next = *_search_cache.first;
1384 if (inclusive && first->when == start) {
1387 /* Move left of cache to this point
1388 * (Optimize for immediate call this cycle within range) */
1389 _search_cache.left = x;
1390 //++_search_cache.range.first;
1395 if (fabs(first->value - next->value) <= 1) {
1396 if (next->when > start) {
1399 /* Move left of cache to this point
1400 * (Optimize for immediate call this cycle within range) */
1401 _search_cache.left = x;
1402 //++_search_cache.range.first;
1403 assert(inclusive ? x >= start : x > start);
1410 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1411 //cerr << "start y: " << start_y << endl;
1413 //y = first->value + (slope * fabs(start - first->when));
1416 if (first->value < next->value) // ramping up
1418 else // ramping down
1421 x = first->when + (y - first->value) / (double)slope;
1423 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1425 if (first->value < next->value) // ramping up
1427 else // ramping down
1430 x = first->when + (y - first->value) / (double)slope;
1433 /*cerr << first->value << " @ " << first->when << " ... "
1434 << next->value << " @ " << next->when
1435 << " = " << y << " @ " << x << endl;*/
1437 assert( (y >= first->value && y <= next->value)
1438 || (y <= first->value && y >= next->value) );
1441 const bool past_start = (inclusive ? x >= start : x > start);
1443 /* Move left of cache to this point
1444 * (Optimize for immediate call this cycle within range) */
1445 _search_cache.left = x;
1446 assert(inclusive ? x >= start : x > start);
1454 _search_cache.left = x;
1458 /* No points in the future, so no steps (towards them) in the future */
1465 /** @param start Start position in model coordinates.
1466 * @param end End position in model coordinates.
1467 * @param op 0 = cut, 1 = copy, 2 = clear.
1469 boost::shared_ptr<ControlList>
1470 ControlList::cut_copy_clear (double start, double end, int op)
1472 boost::shared_ptr<ControlList> nal = create (_parameter);
1474 ControlEvent cp (start, 0.0);
1477 Glib::Threads::Mutex::Lock lm (_lock);
1479 /* first, determine s & e, two iterators that define the range of points
1480 affected by this operation
1483 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1487 /* and the last that is at or after `end' */
1489 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1492 /* if "start" isn't the location of an existing point,
1493 evaluate the curve to get a value for the start. Add a point to
1494 both the existing event list, and if its not a "clear" operation,
1495 to the copy ("nal") as well.
1497 Note that the time positions of the points in each list are different
1498 because we want the copy ("nal") to have a zero time reference.
1502 /* before we begin any cut/clear operations, get the value of the curve
1506 double end_value = unlocked_eval (end);
1508 if ((*s)->when != start) {
1510 double val = unlocked_eval (start);
1512 if (op == 0) { // cut
1513 if (start > _events.front()->when) {
1514 _events.insert (s, (new ControlEvent (start, val)));
1518 if (op != 2) { // ! clear
1519 nal->_events.push_back (new ControlEvent (0, val));
1523 for (iterator x = s; x != e; ) {
1525 /* adjust new points to be relative to start, which
1526 has been set to zero.
1530 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1534 x = _events.erase (x);
1540 if (e == _events.end() || (*e)->when != end) {
1542 /* only add a boundary point if there is a point after "end"
1545 if (op == 0 && (e != _events.end() && end < (*e)->when)) { // cut
1546 _events.insert (e, new ControlEvent (end, end_value));
1549 if (op != 2 && (e != _events.end() && end < (*e)->when)) { // cut/copy
1550 nal->_events.push_back (new ControlEvent (end - start, end_value));
1554 unlocked_invalidate_insert_iterator ();
1559 maybe_signal_changed ();
1566 boost::shared_ptr<ControlList>
1567 ControlList::cut (double start, double end)
1569 return cut_copy_clear (start, end, 0);
1572 boost::shared_ptr<ControlList>
1573 ControlList::copy (double start, double end)
1575 return cut_copy_clear (start, end, 1);
1579 ControlList::clear (double start, double end)
1581 cut_copy_clear (start, end, 2);
1584 /** @param pos Position in model coordinates */
1586 ControlList::paste (ControlList& alist, double pos, float /*times*/)
1588 if (alist._events.empty()) {
1593 Glib::Threads::Mutex::Lock lm (_lock);
1597 ControlEvent cp (pos, 0.0);
1599 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1601 for (iterator i = alist.begin();i != alist.end(); ++i) {
1602 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1603 end = (*i)->when + pos;
1607 /* move all points after the insertion along the timeline by
1611 while (where != _events.end()) {
1613 if ((*where)->when <= end) {
1616 _events.erase(where);
1624 unlocked_invalidate_insert_iterator ();
1628 maybe_signal_changed ();
1632 /** Move automation around according to a list of region movements.
1633 * @param return true if anything was changed, otherwise false (ie nothing needed changing)
1636 ControlList::move_ranges (const list< RangeMove<double> >& movements)
1638 typedef list< RangeMove<double> > RangeMoveList;
1641 Glib::Threads::Mutex::Lock lm (_lock);
1643 /* a copy of the events list before we started moving stuff around */
1644 EventList old_events = _events;
1646 /* clear the source and destination ranges in the new list */
1647 bool things_erased = false;
1648 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1650 if (erase_range_internal (i->from, i->from + i->length, _events)) {
1651 things_erased = true;
1654 if (erase_range_internal (i->to, i->to + i->length, _events)) {
1655 things_erased = true;
1659 /* if nothing was erased, there is nothing to do */
1660 if (!things_erased) {
1664 /* copy the events into the new list */
1665 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1666 iterator j = old_events.begin ();
1667 const double limit = i->from + i->length;
1668 const double dx = i->to - i->from;
1669 while (j != old_events.end () && (*j)->when <= limit) {
1670 if ((*j)->when >= i->from) {
1671 ControlEvent* ev = new ControlEvent (**j);
1673 _events.push_back (ev);
1680 _events.sort (event_time_less_than);
1681 unlocked_invalidate_insert_iterator ();
1683 _sort_pending = true;
1689 maybe_signal_changed ();
1694 ControlList::set_interpolation (InterpolationStyle s)
1696 if (_interpolation == s) {
1701 InterpolationChanged (s); /* EMIT SIGNAL */
1705 ControlList::set_thinning_factor (double v)
1707 _thinning_factor = v;
1711 ControlList::operator!= (ControlList const & other) const
1713 if (_events.size() != other._events.size()) {
1717 EventList::const_iterator i = _events.begin ();
1718 EventList::const_iterator j = other._events.begin ();
1720 while (i != _events.end() && (*i)->when == (*j)->when && (*i)->value == (*j)->value) {
1725 if (i != _events.end ()) {
1730 _parameter != other._parameter ||
1731 _interpolation != other._interpolation ||
1732 _min_yval != other._min_yval ||
1733 _max_yval != other._max_yval ||
1734 _default_value != other._default_value
1739 ControlList::dump (ostream& o)
1741 /* NOT LOCKED ... for debugging only */
1743 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
1744 o << (*x)->value << " @ " << (uint64_t) (*x)->when << endl;
1748 } // namespace Evoral