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"
30 inline bool event_time_less_than (ControlEvent* a, ControlEvent* b)
32 return a->when < b->when;
35 /* this has no units but corresponds to the area of a rectangle
36 computed between three points in the list. If the area is
37 large, it indicates significant non-linearity between the
40 during automation recording we thin the recorded points
41 using this value. if a point is sufficiently co-linear
42 with its neighbours (as defined by the area of the rectangle
43 formed by three of them), we will not include it in the
44 ControlList. a smaller value will exclude less points,
45 a larger value will exclude more points, so it effectively
46 measures the amount of thinning to be done.
49 double ControlList::_thinning_factor = 20.0;
51 ControlList::ControlList (const Parameter& id)
53 , _interpolation(Linear)
57 _changed_when_thawed = false;
61 _lookup_cache.left = -1;
62 _lookup_cache.range.first = _events.end();
63 _search_cache.left = -1;
64 _search_cache.first = _events.end();
65 _sort_pending = false;
68 ControlList::ControlList (const ControlList& other)
69 : _parameter(other._parameter)
70 , _interpolation(Linear)
74 _changed_when_thawed = false;
75 _min_yval = other._min_yval;
76 _max_yval = other._max_yval;
77 _default_value = other._default_value;
78 _lookup_cache.range.first = _events.end();
79 _search_cache.first = _events.end();
80 _sort_pending = false;
87 ControlList::ControlList (const ControlList& other, double start, double end)
88 : _parameter(other._parameter)
89 , _interpolation(Linear)
93 _changed_when_thawed = false;
94 _min_yval = other._min_yval;
95 _max_yval = other._max_yval;
96 _default_value = other._default_value;
97 _lookup_cache.range.first = _events.end();
98 _search_cache.first = _events.end();
99 _sort_pending = false;
101 /* now grab the relevant points, and shift them back if necessary */
103 boost::shared_ptr<ControlList> section = const_cast<ControlList*>(&other)->copy (start, end);
105 if (!section->empty()) {
106 copy_events (*(section.get()));
112 ControlList::~ControlList()
114 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
118 for (list<NascentInfo*>::iterator n = nascent.begin(); n != nascent.end(); ++n) {
119 for (EventList::iterator x = (*n)->events.begin(); x != (*n)->events.end(); ++x) {
128 boost::shared_ptr<ControlList>
129 ControlList::create(Parameter id)
131 return boost::shared_ptr<ControlList>(new ControlList(id));
135 ControlList::operator== (const ControlList& other)
137 return _events == other._events;
141 ControlList::operator= (const ControlList& other)
143 if (this != &other) {
145 _min_yval = other._min_yval;
146 _max_yval = other._max_yval;
147 _default_value = other._default_value;
156 ControlList::copy_events (const ControlList& other)
159 Glib::Mutex::Lock lm (_lock);
161 for (const_iterator i = other.begin(); i != other.end(); ++i) {
162 _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
166 maybe_signal_changed ();
170 ControlList::create_curve()
172 _curve = new Curve(*this);
176 ControlList::destroy_curve()
183 ControlList::maybe_signal_changed ()
188 _changed_when_thawed = true;
193 ControlList::clear ()
196 Glib::Mutex::Lock lm (_lock);
201 maybe_signal_changed ();
205 ControlList::x_scale (double factor)
207 Glib::Mutex::Lock lm (_lock);
212 ControlList::extend_to (double when)
214 Glib::Mutex::Lock lm (_lock);
215 if (_events.empty() || _events.back()->when == when) {
218 double factor = when / _events.back()->when;
224 ControlList::_x_scale (double factor)
226 for (iterator i = _events.begin(); i != _events.end(); ++i) {
227 (*i)->when *= factor;
234 ControlList::write_pass_finished (double when)
236 merge_nascent (when);
240 struct ControlEventTimeComparator {
241 bool operator() (ControlEvent* a, ControlEvent* b) {
242 return a->when < b->when;
247 ControlList::merge_nascent (double when)
250 Glib::Mutex::Lock lm (_lock);
252 if (nascent.empty()) {
256 bool was_empty = _events.empty();
258 for (list<NascentInfo*>::iterator n = nascent.begin(); n != nascent.end(); ++n) {
260 NascentInfo* ninfo = *n;
261 EventList& nascent_events (ninfo->events);
262 bool need_adjacent_start_clamp;
263 bool need_adjacent_end_clamp;
264 EventList::iterator at;
266 if (nascent_events.empty()) {
271 nascent_events.sort (ControlEventTimeComparator ());
273 if (ninfo->start_time < 0.0) {
274 ninfo->start_time = nascent_events.front()->when;
277 if (ninfo->end_time < 0.0) {
278 ninfo->end_time = when;
281 if (_events.empty()) {
283 /* add an initial point just before
284 the nascent data, unless nascent_events
285 contains a point at zero or one
288 if (ninfo->start_time > 0) {
289 nascent_events.insert (nascent_events.begin(), new ControlEvent (ninfo->start_time - 1, _default_value));
292 /* add closing "clamp" point before we insert */
294 nascent_events.insert (nascent_events.end(), new ControlEvent (ninfo->end_time + 1, _default_value));
296 /* insert - front or back doesn't matter since
300 _events.insert (_events.begin(), nascent_events.begin(), nascent_events.end());
302 } else if (ninfo->end_time < _events.front()->when) {
304 /* all points in nascent are before the first existing point */
306 if (ninfo->start_time > (_events.front()->when + 1)) {
307 nascent_events.insert (nascent_events.begin(), new ControlEvent (ninfo->start_time - 1, _default_value));
310 /* add closing "clamp" point before we insert */
312 nascent_events.insert (nascent_events.end(), new ControlEvent (ninfo->end_time + 1, _default_value));
314 /* insert at front */
316 _events.insert (_events.begin(), nascent_events.begin(), nascent_events.end());
318 /* now add another default control point right
319 after the inserted nascent data
322 } else if (ninfo->start_time > _events.back()->when) {
324 /* all points in nascent are after the last existing point */
326 if (ninfo->start_time > (_events.back()->when + 1)) {
327 nascent_events.insert (nascent_events.begin(), new ControlEvent (ninfo->start_time - 1, _default_value));
330 /* add closing "clamp" point before we insert */
332 nascent_events.insert (nascent_events.end(), new ControlEvent (ninfo->end_time + 1, _default_value));
336 _events.insert (_events.end(), nascent_events.begin(), nascent_events.end());
340 /* find the range that overlaps with nascent events,
341 and insert the contents of nascent events.
345 iterator range_begin = _events.end();
346 iterator range_end = _events.end();
347 double end_value = unlocked_eval (ninfo->end_time);
348 double start_value = unlocked_eval (ninfo->start_time - 1);
350 need_adjacent_end_clamp = true;
351 need_adjacent_start_clamp = true;
353 for (i = _events.begin(); i != _events.end(); ++i) {
355 if ((*i)->when == ninfo->start_time) {
356 /* existing point at same time, remove it
357 and the consider the next point instead.
359 i = _events.erase (i);
361 if (i == _events.end()) {
365 if (range_begin == _events.end()) {
367 need_adjacent_start_clamp = false;
369 need_adjacent_end_clamp = false;
372 if ((*i)->when > ninfo->end_time) {
377 } else if ((*i)->when > ninfo->start_time) {
379 if (range_begin == _events.end()) {
383 if ((*i)->when > ninfo->end_time) {
391 range_begin is the first event on our list after the first nascent event
392 range_end is the first event on our list after the last nascent event
394 range_begin may be equal to _events.end() if the last event on our list
395 was at the same time as the first nascent event.
398 if (range_begin != _events.begin()) {
399 /* clamp point before */
400 if (need_adjacent_start_clamp) {
401 _events.insert (range_begin, new ControlEvent (ninfo->start_time, start_value));
405 _events.insert (range_begin, nascent_events.begin(), nascent_events.end());
407 if (range_end != _events.end()) {
408 /* clamp point after */
409 if (need_adjacent_end_clamp) {
410 _events.insert (range_begin, new ControlEvent (ninfo->end_time, end_value));
414 _events.erase (range_begin, range_end);
420 if (was_empty && !_events.empty()) {
421 if (_events.front()->when != 0) {
422 _events.insert (_events.begin(), new ControlEvent (0, _default_value));
429 nascent.push_back (new NascentInfo ());
433 maybe_signal_changed ();
437 ControlList::rt_add (double when, double value)
439 // this is for automation recording
441 if (touch_enabled() && !touching()) {
445 // cerr << "RT: alist " << this << " add " << value << " @ " << when << endl;
447 Glib::Mutex::Lock lm (_lock, Glib::TRY_LOCK);
450 assert (!nascent.empty());
451 /* we don't worry about adding events out of time order as we will
452 sort them in merge_nascent.
455 NascentInfo* ni (nascent.back());
456 EventList& el (ni->events);
458 if (!el.empty() && (when >= el.back()->when) && (value == el.back()->value)) {
460 /* same value, later timestamp, effective slope is
461 * zero, so just move the last point in nascent to our
462 * new time position. this avoids storing an unlimited
463 * number of points to represent a flat line.
466 ni->same_value_cnt++;
468 if (ni->same_value_cnt > 1) {
469 el.back()->when = when;
473 ni->same_value_cnt = 0;
476 el.push_back (new ControlEvent (when, value));
483 Glib::Mutex::Lock lm (_lock);
485 ControlEvent* prevprev = 0;
486 ControlEvent* cur = 0;
487 ControlEvent* prev = 0;
491 for (iterator i = _events.begin(); i != _events.end(); ++i) {
498 double area = fabs ((prevprev->when * (prev->value - cur->value)) +
499 (prev->when * (cur->value - prevprev->value)) +
500 (cur->when * (prevprev->value - prev->value)));
502 if (area < _thinning_factor) {
503 iterator tmp = pprev;
505 /* pprev will change to current
506 i is incremented to the next event
523 ControlList::fast_simple_add (double when, double value)
525 /* to be used only for loading pre-sorted data from saved state */
526 _events.insert (_events.end(), new ControlEvent (when, value));
527 assert(_events.back());
533 ControlList::add (double when, double value)
535 /* this is for making changes from some kind of user interface or
536 control surface (GUI, MIDI, OSC etc)
539 if (!clamp_value (when, value)) {
544 Glib::Mutex::Lock lm (_lock);
545 ControlEvent cp (when, 0.0f);
547 iterator insertion_point;
549 if (_events.empty()) {
551 _events.insert (_events.end(), new ControlEvent (0, _default_value));
555 for (insertion_point = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); insertion_point != _events.end(); ++insertion_point) {
557 /* only one point allowed per time point */
559 if ((*insertion_point)->when == when) {
560 (*insertion_point)->value = value;
565 if ((*insertion_point)->when >= when) {
571 _events.insert (insertion_point, new ControlEvent (when, value));
577 maybe_signal_changed ();
581 ControlList::erase (iterator i)
584 Glib::Mutex::Lock lm (_lock);
588 maybe_signal_changed ();
592 ControlList::erase (iterator start, iterator end)
595 Glib::Mutex::Lock lm (_lock);
596 _events.erase (start, end);
599 maybe_signal_changed ();
602 /** Erase the first event which matches the given time and value */
604 ControlList::erase (double when, double value)
607 Glib::Mutex::Lock lm (_lock);
609 iterator i = begin ();
610 while (i != end() && ((*i)->when != when || (*i)->value != value)) {
621 maybe_signal_changed ();
625 ControlList::erase_range (double start, double endt)
630 Glib::Mutex::Lock lm (_lock);
631 erased = erase_range_internal (start, endt, _events);
640 maybe_signal_changed ();
645 ControlList::erase_range_internal (double start, double endt, EventList & events)
648 ControlEvent cp (start, 0.0f);
652 if ((s = lower_bound (events.begin(), events.end(), &cp, time_comparator)) != events.end()) {
654 e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
665 ControlList::slide (iterator before, double distance)
668 Glib::Mutex::Lock lm (_lock);
670 if (before == _events.end()) {
674 while (before != _events.end()) {
675 (*before)->when += distance;
682 maybe_signal_changed ();
686 ControlList::shift (double pos, double frames)
689 Glib::Mutex::Lock lm (_lock);
691 for (iterator i = _events.begin(); i != _events.end(); ++i) {
692 if ((*i)->when >= pos) {
693 (*i)->when += frames;
700 maybe_signal_changed ();
704 ControlList::modify (iterator iter, double when, double val)
706 /* note: we assume higher level logic is in place to avoid this
707 reordering the time-order of control events in the list. ie. all
708 points after *iter are later than when.
712 Glib::Mutex::Lock lm (_lock);
714 (*iter)->when = when;
715 (*iter)->value = val;
717 if (std::isnan (val)) {
722 _events.sort (event_time_less_than);
724 _sort_pending = true;
730 maybe_signal_changed ();
733 std::pair<ControlList::iterator,ControlList::iterator>
734 ControlList::control_points_adjacent (double xval)
736 Glib::Mutex::Lock lm (_lock);
738 ControlEvent cp (xval, 0.0f);
739 std::pair<iterator,iterator> ret;
741 ret.first = _events.end();
742 ret.second = _events.end();
744 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
746 if (ret.first == _events.end()) {
747 if ((*i)->when >= xval) {
748 if (i != _events.begin()) {
757 if ((*i)->when > xval) {
767 ControlList::freeze ()
782 Glib::Mutex::Lock lm (_lock);
785 _events.sort (event_time_less_than);
786 _sort_pending = false;
792 ControlList::mark_dirty () const
794 _lookup_cache.left = -1;
795 _search_cache.left = -1;
798 _curve->mark_dirty();
801 Dirty (); /* EMIT SIGNAL */
805 ControlList::truncate_end (double last_coordinate)
808 Glib::Mutex::Lock lm (_lock);
809 ControlEvent cp (last_coordinate, 0);
810 ControlList::reverse_iterator i;
813 if (_events.empty()) {
817 if (last_coordinate == _events.back()->when) {
821 if (last_coordinate > _events.back()->when) {
826 iterator foo = _events.begin();
829 if (foo == _events.end()) {
831 } else if (++foo == _events.end()) {
838 /* less than 2 points: add a new point */
839 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
842 /* more than 2 points: check to see if the last 2 values
843 are equal. if so, just move the position of the
844 last point. otherwise, add a new point.
847 iterator penultimate = _events.end();
848 --penultimate; /* points at last point */
849 --penultimate; /* points at the penultimate point */
851 if (_events.back()->value == (*penultimate)->value) {
852 _events.back()->when = last_coordinate;
854 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
862 last_val = unlocked_eval (last_coordinate);
863 last_val = max ((double) _min_yval, last_val);
864 last_val = min ((double) _max_yval, last_val);
866 i = _events.rbegin();
868 /* make i point to the last control point */
872 /* now go backwards, removing control points that are
873 beyond the new last coordinate.
876 // FIXME: SLOW! (size() == O(n))
878 uint32_t sz = _events.size();
880 while (i != _events.rend() && sz > 2) {
881 ControlList::reverse_iterator tmp;
886 if ((*i)->when < last_coordinate) {
890 _events.erase (i.base());
896 _events.back()->when = last_coordinate;
897 _events.back()->value = last_val;
903 maybe_signal_changed ();
907 ControlList::truncate_start (double overall_length)
910 Glib::Mutex::Lock lm (_lock);
912 double first_legal_value;
913 double first_legal_coordinate;
915 assert(!_events.empty());
917 if (overall_length == _events.back()->when) {
918 /* no change in overall length */
922 if (overall_length > _events.back()->when) {
924 /* growing at front: duplicate first point. shift all others */
926 double shift = overall_length - _events.back()->when;
929 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
935 /* less than 2 points: add a new point */
936 _events.push_front (new ControlEvent (0, _events.front()->value));
940 /* more than 2 points: check to see if the first 2 values
941 are equal. if so, just move the position of the
942 first point. otherwise, add a new point.
945 iterator second = _events.begin();
946 ++second; /* points at the second point */
948 if (_events.front()->value == (*second)->value) {
949 /* first segment is flat, just move start point back to zero */
950 _events.front()->when = 0;
952 /* leave non-flat segment in place, add a new leading point. */
953 _events.push_front (new ControlEvent (0, _events.front()->value));
959 /* shrinking at front */
961 first_legal_coordinate = _events.back()->when - overall_length;
962 first_legal_value = unlocked_eval (first_legal_coordinate);
963 first_legal_value = max (_min_yval, first_legal_value);
964 first_legal_value = min (_max_yval, first_legal_value);
966 /* remove all events earlier than the new "front" */
970 while (i != _events.end() && !_events.empty()) {
971 ControlList::iterator tmp;
976 if ((*i)->when > first_legal_coordinate) {
986 /* shift all remaining points left to keep their same
990 for (i = _events.begin(); i != _events.end(); ++i) {
991 (*i)->when -= first_legal_coordinate;
994 /* add a new point for the interpolated new value */
996 _events.push_front (new ControlEvent (0, first_legal_value));
1002 maybe_signal_changed ();
1006 ControlList::unlocked_eval (double x) const
1008 pair<EventList::iterator,EventList::iterator> range;
1014 const_iterator length_check_iter = _events.begin();
1015 for (npoints = 0; npoints < 4; ++npoints, ++length_check_iter) {
1016 if (length_check_iter == _events.end()) {
1023 return _default_value;
1026 return _events.front()->value;
1029 if (x >= _events.back()->when) {
1030 return _events.back()->value;
1031 } else if (x <= _events.front()->when) {
1032 return _events.front()->value;
1035 lpos = _events.front()->when;
1036 lval = _events.front()->value;
1037 upos = _events.back()->when;
1038 uval = _events.back()->value;
1040 if (_interpolation == Discrete) {
1044 /* linear interpolation betweeen the two points */
1045 fraction = (double) (x - lpos) / (double) (upos - lpos);
1046 return lval + (fraction * (uval - lval));
1049 if (x >= _events.back()->when) {
1050 return _events.back()->value;
1051 } else if (x <= _events.front()->when) {
1052 return _events.front()->value;
1055 return multipoint_eval (x);
1058 /*NOTREACHED*/ /* stupid gcc */
1059 return _default_value;
1063 ControlList::multipoint_eval (double x) const
1069 /* "Stepped" lookup (no interpolation) */
1070 /* FIXME: no cache. significant? */
1071 if (_interpolation == Discrete) {
1072 const ControlEvent cp (x, 0);
1073 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
1075 // shouldn't have made it to multipoint_eval
1076 assert(i != _events.end());
1078 if (i == _events.begin() || (*i)->when == x)
1081 return (*(--i))->value;
1084 /* Only do the range lookup if x is in a different range than last time
1085 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
1086 if ((_lookup_cache.left < 0) ||
1087 ((_lookup_cache.left > x) ||
1088 (_lookup_cache.range.first == _events.end()) ||
1089 ((*_lookup_cache.range.second)->when < x))) {
1091 const ControlEvent cp (x, 0);
1093 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
1096 pair<const_iterator,const_iterator> range = _lookup_cache.range;
1098 if (range.first == range.second) {
1100 /* x does not exist within the list as a control point */
1102 _lookup_cache.left = x;
1104 if (range.first != _events.begin()) {
1106 lpos = (*range.first)->when;
1107 lval = (*range.first)->value;
1109 /* we're before the first point */
1110 // return _default_value;
1111 return _events.front()->value;
1114 if (range.second == _events.end()) {
1115 /* we're after the last point */
1116 return _events.back()->value;
1119 upos = (*range.second)->when;
1120 uval = (*range.second)->value;
1122 /* linear interpolation betweeen the two points
1126 fraction = (double) (x - lpos) / (double) (upos - lpos);
1127 return lval + (fraction * (uval - lval));
1131 /* x is a control point in the data */
1132 _lookup_cache.left = -1;
1133 return (*range.first)->value;
1137 ControlList::build_search_cache_if_necessary (double start) const
1139 /* Only do the range lookup if x is in a different range than last time
1140 * this was called (or if the search cache has been marked "dirty" (left<0) */
1141 if (!_events.empty() && ((_search_cache.left < 0) || (_search_cache.left > start))) {
1143 const ControlEvent start_point (start, 0);
1145 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1146 // << start << ".." << end << ")" << endl;
1148 _search_cache.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1149 _search_cache.left = start;
1153 /** Get the earliest event after \a start using the current interpolation style.
1155 * If an event is found, \a x and \a y are set to its coordinates.
1157 * \param inclusive Include events with timestamp exactly equal to \a start
1158 * \return true if event is found (and \a x and \a y are valid).
1161 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
1163 // FIXME: It would be nice if this was unnecessary..
1164 Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
1169 return rt_safe_earliest_event_unlocked (start, x, y, inclusive);
1173 /** Get the earliest event after \a start using the current interpolation style.
1175 * If an event is found, \a x and \a y are set to its coordinates.
1177 * \param inclusive Include events with timestamp exactly equal to \a start
1178 * \return true if event is found (and \a x and \a y are valid).
1181 ControlList::rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool inclusive) const
1183 if (_interpolation == Discrete) {
1184 return rt_safe_earliest_event_discrete_unlocked(start, x, y, inclusive);
1186 return rt_safe_earliest_event_linear_unlocked(start, x, y, inclusive);
1191 /** Get the earliest event after \a start without interpolation.
1193 * If an event is found, \a x and \a y are set to its coordinates.
1195 * \param inclusive Include events with timestamp exactly equal to \a start
1196 * \return true if event is found (and \a x and \a y are valid).
1199 ControlList::rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const
1201 build_search_cache_if_necessary (start);
1203 if (_search_cache.first != _events.end()) {
1204 const ControlEvent* const first = *_search_cache.first;
1206 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1208 /* Earliest points is in range, return it */
1214 /* Move left of cache to this point
1215 * (Optimize for immediate call this cycle within range) */
1216 _search_cache.left = x;
1217 ++_search_cache.first;
1226 /* No points in range */
1232 /** Get the earliest time the line crosses an integer (Linear interpolation).
1234 * If an event is found, \a x and \a y are set to its coordinates.
1236 * \param inclusive Include events with timestamp exactly equal to \a start
1237 * \return true if event is found (and \a x and \a y are valid).
1240 ControlList::rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const
1242 // cout << "earliest_event(start: " << start << ", x: " << x << ", y: " << y << ", inclusive: " << inclusive << ")" << endl;
1244 const_iterator length_check_iter = _events.begin();
1245 if (_events.empty()) { // 0 events
1247 } else if (_events.end() == ++length_check_iter) { // 1 event
1248 return rt_safe_earliest_event_discrete_unlocked (start, x, y, inclusive);
1251 // Hack to avoid infinitely repeating the same event
1252 build_search_cache_if_necessary (start);
1254 if (_search_cache.first != _events.end()) {
1256 const ControlEvent* first = NULL;
1257 const ControlEvent* next = NULL;
1259 /* Step is after first */
1260 if (_search_cache.first == _events.begin() || (*_search_cache.first)->when <= start) {
1261 first = *_search_cache.first;
1262 ++_search_cache.first;
1263 if (_search_cache.first == _events.end()) {
1266 next = *_search_cache.first;
1268 /* Step is before first */
1270 const_iterator prev = _search_cache.first;
1273 next = *_search_cache.first;
1276 if (inclusive && first->when == start) {
1279 /* Move left of cache to this point
1280 * (Optimize for immediate call this cycle within range) */
1281 _search_cache.left = x;
1282 //++_search_cache.range.first;
1287 if (fabs(first->value - next->value) <= 1) {
1288 if (next->when > start) {
1291 /* Move left of cache to this point
1292 * (Optimize for immediate call this cycle within range) */
1293 _search_cache.left = x;
1294 //++_search_cache.range.first;
1295 assert(inclusive ? x >= start : x > start);
1302 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1303 //cerr << "start y: " << start_y << endl;
1305 //y = first->value + (slope * fabs(start - first->when));
1308 if (first->value < next->value) // ramping up
1310 else // ramping down
1313 x = first->when + (y - first->value) / (double)slope;
1315 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1317 if (first->value < next->value) // ramping up
1319 else // ramping down
1322 x = first->when + (y - first->value) / (double)slope;
1325 /*cerr << first->value << " @ " << first->when << " ... "
1326 << next->value << " @ " << next->when
1327 << " = " << y << " @ " << x << endl;*/
1329 assert( (y >= first->value && y <= next->value)
1330 || (y <= first->value && y >= next->value) );
1333 const bool past_start = (inclusive ? x >= start : x > start);
1335 /* Move left of cache to this point
1336 * (Optimize for immediate call this cycle within range) */
1337 _search_cache.left = x;
1338 assert(inclusive ? x >= start : x > start);
1344 /* No points in the future, so no steps (towards them) in the future */
1351 /** @param start Start position in model coordinates.
1352 * @param end End position in model coordinates.
1353 * @param op 0 = cut, 1 = copy, 2 = clear.
1355 boost::shared_ptr<ControlList>
1356 ControlList::cut_copy_clear (double start, double end, int op)
1358 boost::shared_ptr<ControlList> nal = create (_parameter);
1360 ControlEvent cp (start, 0.0);
1363 Glib::Mutex::Lock lm (_lock);
1365 /* first, determine s & e, two iterators that define the range of points
1366 affected by this operation
1369 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1373 /* and the last that is at or after `end' */
1375 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1378 /* if "start" isn't the location of an existing point,
1379 evaluate the curve to get a value for the start. Add a point to
1380 both the existing event list, and if its not a "clear" operation,
1381 to the copy ("nal") as well.
1383 Note that the time positions of the points in each list are different
1384 because we want the copy ("nal") to have a zero time reference.
1388 /* before we begin any cut/clear operations, get the value of the curve
1392 double end_value = unlocked_eval (end);
1394 if ((*s)->when != start) {
1396 double val = unlocked_eval (start);
1398 if (op == 0) { // cut
1399 if (start > _events.front()->when) {
1400 _events.insert (s, (new ControlEvent (start, val)));
1404 if (op != 2) { // ! clear
1405 nal->_events.push_back (new ControlEvent (0, val));
1409 for (iterator x = s; x != e; ) {
1411 /* adjust new points to be relative to start, which
1412 has been set to zero.
1416 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1420 x = _events.erase (x);
1426 if (e == _events.end() || (*e)->when != end) {
1428 /* only add a boundary point if there is a point after "end"
1431 if (op == 0 && (e != _events.end() && end < (*e)->when)) { // cut
1432 _events.insert (e, new ControlEvent (end, end_value));
1435 if (op != 2 && (e != _events.end() && end < (*e)->when)) { // cut/copy
1436 nal->_events.push_back (new ControlEvent (end - start, end_value));
1444 maybe_signal_changed ();
1451 boost::shared_ptr<ControlList>
1452 ControlList::cut (double start, double end)
1454 return cut_copy_clear (start, end, 0);
1457 boost::shared_ptr<ControlList>
1458 ControlList::copy (double start, double end)
1460 return cut_copy_clear (start, end, 1);
1464 ControlList::clear (double start, double end)
1466 cut_copy_clear (start, end, 2);
1469 /** @param pos Position in model coordinates */
1471 ControlList::paste (ControlList& alist, double pos, float /*times*/)
1473 if (alist._events.empty()) {
1478 Glib::Mutex::Lock lm (_lock);
1482 ControlEvent cp (pos, 0.0);
1484 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1486 for (iterator i = alist.begin();i != alist.end(); ++i) {
1487 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1488 end = (*i)->when + pos;
1492 /* move all points after the insertion along the timeline by
1496 while (where != _events.end()) {
1498 if ((*where)->when <= end) {
1501 _events.erase(where);
1512 maybe_signal_changed ();
1516 /** Move automation around according to a list of region movements.
1517 * @param return true if anything was changed, otherwise false (ie nothing needed changing)
1520 ControlList::move_ranges (const list< RangeMove<double> >& movements)
1522 typedef list< RangeMove<double> > RangeMoveList;
1525 Glib::Mutex::Lock lm (_lock);
1527 /* a copy of the events list before we started moving stuff around */
1528 EventList old_events = _events;
1530 /* clear the source and destination ranges in the new list */
1531 bool things_erased = false;
1532 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1534 if (erase_range_internal (i->from, i->from + i->length, _events)) {
1535 things_erased = true;
1538 if (erase_range_internal (i->to, i->to + i->length, _events)) {
1539 things_erased = true;
1543 /* if nothing was erased, there is nothing to do */
1544 if (!things_erased) {
1548 /* copy the events into the new list */
1549 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1550 iterator j = old_events.begin ();
1551 const double limit = i->from + i->length;
1552 const double dx = i->to - i->from;
1553 while (j != old_events.end () && (*j)->when <= limit) {
1554 if ((*j)->when >= i->from) {
1555 ControlEvent* ev = new ControlEvent (**j);
1557 _events.push_back (ev);
1564 _events.sort (event_time_less_than);
1566 _sort_pending = true;
1572 maybe_signal_changed ();
1577 ControlList::set_interpolation (InterpolationStyle s)
1579 if (_interpolation == s) {
1584 InterpolationChanged (s); /* EMIT SIGNAL */
1588 ControlList::set_thinning_factor (double v)
1590 _thinning_factor = v;
1594 ControlList::operator!= (ControlList const & other) const
1596 if (_events.size() != other._events.size()) {
1600 EventList::const_iterator i = _events.begin ();
1601 EventList::const_iterator j = other._events.begin ();
1603 while (i != _events.end() && (*i)->when == (*j)->when && (*i)->value == (*j)->value) {
1608 if (i != _events.end ()) {
1613 _parameter != other._parameter ||
1614 _interpolation != other._interpolation ||
1615 _min_yval != other._min_yval ||
1616 _max_yval != other._max_yval ||
1617 _default_value != other._default_value
1621 } // namespace Evoral