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"
31 inline bool event_time_less_than (ControlEvent* a, ControlEvent* b)
33 return a->when < b->when;
36 /* this has no units but corresponds to the area of a rectangle
37 computed between three points in the list. If the area is
38 large, it indicates significant non-linearity between the
41 during automation recording we thin the recorded points
42 using this value. if a point is sufficiently co-linear
43 with its neighbours (as defined by the area of the rectangle
44 formed by three of them), we will not include it in the
45 ControlList. a smaller value will exclude less points,
46 a larger value will exclude more points, so it effectively
47 measures the amount of thinning to be done.
50 double ControlList::_thinning_factor = 20.0;
52 ControlList::ControlList (const Parameter& id)
54 , _interpolation(Linear)
58 _changed_when_thawed = false;
62 _lookup_cache.left = -1;
63 _lookup_cache.range.first = _events.end();
64 _search_cache.left = -1;
65 _search_cache.first = _events.end();
66 _sort_pending = false;
69 ControlList::ControlList (const ControlList& other)
70 : _parameter(other._parameter)
71 , _interpolation(Linear)
75 _changed_when_thawed = false;
76 _min_yval = other._min_yval;
77 _max_yval = other._max_yval;
78 _default_value = other._default_value;
79 _lookup_cache.range.first = _events.end();
80 _search_cache.first = _events.end();
81 _sort_pending = false;
83 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
84 _events.push_back (new ControlEvent (**i));
90 ControlList::ControlList (const ControlList& other, double start, double end)
91 : _parameter(other._parameter)
92 , _interpolation(Linear)
96 _changed_when_thawed = false;
97 _min_yval = other._min_yval;
98 _max_yval = other._max_yval;
99 _default_value = other._default_value;
100 _lookup_cache.range.first = _events.end();
101 _search_cache.first = _events.end();
102 _sort_pending = false;
104 /* now grab the relevant points, and shift them back if necessary */
106 boost::shared_ptr<ControlList> section = const_cast<ControlList*>(&other)->copy (start, end);
108 if (!section->empty()) {
109 for (iterator i = section->begin(); i != section->end(); ++i) {
110 _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
117 ControlList::~ControlList()
119 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
123 for (list<NascentInfo*>::iterator n = nascent.begin(); n != nascent.end(); ++n) {
124 for (EventList::iterator x = (*n)->events.begin(); x != (*n)->events.end(); ++x) {
133 boost::shared_ptr<ControlList>
134 ControlList::create(Parameter id)
136 return boost::shared_ptr<ControlList>(new ControlList(id));
140 ControlList::operator== (const ControlList& other)
142 return _events == other._events;
146 ControlList::operator= (const ControlList& other)
148 if (this != &other) {
152 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
153 _events.push_back (new ControlEvent (**i));
156 _min_yval = other._min_yval;
157 _max_yval = other._max_yval;
158 _default_value = other._default_value;
161 maybe_signal_changed ();
168 ControlList::create_curve()
170 _curve = new Curve(*this);
174 ControlList::destroy_curve()
181 ControlList::maybe_signal_changed ()
186 _changed_when_thawed = true;
191 ControlList::clear ()
194 Glib::Mutex::Lock lm (_lock);
199 maybe_signal_changed ();
203 ControlList::x_scale (double factor)
205 Glib::Mutex::Lock lm (_lock);
210 ControlList::extend_to (double when)
212 Glib::Mutex::Lock lm (_lock);
213 if (_events.empty() || _events.back()->when == when) {
216 double factor = when / _events.back()->when;
222 ControlList::_x_scale (double factor)
224 for (iterator i = _events.begin(); i != _events.end(); ++i) {
225 (*i)->when *= factor;
232 ControlList::write_pass_finished (double when)
234 merge_nascent (when);
238 struct ControlEventTimeComparator {
239 bool operator() (ControlEvent* a, ControlEvent* b) {
240 return a->when < b->when;
245 ControlList::merge_nascent (double when)
248 Glib::Mutex::Lock lm (_lock);
250 if (nascent.empty()) {
254 for (list<NascentInfo*>::iterator n = nascent.begin(); n != nascent.end(); ++n) {
256 NascentInfo* ninfo = *n;
257 EventList& nascent_events (ninfo->events);
258 bool need_adjacent_start_clamp;
259 bool need_adjacent_end_clamp;
261 if (nascent_events.empty()) {
266 nascent_events.sort (ControlEventTimeComparator ());
268 if (ninfo->start_time < 0.0) {
269 ninfo->start_time = nascent_events.front()->when;
272 if (ninfo->end_time < 0.0) {
273 ninfo->end_time = when;
276 bool preexisting = !_events.empty();
280 _events = nascent_events;
282 } else if (ninfo->end_time < _events.front()->when) {
284 /* all points in nascent are before the first existing point */
286 _events.insert (_events.begin(), nascent_events.begin(), nascent_events.end());
288 } else if (ninfo->start_time > _events.back()->when) {
290 /* all points in nascent are after the last existing point */
292 _events.insert (_events.end(), nascent_events.begin(), nascent_events.end());
296 /* find the range that overlaps with nascent events,
297 and insert the contents of nascent events.
301 iterator range_begin = _events.end();
302 iterator range_end = _events.end();
303 double end_value = unlocked_eval (ninfo->end_time);
304 double start_value = unlocked_eval (ninfo->start_time - 1);
306 need_adjacent_end_clamp = true;
307 need_adjacent_start_clamp = true;
309 for (i = _events.begin(); i != _events.end(); ++i) {
311 if ((*i)->when == ninfo->start_time) {
312 /* existing point at same time, remove it
313 and the consider the next point instead.
315 i = _events.erase (i);
317 if (i == _events.end()) {
321 if (range_begin == _events.end()) {
323 need_adjacent_start_clamp = false;
325 need_adjacent_end_clamp = false;
328 if ((*i)->when > ninfo->end_time) {
333 } else if ((*i)->when > ninfo->start_time) {
335 if (range_begin == _events.end()) {
339 if ((*i)->when > ninfo->end_time) {
347 range_begin is the first event on our list after the first nascent event
348 range_end is the first event on our list after the last nascent event
350 range_begin may be equal to _events.end() iff the last event on our list
351 was at the same time as the first nascent event.
354 if (range_begin != _events.begin()) {
355 /* clamp point before */
356 if (need_adjacent_start_clamp) {
357 _events.insert (range_begin, new ControlEvent (ninfo->start_time, start_value));
361 _events.insert (range_begin, nascent_events.begin(), nascent_events.end());
363 if (range_end != _events.end()) {
364 /* clamp point after */
365 if (need_adjacent_end_clamp) {
366 _events.insert (range_begin, new ControlEvent (ninfo->end_time, end_value));
370 _events.erase (range_begin, range_end);
379 nascent.push_back (new NascentInfo ());
383 maybe_signal_changed ();
387 ControlList::rt_add (double when, double value)
389 // this is for automation recording
391 if (touch_enabled() && !touching()) {
395 //cerr << "RT: alist " << this << " add " << value << " @ " << when << endl;
397 Glib::Mutex::Lock lm (_lock, Glib::TRY_LOCK);
400 assert (!nascent.empty());
401 /* we don't worry about adding events out of time order as we will
402 sort them in merge_nascent.
405 EventList& el (nascent.back()->events);
407 if (el.size() > 1 && (when >= el.back()->when) && (value == el.back()->value)) {
408 /* same value, later timestamp, effective slope is
409 * zero, so just move the last point in nascent to our
410 * new time position. this avoids storing an unlimited
411 * number of points to represent a flat line.
413 el.back()->when = when;
415 nascent.back()->events.push_back (new ControlEvent (when, value));
423 Glib::Mutex::Lock lm (_lock);
425 ControlEvent* prevprev;
431 for (iterator i = _events.begin(); i != _events.end(); ++i) {
438 double area = fabs ((prevprev->when * (prev->value - cur->value)) +
439 (prev->when * (cur->value - prevprev->value)) +
440 (cur->when * (prevprev->value - prev->value)));
442 if (area < _thinning_factor) {
443 iterator tmp = pprev;
445 /* pprev will change to current
446 i is incremented to the next event
463 ControlList::fast_simple_add (double when, double value)
465 /* to be used only for loading pre-sorted data from saved state */
466 _events.insert (_events.end(), new ControlEvent (when, value));
467 assert(_events.back());
473 ControlList::add (double when, double value)
475 /* this is for making changes from some kind of user interface or
476 control surface (GUI, MIDI, OSC etc)
479 if (!clamp_value (when, value)) {
484 Glib::Mutex::Lock lm (_lock);
485 ControlEvent cp (when, 0.0f);
487 iterator insertion_point;
489 for (insertion_point = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); insertion_point != _events.end(); ++insertion_point) {
491 /* only one point allowed per time point */
493 if ((*insertion_point)->when == when) {
494 (*insertion_point)->value = value;
499 if ((*insertion_point)->when >= when) {
506 _events.insert (insertion_point, new ControlEvent (when, value));
513 maybe_signal_changed ();
517 ControlList::erase (iterator i)
520 Glib::Mutex::Lock lm (_lock);
524 maybe_signal_changed ();
528 ControlList::erase (iterator start, iterator end)
531 Glib::Mutex::Lock lm (_lock);
532 _events.erase (start, end);
535 maybe_signal_changed ();
538 /** Erase the first event which matches the given time and value */
540 ControlList::erase (double when, double value)
543 Glib::Mutex::Lock lm (_lock);
545 iterator i = begin ();
546 while (i != end() && ((*i)->when != when || (*i)->value != value)) {
557 maybe_signal_changed ();
561 ControlList::reset_range (double start, double endt)
566 Glib::Mutex::Lock lm (_lock);
567 ControlEvent cp (start, 0.0f);
571 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) != _events.end()) {
574 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
576 for (iterator i = s; i != e; ++i) {
577 (*i)->value = _default_value;
587 maybe_signal_changed ();
592 ControlList::erase_range (double start, double endt)
597 Glib::Mutex::Lock lm (_lock);
598 erased = erase_range_internal (start, endt, _events);
607 maybe_signal_changed ();
612 ControlList::erase_range_internal (double start, double endt, EventList & events)
615 ControlEvent cp (start, 0.0f);
619 if ((s = lower_bound (events.begin(), events.end(), &cp, time_comparator)) != events.end()) {
621 e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
632 ControlList::slide (iterator before, double distance)
635 Glib::Mutex::Lock lm (_lock);
637 if (before == _events.end()) {
641 while (before != _events.end()) {
642 (*before)->when += distance;
649 maybe_signal_changed ();
653 ControlList::shift (double pos, double frames)
656 Glib::Mutex::Lock lm (_lock);
658 for (iterator i = _events.begin(); i != _events.end(); ++i) {
659 if ((*i)->when >= pos) {
660 (*i)->when += frames;
667 maybe_signal_changed ();
671 ControlList::modify (iterator iter, double when, double val)
673 /* note: we assume higher level logic is in place to avoid this
674 reordering the time-order of control events in the list. ie. all
675 points after *iter are later than when.
679 Glib::Mutex::Lock lm (_lock);
681 (*iter)->when = when;
682 (*iter)->value = val;
684 if (std::isnan (val)) {
689 _events.sort (event_time_less_than);
691 _sort_pending = true;
697 maybe_signal_changed ();
700 std::pair<ControlList::iterator,ControlList::iterator>
701 ControlList::control_points_adjacent (double xval)
703 Glib::Mutex::Lock lm (_lock);
705 ControlEvent cp (xval, 0.0f);
706 std::pair<iterator,iterator> ret;
708 ret.first = _events.end();
709 ret.second = _events.end();
711 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
713 if (ret.first == _events.end()) {
714 if ((*i)->when >= xval) {
715 if (i != _events.begin()) {
724 if ((*i)->when > xval) {
734 ControlList::freeze ()
749 Glib::Mutex::Lock lm (_lock);
752 _events.sort (event_time_less_than);
753 _sort_pending = false;
759 ControlList::mark_dirty () const
761 _lookup_cache.left = -1;
762 _search_cache.left = -1;
765 _curve->mark_dirty();
768 Dirty (); /* EMIT SIGNAL */
772 ControlList::truncate_end (double last_coordinate)
775 Glib::Mutex::Lock lm (_lock);
776 ControlEvent cp (last_coordinate, 0);
777 ControlList::reverse_iterator i;
780 if (_events.empty()) {
784 if (last_coordinate == _events.back()->when) {
788 if (last_coordinate > _events.back()->when) {
793 iterator foo = _events.begin();
796 if (foo == _events.end()) {
798 } else if (++foo == _events.end()) {
805 /* less than 2 points: add a new point */
806 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
809 /* more than 2 points: check to see if the last 2 values
810 are equal. if so, just move the position of the
811 last point. otherwise, add a new point.
814 iterator penultimate = _events.end();
815 --penultimate; /* points at last point */
816 --penultimate; /* points at the penultimate point */
818 if (_events.back()->value == (*penultimate)->value) {
819 _events.back()->when = last_coordinate;
821 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
829 last_val = unlocked_eval (last_coordinate);
830 last_val = max ((double) _min_yval, last_val);
831 last_val = min ((double) _max_yval, last_val);
833 i = _events.rbegin();
835 /* make i point to the last control point */
839 /* now go backwards, removing control points that are
840 beyond the new last coordinate.
843 // FIXME: SLOW! (size() == O(n))
845 uint32_t sz = _events.size();
847 while (i != _events.rend() && sz > 2) {
848 ControlList::reverse_iterator tmp;
853 if ((*i)->when < last_coordinate) {
857 _events.erase (i.base());
863 _events.back()->when = last_coordinate;
864 _events.back()->value = last_val;
870 maybe_signal_changed ();
874 ControlList::truncate_start (double overall_length)
877 Glib::Mutex::Lock lm (_lock);
879 double first_legal_value;
880 double first_legal_coordinate;
882 assert(!_events.empty());
884 if (overall_length == _events.back()->when) {
885 /* no change in overall length */
889 if (overall_length > _events.back()->when) {
891 /* growing at front: duplicate first point. shift all others */
893 double shift = overall_length - _events.back()->when;
896 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
902 /* less than 2 points: add a new point */
903 _events.push_front (new ControlEvent (0, _events.front()->value));
907 /* more than 2 points: check to see if the first 2 values
908 are equal. if so, just move the position of the
909 first point. otherwise, add a new point.
912 iterator second = _events.begin();
913 ++second; /* points at the second point */
915 if (_events.front()->value == (*second)->value) {
916 /* first segment is flat, just move start point back to zero */
917 _events.front()->when = 0;
919 /* leave non-flat segment in place, add a new leading point. */
920 _events.push_front (new ControlEvent (0, _events.front()->value));
926 /* shrinking at front */
928 first_legal_coordinate = _events.back()->when - overall_length;
929 first_legal_value = unlocked_eval (first_legal_coordinate);
930 first_legal_value = max (_min_yval, first_legal_value);
931 first_legal_value = min (_max_yval, first_legal_value);
933 /* remove all events earlier than the new "front" */
937 while (i != _events.end() && !_events.empty()) {
938 ControlList::iterator tmp;
943 if ((*i)->when > first_legal_coordinate) {
953 /* shift all remaining points left to keep their same
957 for (i = _events.begin(); i != _events.end(); ++i) {
958 (*i)->when -= first_legal_coordinate;
961 /* add a new point for the interpolated new value */
963 _events.push_front (new ControlEvent (0, first_legal_value));
969 maybe_signal_changed ();
973 ControlList::unlocked_eval (double x) const
975 pair<EventList::iterator,EventList::iterator> range;
981 const_iterator length_check_iter = _events.begin();
982 for (npoints = 0; npoints < 4; ++npoints, ++length_check_iter) {
983 if (length_check_iter == _events.end()) {
990 return _default_value;
993 return _events.front()->value;
996 if (x >= _events.back()->when) {
997 return _events.back()->value;
998 } else if (x <= _events.front()->when) {
999 return _events.front()->value;
1002 lpos = _events.front()->when;
1003 lval = _events.front()->value;
1004 upos = _events.back()->when;
1005 uval = _events.back()->value;
1007 if (_interpolation == Discrete) {
1011 /* linear interpolation betweeen the two points */
1012 fraction = (double) (x - lpos) / (double) (upos - lpos);
1013 return lval + (fraction * (uval - lval));
1016 if (x >= _events.back()->when) {
1017 return _events.back()->value;
1018 } else if (x <= _events.front()->when) {
1019 return _events.front()->value;
1022 return multipoint_eval (x);
1025 /*NOTREACHED*/ /* stupid gcc */
1026 return _default_value;
1030 ControlList::multipoint_eval (double x) const
1036 /* "Stepped" lookup (no interpolation) */
1037 /* FIXME: no cache. significant? */
1038 if (_interpolation == Discrete) {
1039 const ControlEvent cp (x, 0);
1040 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
1042 // shouldn't have made it to multipoint_eval
1043 assert(i != _events.end());
1045 if (i == _events.begin() || (*i)->when == x)
1048 return (*(--i))->value;
1051 /* Only do the range lookup if x is in a different range than last time
1052 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
1053 if ((_lookup_cache.left < 0) ||
1054 ((_lookup_cache.left > x) ||
1055 (_lookup_cache.range.first == _events.end()) ||
1056 ((*_lookup_cache.range.second)->when < x))) {
1058 const ControlEvent cp (x, 0);
1060 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
1063 pair<const_iterator,const_iterator> range = _lookup_cache.range;
1065 if (range.first == range.second) {
1067 /* x does not exist within the list as a control point */
1069 _lookup_cache.left = x;
1071 if (range.first != _events.begin()) {
1073 lpos = (*range.first)->when;
1074 lval = (*range.first)->value;
1076 /* we're before the first point */
1077 // return _default_value;
1078 return _events.front()->value;
1081 if (range.second == _events.end()) {
1082 /* we're after the last point */
1083 return _events.back()->value;
1086 upos = (*range.second)->when;
1087 uval = (*range.second)->value;
1089 /* linear interpolation betweeen the two points
1093 fraction = (double) (x - lpos) / (double) (upos - lpos);
1094 return lval + (fraction * (uval - lval));
1098 /* x is a control point in the data */
1099 _lookup_cache.left = -1;
1100 return (*range.first)->value;
1104 ControlList::build_search_cache_if_necessary (double start) const
1106 /* Only do the range lookup if x is in a different range than last time
1107 * this was called (or if the search cache has been marked "dirty" (left<0) */
1108 if (!_events.empty() && ((_search_cache.left < 0) || (_search_cache.left > start))) {
1110 const ControlEvent start_point (start, 0);
1112 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1113 // << start << ".." << end << ")" << endl;
1115 _search_cache.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1116 _search_cache.left = start;
1120 /** Get the earliest event after \a start using the current interpolation style.
1122 * If an event is found, \a x and \a y are set to its coordinates.
1124 * \param inclusive Include events with timestamp exactly equal to \a start
1125 * \return true if event is found (and \a x and \a y are valid).
1128 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
1130 // FIXME: It would be nice if this was unnecessary..
1131 Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
1136 return rt_safe_earliest_event_unlocked (start, x, y, inclusive);
1140 /** Get the earliest event after \a start using the current interpolation style.
1142 * If an event is found, \a x and \a y are set to its coordinates.
1144 * \param inclusive Include events with timestamp exactly equal to \a start
1145 * \return true if event is found (and \a x and \a y are valid).
1148 ControlList::rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool inclusive) const
1150 if (_interpolation == Discrete) {
1151 return rt_safe_earliest_event_discrete_unlocked(start, x, y, inclusive);
1153 return rt_safe_earliest_event_linear_unlocked(start, x, y, inclusive);
1158 /** Get the earliest event after \a start without interpolation.
1160 * If an event is found, \a x and \a y are set to its coordinates.
1162 * \param inclusive Include events with timestamp exactly equal to \a start
1163 * \return true if event is found (and \a x and \a y are valid).
1166 ControlList::rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const
1168 build_search_cache_if_necessary (start);
1170 if (_search_cache.first != _events.end()) {
1171 const ControlEvent* const first = *_search_cache.first;
1173 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1175 /* Earliest points is in range, return it */
1181 /* Move left of cache to this point
1182 * (Optimize for immediate call this cycle within range) */
1183 _search_cache.left = x;
1184 ++_search_cache.first;
1193 /* No points in range */
1199 /** Get the earliest time the line crosses an integer (Linear interpolation).
1201 * If an event is found, \a x and \a y are set to its coordinates.
1203 * \param inclusive Include events with timestamp exactly equal to \a start
1204 * \return true if event is found (and \a x and \a y are valid).
1207 ControlList::rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const
1209 // cout << "earliest_event(start: " << start << ", x: " << x << ", y: " << y << ", inclusive: " << inclusive << ")" << endl;
1211 const_iterator length_check_iter = _events.begin();
1212 if (_events.empty()) { // 0 events
1214 } else if (_events.end() == ++length_check_iter) { // 1 event
1215 return rt_safe_earliest_event_discrete_unlocked (start, x, y, inclusive);
1218 // Hack to avoid infinitely repeating the same event
1219 build_search_cache_if_necessary (start);
1221 if (_search_cache.first != _events.end()) {
1223 const ControlEvent* first = NULL;
1224 const ControlEvent* next = NULL;
1226 /* Step is after first */
1227 if (_search_cache.first == _events.begin() || (*_search_cache.first)->when <= start) {
1228 first = *_search_cache.first;
1229 ++_search_cache.first;
1230 if (_search_cache.first == _events.end()) {
1233 next = *_search_cache.first;
1235 /* Step is before first */
1237 const_iterator prev = _search_cache.first;
1240 next = *_search_cache.first;
1243 if (inclusive && first->when == start) {
1246 /* Move left of cache to this point
1247 * (Optimize for immediate call this cycle within range) */
1248 _search_cache.left = x;
1249 //++_search_cache.range.first;
1254 if (fabs(first->value - next->value) <= 1) {
1255 if (next->when > start) {
1258 /* Move left of cache to this point
1259 * (Optimize for immediate call this cycle within range) */
1260 _search_cache.left = x;
1261 //++_search_cache.range.first;
1262 assert(inclusive ? x >= start : x > start);
1269 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1270 //cerr << "start y: " << start_y << endl;
1272 //y = first->value + (slope * fabs(start - first->when));
1275 if (first->value < next->value) // ramping up
1277 else // ramping down
1280 x = first->when + (y - first->value) / (double)slope;
1282 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1284 if (first->value < next->value) // ramping up
1286 else // ramping down
1289 x = first->when + (y - first->value) / (double)slope;
1292 /*cerr << first->value << " @ " << first->when << " ... "
1293 << next->value << " @ " << next->when
1294 << " = " << y << " @ " << x << endl;*/
1296 assert( (y >= first->value && y <= next->value)
1297 || (y <= first->value && y >= next->value) );
1300 const bool past_start = (inclusive ? x >= start : x > start);
1302 /* Move left of cache to this point
1303 * (Optimize for immediate call this cycle within range) */
1304 _search_cache.left = x;
1305 assert(inclusive ? x >= start : x > start);
1311 /* No points in the future, so no steps (towards them) in the future */
1318 /** @param start Start position in model coordinates.
1319 * @param end End position in model coordinates.
1320 * @param op 0 = cut, 1 = copy, 2 = clear.
1322 boost::shared_ptr<ControlList>
1323 ControlList::cut_copy_clear (double start, double end, int op)
1325 boost::shared_ptr<ControlList> nal = create (_parameter);
1327 ControlEvent cp (start, 0.0);
1330 Glib::Mutex::Lock lm (_lock);
1332 /* first, determine s & e, two iterators that define the range of points
1333 affected by this operation
1336 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1340 /* and the last that is at or after `end' */
1342 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1345 /* if "start" isn't the location of an existing point,
1346 evaluate the curve to get a value for the start. Add a point to
1347 both the existing event list, and if its not a "clear" operation,
1348 to the copy ("nal") as well.
1350 Note that the time positions of the points in each list are different
1351 because we want the copy ("nal") to have a zero time reference.
1355 /* before we begin any cut/clear operations, get the value of the curve
1359 double end_value = unlocked_eval (end);
1361 if ((*s)->when != start) {
1363 double val = unlocked_eval (start);
1365 if (op == 0) { // cut
1366 if (start > _events.front()->when) {
1367 _events.insert (s, (new ControlEvent (start, val)));
1371 if (op != 2) { // ! clear
1372 nal->_events.push_back (new ControlEvent (0, val));
1376 for (iterator x = s; x != e; ) {
1378 /* adjust new points to be relative to start, which
1379 has been set to zero.
1383 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1387 x = _events.erase (x);
1393 if (e == _events.end() || (*e)->when != end) {
1395 /* only add a boundary point if there is a point after "end"
1398 if (op == 0 && (e != _events.end() && end < (*e)->when)) { // cut
1399 _events.insert (e, new ControlEvent (end, end_value));
1402 if (op != 2 && (e != _events.end() && end < (*e)->when)) { // cut/copy
1403 nal->_events.push_back (new ControlEvent (end - start, end_value));
1411 maybe_signal_changed ();
1418 boost::shared_ptr<ControlList>
1419 ControlList::cut (double start, double end)
1421 return cut_copy_clear (start, end, 0);
1424 boost::shared_ptr<ControlList>
1425 ControlList::copy (double start, double end)
1427 return cut_copy_clear (start, end, 1);
1431 ControlList::clear (double start, double end)
1433 cut_copy_clear (start, end, 2);
1436 /** @param pos Position in model coordinates */
1438 ControlList::paste (ControlList& alist, double pos, float /*times*/)
1440 if (alist._events.empty()) {
1445 Glib::Mutex::Lock lm (_lock);
1449 ControlEvent cp (pos, 0.0);
1451 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1453 for (iterator i = alist.begin();i != alist.end(); ++i) {
1454 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1455 end = (*i)->when + pos;
1459 /* move all points after the insertion along the timeline by
1463 while (where != _events.end()) {
1465 if ((*where)->when <= end) {
1468 _events.erase(where);
1479 maybe_signal_changed ();
1483 /** Move automation around according to a list of region movements.
1484 * @param return true if anything was changed, otherwise false (ie nothing needed changing)
1487 ControlList::move_ranges (const list< RangeMove<double> >& movements)
1489 typedef list< RangeMove<double> > RangeMoveList;
1492 Glib::Mutex::Lock lm (_lock);
1494 /* a copy of the events list before we started moving stuff around */
1495 EventList old_events = _events;
1497 /* clear the source and destination ranges in the new list */
1498 bool things_erased = false;
1499 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1501 if (erase_range_internal (i->from, i->from + i->length, _events)) {
1502 things_erased = true;
1505 if (erase_range_internal (i->to, i->to + i->length, _events)) {
1506 things_erased = true;
1510 /* if nothing was erased, there is nothing to do */
1511 if (!things_erased) {
1515 /* copy the events into the new list */
1516 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1517 iterator j = old_events.begin ();
1518 const double limit = i->from + i->length;
1519 const double dx = i->to - i->from;
1520 while (j != old_events.end () && (*j)->when <= limit) {
1521 if ((*j)->when >= i->from) {
1522 ControlEvent* ev = new ControlEvent (**j);
1524 _events.push_back (ev);
1531 _events.sort (event_time_less_than);
1533 _sort_pending = true;
1539 maybe_signal_changed ();
1544 ControlList::set_interpolation (InterpolationStyle s)
1546 if (_interpolation == s) {
1551 InterpolationChanged (s); /* EMIT SIGNAL */
1555 ControlList::set_thinning_factor (double v)
1557 _thinning_factor = v;
1560 } // namespace Evoral