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 AutomationList::AutomationList (Parameter id, double min_val, double max_val, double default_val)
60 , _interpolation(Linear)
61 , _curve(new Curve(*this))
65 _changed_when_thawed = false;
71 _max_xval = 0; // means "no limit"
72 _default_value = default_val;
73 _rt_insertion_point = _events.end();
74 _lookup_cache.left = -1;
75 _lookup_cache.range.first = _events.end();
76 _search_cache.left = -1;
77 _search_cache.range.first = _events.end();
78 _sort_pending = false;
80 assert(_parameter.type() != NullAutomation);
81 AutomationListCreated(this);
84 AutomationList::AutomationList (const AutomationList& other)
85 : _parameter(other._parameter)
86 , _interpolation(Linear)
87 , _curve(new Curve(*this))
90 _changed_when_thawed = false;
91 _style = other._style;
92 _min_yval = other._min_yval;
93 _max_yval = other._max_yval;
94 _max_xval = other._max_xval;
95 _default_value = other._default_value;
96 _state = other._state;
97 _touching = other._touching;
98 _rt_insertion_point = _events.end();
99 _lookup_cache.range.first = _events.end();
100 _search_cache.range.first = _events.end();
101 _sort_pending = false;
103 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
104 _events.push_back (new ControlEvent (**i));
108 assert(_parameter.type() != NullAutomation);
109 AutomationListCreated(this);
112 AutomationList::AutomationList (const AutomationList& other, double start, double end)
113 : _parameter(other._parameter)
114 , _interpolation(Linear)
115 , _curve(new Curve(*this))
118 _changed_when_thawed = false;
119 _style = other._style;
120 _min_yval = other._min_yval;
121 _max_yval = other._max_yval;
122 _max_xval = other._max_xval;
123 _default_value = other._default_value;
124 _state = other._state;
125 _touching = other._touching;
126 _rt_insertion_point = _events.end();
127 _lookup_cache.range.first = _events.end();
128 _search_cache.range.first = _events.end();
129 _sort_pending = false;
131 /* now grab the relevant points, and shift them back if necessary */
133 AutomationList* section = const_cast<AutomationList*>(&other)->copy (start, end);
135 if (!section->empty()) {
136 for (iterator i = section->begin(); i != section->end(); ++i) {
137 _events.push_back (new ControlEvent ((*i)->when, (*i)->value));
145 assert(_parameter.type() != NullAutomation);
146 AutomationListCreated(this);
149 /** \a id is used for legacy sessions where the type is not present
150 * in or below the <AutomationList> node. It is used if \a id is non-null.
152 AutomationList::AutomationList (const XMLNode& node, Parameter id)
153 : _interpolation(Linear)
154 , _curve(new Curve(*this))
157 _changed_when_thawed = false;
161 _max_xval = 0; // means "no limit"
164 _rt_insertion_point = _events.end();
165 _lookup_cache.range.first = _events.end();
166 _search_cache.range.first = _events.end();
167 _sort_pending = false;
174 assert(_parameter.type() != NullAutomation);
175 AutomationListCreated(this);
178 AutomationList::~AutomationList()
182 for (EventList::iterator x = _events.begin(); x != _events.end(); ++x) {
188 AutomationList::operator== (const AutomationList& other)
190 return _events == other._events;
194 AutomationList::operator= (const AutomationList& other)
196 if (this != &other) {
200 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
201 _events.push_back (new ControlEvent (**i));
204 _min_yval = other._min_yval;
205 _max_yval = other._max_yval;
206 _max_xval = other._max_xval;
207 _default_value = other._default_value;
210 maybe_signal_changed ();
217 AutomationList::maybe_signal_changed ()
222 _changed_when_thawed = true;
229 AutomationList::set_automation_state (AutoState s)
233 automation_state_changed (); /* EMIT SIGNAL */
238 AutomationList::set_automation_style (AutoStyle s)
242 automation_style_changed (); /* EMIT SIGNAL */
247 AutomationList::start_touch ()
254 AutomationList::stop_touch ()
261 AutomationList::clear ()
264 Glib::Mutex::Lock lm (_lock);
269 maybe_signal_changed ();
273 AutomationList::x_scale (double factor)
275 Glib::Mutex::Lock lm (_lock);
280 AutomationList::extend_to (double when)
282 Glib::Mutex::Lock lm (_lock);
283 if (_events.empty() || _events.back()->when == when) {
286 double factor = when / _events.back()->when;
291 void AutomationList::_x_scale (double factor)
293 for (iterator i = _events.begin(); i != _events.end(); ++i) {
294 (*i)->when = floor ((*i)->when * factor);
301 AutomationList::reposition_for_rt_add (double when)
303 _rt_insertion_point = _events.end();
307 AutomationList::rt_add (double when, double value)
309 /* this is for automation recording */
311 if ((_state & Touch) && !_touching) {
315 // cerr << "RT: alist @ " << this << " add " << value << " @ " << when << endl;
318 Glib::Mutex::Lock lm (_lock);
321 ControlEvent cp (when, 0.0);
324 if ((_rt_insertion_point != _events.end()) && ((*_rt_insertion_point)->when < when) ) {
326 /* we have a previous insertion point, so we should delete
327 everything between it and the position where we are going
328 to insert this point.
331 iterator after = _rt_insertion_point;
333 if (++after != _events.end()) {
334 iterator far = after;
336 while (far != _events.end()) {
337 if ((*far)->when > when) {
345 _rt_insertion_point = where;
347 if ((*where)->when == when) {
348 (*where)->value = value;
352 where = _events.erase (after, far);
361 iterator previous = _rt_insertion_point;
364 if (_rt_insertion_point != _events.begin() && (*_rt_insertion_point)->value == value && (*previous)->value == value) {
365 (*_rt_insertion_point)->when = when;
372 where = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
374 if (where != _events.end()) {
375 if ((*where)->when == when) {
376 (*where)->value = value;
383 _rt_insertion_point = _events.insert (where, new ControlEvent (when, value));
390 maybe_signal_changed ();
394 AutomationList::fast_simple_add (double when, double value)
396 /* to be used only for loading pre-sorted data from saved state */
397 _events.insert (_events.end(), new ControlEvent (when, value));
398 assert(_events.back());
402 AutomationList::add (double when, double value)
404 /* this is for graphical editing */
407 Glib::Mutex::Lock lm (_lock);
408 ControlEvent cp (when, 0.0f);
410 iterator insertion_point;
412 for (insertion_point = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); insertion_point != _events.end(); ++insertion_point) {
414 /* only one point allowed per time point */
416 if ((*insertion_point)->when == when) {
417 (*insertion_point)->value = value;
422 if ((*insertion_point)->when >= when) {
429 _events.insert (insertion_point, new ControlEvent (when, value));
430 reposition_for_rt_add (0);
437 maybe_signal_changed ();
441 AutomationList::erase (iterator i)
444 Glib::Mutex::Lock lm (_lock);
446 reposition_for_rt_add (0);
449 maybe_signal_changed ();
453 AutomationList::erase (iterator start, iterator end)
456 Glib::Mutex::Lock lm (_lock);
457 _events.erase (start, end);
458 reposition_for_rt_add (0);
461 maybe_signal_changed ();
465 AutomationList::reset_range (double start, double endt)
470 Glib::Mutex::Lock lm (_lock);
471 ControlEvent cp (start, 0.0f);
475 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) != _events.end()) {
478 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
480 for (iterator i = s; i != e; ++i) {
481 (*i)->value = _default_value;
491 maybe_signal_changed ();
496 AutomationList::erase_range (double start, double endt)
501 Glib::Mutex::Lock lm (_lock);
502 ControlEvent cp (start, 0.0f);
506 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) != _events.end()) {
508 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
509 _events.erase (s, e);
510 reposition_for_rt_add (0);
518 maybe_signal_changed ();
523 AutomationList::move_range (iterator start, iterator end, double xdelta, double ydelta)
525 /* note: we assume higher level logic is in place to avoid this
526 reordering the time-order of control events in the list. ie. all
527 points after end are later than (end)->when.
531 Glib::Mutex::Lock lm (_lock);
533 while (start != end) {
534 (*start)->when += xdelta;
535 (*start)->value += ydelta;
536 if (isnan ((*start)->value)) {
543 _events.sort (sort_events_by_time);
545 _sort_pending = true;
551 maybe_signal_changed ();
555 AutomationList::slide (iterator before, double distance)
558 Glib::Mutex::Lock lm (_lock);
560 if (before == _events.end()) {
564 while (before != _events.end()) {
565 (*before)->when += distance;
570 maybe_signal_changed ();
574 AutomationList::modify (iterator iter, double when, double val)
576 /* note: we assume higher level logic is in place to avoid this
577 reordering the time-order of control events in the list. ie. all
578 points after *iter are later than when.
582 Glib::Mutex::Lock lm (_lock);
584 (*iter)->when = when;
585 (*iter)->value = val;
592 _events.sort (sort_events_by_time);
594 _sort_pending = true;
600 maybe_signal_changed ();
603 std::pair<AutomationList::iterator,AutomationList::iterator>
604 AutomationList::control_points_adjacent (double xval)
606 Glib::Mutex::Lock lm (_lock);
608 ControlEvent cp (xval, 0.0f);
609 std::pair<iterator,iterator> ret;
611 ret.first = _events.end();
612 ret.second = _events.end();
614 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
616 if (ret.first == _events.end()) {
617 if ((*i)->when >= xval) {
618 if (i != _events.begin()) {
627 if ((*i)->when > xval) {
637 AutomationList::freeze ()
643 AutomationList::thaw ()
646 PBD::stacktrace (cerr);
647 fatal << string_compose (_("programming error: %1"), X_("AutomationList::thaw() called while not frozen")) << endmsg;
656 Glib::Mutex::Lock lm (_lock);
659 _events.sort (sort_events_by_time);
660 _sort_pending = false;
664 if (_changed_when_thawed) {
665 StateChanged(); /* EMIT SIGNAL */
670 AutomationList::set_max_xval (double x)
676 AutomationList::mark_dirty ()
678 _lookup_cache.left = -1;
679 _search_cache.left = -1;
680 Dirty (); /* EMIT SIGNAL */
684 AutomationList::truncate_end (double last_coordinate)
687 Glib::Mutex::Lock lm (_lock);
688 ControlEvent cp (last_coordinate, 0);
689 AutomationList::reverse_iterator i;
692 if (_events.empty()) {
696 if (last_coordinate == _events.back()->when) {
700 if (last_coordinate > _events.back()->when) {
705 iterator foo = _events.begin();
708 if (foo == _events.end()) {
710 } else if (++foo == _events.end()) {
717 /* less than 2 points: add a new point */
718 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
721 /* more than 2 points: check to see if the last 2 values
722 are equal. if so, just move the position of the
723 last point. otherwise, add a new point.
726 iterator penultimate = _events.end();
727 --penultimate; /* points at last point */
728 --penultimate; /* points at the penultimate point */
730 if (_events.back()->value == (*penultimate)->value) {
731 _events.back()->when = last_coordinate;
733 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
741 last_val = unlocked_eval (last_coordinate);
742 last_val = max ((double) _min_yval, last_val);
743 last_val = min ((double) _max_yval, last_val);
745 i = _events.rbegin();
747 /* make i point to the last control point */
751 /* now go backwards, removing control points that are
752 beyond the new last coordinate.
755 uint32_t sz = _events.size();
757 while (i != _events.rend() && sz > 2) {
758 AutomationList::reverse_iterator tmp;
763 if ((*i)->when < last_coordinate) {
767 _events.erase (i.base());
773 _events.back()->when = last_coordinate;
774 _events.back()->value = last_val;
777 reposition_for_rt_add (0);
781 maybe_signal_changed ();
785 AutomationList::truncate_start (double overall_length)
788 Glib::Mutex::Lock lm (_lock);
790 double first_legal_value;
791 double first_legal_coordinate;
793 if (_events.empty()) {
794 fatal << _("programming error:")
795 << "AutomationList::truncate_start() called on an empty list"
801 if (overall_length == _events.back()->when) {
802 /* no change in overall length */
806 if (overall_length > _events.back()->when) {
808 /* growing at front: duplicate first point. shift all others */
810 double shift = overall_length - _events.back()->when;
813 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
819 /* less than 2 points: add a new point */
820 _events.push_front (new ControlEvent (0, _events.front()->value));
824 /* more than 2 points: check to see if the first 2 values
825 are equal. if so, just move the position of the
826 first point. otherwise, add a new point.
829 iterator second = _events.begin();
830 ++second; /* points at the second point */
832 if (_events.front()->value == (*second)->value) {
833 /* first segment is flat, just move start point back to zero */
834 _events.front()->when = 0;
836 /* leave non-flat segment in place, add a new leading point. */
837 _events.push_front (new ControlEvent (0, _events.front()->value));
843 /* shrinking at front */
845 first_legal_coordinate = _events.back()->when - overall_length;
846 first_legal_value = unlocked_eval (first_legal_coordinate);
847 first_legal_value = max (_min_yval, first_legal_value);
848 first_legal_value = min (_max_yval, first_legal_value);
850 /* remove all events earlier than the new "front" */
854 while (i != _events.end() && !_events.empty()) {
855 AutomationList::iterator tmp;
860 if ((*i)->when > first_legal_coordinate) {
870 /* shift all remaining points left to keep their same
874 for (i = _events.begin(); i != _events.end(); ++i) {
875 (*i)->when -= first_legal_coordinate;
878 /* add a new point for the interpolated new value */
880 _events.push_front (new ControlEvent (0, first_legal_value));
883 reposition_for_rt_add (0);
888 maybe_signal_changed ();
892 AutomationList::unlocked_eval (double x) const
894 pair<EventList::iterator,EventList::iterator> range;
900 npoints = _events.size();
904 return _default_value;
907 if (x >= _events.front()->when) {
908 return _events.front()->value;
910 // return _default_value;
911 return _events.front()->value;
915 if (x >= _events.back()->when) {
916 return _events.back()->value;
917 } else if (x == _events.front()->when) {
918 return _events.front()->value;
919 } else if (x < _events.front()->when) {
920 // return _default_value;
921 return _events.front()->value;
924 lpos = _events.front()->when;
925 lval = _events.front()->value;
926 upos = _events.back()->when;
927 uval = _events.back()->value;
929 if (_interpolation == Discrete)
932 /* linear interpolation betweeen the two points
935 fraction = (double) (x - lpos) / (double) (upos - lpos);
936 return lval + (fraction * (uval - lval));
940 if (x >= _events.back()->when) {
941 return _events.back()->value;
942 } else if (x == _events.front()->when) {
943 return _events.front()->value;
944 } else if (x < _events.front()->when) {
945 // return _default_value;
946 return _events.front()->value;
949 return multipoint_eval (x);
953 /*NOTREACHED*/ /* stupid gcc */
958 AutomationList::multipoint_eval (double x) const
964 /* "Stepped" lookup (no interpolation) */
965 /* FIXME: no cache. significant? */
966 if (_interpolation == Discrete) {
967 const ControlEvent cp (x, 0);
968 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
970 // shouldn't have made it to multipoint_eval
971 assert(i != _events.end());
973 if (i == _events.begin() || (*i)->when == x)
976 return (*(--i))->value;
979 /* Only do the range lookup if x is in a different range than last time
980 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
981 if ((_lookup_cache.left < 0) ||
982 ((_lookup_cache.left > x) ||
983 (_lookup_cache.range.first == _events.end()) ||
984 ((*_lookup_cache.range.second)->when < x))) {
986 const ControlEvent cp (x, 0);
988 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
991 pair<const_iterator,const_iterator> range = _lookup_cache.range;
993 if (range.first == range.second) {
995 /* x does not exist within the list as a control point */
997 _lookup_cache.left = x;
999 if (range.first != _events.begin()) {
1001 lpos = (*range.first)->when;
1002 lval = (*range.first)->value;
1004 /* we're before the first point */
1005 // return _default_value;
1006 return _events.front()->value;
1009 if (range.second == _events.end()) {
1010 /* we're after the last point */
1011 return _events.back()->value;
1014 upos = (*range.second)->when;
1015 uval = (*range.second)->value;
1017 /* linear interpolation betweeen the two points
1021 fraction = (double) (x - lpos) / (double) (upos - lpos);
1022 return lval + (fraction * (uval - lval));
1026 /* x is a control point in the data */
1027 _lookup_cache.left = -1;
1028 return (*range.first)->value;
1032 AutomationList::build_search_cache_if_necessary(double start, double end) const
1034 /* Only do the range lookup if x is in a different range than last time
1035 * this was called (or if the search cache has been marked "dirty" (left<0) */
1036 if (!_events.empty() && ((_search_cache.left < 0) ||
1037 ((_search_cache.left > start) ||
1038 (_search_cache.right < end)))) {
1040 const ControlEvent start_point (start, 0);
1041 const ControlEvent end_point (end, 0);
1043 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1044 // << start << ".." << end << ")" << endl;
1046 _search_cache.range.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1047 _search_cache.range.second = upper_bound (_events.begin(), _events.end(), &end_point, time_comparator);
1049 _search_cache.left = start;
1050 _search_cache.right = end;
1054 /** Get the earliest event between \a start and \a end, using the current interpolation style.
1056 * If an event is found, \a x and \a y are set to its coordinates.
1058 * \param inclusive Include events with timestamp exactly equal to \a start
1059 * \return true if event is found (and \a x and \a y are valid).
1062 AutomationList::rt_safe_earliest_event(double start, double end, double& x, double& y, bool inclusive) const
1064 // FIXME: It would be nice if this was unnecessary..
1065 Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
1070 return rt_safe_earliest_event_unlocked(start, end, x, y, inclusive);
1074 /** Get the earliest event between \a start and \a end, using the current interpolation style.
1076 * If an event is found, \a x and \a y are set to its coordinates.
1078 * \param inclusive Include events with timestamp exactly equal to \a start
1079 * \return true if event is found (and \a x and \a y are valid).
1082 AutomationList::rt_safe_earliest_event_unlocked(double start, double end, double& x, double& y, bool inclusive) const
1084 if (_interpolation == Discrete)
1085 return rt_safe_earliest_event_discrete_unlocked(start, end, x, y, inclusive);
1087 return rt_safe_earliest_event_linear_unlocked(start, end, x, y, inclusive);
1091 /** Get the earliest event between \a start and \a end (Discrete (lack of) interpolation)
1093 * If an event is found, \a x and \a y are set to its coordinates.
1095 * \param inclusive Include events with timestamp exactly equal to \a start
1096 * \return true if event is found (and \a x and \a y are valid).
1099 AutomationList::rt_safe_earliest_event_discrete_unlocked (double start, double end, double& x, double& y, bool inclusive) const
1101 build_search_cache_if_necessary(start, end);
1103 const pair<const_iterator,const_iterator>& range = _search_cache.range;
1105 if (range.first != _events.end()) {
1106 const ControlEvent* const first = *range.first;
1108 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1110 /* Earliest points is in range, return it */
1111 if (past_start >= start && first->when < end) {
1116 /* Move left of cache to this point
1117 * (Optimize for immediate call this cycle within range) */
1118 _search_cache.left = x;
1119 ++_search_cache.range.first;
1129 /* No points in range */
1135 /** Get the earliest time the line crosses an integer (Linear interpolation).
1137 * If an event is found, \a x and \a y are set to its coordinates.
1139 * \param inclusive Include events with timestamp exactly equal to \a start
1140 * \return true if event is found (and \a x and \a y are valid).
1143 AutomationList::rt_safe_earliest_event_linear_unlocked (double start, double end, double& x, double& y, bool inclusive) const
1145 //cerr << "earliest_event(" << start << ", " << end << ", " << x << ", " << y << ", " << inclusive << endl;
1147 if (_events.size() == 0)
1149 else if (_events.size() == 1)
1150 return rt_safe_earliest_event_discrete_unlocked(start, end, x, y, inclusive);
1152 // Hack to avoid infinitely repeating the same event
1153 build_search_cache_if_necessary(start, end);
1155 pair<const_iterator,const_iterator> range = _search_cache.range;
1157 if (range.first != _events.end()) {
1159 const ControlEvent* first = NULL;
1160 const ControlEvent* next = NULL;
1162 /* Step is after first */
1163 if (range.first == _events.begin() || (*range.first)->when == start) {
1164 first = *range.first;
1165 next = *(++range.first);
1166 ++_search_cache.range.first;
1168 /* Step is before first */
1170 const_iterator prev = range.first;
1173 next = *range.first;
1176 if (inclusive && first->when == start) {
1179 /* Move left of cache to this point
1180 * (Optimize for immediate call this cycle within range) */
1181 _search_cache.left = x;
1182 //++_search_cache.range.first;
1186 if (abs(first->value - next->value) <= 1) {
1187 if (next->when <= end && (!inclusive || next->when > start)) {
1190 /* Move left of cache to this point
1191 * (Optimize for immediate call this cycle within range) */
1192 _search_cache.left = x;
1193 //++_search_cache.range.first;
1200 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1201 //cerr << "start y: " << start_y << endl;
1203 //y = first->value + (slope * fabs(start - first->when));
1206 if (first->value < next->value) // ramping up
1208 else // ramping down
1211 x = first->when + (y - first->value) / (double)slope;
1213 while ((inclusive && x < start) || (x <= start && y != next->value)) {
1215 if (first->value < next->value) // ramping up
1217 else // ramping down
1220 x = first->when + (y - first->value) / (double)slope;
1223 /*cerr << first->value << " @ " << first->when << " ... "
1224 << next->value << " @ " << next->when
1225 << " = " << y << " @ " << x << endl;*/
1227 assert( (y >= first->value && y <= next->value)
1228 || (y <= first->value && y >= next->value) );
1231 const bool past_start = (inclusive ? x >= start : x > start);
1232 if (past_start && x < end) {
1233 /* Move left of cache to this point
1234 * (Optimize for immediate call this cycle within range) */
1235 _search_cache.left = x;
1243 /* No points in the future, so no steps (towards them) in the future */
1250 AutomationList::cut (iterator start, iterator end)
1252 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1255 Glib::Mutex::Lock lm (_lock);
1257 for (iterator x = start; x != end; ) {
1263 nal->_events.push_back (new ControlEvent (**x));
1266 reposition_for_rt_add (0);
1274 maybe_signal_changed ();
1280 AutomationList::cut_copy_clear (double start, double end, int op)
1282 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1284 ControlEvent cp (start, 0.0);
1285 bool changed = false;
1288 Glib::Mutex::Lock lm (_lock);
1290 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1295 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1297 if (op != 2 && (*s)->when != start) {
1298 nal->_events.push_back (new ControlEvent (0, unlocked_eval (start)));
1301 for (iterator x = s; x != e; ) {
1309 /* adjust new points to be relative to start, which
1310 has been set to zero.
1314 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1324 if (op != 2 && nal->_events.back()->when != end - start) {
1325 nal->_events.push_back (new ControlEvent (end - start, unlocked_eval (end)));
1329 reposition_for_rt_add (0);
1335 maybe_signal_changed ();
1342 AutomationList::copy (iterator start, iterator end)
1344 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1347 Glib::Mutex::Lock lm (_lock);
1349 for (iterator x = start; x != end; ) {
1355 nal->_events.push_back (new ControlEvent (**x));
1365 AutomationList::cut (double start, double end)
1367 return cut_copy_clear (start, end, 0);
1371 AutomationList::copy (double start, double end)
1373 return cut_copy_clear (start, end, 1);
1377 AutomationList::clear (double start, double end)
1379 (void) cut_copy_clear (start, end, 2);
1383 AutomationList::paste (AutomationList& alist, double pos, float times)
1385 if (alist._events.empty()) {
1390 Glib::Mutex::Lock lm (_lock);
1394 ControlEvent cp (pos, 0.0);
1396 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1398 for (iterator i = alist.begin();i != alist.end(); ++i) {
1399 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1400 end = (*i)->when + pos;
1404 /* move all points after the insertion along the timeline by
1408 while (where != _events.end()) {
1410 if ((*where)->when <= end) {
1413 _events.erase(where);
1421 reposition_for_rt_add (0);
1425 maybe_signal_changed ();
1430 AutomationList::get_state ()
1432 return state (true);
1436 AutomationList::state (bool full)
1438 XMLNode* root = new XMLNode (X_("AutomationList"));
1440 LocaleGuard lg (X_("POSIX"));
1442 root->add_property ("automation-id", _parameter.to_string());
1444 root->add_property ("id", _id.to_s());
1446 snprintf (buf, sizeof (buf), "%.12g", _default_value);
1447 root->add_property ("default", buf);
1448 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
1449 root->add_property ("min_yval", buf);
1450 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
1451 root->add_property ("max_yval", buf);
1452 snprintf (buf, sizeof (buf), "%.12g", _max_xval);
1453 root->add_property ("max_xval", buf);
1455 root->add_property ("interpolation-style", enum_2_string (_interpolation));
1458 root->add_property ("state", auto_state_to_string (_state));
1460 /* never save anything but Off for automation state to a template */
1461 root->add_property ("state", auto_state_to_string (Off));
1464 root->add_property ("style", auto_style_to_string (_style));
1466 if (!_events.empty()) {
1467 root->add_child_nocopy (serialize_events());
1474 AutomationList::serialize_events ()
1476 XMLNode* node = new XMLNode (X_("events"));
1479 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
1480 str << (double) (*xx)->when;
1482 str <<(double) (*xx)->value;
1486 /* XML is a bit wierd */
1488 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
1489 content_node->set_content (str.str());
1491 node->add_child_nocopy (*content_node);
1497 AutomationList::deserialize_events (const XMLNode& node)
1499 if (node.children().empty()) {
1503 XMLNode* content_node = node.children().front();
1505 if (content_node->content().empty()) {
1512 stringstream str (content_node->content());
1528 fast_simple_add (x, y);
1533 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
1536 reposition_for_rt_add (0);
1537 maybe_signal_changed ();
1546 AutomationList::set_state (const XMLNode& node)
1548 XMLNodeList nlist = node.children();
1550 XMLNodeIterator niter;
1551 const XMLProperty* prop;
1553 if (node.name() == X_("events")) {
1554 /* partial state setting*/
1555 return deserialize_events (node);
1558 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
1560 if ((nsos = node.child (X_("AutomationList")))) {
1561 /* new school in old school clothing */
1562 return set_state (*nsos);
1567 const XMLNodeList& elist = node.children();
1568 XMLNodeConstIterator i;
1576 for (i = elist.begin(); i != elist.end(); ++i) {
1578 if ((prop = (*i)->property ("x")) == 0) {
1579 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
1582 x = atoi (prop->value().c_str());
1584 if ((prop = (*i)->property ("y")) == 0) {
1585 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
1588 y = atof (prop->value().c_str());
1590 fast_simple_add (x, y);
1598 if (node.name() != X_("AutomationList") ) {
1599 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
1603 if ((prop = node.property ("id")) != 0) {
1604 _id = prop->value ();
1605 /* update session AL list */
1606 AutomationListCreated(this);
1609 if ((prop = node.property (X_("automation-id"))) != 0){
1610 _parameter = Parameter(prop->value());
1612 warning << "Legacy session: automation list has no automation-id property.";
1615 if ((prop = node.property (X_("interpolation-style"))) != 0) {
1616 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
1618 _interpolation = Linear;
1621 if ((prop = node.property (X_("default"))) != 0){
1622 _default_value = atof (prop->value().c_str());
1624 _default_value = 0.0;
1627 if ((prop = node.property (X_("style"))) != 0) {
1628 _style = string_to_auto_style (prop->value());
1633 if ((prop = node.property (X_("state"))) != 0) {
1634 _state = string_to_auto_state (prop->value());
1639 if ((prop = node.property (X_("min_yval"))) != 0) {
1640 _min_yval = atof (prop->value ().c_str());
1642 _min_yval = FLT_MIN;
1645 if ((prop = node.property (X_("max_yval"))) != 0) {
1646 _max_yval = atof (prop->value ().c_str());
1648 _max_yval = FLT_MAX;
1651 if ((prop = node.property (X_("max_xval"))) != 0) {
1652 _max_xval = atof (prop->value ().c_str());
1654 _max_xval = 0; // means "no limit ;
1657 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1658 if ((*niter)->name() == X_("events")) {
1659 deserialize_events (*(*niter));