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;
37 ControlList::ControlList (const Parameter& id)
39 , _interpolation(Linear)
43 _changed_when_thawed = false;
46 _max_xval = 0; // means "no limit"
48 _lookup_cache.left = -1;
49 _lookup_cache.range.first = _events.end();
50 _search_cache.left = -1;
51 _search_cache.first = _events.end();
52 _sort_pending = false;
55 ControlList::ControlList (const ControlList& other)
56 : _parameter(other._parameter)
57 , _interpolation(Linear)
61 _changed_when_thawed = false;
62 _min_yval = other._min_yval;
63 _max_yval = other._max_yval;
64 _max_xval = other._max_xval;
65 _default_value = other._default_value;
66 _lookup_cache.range.first = _events.end();
67 _search_cache.first = _events.end();
68 _sort_pending = false;
70 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
71 _events.push_back (new ControlEvent (**i));
77 ControlList::ControlList (const ControlList& other, double start, double end)
78 : _parameter(other._parameter)
79 , _interpolation(Linear)
83 _changed_when_thawed = false;
84 _min_yval = other._min_yval;
85 _max_yval = other._max_yval;
86 _max_xval = other._max_xval;
87 _default_value = other._default_value;
88 _lookup_cache.range.first = _events.end();
89 _search_cache.first = _events.end();
90 _sort_pending = false;
92 /* now grab the relevant points, and shift them back if necessary */
94 boost::shared_ptr<ControlList> section = const_cast<ControlList*>(&other)->copy (start, end);
96 if (!section->empty()) {
97 for (iterator i = section->begin(); i != section->end(); ++i) {
98 _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
105 ControlList::~ControlList()
107 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
111 for (list<NascentInfo*>::iterator n = nascent.begin(); n != nascent.end(); ++n) {
112 for (EventList::iterator x = (*n)->events.begin(); x != (*n)->events.end(); ++x) {
121 boost::shared_ptr<ControlList>
122 ControlList::create(Parameter id)
124 return boost::shared_ptr<ControlList>(new ControlList(id));
128 ControlList::operator== (const ControlList& other)
130 return _events == other._events;
134 ControlList::operator= (const ControlList& other)
136 if (this != &other) {
140 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
141 _events.push_back (new ControlEvent (**i));
144 _min_yval = other._min_yval;
145 _max_yval = other._max_yval;
146 _max_xval = other._max_xval;
147 _default_value = other._default_value;
150 maybe_signal_changed ();
157 ControlList::create_curve()
159 _curve = new Curve(*this);
163 ControlList::destroy_curve()
170 ControlList::maybe_signal_changed ()
175 _changed_when_thawed = true;
180 ControlList::clear ()
183 Glib::Mutex::Lock lm (_lock);
188 maybe_signal_changed ();
192 ControlList::x_scale (double factor)
194 Glib::Mutex::Lock lm (_lock);
199 ControlList::extend_to (double when)
201 Glib::Mutex::Lock lm (_lock);
202 if (_events.empty() || _events.back()->when == when) {
205 double factor = when / _events.back()->when;
211 ControlList::_x_scale (double factor)
213 for (iterator i = _events.begin(); i != _events.end(); ++i) {
214 (*i)->when = floor ((*i)->when * factor);
221 ControlList::write_pass_finished (double when)
223 merge_nascent (when);
227 struct ControlEventTimeComparator {
228 bool operator() (ControlEvent* a, ControlEvent* b) {
229 return a->when < b->when;
234 ControlList::merge_nascent (double when)
237 Glib::Mutex::Lock lm (_lock);
239 if (nascent.empty()) {
243 for (list<NascentInfo*>::iterator n = nascent.begin(); n != nascent.end(); ++n) {
245 NascentInfo* ninfo = *n;
246 EventList& nascent_events (ninfo->events);
247 bool need_adjacent_start_clamp;
248 bool need_adjacent_end_clamp;
250 if (nascent_events.empty()) {
255 nascent_events.sort (ControlEventTimeComparator ());
257 if (ninfo->start_time < 0.0) {
258 ninfo->start_time = nascent_events.front()->when;
261 if (ninfo->end_time < 0.0) {
262 ninfo->end_time = when;
265 bool preexisting = !_events.empty();
269 _events = nascent_events;
271 } else if (ninfo->end_time < _events.front()->when) {
273 /* all points in nascent are before the first existing point */
275 _events.insert (_events.begin(), nascent_events.begin(), nascent_events.end());
277 } else if (ninfo->start_time > _events.back()->when) {
279 /* all points in nascent are after the last existing point */
281 _events.insert (_events.end(), nascent_events.begin(), nascent_events.end());
285 /* find the range that overlaps with nascent events,
286 and insert the contents of nascent events.
290 iterator range_begin = _events.end();
291 iterator range_end = _events.end();
292 double end_value = unlocked_eval (ninfo->end_time);
293 double start_value = unlocked_eval (ninfo->start_time - 1);
295 need_adjacent_end_clamp = true;
296 need_adjacent_start_clamp = true;
298 for (i = _events.begin(); i != _events.end(); ++i) {
300 if ((*i)->when == ninfo->start_time) {
301 /* existing point at same time, remove it
302 and the consider the next point instead.
304 i = _events.erase (i);
306 if (i == _events.end()) {
310 if (range_begin == _events.end()) {
312 need_adjacent_start_clamp = false;
314 need_adjacent_end_clamp = false;
317 if ((*i)->when > ninfo->end_time) {
322 } else if ((*i)->when > ninfo->start_time) {
324 if (range_begin == _events.end()) {
328 if ((*i)->when > ninfo->end_time) {
336 range_begin is the first event on our list after the first nascent event
337 range_end is the first event on our list after the last nascent event
339 range_begin may be equal to _events.end() iff the last event on our list
340 was at the same time as the first nascent event.
343 if (range_begin != _events.begin()) {
344 /* clamp point before */
345 if (need_adjacent_start_clamp) {
346 _events.insert (range_begin, new ControlEvent (ninfo->start_time, start_value));
350 _events.insert (range_begin, nascent_events.begin(), nascent_events.end());
352 if (range_end != _events.end()) {
353 /* clamp point after */
354 if (need_adjacent_end_clamp) {
355 _events.insert (range_begin, new ControlEvent (ninfo->end_time, end_value));
359 _events.erase (range_begin, range_end);
368 nascent.push_back (new NascentInfo ());
372 maybe_signal_changed ();
376 ControlList::rt_add (double when, double value)
378 // this is for automation recording
380 if (touch_enabled() && !touching()) {
384 //cerr << "RT: alist " << this << " add " << value << " @ " << when << endl;
386 Glib::Mutex::Lock lm (_lock, Glib::TRY_LOCK);
389 assert (!nascent.empty());
390 /* we don't worry about adding events out of time order as we will
391 sort them in merge_nascent.
394 EventList& el (nascent.back()->events);
396 if (el.size() > 1 && (when >= el.back()->when) && (value == el.back()->value)) {
397 /* same value, later timestamp, effective slope is
398 * zero, so just move the last point in nascent to our
399 * new time position. this avoids storing an unlimited
400 * number of points to represent a flat line.
402 el.back()->when = when;
404 nascent.back()->events.push_back (new ControlEvent (when, value));
412 Glib::Mutex::Lock lm (_lock);
414 ControlEvent* prevprev;
420 for (iterator i = _events.begin(); i != _events.end(); ++i) {
427 double area = fabs (0.5 *
428 (prevprev->when * (prev->value - cur->value)) +
429 (prev->when * (cur->value - prevprev->value)) +
430 (cur->when * (prevprev->value - prev->value)));
432 /* the number 10.0 is an arbitrary value that needs to
433 * be controlled by some user-controllable
434 * configuration utility.
438 iterator tmp = pprev;
440 /* pprev will change to current
441 i is incremented to the next event
458 ControlList::fast_simple_add (double when, double value)
460 /* to be used only for loading pre-sorted data from saved state */
461 _events.insert (_events.end(), new ControlEvent (when, value));
462 assert(_events.back());
466 ControlList::add (double when, double value)
468 /* this is for making changes from some kind of user interface or
469 control surface (GUI, MIDI, OSC etc)
472 if (!clamp_value (when, value)) {
477 Glib::Mutex::Lock lm (_lock);
478 ControlEvent cp (when, 0.0f);
480 iterator insertion_point;
482 for (insertion_point = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); insertion_point != _events.end(); ++insertion_point) {
484 /* only one point allowed per time point */
486 if ((*insertion_point)->when == when) {
487 (*insertion_point)->value = value;
492 if ((*insertion_point)->when >= when) {
499 _events.insert (insertion_point, new ControlEvent (when, value));
506 maybe_signal_changed ();
510 ControlList::erase (iterator i)
513 Glib::Mutex::Lock lm (_lock);
517 maybe_signal_changed ();
521 ControlList::erase (iterator start, iterator end)
524 Glib::Mutex::Lock lm (_lock);
525 _events.erase (start, end);
528 maybe_signal_changed ();
531 /** Erase the first event which matches the given time and value */
533 ControlList::erase (double when, double value)
536 Glib::Mutex::Lock lm (_lock);
538 iterator i = begin ();
539 while (i != end() && ((*i)->when != when || (*i)->value != value)) {
550 maybe_signal_changed ();
554 ControlList::reset_range (double start, double endt)
559 Glib::Mutex::Lock lm (_lock);
560 ControlEvent cp (start, 0.0f);
564 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) != _events.end()) {
567 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
569 for (iterator i = s; i != e; ++i) {
570 (*i)->value = _default_value;
580 maybe_signal_changed ();
585 ControlList::erase_range (double start, double endt)
590 Glib::Mutex::Lock lm (_lock);
591 erased = erase_range_internal (start, endt, _events);
600 maybe_signal_changed ();
605 ControlList::erase_range_internal (double start, double endt, EventList & events)
608 ControlEvent cp (start, 0.0f);
612 if ((s = lower_bound (events.begin(), events.end(), &cp, time_comparator)) != events.end()) {
614 e = upper_bound (events.begin(), events.end(), &cp, time_comparator);
625 ControlList::slide (iterator before, double distance)
628 Glib::Mutex::Lock lm (_lock);
630 if (before == _events.end()) {
634 while (before != _events.end()) {
635 (*before)->when += distance;
642 maybe_signal_changed ();
646 ControlList::shift (double pos, double frames)
649 Glib::Mutex::Lock lm (_lock);
651 for (iterator i = _events.begin(); i != _events.end(); ++i) {
652 if ((*i)->when >= pos) {
653 (*i)->when += frames;
660 maybe_signal_changed ();
664 ControlList::modify (iterator iter, double when, double val)
666 /* note: we assume higher level logic is in place to avoid this
667 reordering the time-order of control events in the list. ie. all
668 points after *iter are later than when.
672 Glib::Mutex::Lock lm (_lock);
674 (*iter)->when = when;
675 (*iter)->value = val;
682 _events.sort (event_time_less_than);
684 _sort_pending = true;
690 maybe_signal_changed ();
693 std::pair<ControlList::iterator,ControlList::iterator>
694 ControlList::control_points_adjacent (double xval)
696 Glib::Mutex::Lock lm (_lock);
698 ControlEvent cp (xval, 0.0f);
699 std::pair<iterator,iterator> ret;
701 ret.first = _events.end();
702 ret.second = _events.end();
704 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
706 if (ret.first == _events.end()) {
707 if ((*i)->when >= xval) {
708 if (i != _events.begin()) {
717 if ((*i)->when > xval) {
727 ControlList::set_max_xval (double x)
733 ControlList::freeze ()
748 Glib::Mutex::Lock lm (_lock);
751 _events.sort (event_time_less_than);
752 _sort_pending = false;
758 ControlList::mark_dirty () const
760 _lookup_cache.left = -1;
761 _search_cache.left = -1;
764 _curve->mark_dirty();
767 Dirty (); /* EMIT SIGNAL */
771 ControlList::truncate_end (double last_coordinate)
774 Glib::Mutex::Lock lm (_lock);
775 ControlEvent cp (last_coordinate, 0);
776 ControlList::reverse_iterator i;
779 if (_events.empty()) {
783 if (last_coordinate == _events.back()->when) {
787 if (last_coordinate > _events.back()->when) {
792 iterator foo = _events.begin();
795 if (foo == _events.end()) {
797 } else if (++foo == _events.end()) {
804 /* less than 2 points: add a new point */
805 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
808 /* more than 2 points: check to see if the last 2 values
809 are equal. if so, just move the position of the
810 last point. otherwise, add a new point.
813 iterator penultimate = _events.end();
814 --penultimate; /* points at last point */
815 --penultimate; /* points at the penultimate point */
817 if (_events.back()->value == (*penultimate)->value) {
818 _events.back()->when = last_coordinate;
820 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
828 last_val = unlocked_eval (last_coordinate);
829 last_val = max ((double) _min_yval, last_val);
830 last_val = min ((double) _max_yval, last_val);
832 i = _events.rbegin();
834 /* make i point to the last control point */
838 /* now go backwards, removing control points that are
839 beyond the new last coordinate.
842 // FIXME: SLOW! (size() == O(n))
844 uint32_t sz = _events.size();
846 while (i != _events.rend() && sz > 2) {
847 ControlList::reverse_iterator tmp;
852 if ((*i)->when < last_coordinate) {
856 _events.erase (i.base());
862 _events.back()->when = last_coordinate;
863 _events.back()->value = last_val;
869 maybe_signal_changed ();
873 ControlList::truncate_start (double overall_length)
876 Glib::Mutex::Lock lm (_lock);
878 double first_legal_value;
879 double first_legal_coordinate;
881 assert(!_events.empty());
883 if (overall_length == _events.back()->when) {
884 /* no change in overall length */
888 if (overall_length > _events.back()->when) {
890 /* growing at front: duplicate first point. shift all others */
892 double shift = overall_length - _events.back()->when;
895 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
901 /* less than 2 points: add a new point */
902 _events.push_front (new ControlEvent (0, _events.front()->value));
906 /* more than 2 points: check to see if the first 2 values
907 are equal. if so, just move the position of the
908 first point. otherwise, add a new point.
911 iterator second = _events.begin();
912 ++second; /* points at the second point */
914 if (_events.front()->value == (*second)->value) {
915 /* first segment is flat, just move start point back to zero */
916 _events.front()->when = 0;
918 /* leave non-flat segment in place, add a new leading point. */
919 _events.push_front (new ControlEvent (0, _events.front()->value));
925 /* shrinking at front */
927 first_legal_coordinate = _events.back()->when - overall_length;
928 first_legal_value = unlocked_eval (first_legal_coordinate);
929 first_legal_value = max (_min_yval, first_legal_value);
930 first_legal_value = min (_max_yval, first_legal_value);
932 /* remove all events earlier than the new "front" */
936 while (i != _events.end() && !_events.empty()) {
937 ControlList::iterator tmp;
942 if ((*i)->when > first_legal_coordinate) {
952 /* shift all remaining points left to keep their same
956 for (i = _events.begin(); i != _events.end(); ++i) {
957 (*i)->when -= first_legal_coordinate;
960 /* add a new point for the interpolated new value */
962 _events.push_front (new ControlEvent (0, first_legal_value));
968 maybe_signal_changed ();
972 ControlList::unlocked_eval (double x) const
974 pair<EventList::iterator,EventList::iterator> range;
980 const_iterator length_check_iter = _events.begin();
981 for (npoints = 0; npoints < 4; ++npoints, ++length_check_iter) {
982 if (length_check_iter == _events.end()) {
989 return _default_value;
992 return _events.front()->value;
995 if (x >= _events.back()->when) {
996 return _events.back()->value;
997 } else if (x <= _events.front()->when) {
998 return _events.front()->value;
1001 lpos = _events.front()->when;
1002 lval = _events.front()->value;
1003 upos = _events.back()->when;
1004 uval = _events.back()->value;
1006 if (_interpolation == Discrete) {
1010 /* linear interpolation betweeen the two points */
1011 fraction = (double) (x - lpos) / (double) (upos - lpos);
1012 return lval + (fraction * (uval - lval));
1015 if (x >= _events.back()->when) {
1016 return _events.back()->value;
1017 } else if (x <= _events.front()->when) {
1018 return _events.front()->value;
1021 return multipoint_eval (x);
1024 /*NOTREACHED*/ /* stupid gcc */
1025 return _default_value;
1029 ControlList::multipoint_eval (double x) const
1035 /* "Stepped" lookup (no interpolation) */
1036 /* FIXME: no cache. significant? */
1037 if (_interpolation == Discrete) {
1038 const ControlEvent cp (x, 0);
1039 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
1041 // shouldn't have made it to multipoint_eval
1042 assert(i != _events.end());
1044 if (i == _events.begin() || (*i)->when == x)
1047 return (*(--i))->value;
1050 /* Only do the range lookup if x is in a different range than last time
1051 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
1052 if ((_lookup_cache.left < 0) ||
1053 ((_lookup_cache.left > x) ||
1054 (_lookup_cache.range.first == _events.end()) ||
1055 ((*_lookup_cache.range.second)->when < x))) {
1057 const ControlEvent cp (x, 0);
1059 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
1062 pair<const_iterator,const_iterator> range = _lookup_cache.range;
1064 if (range.first == range.second) {
1066 /* x does not exist within the list as a control point */
1068 _lookup_cache.left = x;
1070 if (range.first != _events.begin()) {
1072 lpos = (*range.first)->when;
1073 lval = (*range.first)->value;
1075 /* we're before the first point */
1076 // return _default_value;
1077 return _events.front()->value;
1080 if (range.second == _events.end()) {
1081 /* we're after the last point */
1082 return _events.back()->value;
1085 upos = (*range.second)->when;
1086 uval = (*range.second)->value;
1088 /* linear interpolation betweeen the two points
1092 fraction = (double) (x - lpos) / (double) (upos - lpos);
1093 return lval + (fraction * (uval - lval));
1097 /* x is a control point in the data */
1098 _lookup_cache.left = -1;
1099 return (*range.first)->value;
1103 ControlList::build_search_cache_if_necessary (double start) const
1105 /* Only do the range lookup if x is in a different range than last time
1106 * this was called (or if the search cache has been marked "dirty" (left<0) */
1107 if (!_events.empty() && ((_search_cache.left < 0) || (_search_cache.left > start))) {
1109 const ControlEvent start_point (start, 0);
1111 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1112 // << start << ".." << end << ")" << endl;
1114 _search_cache.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1115 _search_cache.left = start;
1119 /** Get the earliest event after \a start using the current interpolation style.
1121 * If an event is found, \a x and \a y are set to its coordinates.
1123 * \param inclusive Include events with timestamp exactly equal to \a start
1124 * \return true if event is found (and \a x and \a y are valid).
1127 ControlList::rt_safe_earliest_event (double start, double& x, double& y, bool inclusive) const
1129 // FIXME: It would be nice if this was unnecessary..
1130 Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
1135 return rt_safe_earliest_event_unlocked (start, x, y, inclusive);
1139 /** Get the earliest event after \a start using the current interpolation style.
1141 * If an event is found, \a x and \a y are set to its coordinates.
1143 * \param inclusive Include events with timestamp exactly equal to \a start
1144 * \return true if event is found (and \a x and \a y are valid).
1147 ControlList::rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool inclusive) const
1149 if (_interpolation == Discrete) {
1150 return rt_safe_earliest_event_discrete_unlocked(start, x, y, inclusive);
1152 return rt_safe_earliest_event_linear_unlocked(start, x, y, inclusive);
1157 /** Get the earliest event after \a start without interpolation.
1159 * If an event is found, \a x and \a y are set to its coordinates.
1161 * \param inclusive Include events with timestamp exactly equal to \a start
1162 * \return true if event is found (and \a x and \a y are valid).
1165 ControlList::rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const
1167 build_search_cache_if_necessary (start);
1169 if (_search_cache.first != _events.end()) {
1170 const ControlEvent* const first = *_search_cache.first;
1172 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1174 /* Earliest points is in range, return it */
1180 /* Move left of cache to this point
1181 * (Optimize for immediate call this cycle within range) */
1182 _search_cache.left = x;
1183 ++_search_cache.first;
1192 /* No points in range */
1198 /** Get the earliest time the line crosses an integer (Linear interpolation).
1200 * If an event is found, \a x and \a y are set to its coordinates.
1202 * \param inclusive Include events with timestamp exactly equal to \a start
1203 * \return true if event is found (and \a x and \a y are valid).
1206 ControlList::rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const
1208 // cout << "earliest_event(start: " << start << ", x: " << x << ", y: " << y << ", inclusive: " << inclusive << ")" << endl;
1210 const_iterator length_check_iter = _events.begin();
1211 if (_events.empty()) { // 0 events
1213 } else if (_events.end() == ++length_check_iter) { // 1 event
1214 return rt_safe_earliest_event_discrete_unlocked (start, x, y, inclusive);
1217 // Hack to avoid infinitely repeating the same event
1218 build_search_cache_if_necessary (start);
1220 if (_search_cache.first != _events.end()) {
1222 const ControlEvent* first = NULL;
1223 const ControlEvent* next = NULL;
1225 /* Step is after first */
1226 if (_search_cache.first == _events.begin() || (*_search_cache.first)->when <= start) {
1227 first = *_search_cache.first;
1228 ++_search_cache.first;
1229 if (_search_cache.first == _events.end()) {
1232 next = *_search_cache.first;
1234 /* Step is before first */
1236 const_iterator prev = _search_cache.first;
1239 next = *_search_cache.first;
1242 if (inclusive && first->when == start) {
1245 /* Move left of cache to this point
1246 * (Optimize for immediate call this cycle within range) */
1247 _search_cache.left = x;
1248 //++_search_cache.range.first;
1253 if (fabs(first->value - next->value) <= 1) {
1254 if (next->when > start) {
1257 /* Move left of cache to this point
1258 * (Optimize for immediate call this cycle within range) */
1259 _search_cache.left = x;
1260 //++_search_cache.range.first;
1261 assert(inclusive ? x >= start : x > start);
1268 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1269 //cerr << "start y: " << start_y << endl;
1271 //y = first->value + (slope * fabs(start - first->when));
1274 if (first->value < next->value) // ramping up
1276 else // ramping down
1279 x = first->when + (y - first->value) / (double)slope;
1281 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1283 if (first->value < next->value) // ramping up
1285 else // ramping down
1288 x = first->when + (y - first->value) / (double)slope;
1291 /*cerr << first->value << " @ " << first->when << " ... "
1292 << next->value << " @ " << next->when
1293 << " = " << y << " @ " << x << endl;*/
1295 assert( (y >= first->value && y <= next->value)
1296 || (y <= first->value && y >= next->value) );
1299 const bool past_start = (inclusive ? x >= start : x > start);
1301 /* Move left of cache to this point
1302 * (Optimize for immediate call this cycle within range) */
1303 _search_cache.left = x;
1304 assert(inclusive ? x >= start : x > start);
1310 /* No points in the future, so no steps (towards them) in the future */
1317 /** @param start Start position in model coordinates.
1318 * @param end End position in model coordinates.
1319 * @param op 0 = cut, 1 = copy, 2 = clear.
1321 boost::shared_ptr<ControlList>
1322 ControlList::cut_copy_clear (double start, double end, int op)
1324 boost::shared_ptr<ControlList> nal = create (_parameter);
1326 ControlEvent cp (start, 0.0);
1329 Glib::Mutex::Lock lm (_lock);
1331 /* first, determine s & e, two iterators that define the range of points
1332 affected by this operation
1335 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1339 /* and the last that is at or after `end' */
1341 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1344 /* if "start" isn't the location of an existing point,
1345 evaluate the curve to get a value for the start. Add a point to
1346 both the existing event list, and if its not a "clear" operation,
1347 to the copy ("nal") as well.
1349 Note that the time positions of the points in each list are different
1350 because we want the copy ("nal") to have a zero time reference.
1354 /* before we begin any cut/clear operations, get the value of the curve
1358 double end_value = unlocked_eval (end);
1360 if ((*s)->when != start) {
1362 double val = unlocked_eval (start);
1364 if (op == 0) { // cut
1365 if (start > _events.front()->when) {
1366 _events.insert (s, (new ControlEvent (start, val)));
1370 if (op != 2) { // ! clear
1371 nal->_events.push_back (new ControlEvent (0, val));
1375 for (iterator x = s; x != e; ) {
1377 /* adjust new points to be relative to start, which
1378 has been set to zero.
1382 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1386 x = _events.erase (x);
1392 if (e == _events.end() || (*e)->when != end) {
1394 /* only add a boundary point if there is a point after "end"
1397 if (op == 0 && (e != _events.end() && end < (*e)->when)) { // cut
1398 _events.insert (e, new ControlEvent (end, end_value));
1401 if (op != 2 && (e != _events.end() && end < (*e)->when)) { // cut/copy
1402 nal->_events.push_back (new ControlEvent (end - start, end_value));
1410 maybe_signal_changed ();
1417 boost::shared_ptr<ControlList>
1418 ControlList::cut (double start, double end)
1420 return cut_copy_clear (start, end, 0);
1423 boost::shared_ptr<ControlList>
1424 ControlList::copy (double start, double end)
1426 return cut_copy_clear (start, end, 1);
1430 ControlList::clear (double start, double end)
1432 cut_copy_clear (start, end, 2);
1435 /** @param pos Position in model coordinates */
1437 ControlList::paste (ControlList& alist, double pos, float /*times*/)
1439 if (alist._events.empty()) {
1444 Glib::Mutex::Lock lm (_lock);
1448 ControlEvent cp (pos, 0.0);
1450 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1452 for (iterator i = alist.begin();i != alist.end(); ++i) {
1453 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1454 end = (*i)->when + pos;
1458 /* move all points after the insertion along the timeline by
1462 while (where != _events.end()) {
1464 if ((*where)->when <= end) {
1467 _events.erase(where);
1478 maybe_signal_changed ();
1482 /** Move automation around according to a list of region movements.
1483 * @param return true if anything was changed, otherwise false (ie nothing needed changing)
1486 ControlList::move_ranges (const list< RangeMove<double> >& movements)
1488 typedef list< RangeMove<double> > RangeMoveList;
1491 Glib::Mutex::Lock lm (_lock);
1493 /* a copy of the events list before we started moving stuff around */
1494 EventList old_events = _events;
1496 /* clear the source and destination ranges in the new list */
1497 bool things_erased = false;
1498 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1500 if (erase_range_internal (i->from, i->from + i->length, _events)) {
1501 things_erased = true;
1504 if (erase_range_internal (i->to, i->to + i->length, _events)) {
1505 things_erased = true;
1509 /* if nothing was erased, there is nothing to do */
1510 if (!things_erased) {
1514 /* copy the events into the new list */
1515 for (RangeMoveList::const_iterator i = movements.begin (); i != movements.end (); ++i) {
1516 iterator j = old_events.begin ();
1517 const double limit = i->from + i->length;
1518 const double dx = i->to - i->from;
1519 while (j != old_events.end () && (*j)->when <= limit) {
1520 if ((*j)->when >= i->from) {
1521 ControlEvent* ev = new ControlEvent (**j);
1523 _events.push_back (ev);
1530 _events.sort (event_time_less_than);
1532 _sort_pending = true;
1538 maybe_signal_changed ();
1543 ControlList::set_interpolation (InterpolationStyle s)
1545 if (_interpolation == s) {
1550 InterpolationChanged (s); /* EMIT SIGNAL */
1553 } // namespace Evoral