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
19 // 'std::isnan()' is not available in MSVC.
28 #include "evoral/ControlList.hpp"
29 #include "evoral/Curve.hpp"
31 #include "pbd/compose.h"
38 inline bool event_time_less_than (ControlEvent* a, ControlEvent* b)
40 return a->when < b->when;
43 /* this has no units but corresponds to the area of a rectangle
44 computed between three points in the list. If the area is
45 large, it indicates significant non-linearity between the
48 during automation recording we thin the recorded points
49 using this value. if a point is sufficiently co-linear
50 with its neighbours (as defined by the area of the rectangle
51 formed by three of them), we will not include it in the
52 ControlList. a smaller value will exclude less points,
53 a larger value will exclude more points, so it effectively
54 measures the amount of thinning to be done.
57 double ControlList::_thinning_factor = 20.0;
59 ControlList::ControlList (const Parameter& id)
61 , _interpolation(Linear)
65 _changed_when_thawed = false;
68 _default_value = id.normal();
69 _lookup_cache.left = -1;
70 _lookup_cache.range.first = _events.end();
71 _search_cache.left = -1;
72 _search_cache.first = _events.end();
73 _sort_pending = false;
74 new_write_pass = true;
75 _in_write_pass = false;
76 did_write_during_pass = false;
78 most_recent_insert_iterator = _events.end();
81 ControlList::ControlList (const ControlList& other)
82 : _parameter(other._parameter)
83 , _interpolation(Linear)
87 _changed_when_thawed = false;
88 _min_yval = other._min_yval;
89 _max_yval = other._max_yval;
90 _default_value = other._default_value;
91 _lookup_cache.range.first = _events.end();
92 _search_cache.first = _events.end();
93 _sort_pending = false;
94 new_write_pass = true;
95 _in_write_pass = false;
96 did_write_during_pass = false;
98 most_recent_insert_iterator = _events.end();
105 ControlList::ControlList (const ControlList& other, double start, double end)
106 : _parameter(other._parameter)
107 , _interpolation(Linear)
111 _changed_when_thawed = false;
112 _min_yval = other._min_yval;
113 _max_yval = other._max_yval;
114 _default_value = other._default_value;
115 _lookup_cache.range.first = _events.end();
116 _search_cache.first = _events.end();
117 _sort_pending = false;
119 /* now grab the relevant points, and shift them back if necessary */
121 boost::shared_ptr<ControlList> section = const_cast<ControlList*>(&other)->copy (start, end);
123 if (!section->empty()) {
124 copy_events (*(section.get()));
127 new_write_pass = false;
128 _in_write_pass = false;
129 did_write_during_pass = false;
130 insert_position = -1;
131 most_recent_insert_iterator = _events.end();
136 ControlList::~ControlList()
138 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
145 boost::shared_ptr<ControlList>
146 ControlList::create(Parameter id)
148 return boost::shared_ptr<ControlList>(new ControlList(id));
152 ControlList::operator== (const ControlList& other)
154 return _events == other._events;
158 ControlList::operator= (const ControlList& other)
160 if (this != &other) {
162 _min_yval = other._min_yval;
163 _max_yval = other._max_yval;
164 _default_value = other._default_value;
173 ControlList::copy_events (const ControlList& other)
176 Glib::Threads::Mutex::Lock lm (_lock);
178 for (const_iterator i = other.begin(); i != other.end(); ++i) {
179 _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
181 unlocked_invalidate_insert_iterator ();
184 maybe_signal_changed ();
188 ControlList::create_curve()
190 _curve = new Curve(*this);
194 ControlList::destroy_curve()
201 ControlList::maybe_signal_changed ()
206 _changed_when_thawed = true;
211 ControlList::clear ()
214 Glib::Threads::Mutex::Lock lm (_lock);
216 unlocked_invalidate_insert_iterator ();
220 maybe_signal_changed ();
224 ControlList::x_scale (double factor)
226 Glib::Threads::Mutex::Lock lm (_lock);
231 ControlList::extend_to (double when)
233 Glib::Threads::Mutex::Lock lm (_lock);
234 if (_events.empty() || _events.back()->when == when) {
237 double factor = when / _events.back()->when;
243 ControlList::_x_scale (double factor)
245 for (iterator i = _events.begin(); i != _events.end(); ++i) {
246 (*i)->when *= factor;
252 struct ControlEventTimeComparator {
253 bool operator() (ControlEvent* a, ControlEvent* b) {
254 return a->when < b->when;
261 bool changed = false;
264 Glib::Threads::Mutex::Lock lm (_lock);
266 ControlEvent* prevprev = 0;
267 ControlEvent* cur = 0;
268 ControlEvent* prev = 0;
272 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 thin from %2 events\n", this, _events.size()));
274 for (iterator i = _events.begin(); i != _events.end(); ++i) {
281 /* compute the area of the triangle formed by 3 points
284 double area = fabs ((prevprev->when * (prev->value - cur->value)) +
285 (prev->when * (cur->value - prevprev->value)) +
286 (cur->when * (prevprev->value - prev->value)));
288 if (area < _thinning_factor) {
289 iterator tmp = pprev;
291 /* pprev will change to current
292 i is incremented to the next event
308 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 thin => %2 events\n", this, _events.size()));
311 unlocked_invalidate_insert_iterator ();
317 maybe_signal_changed ();
322 ControlList::fast_simple_add (double when, double value)
324 Glib::Threads::Mutex::Lock lm (_lock);
325 /* to be used only for loading pre-sorted data from saved state */
326 _events.insert (_events.end(), new ControlEvent (when, value));
327 assert(_events.back());
333 ControlList::invalidate_insert_iterator ()
335 Glib::Threads::Mutex::Lock lm (_lock);
336 unlocked_invalidate_insert_iterator ();
340 ControlList::unlocked_invalidate_insert_iterator ()
342 most_recent_insert_iterator = _events.end();
346 ControlList::start_write_pass (double when)
348 Glib::Threads::Mutex::Lock lm (_lock);
350 new_write_pass = true;
351 did_write_during_pass = false;
352 insert_position = when;
354 /* leave the insert iterator invalid, so that we will do the lookup
355 of where it should be in a "lazy" way - deferring it until
356 we actually add the first point (which may never happen).
359 unlocked_invalidate_insert_iterator ();
363 ControlList::write_pass_finished (double /*when*/)
365 if (did_write_during_pass) {
367 did_write_during_pass = false;
369 new_write_pass = true;
370 _in_write_pass = false;
374 ControlList::set_in_write_pass (bool yn, bool add_point, double when)
378 if (yn && add_point) {
379 add_guard_point (when);
384 ControlList::add_guard_point (double when)
386 ControlEvent cp (when, 0.0);
387 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
389 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 ADD GUARD POINT @ %2looked up insert iterator for new write pass\n", this, when));
391 double eval_value = unlocked_eval (insert_position);
393 if (most_recent_insert_iterator == _events.end()) {
395 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at end, adding eval-value there %2\n", this, eval_value));
396 _events.push_back (new ControlEvent (when, eval_value));
397 /* leave insert iterator at the end */
399 } else if ((*most_recent_insert_iterator)->when == when) {
401 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at existing point, setting eval-value there %2\n", this, eval_value));
403 /* most_recent_insert_iterator points to a control event
404 already at the insert position, so there is
409 advance most_recent_insert_iterator so that the "real"
410 insert occurs in the right place, since it
411 points to the control event just inserted.
414 ++most_recent_insert_iterator;
417 /* insert a new control event at the right spot
420 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert eval-value %2 just before iterator @ %3\n",
421 this, eval_value, (*most_recent_insert_iterator)->when));
423 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator, new ControlEvent (when, eval_value));
425 /* advance most_recent_insert_iterator so that the "real"
426 * insert occurs in the right place, since it
427 * points to the control event just inserted.
430 ++most_recent_insert_iterator;
433 /* don't do this again till the next write pass */
435 new_write_pass = false;
439 ControlList::in_write_pass () const
441 return _in_write_pass;
445 ControlList::add (double when, double value)
447 /* this is for making changes from some kind of user interface or
448 control surface (GUI, MIDI, OSC etc)
451 if (!clamp_value (when, value)) {
455 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 add %2 at %3 w/erase = %4 at end ? %5\n",
456 this, value, when, _in_write_pass, (most_recent_insert_iterator == _events.end())));
458 Glib::Threads::Mutex::Lock lm (_lock);
459 ControlEvent cp (when, 0.0f);
460 iterator insertion_point;
462 if (_events.empty()) {
464 /* as long as the point we're adding is not at zero,
465 * add an "anchor" point there.
469 _events.insert (_events.end(), new ControlEvent (0, _default_value));
470 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added default value %2 at zero\n", this, _default_value));
474 if (_in_write_pass && new_write_pass) {
476 add_guard_point (insert_position);
477 did_write_during_pass = true;
479 } else if (most_recent_insert_iterator == _events.end() || when > (*most_recent_insert_iterator)->when) {
481 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 %2 erase from existing iterator (@end ? %3)\n",
482 this, (_in_write_pass ? "DO" : "DON'T"),
483 (most_recent_insert_iterator == _events.end())));
485 if (_in_write_pass) {
486 while (most_recent_insert_iterator != _events.end()) {
487 if ((*most_recent_insert_iterator)->when < when) {
488 if (_in_write_pass) {
489 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase existing @ %2\n", this, (*most_recent_insert_iterator)));
490 delete *most_recent_insert_iterator;
491 most_recent_insert_iterator = _events.erase (most_recent_insert_iterator);
494 } else if ((*most_recent_insert_iterator)->when >= when) {
497 ++most_recent_insert_iterator;
500 if (most_recent_insert_iterator != _events.end()) {
501 if ((*most_recent_insert_iterator)->when - when > 64) {
502 /* next control point is some
503 * distance from where our new
504 * point is going to go - add a
505 * new point to avoid changing
506 * the shape of the line too
507 * much. the insert iterator needs
508 * to point to the new control
509 * point so that our insert
510 * will happen correctly.
512 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator,
513 new ControlEvent (when+1, (*most_recent_insert_iterator)->value));
514 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added post-erase guard point @ %2 = %3\n",
516 (*most_recent_insert_iterator)->value));
522 /* not in a write pass: figure out the iterator we should insert in front of */
524 DEBUG_TRACE (DEBUG::ControlList, string_compose ("compute MRI for position %1\n", when));
525 ControlEvent cp (when, 0.0f);
526 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
531 /* not in a write pass, adding a point within existing
532 * data: figure out the iterator we should insert in
536 DEBUG_TRACE (DEBUG::ControlList, string_compose ("compute(b) MRI for position %1\n", when));
537 ControlEvent cp (when, 0.0f);
538 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
541 /* OK, now we're really ready to add a new point
544 if (most_recent_insert_iterator == _events.end()) {
545 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 appending new point at end\n", this));
549 /* check if would just be adding to a straight line,
550 * and don't add another point if so
553 if (!_events.empty()) { // avoid O(N) _events.size() here
554 if (_events.back()->value == value) {
555 EventList::iterator b = _events.end();
556 --b; // final point, which we know exists
557 if (b != _events.begin()) { // step back again, but check first that it is legal
558 --b; // penultimate-point
559 if ((*b)->value == value) {
560 /* there are at least two points with the exact same value ...
561 * straight line - just move the final
562 * point to the new time
564 _events.back()->when = when;
566 DEBUG_TRACE (DEBUG::ControlList, string_compose ("final value of %1 moved to %2\n", value, when));
573 _events.push_back (new ControlEvent (when, value));
574 DEBUG_TRACE (DEBUG::ControlList, string_compose ("\tactually appended, size now %1\n", _events.size()));
577 if (!_in_write_pass) {
578 most_recent_insert_iterator = _events.end();
579 --most_recent_insert_iterator;
582 } else if ((*most_recent_insert_iterator)->when == when) {
584 if ((*most_recent_insert_iterator)->value != value) {
585 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 reset existing point to new value %2\n", this, value));
587 /* only one point allowed per time point, so just
588 * reset the value here.
591 (*most_recent_insert_iterator)->value = value;
593 /* if we modified the final value, then its as
594 * if we inserted a new point as far as the
595 * next addition, so make sure we know that.
598 if (_in_write_pass && _events.back()->when == when) {
599 most_recent_insert_iterator = _events.end();
603 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 same time %2, same value value %3\n", this, when, value));
607 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert new point at %2 at iterator at %3\n", this, when, (*most_recent_insert_iterator)->when));
611 /* check if would just be adding to a straight line,
612 * and don't add another point if so
615 if (most_recent_insert_iterator != _events.begin()) {
616 EventList::iterator b = most_recent_insert_iterator;
617 --b; // prior point, which we know exists
618 if ((*b)->value == value) { // same value as the point we plan to insert
619 if (b != _events.begin()) { // step back again, which may not be possible
620 EventList::iterator bb = b;
621 --bb; // next-to-prior-point
622 if ((*bb)->value == value) {
623 /* straight line - just move the prior
624 * point to the new time
628 if (!_in_write_pass) {
629 most_recent_insert_iterator = b;
632 DEBUG_TRACE (DEBUG::ControlList, string_compose ("final value of %1 moved to %2\n", value, when));
639 if (most_recent_insert_iterator != _events.end()) {
640 if ((*most_recent_insert_iterator)->when - when > 64) {
641 /* next control point is some
642 * distance from where our new
643 * point is going to go - add a
644 * new point to avoid changing
645 * the shape of the line too
646 * much. the insert iterator needs
647 * to point to the new control
648 * point so that our insert
649 * will happen correctly.
651 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator,
652 new ControlEvent (when+1, (*most_recent_insert_iterator)->value));
653 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added insert-post-erase guard point @ %2 = %3\n",
655 (*most_recent_insert_iterator)->value));
660 EventList::iterator x = _events.insert (most_recent_insert_iterator, new ControlEvent (when, value));
661 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 inserted new value before MRI, size now %2\n", this, _events.size()));
663 if (!_in_write_pass) {
664 most_recent_insert_iterator = x;
672 maybe_signal_changed ();
676 ControlList::erase (iterator i)
679 Glib::Threads::Mutex::Lock lm (_lock);
680 if (most_recent_insert_iterator == i) {
681 unlocked_invalidate_insert_iterator ();
686 maybe_signal_changed ();
690 ControlList::erase (iterator start, iterator end)
693 Glib::Threads::Mutex::Lock lm (_lock);
694 _events.erase (start, end);
695 unlocked_invalidate_insert_iterator ();
698 maybe_signal_changed ();
701 /** Erase the first event which matches the given time and value */
703 ControlList::erase (double when, double value)
706 Glib::Threads::Mutex::Lock lm (_lock);
708 iterator i = begin ();
709 while (i != end() && ((*i)->when != when || (*i)->value != value)) {
715 if (most_recent_insert_iterator == i) {
716 unlocked_invalidate_insert_iterator ();
723 maybe_signal_changed ();
727 ControlList::erase_range (double start, double endt)
732 Glib::Threads::Mutex::Lock lm (_lock);
733 erased = erase_range_internal (start, endt, _events);
742 maybe_signal_changed ();
747 ControlList::erase_range_internal (double start, double endt, EventList & events)
750 ControlEvent cp (start, 0.0f);
754 if ((s = lower_bound (events.begin(), events.end(), &cp, time_comparator)) != events.end()) {
756 e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
759 unlocked_invalidate_insert_iterator ();
768 ControlList::slide (iterator before, double distance)
771 Glib::Threads::Mutex::Lock lm (_lock);
773 if (before == _events.end()) {
777 while (before != _events.end()) {
778 (*before)->when += distance;
785 maybe_signal_changed ();
789 ControlList::shift (double pos, double frames)
792 Glib::Threads::Mutex::Lock lm (_lock);
794 for (iterator i = _events.begin(); i != _events.end(); ++i) {
795 if ((*i)->when >= pos) {
796 (*i)->when += frames;
803 maybe_signal_changed ();
807 ControlList::modify (iterator iter, double when, double val)
809 /* note: we assume higher level logic is in place to avoid this
810 reordering the time-order of control events in the list. ie. all
811 points after *iter are later than when.
815 Glib::Threads::Mutex::Lock lm (_lock);
817 (*iter)->when = when;
818 (*iter)->value = val;
825 _events.sort (event_time_less_than);
826 unlocked_invalidate_insert_iterator ();
828 _sort_pending = true;
834 maybe_signal_changed ();
837 std::pair<ControlList::iterator,ControlList::iterator>
838 ControlList::control_points_adjacent (double xval)
840 Glib::Threads::Mutex::Lock lm (_lock);
842 ControlEvent cp (xval, 0.0f);
843 std::pair<iterator,iterator> ret;
845 ret.first = _events.end();
846 ret.second = _events.end();
848 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
850 if (ret.first == _events.end()) {
851 if ((*i)->when >= xval) {
852 if (i != _events.begin()) {
861 if ((*i)->when > xval) {
871 ControlList::freeze ()
886 Glib::Threads::Mutex::Lock lm (_lock);
889 _events.sort (event_time_less_than);
890 unlocked_invalidate_insert_iterator ();
891 _sort_pending = false;
897 ControlList::mark_dirty () const
899 _lookup_cache.left = -1;
900 _search_cache.left = -1;
903 _curve->mark_dirty();
906 Dirty (); /* EMIT SIGNAL */
910 ControlList::truncate_end (double last_coordinate)
913 Glib::Threads::Mutex::Lock lm (_lock);
914 ControlEvent cp (last_coordinate, 0);
915 ControlList::reverse_iterator i;
918 if (_events.empty()) {
922 if (last_coordinate == _events.back()->when) {
926 if (last_coordinate > _events.back()->when) {
931 iterator foo = _events.begin();
934 if (foo == _events.end()) {
936 } else if (++foo == _events.end()) {
943 /* less than 2 points: add a new point */
944 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
947 /* more than 2 points: check to see if the last 2 values
948 are equal. if so, just move the position of the
949 last point. otherwise, add a new point.
952 iterator penultimate = _events.end();
953 --penultimate; /* points at last point */
954 --penultimate; /* points at the penultimate point */
956 if (_events.back()->value == (*penultimate)->value) {
957 _events.back()->when = last_coordinate;
959 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
967 last_val = unlocked_eval (last_coordinate);
968 last_val = max ((double) _min_yval, last_val);
969 last_val = min ((double) _max_yval, last_val);
971 i = _events.rbegin();
973 /* make i point to the last control point */
977 /* now go backwards, removing control points that are
978 beyond the new last coordinate.
981 // FIXME: SLOW! (size() == O(n))
983 uint32_t sz = _events.size();
985 while (i != _events.rend() && sz > 2) {
986 ControlList::reverse_iterator tmp;
991 if ((*i)->when < last_coordinate) {
995 _events.erase (i.base());
1001 _events.back()->when = last_coordinate;
1002 _events.back()->value = last_val;
1005 unlocked_invalidate_insert_iterator ();
1009 maybe_signal_changed ();
1013 ControlList::truncate_start (double overall_length)
1016 Glib::Threads::Mutex::Lock lm (_lock);
1018 double first_legal_value;
1019 double first_legal_coordinate;
1021 assert(!_events.empty());
1023 if (overall_length == _events.back()->when) {
1024 /* no change in overall length */
1028 if (overall_length > _events.back()->when) {
1030 /* growing at front: duplicate first point. shift all others */
1032 double shift = overall_length - _events.back()->when;
1035 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
1036 (*i)->when += shift;
1041 /* less than 2 points: add a new point */
1042 _events.push_front (new ControlEvent (0, _events.front()->value));
1046 /* more than 2 points: check to see if the first 2 values
1047 are equal. if so, just move the position of the
1048 first point. otherwise, add a new point.
1051 iterator second = _events.begin();
1052 ++second; /* points at the second point */
1054 if (_events.front()->value == (*second)->value) {
1055 /* first segment is flat, just move start point back to zero */
1056 _events.front()->when = 0;
1058 /* leave non-flat segment in place, add a new leading point. */
1059 _events.push_front (new ControlEvent (0, _events.front()->value));
1065 /* shrinking at front */
1067 first_legal_coordinate = _events.back()->when - overall_length;
1068 first_legal_value = unlocked_eval (first_legal_coordinate);
1069 first_legal_value = max (_min_yval, first_legal_value);
1070 first_legal_value = min (_max_yval, first_legal_value);
1072 /* remove all events earlier than the new "front" */
1074 i = _events.begin();
1076 while (i != _events.end() && !_events.empty()) {
1077 ControlList::iterator tmp;
1082 if ((*i)->when > first_legal_coordinate) {
1092 /* shift all remaining points left to keep their same
1096 for (i = _events.begin(); i != _events.end(); ++i) {
1097 (*i)->when -= first_legal_coordinate;
1100 /* add a new point for the interpolated new value */
1102 _events.push_front (new ControlEvent (0, first_legal_value));
1105 unlocked_invalidate_insert_iterator ();
1109 maybe_signal_changed ();
1113 ControlList::unlocked_eval (double x) const
1115 pair<EventList::iterator,EventList::iterator> range;
1121 const_iterator length_check_iter = _events.begin();
1122 for (npoints = 0; npoints < 4; ++npoints, ++length_check_iter) {
1123 if (length_check_iter == _events.end()) {
1130 return _default_value;
1133 return _events.front()->value;
1136 if (x >= _events.back()->when) {
1137 return _events.back()->value;
1138 } else if (x <= _events.front()->when) {
1139 return _events.front()->value;
1142 lpos = _events.front()->when;
1143 lval = _events.front()->value;
1144 upos = _events.back()->when;
1145 uval = _events.back()->value;
1147 if (_interpolation == Discrete) {
1151 /* linear interpolation betweeen the two points */
1152 fraction = (double) (x - lpos) / (double) (upos - lpos);
1153 return lval + (fraction * (uval - lval));
1156 if (x >= _events.back()->when) {
1157 return _events.back()->value;
1158 } else if (x <= _events.front()->when) {
1159 return _events.front()->value;
1162 return multipoint_eval (x);
1165 /*NOTREACHED*/ /* stupid gcc */
1166 return _default_value;
1170 ControlList::multipoint_eval (double x) const
1176 /* "Stepped" lookup (no interpolation) */
1177 /* FIXME: no cache. significant? */
1178 if (_interpolation == Discrete) {
1179 const ControlEvent cp (x, 0);
1180 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
1182 // shouldn't have made it to multipoint_eval
1183 assert(i != _events.end());
1185 if (i == _events.begin() || (*i)->when == x)
1188 return (*(--i))->value;
1191 /* Only do the range lookup if x is in a different range than last time
1192 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
1193 if ((_lookup_cache.left < 0) ||
1194 ((_lookup_cache.left > x) ||
1195 (_lookup_cache.range.first == _events.end()) ||
1196 ((*_lookup_cache.range.second)->when < x))) {
1198 const ControlEvent cp (x, 0);
1200 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
1203 pair<const_iterator,const_iterator> range = _lookup_cache.range;
1205 if (range.first == range.second) {
1207 /* x does not exist within the list as a control point */
1209 _lookup_cache.left = x;
1211 if (range.first != _events.begin()) {
1213 lpos = (*range.first)->when;
1214 lval = (*range.first)->value;
1216 /* we're before the first point */
1217 // return _default_value;
1218 return _events.front()->value;
1221 if (range.second == _events.end()) {
1222 /* we're after the last point */
1223 return _events.back()->value;
1226 upos = (*range.second)->when;
1227 uval = (*range.second)->value;
1229 /* linear interpolation betweeen the two points
1233 fraction = (double) (x - lpos) / (double) (upos - lpos);
1234 return lval + (fraction * (uval - lval));
1238 /* x is a control point in the data */
1239 _lookup_cache.left = -1;
1240 return (*range.first)->value;
1244 ControlList::build_search_cache_if_necessary (double start) const
1246 /* Only do the range lookup if x is in a different range than last time
1247 * this was called (or if the search cache has been marked "dirty" (left<0) */
1248 if (!_events.empty() && ((_search_cache.left < 0) || (_search_cache.left > start))) {
1250 const ControlEvent start_point (start, 0);
1252 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1253 // << start << ".." << end << ")" << endl;
1255 _search_cache.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1256 _search_cache.left = start;
1260 /** Get the earliest event after \a start using the current interpolation style.
1262 * If an event is found, \a x and \a y are set to its coordinates.
1264 * \param inclusive Include events with timestamp exactly equal to \a start
1265 * \return true if event is found (and \a x and \a y are valid).
1268 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
1270 // FIXME: It would be nice if this was unnecessary..
1271 Glib::Threads::Mutex::Lock lm(_lock, Glib::Threads::TRY_LOCK);
1276 return rt_safe_earliest_event_unlocked (start, x, y, inclusive);
1280 /** Get the earliest event after \a start using the current interpolation style.
1282 * If an event is found, \a x and \a y are set to its coordinates.
1284 * \param inclusive Include events with timestamp exactly equal to \a start
1285 * \return true if event is found (and \a x and \a y are valid).
1288 ControlList::rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool inclusive) const
1290 if (_interpolation == Discrete) {
1291 return rt_safe_earliest_event_discrete_unlocked(start, x, y, inclusive);
1293 return rt_safe_earliest_event_linear_unlocked(start, x, y, inclusive);
1298 /** Get the earliest event after \a start without interpolation.
1300 * If an event is found, \a x and \a y are set to its coordinates.
1302 * \param inclusive Include events with timestamp exactly equal to \a start
1303 * \return true if event is found (and \a x and \a y are valid).
1306 ControlList::rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const
1308 build_search_cache_if_necessary (start);
1310 if (_search_cache.first != _events.end()) {
1311 const ControlEvent* const first = *_search_cache.first;
1313 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1315 /* Earliest points is in range, return it */
1321 /* Move left of cache to this point
1322 * (Optimize for immediate call this cycle within range) */
1323 _search_cache.left = x;
1324 ++_search_cache.first;
1333 /* No points in range */
1339 /** Get the earliest time the line crosses an integer (Linear interpolation).
1341 * If an event is found, \a x and \a y are set to its coordinates.
1343 * \param inclusive Include events with timestamp exactly equal to \a start
1344 * \return true if event is found (and \a x and \a y are valid).
1347 ControlList::rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const
1349 // cout << "earliest_event(start: " << start << ", x: " << x << ", y: " << y << ", inclusive: " << inclusive << ")" << endl;
1351 const_iterator length_check_iter = _events.begin();
1352 if (_events.empty()) { // 0 events
1354 } else if (_events.end() == ++length_check_iter) { // 1 event
1355 return rt_safe_earliest_event_discrete_unlocked (start, x, y, inclusive);
1358 // Hack to avoid infinitely repeating the same event
1359 build_search_cache_if_necessary (start);
1361 if (_search_cache.first != _events.end()) {
1363 const ControlEvent* first = NULL;
1364 const ControlEvent* next = NULL;
1366 /* Step is after first */
1367 if (_search_cache.first == _events.begin() || (*_search_cache.first)->when <= start) {
1368 first = *_search_cache.first;
1369 ++_search_cache.first;
1370 if (_search_cache.first == _events.end()) {
1373 next = *_search_cache.first;
1375 /* Step is before first */
1377 const_iterator prev = _search_cache.first;
1380 next = *_search_cache.first;
1383 if (inclusive && first->when == start) {
1386 /* Move left of cache to this point
1387 * (Optimize for immediate call this cycle within range) */
1388 _search_cache.left = x;
1389 //++_search_cache.range.first;
1394 if (fabs(first->value - next->value) <= 1) {
1395 if (next->when > start) {
1398 /* Move left of cache to this point
1399 * (Optimize for immediate call this cycle within range) */
1400 _search_cache.left = x;
1401 //++_search_cache.range.first;
1402 assert(inclusive ? x >= start : x > start);
1409 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1410 //cerr << "start y: " << start_y << endl;
1412 //y = first->value + (slope * fabs(start - first->when));
1415 if (first->value < next->value) // ramping up
1417 else // ramping down
1420 x = first->when + (y - first->value) / (double)slope;
1422 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1424 if (first->value < next->value) // ramping up
1426 else // ramping down
1429 x = first->when + (y - first->value) / (double)slope;
1432 /*cerr << first->value << " @ " << first->when << " ... "
1433 << next->value << " @ " << next->when
1434 << " = " << y << " @ " << x << endl;*/
1436 assert( (y >= first->value && y <= next->value)
1437 || (y <= first->value && y >= next->value) );
1440 const bool past_start = (inclusive ? x >= start : x > start);
1442 /* Move left of cache to this point
1443 * (Optimize for immediate call this cycle within range) */
1444 _search_cache.left = x;
1445 assert(inclusive ? x >= start : x > start);
1451 /* No points in the future, so no steps (towards them) in the future */
1458 /** @param start Start position in model coordinates.
1459 * @param end End position in model coordinates.
1460 * @param op 0 = cut, 1 = copy, 2 = clear.
1462 boost::shared_ptr<ControlList>
1463 ControlList::cut_copy_clear (double start, double end, int op)
1465 boost::shared_ptr<ControlList> nal = create (_parameter);
1467 ControlEvent cp (start, 0.0);
1470 Glib::Threads::Mutex::Lock lm (_lock);
1472 /* first, determine s & e, two iterators that define the range of points
1473 affected by this operation
1476 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1480 /* and the last that is at or after `end' */
1482 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1485 /* if "start" isn't the location of an existing point,
1486 evaluate the curve to get a value for the start. Add a point to
1487 both the existing event list, and if its not a "clear" operation,
1488 to the copy ("nal") as well.
1490 Note that the time positions of the points in each list are different
1491 because we want the copy ("nal") to have a zero time reference.
1495 /* before we begin any cut/clear operations, get the value of the curve
1499 double end_value = unlocked_eval (end);
1501 if ((*s)->when != start) {
1503 double val = unlocked_eval (start);
1505 if (op == 0) { // cut
1506 if (start > _events.front()->when) {
1507 _events.insert (s, (new ControlEvent (start, val)));
1511 if (op != 2) { // ! clear
1512 nal->_events.push_back (new ControlEvent (0, val));
1516 for (iterator x = s; x != e; ) {
1518 /* adjust new points to be relative to start, which
1519 has been set to zero.
1523 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1527 x = _events.erase (x);
1533 if (e == _events.end() || (*e)->when != end) {
1535 /* only add a boundary point if there is a point after "end"
1538 if (op == 0 && (e != _events.end() && end < (*e)->when)) { // cut
1539 _events.insert (e, new ControlEvent (end, end_value));
1542 if (op != 2 && (e != _events.end() && end < (*e)->when)) { // cut/copy
1543 nal->_events.push_back (new ControlEvent (end - start, end_value));
1547 unlocked_invalidate_insert_iterator ();
1552 maybe_signal_changed ();
1559 boost::shared_ptr<ControlList>
1560 ControlList::cut (double start, double end)
1562 return cut_copy_clear (start, end, 0);
1565 boost::shared_ptr<ControlList>
1566 ControlList::copy (double start, double end)
1568 return cut_copy_clear (start, end, 1);
1572 ControlList::clear (double start, double end)
1574 cut_copy_clear (start, end, 2);
1577 /** @param pos Position in model coordinates */
1579 ControlList::paste (ControlList& alist, double pos, float /*times*/)
1581 if (alist._events.empty()) {
1586 Glib::Threads::Mutex::Lock lm (_lock);
1590 ControlEvent cp (pos, 0.0);
1592 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1594 for (iterator i = alist.begin();i != alist.end(); ++i) {
1595 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1596 end = (*i)->when + pos;
1600 /* move all points after the insertion along the timeline by
1604 while (where != _events.end()) {
1606 if ((*where)->when <= end) {
1609 _events.erase(where);
1617 unlocked_invalidate_insert_iterator ();
1621 maybe_signal_changed ();
1625 /** Move automation around according to a list of region movements.
1626 * @param return true if anything was changed, otherwise false (ie nothing needed changing)
1629 ControlList::move_ranges (const list< RangeMove<double> >& movements)
1631 typedef list< RangeMove<double> > RangeMoveList;
1634 Glib::Threads::Mutex::Lock lm (_lock);
1636 /* a copy of the events list before we started moving stuff around */
1637 EventList old_events = _events;
1639 /* clear the source and destination ranges in the new list */
1640 bool things_erased = false;
1641 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1643 if (erase_range_internal (i->from, i->from + i->length, _events)) {
1644 things_erased = true;
1647 if (erase_range_internal (i->to, i->to + i->length, _events)) {
1648 things_erased = true;
1652 /* if nothing was erased, there is nothing to do */
1653 if (!things_erased) {
1657 /* copy the events into the new list */
1658 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1659 iterator j = old_events.begin ();
1660 const double limit = i->from + i->length;
1661 const double dx = i->to - i->from;
1662 while (j != old_events.end () && (*j)->when <= limit) {
1663 if ((*j)->when >= i->from) {
1664 ControlEvent* ev = new ControlEvent (**j);
1666 _events.push_back (ev);
1673 _events.sort (event_time_less_than);
1674 unlocked_invalidate_insert_iterator ();
1676 _sort_pending = true;
1682 maybe_signal_changed ();
1687 ControlList::set_interpolation (InterpolationStyle s)
1689 if (_interpolation == s) {
1694 InterpolationChanged (s); /* EMIT SIGNAL */
1698 ControlList::set_thinning_factor (double v)
1700 _thinning_factor = v;
1704 ControlList::operator!= (ControlList const & other) const
1706 if (_events.size() != other._events.size()) {
1710 EventList::const_iterator i = _events.begin ();
1711 EventList::const_iterator j = other._events.begin ();
1713 while (i != _events.end() && (*i)->when == (*j)->when && (*i)->value == (*j)->value) {
1718 if (i != _events.end ()) {
1723 _parameter != other._parameter ||
1724 _interpolation != other._interpolation ||
1725 _min_yval != other._min_yval ||
1726 _max_yval != other._max_yval ||
1727 _default_value != other._default_value
1732 ControlList::dump (ostream& o)
1734 /* NOT LOCKED ... for debugging only */
1736 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
1737 o << (*x)->value << " @ " << (*x)->when << endl;
1741 } // namespace Evoral