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(other._interpolation)
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(other._interpolation)
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;
161 _interpolation = other._interpolation;
162 _default_value = other._default_value;
171 ControlList::copy_events (const ControlList& other)
174 Glib::Threads::Mutex::Lock lm (_lock);
176 for (const_iterator i = other.begin(); i != other.end(); ++i) {
177 _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
179 unlocked_invalidate_insert_iterator ();
182 maybe_signal_changed ();
186 ControlList::create_curve()
188 _curve = new Curve(*this);
192 ControlList::destroy_curve()
199 ControlList::maybe_signal_changed ()
204 _changed_when_thawed = true;
209 ControlList::clear ()
212 Glib::Threads::Mutex::Lock lm (_lock);
214 unlocked_invalidate_insert_iterator ();
218 maybe_signal_changed ();
222 ControlList::x_scale (double factor)
224 Glib::Threads::Mutex::Lock lm (_lock);
229 ControlList::extend_to (double when)
231 Glib::Threads::Mutex::Lock lm (_lock);
232 if (_events.empty() || _events.back()->when == when) {
235 double factor = when / _events.back()->when;
241 ControlList::_x_scale (double factor)
243 for (iterator i = _events.begin(); i != _events.end(); ++i) {
244 (*i)->when *= factor;
250 struct ControlEventTimeComparator {
251 bool operator() (ControlEvent* a, ControlEvent* b) {
252 return a->when < b->when;
259 bool changed = false;
262 Glib::Threads::Mutex::Lock lm (_lock);
264 ControlEvent* prevprev = 0;
265 ControlEvent* cur = 0;
266 ControlEvent* prev = 0;
270 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 thin from %2 events\n", this, _events.size()));
272 for (iterator i = _events.begin(); i != _events.end(); ++i) {
279 /* compute the area of the triangle formed by 3 points
282 double area = fabs ((prevprev->when * (prev->value - cur->value)) +
283 (prev->when * (cur->value - prevprev->value)) +
284 (cur->when * (prevprev->value - prev->value)));
286 if (area < _thinning_factor) {
287 iterator tmp = pprev;
289 /* pprev will change to current
290 i is incremented to the next event
306 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 thin => %2 events\n", this, _events.size()));
309 unlocked_invalidate_insert_iterator ();
315 maybe_signal_changed ();
320 ControlList::fast_simple_add (double when, double value)
322 Glib::Threads::Mutex::Lock lm (_lock);
323 /* to be used only for loading pre-sorted data from saved state */
324 _events.insert (_events.end(), new ControlEvent (when, value));
325 assert(_events.back());
331 ControlList::invalidate_insert_iterator ()
333 Glib::Threads::Mutex::Lock lm (_lock);
334 unlocked_invalidate_insert_iterator ();
338 ControlList::unlocked_invalidate_insert_iterator ()
340 most_recent_insert_iterator = _events.end();
344 ControlList::start_write_pass (double when)
346 Glib::Threads::Mutex::Lock lm (_lock);
348 DEBUG_TRACE (DEBUG::ControlList, string_compose ("%1: setup write pass @ %2\n", this, when));
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 DEBUG_TRACE (DEBUG::ControlList, "write pass finished\n");
367 if (did_write_during_pass) {
369 did_write_during_pass = false;
371 new_write_pass = true;
372 _in_write_pass = false;
376 ControlList::set_in_write_pass (bool yn, bool add_point, double when)
378 DEBUG_TRACE (DEBUG::ControlList, string_compose ("now in write pass @ %1, add point ? %2\n", when, add_point));
382 if (yn && add_point) {
383 add_guard_point (when);
388 ControlList::add_guard_point (double when)
390 ControlEvent cp (when, 0.0);
391 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
393 double eval_value = unlocked_eval (insert_position);
395 if (most_recent_insert_iterator == _events.end()) {
397 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at end, adding eval-value there %2\n", this, eval_value));
398 _events.push_back (new ControlEvent (when, eval_value));
399 /* leave insert iterator at the end */
401 } else if ((*most_recent_insert_iterator)->when == when) {
403 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert iterator at existing point, setting eval-value there %2\n", this, eval_value));
405 /* most_recent_insert_iterator points to a control event
406 already at the insert position, so there is
411 advance most_recent_insert_iterator so that the "real"
412 insert occurs in the right place, since it
413 points to the control event just inserted.
416 ++most_recent_insert_iterator;
419 /* insert a new control event at the right spot
422 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert eval-value %2 just before iterator @ %3\n",
423 this, eval_value, (*most_recent_insert_iterator)->when));
425 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator, new ControlEvent (when, eval_value));
427 /* advance most_recent_insert_iterator so that the "real"
428 * insert occurs in the right place, since it
429 * points to the control event just inserted.
432 ++most_recent_insert_iterator;
435 /* don't do this again till the next write pass */
437 new_write_pass = false;
441 ControlList::in_write_pass () const
443 return _in_write_pass;
447 ControlList::editor_add (double when, double value)
449 /* this is for making changes from a graphical line editor
452 if (!clamp_value (when, value)) {
456 if (_events.empty()) {
458 /* as long as the point we're adding is not at zero,
459 * add an "anchor" point there.
463 _events.insert (_events.end(), new ControlEvent (0, _default_value));
464 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added default value %2 at zero\n", this, _default_value));
468 ControlEvent cp (when, 0.0f);
469 iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
470 DEBUG_TRACE (DEBUG::ControlList, string_compose ("editor_add: actually add when= %1 value= %2\n", when, value));
471 _events.insert (i, new ControlEvent (when, value));
475 maybe_signal_changed ();
480 ControlList::add (double when, double value, bool with_guards)
482 /* this is for making changes from some kind of user interface or
483 control surface (GUI, MIDI, OSC etc)
486 if (!clamp_value (when, value)) {
490 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 add %2 at %3 w/erase = %4 (new ? %6) at end ? %5\n",
491 this, value, when, _in_write_pass, (most_recent_insert_iterator == _events.end()), new_write_pass));
493 Glib::Threads::Mutex::Lock lm (_lock);
494 ControlEvent cp (when, 0.0f);
495 iterator insertion_point;
497 if (_events.empty()) {
499 /* as long as the point we're adding is not at zero,
500 * add an "anchor" point there.
504 _events.insert (_events.end(), new ControlEvent (0, _default_value));
505 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added default value %2 at zero\n", this, _default_value));
509 if (_in_write_pass && new_write_pass) {
512 add_guard_point (insert_position);
513 did_write_during_pass = true;
516 } else if (most_recent_insert_iterator == _events.end() || when > (*most_recent_insert_iterator)->when) {
518 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 %2 erase from existing iterator (@end ? %3)\n",
519 this, (_in_write_pass ? "DO" : "DON'T"),
520 (most_recent_insert_iterator == _events.end())));
522 if (_in_write_pass) {
523 while (most_recent_insert_iterator != _events.end()) {
524 if ((*most_recent_insert_iterator)->when < when) {
525 if (_in_write_pass) {
526 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 erase existing @ %2\n", this, (*most_recent_insert_iterator)));
527 delete *most_recent_insert_iterator;
528 most_recent_insert_iterator = _events.erase (most_recent_insert_iterator);
531 } else if ((*most_recent_insert_iterator)->when >= when) {
534 ++most_recent_insert_iterator;
537 if (with_guards && most_recent_insert_iterator != _events.end()) {
538 if ((*most_recent_insert_iterator)->when - when > 64) {
539 /* next control point is some
540 * distance from where our new
541 * point is going to go - add a
542 * new point to avoid changing
543 * the shape of the line too
544 * much. the insert iterator needs
545 * to point to the new control
546 * point so that our insert
547 * will happen correctly.
549 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator,
550 new ControlEvent (when+1, (*most_recent_insert_iterator)->value));
551 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added post-erase guard point @ %2 = %3\n",
553 (*most_recent_insert_iterator)->value));
559 /* not in a write pass: figure out the iterator we should insert in front of */
561 DEBUG_TRACE (DEBUG::ControlList, string_compose ("compute MRI for position %1\n", when));
562 ControlEvent cp (when, 0.0f);
563 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
568 /* not in a write pass, adding a point within existing
569 * data: figure out the iterator we should insert in
573 DEBUG_TRACE (DEBUG::ControlList, string_compose ("compute(b) MRI for position %1\n", when));
574 ControlEvent cp (when, 0.0f);
575 most_recent_insert_iterator = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
578 /* OK, now we're really ready to add a new point
581 if (most_recent_insert_iterator == _events.end()) {
582 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 appending new point at end\n", this));
586 /* check if would just be adding to a straight line,
587 * and don't add another point if so
590 if (!_events.empty()) { // avoid O(N) _events.size() here
591 if (_events.back()->value == value) {
592 EventList::iterator b = _events.end();
593 --b; // final point, which we know exists
594 if (b != _events.begin()) { // step back again, but check first that it is legal
595 --b; // penultimate-point
596 if ((*b)->value == value) {
597 /* there are at least two points with the exact same value ...
598 * straight line - just move the final
599 * point to the new time
601 _events.back()->when = when;
603 DEBUG_TRACE (DEBUG::ControlList, string_compose ("final value of %1 moved to %2\n", value, when));
610 _events.push_back (new ControlEvent (when, value));
611 DEBUG_TRACE (DEBUG::ControlList, string_compose ("\tactually appended, size now %1\n", _events.size()));
614 if (!_in_write_pass) {
615 most_recent_insert_iterator = _events.end();
616 --most_recent_insert_iterator;
619 } else if ((*most_recent_insert_iterator)->when == when) {
621 if ((*most_recent_insert_iterator)->value != value) {
622 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 reset existing point to new value %2\n", this, value));
624 /* only one point allowed per time point, so just
625 * reset the value here.
628 (*most_recent_insert_iterator)->value = value;
630 /* if we modified the final value, then its as
631 * if we inserted a new point as far as the
632 * next addition, so make sure we know that.
635 if (_in_write_pass && _events.back()->when == when) {
636 most_recent_insert_iterator = _events.end();
640 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 same time %2, same value value %3\n", this, when, value));
644 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 insert new point at %2 at iterator at %3\n", this, when, (*most_recent_insert_iterator)->when));
648 /* check if would just be adding to a straight line,
649 * and don't add another point if so
652 if (most_recent_insert_iterator != _events.begin()) {
653 EventList::iterator b = most_recent_insert_iterator;
654 --b; // prior point, which we know exists
655 if ((*b)->value == value) { // same value as the point we plan to insert
656 if (b != _events.begin()) { // step back again, which may not be possible
657 EventList::iterator bb = b;
658 --bb; // next-to-prior-point
659 if ((*bb)->value == value) {
660 /* straight line - just move the prior
661 * point to the new time
665 if (!_in_write_pass) {
666 most_recent_insert_iterator = b;
669 DEBUG_TRACE (DEBUG::ControlList, string_compose ("final value of %1 moved to %2\n", value, when));
676 if (with_guards && most_recent_insert_iterator != _events.end()) {
677 if ((*most_recent_insert_iterator)->when - when > 64) {
678 /* next control point is some
679 * distance from where our new
680 * point is going to go - add a
681 * new point to avoid changing
682 * the shape of the line too
683 * much. the insert iterator needs
684 * to point to the new control
685 * point so that our insert
686 * will happen correctly.
688 most_recent_insert_iterator = _events.insert (most_recent_insert_iterator,
689 new ControlEvent (when+1, (*most_recent_insert_iterator)->value));
690 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 added insert-post-erase guard point @ %2 = %3\n",
692 (*most_recent_insert_iterator)->value));
697 EventList::iterator x = _events.insert (most_recent_insert_iterator, new ControlEvent (when, value));
698 DEBUG_TRACE (DEBUG::ControlList, string_compose ("@%1 inserted new value before MRI, size now %2\n", this, _events.size()));
700 if (!_in_write_pass) {
701 most_recent_insert_iterator = x;
709 maybe_signal_changed ();
713 ControlList::erase (iterator i)
716 Glib::Threads::Mutex::Lock lm (_lock);
717 if (most_recent_insert_iterator == i) {
718 unlocked_invalidate_insert_iterator ();
723 maybe_signal_changed ();
727 ControlList::erase (iterator start, iterator end)
730 Glib::Threads::Mutex::Lock lm (_lock);
731 _events.erase (start, end);
732 unlocked_invalidate_insert_iterator ();
735 maybe_signal_changed ();
738 /** Erase the first event which matches the given time and value */
740 ControlList::erase (double when, double value)
743 Glib::Threads::Mutex::Lock lm (_lock);
745 iterator i = begin ();
746 while (i != end() && ((*i)->when != when || (*i)->value != value)) {
752 if (most_recent_insert_iterator == i) {
753 unlocked_invalidate_insert_iterator ();
760 maybe_signal_changed ();
764 ControlList::erase_range (double start, double endt)
769 Glib::Threads::Mutex::Lock lm (_lock);
770 erased = erase_range_internal (start, endt, _events);
779 maybe_signal_changed ();
784 ControlList::erase_range_internal (double start, double endt, EventList & events)
787 ControlEvent cp (start, 0.0f);
791 if ((s = lower_bound (events.begin(), events.end(), &cp, time_comparator)) != events.end()) {
793 e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
796 unlocked_invalidate_insert_iterator ();
805 ControlList::slide (iterator before, double distance)
808 Glib::Threads::Mutex::Lock lm (_lock);
810 if (before == _events.end()) {
814 while (before != _events.end()) {
815 (*before)->when += distance;
822 maybe_signal_changed ();
826 ControlList::shift (double pos, double frames)
829 Glib::Threads::Mutex::Lock lm (_lock);
831 for (iterator i = _events.begin(); i != _events.end(); ++i) {
832 if ((*i)->when >= pos) {
833 (*i)->when += frames;
840 maybe_signal_changed ();
844 ControlList::modify (iterator iter, double when, double val)
846 /* note: we assume higher level logic is in place to avoid this
847 reordering the time-order of control events in the list. ie. all
848 points after *iter are later than when.
852 Glib::Threads::Mutex::Lock lm (_lock);
854 (*iter)->when = when;
855 (*iter)->value = val;
862 _events.sort (event_time_less_than);
863 unlocked_invalidate_insert_iterator ();
865 _sort_pending = true;
871 maybe_signal_changed ();
874 std::pair<ControlList::iterator,ControlList::iterator>
875 ControlList::control_points_adjacent (double xval)
877 Glib::Threads::Mutex::Lock lm (_lock);
879 ControlEvent cp (xval, 0.0f);
880 std::pair<iterator,iterator> ret;
882 ret.first = _events.end();
883 ret.second = _events.end();
885 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
887 if (ret.first == _events.end()) {
888 if ((*i)->when >= xval) {
889 if (i != _events.begin()) {
898 if ((*i)->when > xval) {
908 ControlList::freeze ()
923 Glib::Threads::Mutex::Lock lm (_lock);
926 _events.sort (event_time_less_than);
927 unlocked_invalidate_insert_iterator ();
928 _sort_pending = false;
934 ControlList::mark_dirty () const
936 _lookup_cache.left = -1;
937 _search_cache.left = -1;
940 _curve->mark_dirty();
943 Dirty (); /* EMIT SIGNAL */
947 ControlList::truncate_end (double last_coordinate)
950 Glib::Threads::Mutex::Lock lm (_lock);
951 ControlEvent cp (last_coordinate, 0);
952 ControlList::reverse_iterator i;
955 if (_events.empty()) {
959 if (last_coordinate == _events.back()->when) {
963 if (last_coordinate > _events.back()->when) {
968 iterator foo = _events.begin();
971 if (foo == _events.end()) {
973 } else if (++foo == _events.end()) {
980 /* less than 2 points: add a new point */
981 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
984 /* more than 2 points: check to see if the last 2 values
985 are equal. if so, just move the position of the
986 last point. otherwise, add a new point.
989 iterator penultimate = _events.end();
990 --penultimate; /* points at last point */
991 --penultimate; /* points at the penultimate point */
993 if (_events.back()->value == (*penultimate)->value) {
994 _events.back()->when = last_coordinate;
996 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
1002 /* shortening end */
1004 last_val = unlocked_eval (last_coordinate);
1005 last_val = max ((double) _min_yval, last_val);
1006 last_val = min ((double) _max_yval, last_val);
1008 i = _events.rbegin();
1010 /* make i point to the last control point */
1014 /* now go backwards, removing control points that are
1015 beyond the new last coordinate.
1018 // FIXME: SLOW! (size() == O(n))
1020 uint32_t sz = _events.size();
1022 while (i != _events.rend() && sz > 2) {
1023 ControlList::reverse_iterator tmp;
1028 if ((*i)->when < last_coordinate) {
1032 _events.erase (i.base());
1038 _events.back()->when = last_coordinate;
1039 _events.back()->value = last_val;
1042 unlocked_invalidate_insert_iterator ();
1046 maybe_signal_changed ();
1050 ControlList::truncate_start (double overall_length)
1053 Glib::Threads::Mutex::Lock lm (_lock);
1055 double first_legal_value;
1056 double first_legal_coordinate;
1058 assert(!_events.empty());
1060 if (overall_length == _events.back()->when) {
1061 /* no change in overall length */
1065 if (overall_length > _events.back()->when) {
1067 /* growing at front: duplicate first point. shift all others */
1069 double shift = overall_length - _events.back()->when;
1072 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
1073 (*i)->when += shift;
1078 /* less than 2 points: add a new point */
1079 _events.push_front (new ControlEvent (0, _events.front()->value));
1083 /* more than 2 points: check to see if the first 2 values
1084 are equal. if so, just move the position of the
1085 first point. otherwise, add a new point.
1088 iterator second = _events.begin();
1089 ++second; /* points at the second point */
1091 if (_events.front()->value == (*second)->value) {
1092 /* first segment is flat, just move start point back to zero */
1093 _events.front()->when = 0;
1095 /* leave non-flat segment in place, add a new leading point. */
1096 _events.push_front (new ControlEvent (0, _events.front()->value));
1102 /* shrinking at front */
1104 first_legal_coordinate = _events.back()->when - overall_length;
1105 first_legal_value = unlocked_eval (first_legal_coordinate);
1106 first_legal_value = max (_min_yval, first_legal_value);
1107 first_legal_value = min (_max_yval, first_legal_value);
1109 /* remove all events earlier than the new "front" */
1111 i = _events.begin();
1113 while (i != _events.end() && !_events.empty()) {
1114 ControlList::iterator tmp;
1119 if ((*i)->when > first_legal_coordinate) {
1129 /* shift all remaining points left to keep their same
1133 for (i = _events.begin(); i != _events.end(); ++i) {
1134 (*i)->when -= first_legal_coordinate;
1137 /* add a new point for the interpolated new value */
1139 _events.push_front (new ControlEvent (0, first_legal_value));
1142 unlocked_invalidate_insert_iterator ();
1146 maybe_signal_changed ();
1150 ControlList::unlocked_eval (double x) const
1152 pair<EventList::iterator,EventList::iterator> range;
1158 const_iterator length_check_iter = _events.begin();
1159 for (npoints = 0; npoints < 4; ++npoints, ++length_check_iter) {
1160 if (length_check_iter == _events.end()) {
1167 return _default_value;
1170 return _events.front()->value;
1173 if (x >= _events.back()->when) {
1174 return _events.back()->value;
1175 } else if (x <= _events.front()->when) {
1176 return _events.front()->value;
1179 lpos = _events.front()->when;
1180 lval = _events.front()->value;
1181 upos = _events.back()->when;
1182 uval = _events.back()->value;
1184 if (_interpolation == Discrete) {
1188 /* linear interpolation betweeen the two points */
1189 fraction = (double) (x - lpos) / (double) (upos - lpos);
1190 return lval + (fraction * (uval - lval));
1193 if (x >= _events.back()->when) {
1194 return _events.back()->value;
1195 } else if (x <= _events.front()->when) {
1196 return _events.front()->value;
1199 return multipoint_eval (x);
1202 /*NOTREACHED*/ /* stupid gcc */
1203 return _default_value;
1207 ControlList::multipoint_eval (double x) const
1213 /* "Stepped" lookup (no interpolation) */
1214 /* FIXME: no cache. significant? */
1215 if (_interpolation == Discrete) {
1216 const ControlEvent cp (x, 0);
1217 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
1219 // shouldn't have made it to multipoint_eval
1220 assert(i != _events.end());
1222 if (i == _events.begin() || (*i)->when == x)
1225 return (*(--i))->value;
1228 /* Only do the range lookup if x is in a different range than last time
1229 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
1230 if ((_lookup_cache.left < 0) ||
1231 ((_lookup_cache.left > x) ||
1232 (_lookup_cache.range.first == _events.end()) ||
1233 ((*_lookup_cache.range.second)->when < x))) {
1235 const ControlEvent cp (x, 0);
1237 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
1240 pair<const_iterator,const_iterator> range = _lookup_cache.range;
1242 if (range.first == range.second) {
1244 /* x does not exist within the list as a control point */
1246 _lookup_cache.left = x;
1248 if (range.first != _events.begin()) {
1250 lpos = (*range.first)->when;
1251 lval = (*range.first)->value;
1253 /* we're before the first point */
1254 // return _default_value;
1255 return _events.front()->value;
1258 if (range.second == _events.end()) {
1259 /* we're after the last point */
1260 return _events.back()->value;
1263 upos = (*range.second)->when;
1264 uval = (*range.second)->value;
1266 /* linear interpolation betweeen the two points
1270 fraction = (double) (x - lpos) / (double) (upos - lpos);
1271 return lval + (fraction * (uval - lval));
1275 /* x is a control point in the data */
1276 _lookup_cache.left = -1;
1277 return (*range.first)->value;
1281 ControlList::build_search_cache_if_necessary (double start) const
1283 /* Only do the range lookup if x is in a different range than last time
1284 * this was called (or if the search cache has been marked "dirty" (left<0) */
1285 if (!_events.empty() && ((_search_cache.left < 0) || (_search_cache.left > start))) {
1287 const ControlEvent start_point (start, 0);
1289 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1290 // << start << ".." << end << ")" << endl;
1292 _search_cache.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1293 _search_cache.left = start;
1297 /** Get the earliest event after \a start using the current interpolation style.
1299 * If an event is found, \a x and \a y are set to its coordinates.
1301 * \param inclusive Include events with timestamp exactly equal to \a start
1302 * \return true if event is found (and \a x and \a y are valid).
1305 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
1307 // FIXME: It would be nice if this was unnecessary..
1308 Glib::Threads::Mutex::Lock lm(_lock, Glib::Threads::TRY_LOCK);
1313 return rt_safe_earliest_event_unlocked (start, x, y, inclusive);
1317 /** Get the earliest event after \a start using the current interpolation style.
1319 * If an event is found, \a x and \a y are set to its coordinates.
1321 * \param inclusive Include events with timestamp exactly equal to \a start
1322 * \return true if event is found (and \a x and \a y are valid).
1325 ControlList::rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool inclusive) const
1327 if (_interpolation == Discrete) {
1328 return rt_safe_earliest_event_discrete_unlocked(start, x, y, inclusive);
1330 return rt_safe_earliest_event_linear_unlocked(start, x, y, inclusive);
1335 /** Get the earliest event after \a start without interpolation.
1337 * If an event is found, \a x and \a y are set to its coordinates.
1339 * \param inclusive Include events with timestamp exactly equal to \a start
1340 * \return true if event is found (and \a x and \a y are valid).
1343 ControlList::rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const
1345 build_search_cache_if_necessary (start);
1347 if (_search_cache.first != _events.end()) {
1348 const ControlEvent* const first = *_search_cache.first;
1350 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1352 /* Earliest points is in range, return it */
1358 /* Move left of cache to this point
1359 * (Optimize for immediate call this cycle within range) */
1360 _search_cache.left = x;
1361 ++_search_cache.first;
1370 /* No points in range */
1376 /** Get the earliest time the line crosses an integer (Linear interpolation).
1378 * If an event is found, \a x and \a y are set to its coordinates.
1380 * \param inclusive Include events with timestamp exactly equal to \a start
1381 * \return true if event is found (and \a x and \a y are valid).
1384 ControlList::rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const
1386 // cout << "earliest_event(start: " << start << ", x: " << x << ", y: " << y << ", inclusive: " << inclusive << ")" << endl;
1388 const_iterator length_check_iter = _events.begin();
1389 if (_events.empty()) { // 0 events
1391 } else if (_events.end() == ++length_check_iter) { // 1 event
1392 return rt_safe_earliest_event_discrete_unlocked (start, x, y, inclusive);
1395 // Hack to avoid infinitely repeating the same event
1396 build_search_cache_if_necessary (start);
1398 if (_search_cache.first != _events.end()) {
1400 const ControlEvent* first = NULL;
1401 const ControlEvent* next = NULL;
1403 /* Step is after first */
1404 if (_search_cache.first == _events.begin() || (*_search_cache.first)->when <= start) {
1405 first = *_search_cache.first;
1406 ++_search_cache.first;
1407 if (_search_cache.first == _events.end()) {
1410 next = *_search_cache.first;
1412 /* Step is before first */
1414 const_iterator prev = _search_cache.first;
1417 next = *_search_cache.first;
1420 if (inclusive && first->when == start) {
1423 /* Move left of cache to this point
1424 * (Optimize for immediate call this cycle within range) */
1425 _search_cache.left = x;
1426 //++_search_cache.range.first;
1431 if (fabs(first->value - next->value) <= 1) {
1432 if (next->when > start) {
1435 /* Move left of cache to this point
1436 * (Optimize for immediate call this cycle within range) */
1437 _search_cache.left = x;
1438 //++_search_cache.range.first;
1439 assert(inclusive ? x >= start : x > start);
1446 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1447 //cerr << "start y: " << start_y << endl;
1449 //y = first->value + (slope * fabs(start - first->when));
1452 if (first->value < next->value) // ramping up
1454 else // ramping down
1457 x = first->when + (y - first->value) / (double)slope;
1459 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1461 if (first->value < next->value) // ramping up
1463 else // ramping down
1466 x = first->when + (y - first->value) / (double)slope;
1469 /*cerr << first->value << " @ " << first->when << " ... "
1470 << next->value << " @ " << next->when
1471 << " = " << y << " @ " << x << endl;*/
1473 assert( (y >= first->value && y <= next->value)
1474 || (y <= first->value && y >= next->value) );
1477 const bool past_start = (inclusive ? x >= start : x > start);
1479 /* Move left of cache to this point
1480 * (Optimize for immediate call this cycle within range) */
1481 _search_cache.left = x;
1482 assert(inclusive ? x >= start : x > start);
1490 _search_cache.left = x;
1494 /* No points in the future, so no steps (towards them) in the future */
1501 /** @param start Start position in model coordinates.
1502 * @param end End position in model coordinates.
1503 * @param op 0 = cut, 1 = copy, 2 = clear.
1505 boost::shared_ptr<ControlList>
1506 ControlList::cut_copy_clear (double start, double end, int op)
1508 boost::shared_ptr<ControlList> nal = create (_parameter);
1510 ControlEvent cp (start, 0.0);
1513 Glib::Threads::Mutex::Lock lm (_lock);
1515 /* first, determine s & e, two iterators that define the range of points
1516 affected by this operation
1519 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1523 /* and the last that is at or after `end' */
1525 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1528 /* if "start" isn't the location of an existing point,
1529 evaluate the curve to get a value for the start. Add a point to
1530 both the existing event list, and if its not a "clear" operation,
1531 to the copy ("nal") as well.
1533 Note that the time positions of the points in each list are different
1534 because we want the copy ("nal") to have a zero time reference.
1538 /* before we begin any cut/clear operations, get the value of the curve
1542 double end_value = unlocked_eval (end);
1544 if ((*s)->when != start) {
1546 double val = unlocked_eval (start);
1548 if (op == 0) { // cut
1549 if (start > _events.front()->when) {
1550 _events.insert (s, (new ControlEvent (start, val)));
1554 if (op != 2) { // ! clear
1555 nal->_events.push_back (new ControlEvent (0, val));
1559 for (iterator x = s; x != e; ) {
1561 /* adjust new points to be relative to start, which
1562 has been set to zero.
1566 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1570 x = _events.erase (x);
1576 if (e == _events.end() || (*e)->when != end) {
1578 /* only add a boundary point if there is a point after "end"
1581 if (op == 0 && (e != _events.end() && end < (*e)->when)) { // cut
1582 _events.insert (e, new ControlEvent (end, end_value));
1585 if (op != 2 && (e != _events.end() && end < (*e)->when)) { // cut/copy
1586 nal->_events.push_back (new ControlEvent (end - start, end_value));
1590 unlocked_invalidate_insert_iterator ();
1595 maybe_signal_changed ();
1602 boost::shared_ptr<ControlList>
1603 ControlList::cut (double start, double end)
1605 return cut_copy_clear (start, end, 0);
1608 boost::shared_ptr<ControlList>
1609 ControlList::copy (double start, double end)
1611 return cut_copy_clear (start, end, 1);
1615 ControlList::clear (double start, double end)
1617 cut_copy_clear (start, end, 2);
1620 /** @param pos Position in model coordinates */
1622 ControlList::paste (ControlList& alist, double pos, float /*times*/)
1624 if (alist._events.empty()) {
1629 Glib::Threads::Mutex::Lock lm (_lock);
1633 ControlEvent cp (pos, 0.0);
1635 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1637 for (iterator i = alist.begin();i != alist.end(); ++i) {
1638 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1639 end = (*i)->when + pos;
1643 /* move all points after the insertion along the timeline by
1647 while (where != _events.end()) {
1649 if ((*where)->when <= end) {
1652 _events.erase(where);
1660 unlocked_invalidate_insert_iterator ();
1664 maybe_signal_changed ();
1668 /** Move automation around according to a list of region movements.
1669 * @param return true if anything was changed, otherwise false (ie nothing needed changing)
1672 ControlList::move_ranges (const list< RangeMove<double> >& movements)
1674 typedef list< RangeMove<double> > RangeMoveList;
1677 Glib::Threads::Mutex::Lock lm (_lock);
1679 /* a copy of the events list before we started moving stuff around */
1680 EventList old_events = _events;
1682 /* clear the source and destination ranges in the new list */
1683 bool things_erased = false;
1684 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1686 if (erase_range_internal (i->from, i->from + i->length, _events)) {
1687 things_erased = true;
1690 if (erase_range_internal (i->to, i->to + i->length, _events)) {
1691 things_erased = true;
1695 /* if nothing was erased, there is nothing to do */
1696 if (!things_erased) {
1700 /* copy the events into the new list */
1701 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1702 iterator j = old_events.begin ();
1703 const double limit = i->from + i->length;
1704 const double dx = i->to - i->from;
1705 while (j != old_events.end () && (*j)->when <= limit) {
1706 if ((*j)->when >= i->from) {
1707 ControlEvent* ev = new ControlEvent (**j);
1709 _events.push_back (ev);
1716 _events.sort (event_time_less_than);
1717 unlocked_invalidate_insert_iterator ();
1719 _sort_pending = true;
1725 maybe_signal_changed ();
1730 ControlList::set_interpolation (InterpolationStyle s)
1732 if (_interpolation == s) {
1737 InterpolationChanged (s); /* EMIT SIGNAL */
1741 ControlList::set_thinning_factor (double v)
1743 _thinning_factor = v;
1747 ControlList::operator!= (ControlList const & other) const
1749 if (_events.size() != other._events.size()) {
1753 EventList::const_iterator i = _events.begin ();
1754 EventList::const_iterator j = other._events.begin ();
1756 while (i != _events.end() && (*i)->when == (*j)->when && (*i)->value == (*j)->value) {
1761 if (i != _events.end ()) {
1766 _parameter != other._parameter ||
1767 _interpolation != other._interpolation ||
1768 _min_yval != other._min_yval ||
1769 _max_yval != other._max_yval ||
1770 _default_value != other._default_value
1775 ControlList::dump (ostream& o)
1777 /* NOT LOCKED ... for debugging only */
1779 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
1780 o << (*x)->value << " @ " << (uint64_t) (*x)->when << endl;
1784 } // namespace Evoral