2 Copyright (C) 2002 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <sigc++/bind.h>
27 #include <ardour/parameter.h>
28 #include <ardour/automation_event.h>
29 #include <ardour/curve.h>
30 #include <pbd/stacktrace.h>
31 #include <pbd/enumwriter.h>
36 using namespace ARDOUR;
40 sigc::signal<void,AutomationList *> AutomationList::AutomationListCreated;
42 static bool sort_events_by_time (ControlEvent* a, ControlEvent* b)
44 return a->when < b->when;
48 static void dumpit (const AutomationList& al, string prefix = "")
50 cerr << prefix << &al << endl;
51 for (AutomationList::const_iterator i = al.const_begin(); i != al.const_end(); ++i) {
52 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
58 /* XXX: min_val max_val redundant? (param.min() param.max()) */
59 AutomationList::AutomationList (Parameter id, double min_val, double max_val, double default_val)
61 , _interpolation(Linear)
62 , _curve(new Curve(*this))
66 _changed_when_thawed = false;
72 _max_xval = 0; // means "no limit"
73 _default_value = default_val;
74 _rt_insertion_point = _events.end();
75 _lookup_cache.left = -1;
76 _lookup_cache.range.first = _events.end();
77 _search_cache.left = -1;
78 _search_cache.range.first = _events.end();
79 _sort_pending = false;
81 assert(_parameter.type() != NullAutomation);
82 AutomationListCreated(this);
85 AutomationList::AutomationList (const AutomationList& other)
86 : _parameter(other._parameter)
87 , _interpolation(Linear)
88 , _curve(new Curve(*this))
91 _changed_when_thawed = false;
92 _style = other._style;
93 _min_yval = other._min_yval;
94 _max_yval = other._max_yval;
95 _max_xval = other._max_xval;
96 _default_value = other._default_value;
97 _state = other._state;
98 _touching = other._touching;
99 _rt_insertion_point = _events.end();
100 _lookup_cache.range.first = _events.end();
101 _search_cache.range.first = _events.end();
102 _sort_pending = false;
104 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
105 _events.push_back (new ControlEvent (**i));
109 assert(_parameter.type() != NullAutomation);
110 AutomationListCreated(this);
113 AutomationList::AutomationList (const AutomationList& other, double start, double end)
114 : _parameter(other._parameter)
115 , _interpolation(Linear)
116 , _curve(new Curve(*this))
119 _changed_when_thawed = false;
120 _style = other._style;
121 _min_yval = other._min_yval;
122 _max_yval = other._max_yval;
123 _max_xval = other._max_xval;
124 _default_value = other._default_value;
125 _state = other._state;
126 _touching = other._touching;
127 _rt_insertion_point = _events.end();
128 _lookup_cache.range.first = _events.end();
129 _search_cache.range.first = _events.end();
130 _sort_pending = false;
132 /* now grab the relevant points, and shift them back if necessary */
134 AutomationList* section = const_cast<AutomationList*>(&other)->copy (start, end);
136 if (!section->empty()) {
137 for (iterator i = section->begin(); i != section->end(); ++i) {
138 _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
146 assert(_parameter.type() != NullAutomation);
147 AutomationListCreated(this);
150 /** \a id is used for legacy sessions where the type is not present
151 * in or below the <AutomationList> node. It is used if \a id is non-null.
153 AutomationList::AutomationList (const XMLNode& node, Parameter id)
154 : _interpolation(Linear)
155 , _curve(new Curve(*this))
158 _changed_when_thawed = false;
162 _max_xval = 0; // means "no limit"
165 _rt_insertion_point = _events.end();
166 _lookup_cache.range.first = _events.end();
167 _search_cache.range.first = _events.end();
168 _sort_pending = false;
175 assert(_parameter.type() != NullAutomation);
176 AutomationListCreated(this);
179 AutomationList::~AutomationList()
183 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
189 AutomationList::operator== (const AutomationList& other)
191 return _events == other._events;
195 AutomationList::operator= (const AutomationList& other)
197 if (this != &other) {
201 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
202 _events.push_back (new ControlEvent (**i));
205 _min_yval = other._min_yval;
206 _max_yval = other._max_yval;
207 _max_xval = other._max_xval;
208 _default_value = other._default_value;
211 maybe_signal_changed ();
218 AutomationList::maybe_signal_changed ()
223 _changed_when_thawed = true;
230 AutomationList::set_automation_state (AutoState s)
234 automation_state_changed (); /* EMIT SIGNAL */
239 AutomationList::set_automation_style (AutoStyle s)
243 automation_style_changed (); /* EMIT SIGNAL */
248 AutomationList::start_touch ()
255 AutomationList::stop_touch ()
262 AutomationList::clear ()
265 Glib::Mutex::Lock lm (_lock);
270 maybe_signal_changed ();
274 AutomationList::x_scale (double factor)
276 Glib::Mutex::Lock lm (_lock);
281 AutomationList::extend_to (double when)
283 Glib::Mutex::Lock lm (_lock);
284 if (_events.empty() || _events.back()->when == when) {
287 double factor = when / _events.back()->when;
292 void AutomationList::_x_scale (double factor)
294 for (iterator i = _events.begin(); i != _events.end(); ++i) {
295 (*i)->when = floor ((*i)->when * factor);
302 AutomationList::reposition_for_rt_add (double when)
304 _rt_insertion_point = _events.end();
308 AutomationList::rt_add (double when, double value)
310 /* this is for automation recording */
312 if ((_state & Touch) && !_touching) {
316 // cerr << "RT: alist @ " << this << " add " << value << " @ " << when << endl;
319 Glib::Mutex::Lock lm (_lock);
322 ControlEvent cp (when, 0.0);
325 if ((_rt_insertion_point != _events.end()) && ((*_rt_insertion_point)->when < when) ) {
327 /* we have a previous insertion point, so we should delete
328 everything between it and the position where we are going
329 to insert this point.
332 iterator after = _rt_insertion_point;
334 if (++after != _events.end()) {
335 iterator far = after;
337 while (far != _events.end()) {
338 if ((*far)->when > when) {
346 _rt_insertion_point = where;
348 if ((*where)->when == when) {
349 (*where)->value = value;
353 where = _events.erase (after, far);
362 iterator previous = _rt_insertion_point;
365 if (_rt_insertion_point != _events.begin() && (*_rt_insertion_point)->value == value && (*previous)->value == value) {
366 (*_rt_insertion_point)->when = when;
373 where = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
375 if (where != _events.end()) {
376 if ((*where)->when == when) {
377 (*where)->value = value;
384 _rt_insertion_point = _events.insert (where, new ControlEvent (when, value));
391 maybe_signal_changed ();
395 AutomationList::fast_simple_add (double when, double value)
397 /* to be used only for loading pre-sorted data from saved state */
398 _events.insert (_events.end(), new ControlEvent (when, value));
399 assert(_events.back());
403 AutomationList::add (double when, double value)
405 /* this is for graphical editing */
408 Glib::Mutex::Lock lm (_lock);
409 ControlEvent cp (when, 0.0f);
411 iterator insertion_point;
413 for (insertion_point = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); insertion_point != _events.end(); ++insertion_point) {
415 /* only one point allowed per time point */
417 if ((*insertion_point)->when == when) {
418 (*insertion_point)->value = value;
423 if ((*insertion_point)->when >= when) {
430 _events.insert (insertion_point, new ControlEvent (when, value));
431 reposition_for_rt_add (0);
438 maybe_signal_changed ();
442 AutomationList::erase (iterator i)
445 Glib::Mutex::Lock lm (_lock);
447 reposition_for_rt_add (0);
450 maybe_signal_changed ();
454 AutomationList::erase (iterator start, iterator end)
457 Glib::Mutex::Lock lm (_lock);
458 _events.erase (start, end);
459 reposition_for_rt_add (0);
462 maybe_signal_changed ();
466 AutomationList::reset_range (double start, double endt)
471 Glib::Mutex::Lock lm (_lock);
472 ControlEvent cp (start, 0.0f);
476 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) != _events.end()) {
479 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
481 for (iterator i = s; i != e; ++i) {
482 (*i)->value = _default_value;
492 maybe_signal_changed ();
497 AutomationList::erase_range (double start, double endt)
502 Glib::Mutex::Lock lm (_lock);
503 ControlEvent cp (start, 0.0f);
507 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) != _events.end()) {
509 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
510 _events.erase (s, e);
511 reposition_for_rt_add (0);
519 maybe_signal_changed ();
524 AutomationList::move_range (iterator start, iterator end, double xdelta, double ydelta)
526 /* note: we assume higher level logic is in place to avoid this
527 reordering the time-order of control events in the list. ie. all
528 points after end are later than (end)->when.
532 Glib::Mutex::Lock lm (_lock);
534 while (start != end) {
535 (*start)->when += xdelta;
536 (*start)->value += ydelta;
537 if (isnan ((*start)->value)) {
544 _events.sort (sort_events_by_time);
546 _sort_pending = true;
552 maybe_signal_changed ();
556 AutomationList::slide (iterator before, double distance)
559 Glib::Mutex::Lock lm (_lock);
561 if (before == _events.end()) {
565 while (before != _events.end()) {
566 (*before)->when += distance;
571 maybe_signal_changed ();
575 AutomationList::modify (iterator iter, double when, double val)
577 /* note: we assume higher level logic is in place to avoid this
578 reordering the time-order of control events in the list. ie. all
579 points after *iter are later than when.
583 Glib::Mutex::Lock lm (_lock);
585 (*iter)->when = when;
586 (*iter)->value = val;
593 _events.sort (sort_events_by_time);
595 _sort_pending = true;
601 maybe_signal_changed ();
604 std::pair<AutomationList::iterator,AutomationList::iterator>
605 AutomationList::control_points_adjacent (double xval)
607 Glib::Mutex::Lock lm (_lock);
609 ControlEvent cp (xval, 0.0f);
610 std::pair<iterator,iterator> ret;
612 ret.first = _events.end();
613 ret.second = _events.end();
615 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
617 if (ret.first == _events.end()) {
618 if ((*i)->when >= xval) {
619 if (i != _events.begin()) {
628 if ((*i)->when > xval) {
638 AutomationList::freeze ()
644 AutomationList::thaw ()
647 PBD::stacktrace (cerr);
648 fatal << string_compose (_("programming error: %1"), X_("AutomationList::thaw() called while not frozen")) << endmsg;
657 Glib::Mutex::Lock lm (_lock);
660 _events.sort (sort_events_by_time);
661 _sort_pending = false;
665 if (_changed_when_thawed) {
666 StateChanged(); /* EMIT SIGNAL */
671 AutomationList::set_max_xval (double x)
677 AutomationList::mark_dirty ()
679 _lookup_cache.left = -1;
680 _search_cache.left = -1;
681 Dirty (); /* EMIT SIGNAL */
685 AutomationList::truncate_end (double last_coordinate)
688 Glib::Mutex::Lock lm (_lock);
689 ControlEvent cp (last_coordinate, 0);
690 AutomationList::reverse_iterator i;
693 if (_events.empty()) {
697 if (last_coordinate == _events.back()->when) {
701 if (last_coordinate > _events.back()->when) {
706 iterator foo = _events.begin();
709 if (foo == _events.end()) {
711 } else if (++foo == _events.end()) {
718 /* less than 2 points: add a new point */
719 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
722 /* more than 2 points: check to see if the last 2 values
723 are equal. if so, just move the position of the
724 last point. otherwise, add a new point.
727 iterator penultimate = _events.end();
728 --penultimate; /* points at last point */
729 --penultimate; /* points at the penultimate point */
731 if (_events.back()->value == (*penultimate)->value) {
732 _events.back()->when = last_coordinate;
734 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
742 last_val = unlocked_eval (last_coordinate);
743 last_val = max ((double) _min_yval, last_val);
744 last_val = min ((double) _max_yval, last_val);
746 i = _events.rbegin();
748 /* make i point to the last control point */
752 /* now go backwards, removing control points that are
753 beyond the new last coordinate.
756 uint32_t sz = _events.size();
758 while (i != _events.rend() && sz > 2) {
759 AutomationList::reverse_iterator tmp;
764 if ((*i)->when < last_coordinate) {
768 _events.erase (i.base());
774 _events.back()->when = last_coordinate;
775 _events.back()->value = last_val;
778 reposition_for_rt_add (0);
782 maybe_signal_changed ();
786 AutomationList::truncate_start (double overall_length)
789 Glib::Mutex::Lock lm (_lock);
791 double first_legal_value;
792 double first_legal_coordinate;
794 if (_events.empty()) {
795 fatal << _("programming error:")
796 << "AutomationList::truncate_start() called on an empty list"
802 if (overall_length == _events.back()->when) {
803 /* no change in overall length */
807 if (overall_length > _events.back()->when) {
809 /* growing at front: duplicate first point. shift all others */
811 double shift = overall_length - _events.back()->when;
814 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
820 /* less than 2 points: add a new point */
821 _events.push_front (new ControlEvent (0, _events.front()->value));
825 /* more than 2 points: check to see if the first 2 values
826 are equal. if so, just move the position of the
827 first point. otherwise, add a new point.
830 iterator second = _events.begin();
831 ++second; /* points at the second point */
833 if (_events.front()->value == (*second)->value) {
834 /* first segment is flat, just move start point back to zero */
835 _events.front()->when = 0;
837 /* leave non-flat segment in place, add a new leading point. */
838 _events.push_front (new ControlEvent (0, _events.front()->value));
844 /* shrinking at front */
846 first_legal_coordinate = _events.back()->when - overall_length;
847 first_legal_value = unlocked_eval (first_legal_coordinate);
848 first_legal_value = max (_min_yval, first_legal_value);
849 first_legal_value = min (_max_yval, first_legal_value);
851 /* remove all events earlier than the new "front" */
855 while (i != _events.end() && !_events.empty()) {
856 AutomationList::iterator tmp;
861 if ((*i)->when > first_legal_coordinate) {
871 /* shift all remaining points left to keep their same
875 for (i = _events.begin(); i != _events.end(); ++i) {
876 (*i)->when -= first_legal_coordinate;
879 /* add a new point for the interpolated new value */
881 _events.push_front (new ControlEvent (0, first_legal_value));
884 reposition_for_rt_add (0);
889 maybe_signal_changed ();
893 AutomationList::unlocked_eval (double x) const
895 pair<EventList::iterator,EventList::iterator> range;
901 npoints = _events.size();
905 return _default_value;
908 if (x >= _events.front()->when) {
909 return _events.front()->value;
911 // return _default_value;
912 return _events.front()->value;
916 if (x >= _events.back()->when) {
917 return _events.back()->value;
918 } else if (x == _events.front()->when) {
919 return _events.front()->value;
920 } else if (x < _events.front()->when) {
921 // return _default_value;
922 return _events.front()->value;
925 lpos = _events.front()->when;
926 lval = _events.front()->value;
927 upos = _events.back()->when;
928 uval = _events.back()->value;
930 if (_interpolation == Discrete)
933 /* linear interpolation betweeen the two points
936 fraction = (double) (x - lpos) / (double) (upos - lpos);
937 return lval + (fraction * (uval - lval));
941 if (x >= _events.back()->when) {
942 return _events.back()->value;
943 } else if (x == _events.front()->when) {
944 return _events.front()->value;
945 } else if (x < _events.front()->when) {
946 // return _default_value;
947 return _events.front()->value;
950 return multipoint_eval (x);
954 /*NOTREACHED*/ /* stupid gcc */
959 AutomationList::multipoint_eval (double x) const
965 /* "Stepped" lookup (no interpolation) */
966 /* FIXME: no cache. significant? */
967 if (_interpolation == Discrete) {
968 const ControlEvent cp (x, 0);
969 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
971 // shouldn't have made it to multipoint_eval
972 assert(i != _events.end());
974 if (i == _events.begin() || (*i)->when == x)
977 return (*(--i))->value;
980 /* Only do the range lookup if x is in a different range than last time
981 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
982 if ((_lookup_cache.left < 0) ||
983 ((_lookup_cache.left > x) ||
984 (_lookup_cache.range.first == _events.end()) ||
985 ((*_lookup_cache.range.second)->when < x))) {
987 const ControlEvent cp (x, 0);
989 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
992 pair<const_iterator,const_iterator> range = _lookup_cache.range;
994 if (range.first == range.second) {
996 /* x does not exist within the list as a control point */
998 _lookup_cache.left = x;
1000 if (range.first != _events.begin()) {
1002 lpos = (*range.first)->when;
1003 lval = (*range.first)->value;
1005 /* we're before the first point */
1006 // return _default_value;
1007 return _events.front()->value;
1010 if (range.second == _events.end()) {
1011 /* we're after the last point */
1012 return _events.back()->value;
1015 upos = (*range.second)->when;
1016 uval = (*range.second)->value;
1018 /* linear interpolation betweeen the two points
1022 fraction = (double) (x - lpos) / (double) (upos - lpos);
1023 return lval + (fraction * (uval - lval));
1027 /* x is a control point in the data */
1028 _lookup_cache.left = -1;
1029 return (*range.first)->value;
1033 AutomationList::build_search_cache_if_necessary(double start, double end) const
1035 /* Only do the range lookup if x is in a different range than last time
1036 * this was called (or if the search cache has been marked "dirty" (left<0) */
1037 if (!_events.empty() && ((_search_cache.left < 0) ||
1038 ((_search_cache.left > start) ||
1039 (_search_cache.right < end)))) {
1041 const ControlEvent start_point (start, 0);
1042 const ControlEvent end_point (end, 0);
1044 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1045 // << start << ".." << end << ")" << endl;
1047 _search_cache.range.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1048 _search_cache.range.second = upper_bound (_events.begin(), _events.end(), &end_point, time_comparator);
1050 _search_cache.left = start;
1051 _search_cache.right = end;
1055 /** Get the earliest event between \a start and \a end, using the current interpolation style.
1057 * If an event is found, \a x and \a y are set to its coordinates.
1059 * \param inclusive Include events with timestamp exactly equal to \a start
1060 * \return true if event is found (and \a x and \a y are valid).
1063 AutomationList::rt_safe_earliest_event(double start, double end, double& x, double& y, bool inclusive) const
1065 // FIXME: It would be nice if this was unnecessary..
1066 Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
1071 return rt_safe_earliest_event_unlocked(start, end, x, y, inclusive);
1075 /** Get the earliest event between \a start and \a end, using the current interpolation style.
1077 * If an event is found, \a x and \a y are set to its coordinates.
1079 * \param inclusive Include events with timestamp exactly equal to \a start
1080 * \return true if event is found (and \a x and \a y are valid).
1083 AutomationList::rt_safe_earliest_event_unlocked(double start, double end, double& x, double& y, bool inclusive) const
1085 if (_interpolation == Discrete)
1086 return rt_safe_earliest_event_discrete_unlocked(start, end, x, y, inclusive);
1088 return rt_safe_earliest_event_linear_unlocked(start, end, x, y, inclusive);
1092 /** Get the earliest event between \a start and \a end (Discrete (lack of) interpolation)
1094 * If an event is found, \a x and \a y are set to its coordinates.
1096 * \param inclusive Include events with timestamp exactly equal to \a start
1097 * \return true if event is found (and \a x and \a y are valid).
1100 AutomationList::rt_safe_earliest_event_discrete_unlocked (double start, double end, double& x, double& y, bool inclusive) const
1102 build_search_cache_if_necessary(start, end);
1104 const pair<const_iterator,const_iterator>& range = _search_cache.range;
1106 if (range.first != _events.end()) {
1107 const ControlEvent* const first = *range.first;
1109 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1111 /* Earliest points is in range, return it */
1112 if (past_start >= start && first->when < end) {
1117 /* Move left of cache to this point
1118 * (Optimize for immediate call this cycle within range) */
1119 _search_cache.left = x;
1120 ++_search_cache.range.first;
1130 /* No points in range */
1136 /** Get the earliest time the line crosses an integer (Linear interpolation).
1138 * If an event is found, \a x and \a y are set to its coordinates.
1140 * \param inclusive Include events with timestamp exactly equal to \a start
1141 * \return true if event is found (and \a x and \a y are valid).
1144 AutomationList::rt_safe_earliest_event_linear_unlocked (double start, double end, double& x, double& y, bool inclusive) const
1146 //cerr << "earliest_event(" << start << ", " << end << ", " << x << ", " << y << ", " << inclusive << endl;
1148 if (_events.size() == 0)
1150 else if (_events.size() == 1)
1151 return rt_safe_earliest_event_discrete_unlocked(start, end, x, y, inclusive);
1153 // Hack to avoid infinitely repeating the same event
1154 build_search_cache_if_necessary(start, end);
1156 pair<const_iterator,const_iterator> range = _search_cache.range;
1158 if (range.first != _events.end()) {
1160 const ControlEvent* first = NULL;
1161 const ControlEvent* next = NULL;
1163 /* Step is after first */
1164 if (range.first == _events.begin() || (*range.first)->when == start) {
1165 first = *range.first;
1166 next = *(++range.first);
1167 ++_search_cache.range.first;
1169 /* Step is before first */
1171 const_iterator prev = range.first;
1174 next = *range.first;
1177 if (inclusive && first->when == start) {
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.range.first;
1187 if (abs(first->value - next->value) <= 1) {
1188 if (next->when <= end && (!inclusive || next->when > start)) {
1191 /* Move left of cache to this point
1192 * (Optimize for immediate call this cycle within range) */
1193 _search_cache.left = x;
1194 //++_search_cache.range.first;
1201 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1202 //cerr << "start y: " << start_y << endl;
1204 //y = first->value + (slope * fabs(start - first->when));
1207 if (first->value < next->value) // ramping up
1209 else // ramping down
1212 x = first->when + (y - first->value) / (double)slope;
1214 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1216 if (first->value < next->value) // ramping up
1218 else // ramping down
1221 x = first->when + (y - first->value) / (double)slope;
1224 /*cerr << first->value << " @ " << first->when << " ... "
1225 << next->value << " @ " << next->when
1226 << " = " << y << " @ " << x << endl;*/
1228 assert( (y >= first->value && y <= next->value)
1229 || (y <= first->value && y >= next->value) );
1232 const bool past_start = (inclusive ? x >= start : x > start);
1233 if (past_start && x < end) {
1234 /* Move left of cache to this point
1235 * (Optimize for immediate call this cycle within range) */
1236 _search_cache.left = x;
1244 /* No points in the future, so no steps (towards them) in the future */
1251 AutomationList::cut (iterator start, iterator end)
1253 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1256 Glib::Mutex::Lock lm (_lock);
1258 for (iterator x = start; x != end; ) {
1264 nal->_events.push_back (new ControlEvent (**x));
1267 reposition_for_rt_add (0);
1275 maybe_signal_changed ();
1281 AutomationList::cut_copy_clear (double start, double end, int op)
1283 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1285 ControlEvent cp (start, 0.0);
1286 bool changed = false;
1289 Glib::Mutex::Lock lm (_lock);
1291 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1296 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1298 if (op != 2 && (*s)->when != start) {
1299 nal->_events.push_back (new ControlEvent (0, unlocked_eval (start)));
1302 for (iterator x = s; x != e; ) {
1310 /* adjust new points to be relative to start, which
1311 has been set to zero.
1315 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1325 if (op != 2 && nal->_events.back()->when != end - start) {
1326 nal->_events.push_back (new ControlEvent (end - start, unlocked_eval (end)));
1330 reposition_for_rt_add (0);
1336 maybe_signal_changed ();
1343 AutomationList::copy (iterator start, iterator end)
1345 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1348 Glib::Mutex::Lock lm (_lock);
1350 for (iterator x = start; x != end; ) {
1356 nal->_events.push_back (new ControlEvent (**x));
1366 AutomationList::cut (double start, double end)
1368 return cut_copy_clear (start, end, 0);
1372 AutomationList::copy (double start, double end)
1374 return cut_copy_clear (start, end, 1);
1378 AutomationList::clear (double start, double end)
1380 (void) cut_copy_clear (start, end, 2);
1384 AutomationList::paste (AutomationList& alist, double pos, float times)
1386 if (alist._events.empty()) {
1391 Glib::Mutex::Lock lm (_lock);
1395 ControlEvent cp (pos, 0.0);
1397 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1399 for (iterator i = alist.begin();i != alist.end(); ++i) {
1400 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1401 end = (*i)->when + pos;
1405 /* move all points after the insertion along the timeline by
1409 while (where != _events.end()) {
1411 if ((*where)->when <= end) {
1414 _events.erase(where);
1422 reposition_for_rt_add (0);
1426 maybe_signal_changed ();
1431 AutomationList::get_state ()
1433 return state (true);
1437 AutomationList::state (bool full)
1439 XMLNode* root = new XMLNode (X_("AutomationList"));
1441 LocaleGuard lg (X_("POSIX"));
1443 root->add_property ("automation-id", _parameter.to_string());
1445 root->add_property ("id", _id.to_s());
1447 snprintf (buf, sizeof (buf), "%.12g", _default_value);
1448 root->add_property ("default", buf);
1449 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
1450 root->add_property ("min_yval", buf);
1451 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
1452 root->add_property ("max_yval", buf);
1453 snprintf (buf, sizeof (buf), "%.12g", _max_xval);
1454 root->add_property ("max_xval", buf);
1456 root->add_property ("interpolation-style", enum_2_string (_interpolation));
1459 root->add_property ("state", auto_state_to_string (_state));
1461 /* never save anything but Off for automation state to a template */
1462 root->add_property ("state", auto_state_to_string (Off));
1465 root->add_property ("style", auto_style_to_string (_style));
1467 if (!_events.empty()) {
1468 root->add_child_nocopy (serialize_events());
1475 AutomationList::serialize_events ()
1477 XMLNode* node = new XMLNode (X_("events"));
1480 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
1481 str << (double) (*xx)->when;
1483 str <<(double) (*xx)->value;
1487 /* XML is a bit wierd */
1489 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
1490 content_node->set_content (str.str());
1492 node->add_child_nocopy (*content_node);
1498 AutomationList::deserialize_events (const XMLNode& node)
1500 if (node.children().empty()) {
1504 XMLNode* content_node = node.children().front();
1506 if (content_node->content().empty()) {
1513 stringstream str (content_node->content());
1529 fast_simple_add (x, y);
1534 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
1537 reposition_for_rt_add (0);
1538 maybe_signal_changed ();
1547 AutomationList::set_state (const XMLNode& node)
1549 XMLNodeList nlist = node.children();
1551 XMLNodeIterator niter;
1552 const XMLProperty* prop;
1554 if (node.name() == X_("events")) {
1555 /* partial state setting*/
1556 return deserialize_events (node);
1559 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
1561 if ((nsos = node.child (X_("AutomationList")))) {
1562 /* new school in old school clothing */
1563 return set_state (*nsos);
1568 const XMLNodeList& elist = node.children();
1569 XMLNodeConstIterator i;
1577 for (i = elist.begin(); i != elist.end(); ++i) {
1579 if ((prop = (*i)->property ("x")) == 0) {
1580 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
1583 x = atoi (prop->value().c_str());
1585 if ((prop = (*i)->property ("y")) == 0) {
1586 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
1589 y = atof (prop->value().c_str());
1591 fast_simple_add (x, y);
1599 if (node.name() != X_("AutomationList") ) {
1600 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
1604 if ((prop = node.property ("id")) != 0) {
1605 _id = prop->value ();
1606 /* update session AL list */
1607 AutomationListCreated(this);
1610 if ((prop = node.property (X_("automation-id"))) != 0){
1611 _parameter = Parameter(prop->value());
1613 warning << "Legacy session: automation list has no automation-id property.";
1616 if ((prop = node.property (X_("interpolation-style"))) != 0) {
1617 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
1619 _interpolation = Linear;
1622 if ((prop = node.property (X_("default"))) != 0){
1623 _default_value = atof (prop->value().c_str());
1625 _default_value = 0.0;
1628 if ((prop = node.property (X_("style"))) != 0) {
1629 _style = string_to_auto_style (prop->value());
1634 if ((prop = node.property (X_("state"))) != 0) {
1635 _state = string_to_auto_state (prop->value());
1640 if ((prop = node.property (X_("min_yval"))) != 0) {
1641 _min_yval = atof (prop->value ().c_str());
1643 _min_yval = FLT_MIN;
1646 if ((prop = node.property (X_("max_yval"))) != 0) {
1647 _max_yval = atof (prop->value ().c_str());
1649 _max_yval = FLT_MAX;
1652 if ((prop = node.property (X_("max_xval"))) != 0) {
1653 _max_xval = atof (prop->value ().c_str());
1655 _max_xval = 0; // means "no limit ;
1658 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1659 if ((*niter)->name() == X_("events")) {
1660 deserialize_events (*(*niter));