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));
401 AutomationList::add (double when, double value)
403 /* this is for graphical editing */
406 Glib::Mutex::Lock lm (_lock);
407 ControlEvent cp (when, 0.0f);
409 iterator insertion_point;
411 for (insertion_point = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); insertion_point != _events.end(); ++insertion_point) {
413 /* only one point allowed per time point */
415 if ((*insertion_point)->when == when) {
416 (*insertion_point)->value = value;
421 if ((*insertion_point)->when >= when) {
428 _events.insert (insertion_point, new ControlEvent (when, value));
429 reposition_for_rt_add (0);
436 maybe_signal_changed ();
440 AutomationList::erase (iterator i)
443 Glib::Mutex::Lock lm (_lock);
445 reposition_for_rt_add (0);
448 maybe_signal_changed ();
452 AutomationList::erase (iterator start, iterator end)
455 Glib::Mutex::Lock lm (_lock);
456 _events.erase (start, end);
457 reposition_for_rt_add (0);
460 maybe_signal_changed ();
464 AutomationList::reset_range (double start, double endt)
469 Glib::Mutex::Lock lm (_lock);
470 ControlEvent cp (start, 0.0f);
474 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) != _events.end()) {
477 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
479 for (iterator i = s; i != e; ++i) {
480 (*i)->value = _default_value;
490 maybe_signal_changed ();
495 AutomationList::erase_range (double start, double endt)
500 Glib::Mutex::Lock lm (_lock);
501 ControlEvent cp (start, 0.0f);
505 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) != _events.end()) {
507 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
508 _events.erase (s, e);
509 reposition_for_rt_add (0);
517 maybe_signal_changed ();
522 AutomationList::move_range (iterator start, iterator end, double xdelta, double ydelta)
524 /* note: we assume higher level logic is in place to avoid this
525 reordering the time-order of control events in the list. ie. all
526 points after end are later than (end)->when.
530 Glib::Mutex::Lock lm (_lock);
532 while (start != end) {
533 (*start)->when += xdelta;
534 (*start)->value += ydelta;
535 if (isnan ((*start)->value)) {
542 _events.sort (sort_events_by_time);
544 _sort_pending = true;
550 maybe_signal_changed ();
554 AutomationList::slide (iterator before, double distance)
557 Glib::Mutex::Lock lm (_lock);
559 if (before == _events.end()) {
563 while (before != _events.end()) {
564 (*before)->when += distance;
569 maybe_signal_changed ();
573 AutomationList::modify (iterator iter, double when, double val)
575 /* note: we assume higher level logic is in place to avoid this
576 reordering the time-order of control events in the list. ie. all
577 points after *iter are later than when.
581 Glib::Mutex::Lock lm (_lock);
583 (*iter)->when = when;
584 (*iter)->value = val;
591 _events.sort (sort_events_by_time);
593 _sort_pending = true;
599 maybe_signal_changed ();
602 std::pair<AutomationList::iterator,AutomationList::iterator>
603 AutomationList::control_points_adjacent (double xval)
605 Glib::Mutex::Lock lm (_lock);
607 ControlEvent cp (xval, 0.0f);
608 std::pair<iterator,iterator> ret;
610 ret.first = _events.end();
611 ret.second = _events.end();
613 for (i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator); i != _events.end(); ++i) {
615 if (ret.first == _events.end()) {
616 if ((*i)->when >= xval) {
617 if (i != _events.begin()) {
626 if ((*i)->when > xval) {
636 AutomationList::freeze ()
642 AutomationList::thaw ()
645 PBD::stacktrace (cerr);
646 fatal << string_compose (_("programming error: %1"), X_("AutomationList::thaw() called while not frozen")) << endmsg;
655 Glib::Mutex::Lock lm (_lock);
658 _events.sort (sort_events_by_time);
659 _sort_pending = false;
663 if (_changed_when_thawed) {
664 StateChanged(); /* EMIT SIGNAL */
669 AutomationList::set_max_xval (double x)
675 AutomationList::mark_dirty ()
677 _lookup_cache.left = -1;
678 _search_cache.left = -1;
679 Dirty (); /* EMIT SIGNAL */
683 AutomationList::truncate_end (double last_coordinate)
686 Glib::Mutex::Lock lm (_lock);
687 ControlEvent cp (last_coordinate, 0);
688 list<ControlEvent*>::reverse_iterator i;
691 if (_events.empty()) {
695 if (last_coordinate == _events.back()->when) {
699 if (last_coordinate > _events.back()->when) {
704 iterator foo = _events.begin();
707 if (foo == _events.end()) {
709 } else if (++foo == _events.end()) {
716 /* less than 2 points: add a new point */
717 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
720 /* more than 2 points: check to see if the last 2 values
721 are equal. if so, just move the position of the
722 last point. otherwise, add a new point.
725 iterator penultimate = _events.end();
726 --penultimate; /* points at last point */
727 --penultimate; /* points at the penultimate point */
729 if (_events.back()->value == (*penultimate)->value) {
730 _events.back()->when = last_coordinate;
732 _events.push_back (new ControlEvent (last_coordinate, _events.back()->value));
740 last_val = unlocked_eval (last_coordinate);
741 last_val = max ((double) _min_yval, last_val);
742 last_val = min ((double) _max_yval, last_val);
744 i = _events.rbegin();
746 /* make i point to the last control point */
750 /* now go backwards, removing control points that are
751 beyond the new last coordinate.
754 uint32_t sz = _events.size();
756 while (i != _events.rend() && sz > 2) {
757 list<ControlEvent*>::reverse_iterator tmp;
762 if ((*i)->when < last_coordinate) {
766 _events.erase (i.base());
772 _events.back()->when = last_coordinate;
773 _events.back()->value = last_val;
776 reposition_for_rt_add (0);
780 maybe_signal_changed ();
784 AutomationList::truncate_start (double overall_length)
787 Glib::Mutex::Lock lm (_lock);
789 double first_legal_value;
790 double first_legal_coordinate;
792 if (_events.empty()) {
793 fatal << _("programming error:")
794 << "AutomationList::truncate_start() called on an empty list"
800 if (overall_length == _events.back()->when) {
801 /* no change in overall length */
805 if (overall_length > _events.back()->when) {
807 /* growing at front: duplicate first point. shift all others */
809 double shift = overall_length - _events.back()->when;
812 for (np = 0, i = _events.begin(); i != _events.end(); ++i, ++np) {
818 /* less than 2 points: add a new point */
819 _events.push_front (new ControlEvent (0, _events.front()->value));
823 /* more than 2 points: check to see if the first 2 values
824 are equal. if so, just move the position of the
825 first point. otherwise, add a new point.
828 iterator second = _events.begin();
829 ++second; /* points at the second point */
831 if (_events.front()->value == (*second)->value) {
832 /* first segment is flat, just move start point back to zero */
833 _events.front()->when = 0;
835 /* leave non-flat segment in place, add a new leading point. */
836 _events.push_front (new ControlEvent (0, _events.front()->value));
842 /* shrinking at front */
844 first_legal_coordinate = _events.back()->when - overall_length;
845 first_legal_value = unlocked_eval (first_legal_coordinate);
846 first_legal_value = max (_min_yval, first_legal_value);
847 first_legal_value = min (_max_yval, first_legal_value);
849 /* remove all events earlier than the new "front" */
853 while (i != _events.end() && !_events.empty()) {
854 list<ControlEvent*>::iterator tmp;
859 if ((*i)->when > first_legal_coordinate) {
869 /* shift all remaining points left to keep their same
873 for (i = _events.begin(); i != _events.end(); ++i) {
874 (*i)->when -= first_legal_coordinate;
877 /* add a new point for the interpolated new value */
879 _events.push_front (new ControlEvent (0, first_legal_value));
882 reposition_for_rt_add (0);
887 maybe_signal_changed ();
891 AutomationList::unlocked_eval (double x) const
893 pair<EventList::iterator,EventList::iterator> range;
899 npoints = _events.size();
903 return _default_value;
906 if (x >= _events.front()->when) {
907 return _events.front()->value;
909 // return _default_value;
910 return _events.front()->value;
914 if (x >= _events.back()->when) {
915 return _events.back()->value;
916 } else if (x == _events.front()->when) {
917 return _events.front()->value;
918 } else if (x < _events.front()->when) {
919 // return _default_value;
920 return _events.front()->value;
923 lpos = _events.front()->when;
924 lval = _events.front()->value;
925 upos = _events.back()->when;
926 uval = _events.back()->value;
928 if (_interpolation == Discrete)
931 /* linear interpolation betweeen the two points
934 fraction = (double) (x - lpos) / (double) (upos - lpos);
935 return lval + (fraction * (uval - lval));
939 if (x >= _events.back()->when) {
940 return _events.back()->value;
941 } else if (x == _events.front()->when) {
942 return _events.front()->value;
943 } else if (x < _events.front()->when) {
944 // return _default_value;
945 return _events.front()->value;
948 return multipoint_eval (x);
954 AutomationList::multipoint_eval (double x) const
960 /* "Stepped" lookup (no interpolation) */
961 /* FIXME: no cache. significant? */
962 if (_interpolation == Discrete) {
963 const ControlEvent cp (x, 0);
964 EventList::const_iterator i = lower_bound (_events.begin(), _events.end(), &cp, time_comparator);
966 // shouldn't have made it to multipoint_eval
967 assert(i != _events.end());
969 if (i == _events.begin() || (*i)->when == x)
972 return (*(--i))->value;
975 /* Only do the range lookup if x is in a different range than last time
976 * this was called (or if the lookup cache has been marked "dirty" (left<0) */
977 if ((_lookup_cache.left < 0) ||
978 ((_lookup_cache.left > x) ||
979 (_lookup_cache.range.first == _events.end()) ||
980 ((*_lookup_cache.range.second)->when < x))) {
982 const ControlEvent cp (x, 0);
984 _lookup_cache.range = equal_range (_events.begin(), _events.end(), &cp, time_comparator);
987 pair<const_iterator,const_iterator> range = _lookup_cache.range;
989 if (range.first == range.second) {
991 /* x does not exist within the list as a control point */
993 _lookup_cache.left = x;
995 if (range.first != _events.begin()) {
997 lpos = (*range.first)->when;
998 lval = (*range.first)->value;
1000 /* we're before the first point */
1001 // return _default_value;
1002 return _events.front()->value;
1005 if (range.second == _events.end()) {
1006 /* we're after the last point */
1007 return _events.back()->value;
1010 upos = (*range.second)->when;
1011 uval = (*range.second)->value;
1013 /* linear interpolation betweeen the two points
1017 fraction = (double) (x - lpos) / (double) (upos - lpos);
1018 return lval + (fraction * (uval - lval));
1022 /* x is a control point in the data */
1023 _lookup_cache.left = -1;
1024 return (*range.first)->value;
1028 AutomationList::build_search_cache_if_necessary(double start, double end) const
1030 /* Only do the range lookup if x is in a different range than last time
1031 * this was called (or if the search cache has been marked "dirty" (left<0) */
1032 if ((_search_cache.left < 0) ||
1033 ((_search_cache.left > start) ||
1034 (_search_cache.right < end))) {
1036 const ControlEvent start_point (start, 0);
1037 const ControlEvent end_point (end, 0);
1039 //cerr << "REBUILD: (" << _search_cache.left << ".." << _search_cache.right << ") := ("
1040 // << start << ".." << end << ")" << endl;
1042 _search_cache.range.first = lower_bound (_events.begin(), _events.end(), &start_point, time_comparator);
1043 _search_cache.range.second = upper_bound (_events.begin(), _events.end(), &end_point, time_comparator);
1045 _search_cache.left = start;
1046 _search_cache.right = end;
1050 /** Get the earliest event between \a start and \a end, using the current interpolation style.
1052 * If an event is found, \a x and \a y are set to its coordinates.
1054 * \param inclusive Include events with timestamp exactly equal to \a start
1055 * \return true if event is found (and \a x and \a y are valid).
1058 AutomationList::rt_safe_earliest_event(double start, double end, double& x, double& y, bool inclusive) const
1060 // FIXME: It would be nice if this was unnecessary..
1061 Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
1066 return rt_safe_earliest_event_unlocked(start, end, x, y, inclusive);
1070 /** Get the earliest event between \a start and \a end, using the current interpolation style.
1072 * If an event is found, \a x and \a y are set to its coordinates.
1074 * \param inclusive Include events with timestamp exactly equal to \a start
1075 * \return true if event is found (and \a x and \a y are valid).
1078 AutomationList::rt_safe_earliest_event_unlocked(double start, double end, double& x, double& y, bool inclusive) const
1080 if (_interpolation == Discrete)
1081 return rt_safe_earliest_event_discrete_unlocked(start, end, x, y, inclusive);
1083 return rt_safe_earliest_event_linear_unlocked(start, end, x, y, inclusive);
1087 /** Get the earliest event between \a start and \a end (Discrete (lack of) interpolation)
1089 * If an event is found, \a x and \a y are set to its coordinates.
1091 * \param inclusive Include events with timestamp exactly equal to \a start
1092 * \return true if event is found (and \a x and \a y are valid).
1095 AutomationList::rt_safe_earliest_event_discrete_unlocked (double start, double end, double& x, double& y, bool inclusive) const
1097 build_search_cache_if_necessary(start, end);
1099 const pair<const_iterator,const_iterator>& range = _search_cache.range;
1101 if (range.first != _events.end()) {
1102 const ControlEvent* const first = *range.first;
1104 const bool past_start = (inclusive ? first->when >= start : first->when > start);
1106 /* Earliest points is in range, return it */
1107 if (past_start >= start && first->when < end) {
1112 /* Move left of cache to this point
1113 * (Optimize for immediate call this cycle within range) */
1114 _search_cache.left = x;
1115 ++_search_cache.range.first;
1125 /* No points in range */
1131 /** Get the earliest time the line crosses an integer (Linear interpolation).
1133 * If an event is found, \a x and \a y are set to its coordinates.
1135 * \param inclusive Include events with timestamp exactly equal to \a start
1136 * \return true if event is found (and \a x and \a y are valid).
1139 AutomationList::rt_safe_earliest_event_linear_unlocked (double start, double end, double& x, double& y, bool inclusive) const
1141 //cerr << "earliest_event(" << start << ", " << end << ", " << x << ", " << y << ", " << inclusive << endl;
1143 if (_events.size() < 2)
1144 return rt_safe_earliest_event_discrete_unlocked(start, end, x, y, inclusive);
1146 // Hack to avoid infinitely repeating the same event
1147 build_search_cache_if_necessary(start, end);
1149 pair<const_iterator,const_iterator> range = _search_cache.range;
1151 if (range.first != _events.end()) {
1153 const ControlEvent* first = NULL;
1154 const ControlEvent* next = NULL;
1156 /* Step is after first */
1157 if (range.first == _events.begin() || (*range.first)->when == start) {
1158 first = *range.first;
1159 next = *(++range.first);
1160 ++_search_cache.range.first;
1162 /* Step is before first */
1164 const_iterator prev = range.first;
1167 next = *range.first;
1170 if (inclusive && first->when == start) {
1173 /* Move left of cache to this point
1174 * (Optimize for immediate call this cycle within range) */
1175 _search_cache.left = x;
1176 //++_search_cache.range.first;
1180 if (abs(first->value - next->value) <= 1) {
1181 if (next->when <= end && (!inclusive || next->when > start)) {
1184 /* Move left of cache to this point
1185 * (Optimize for immediate call this cycle within range) */
1186 _search_cache.left = x;
1187 //++_search_cache.range.first;
1194 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1195 //cerr << "start y: " << start_y << endl;
1197 //y = first->value + (slope * fabs(start - first->when));
1200 if (first->value < next->value) // ramping up
1202 else // ramping down
1205 x = first->when + (y - first->value) / (double)slope;
1207 while ((inclusive && x < start) || x <= start && y != next->value) {
1209 if (first->value < next->value) // ramping up
1211 else // ramping down
1214 x = first->when + (y - first->value) / (double)slope;
1217 /*cerr << first->value << " @ " << first->when << " ... "
1218 << next->value << " @ " << next->when
1219 << " = " << y << " @ " << x << endl;*/
1221 assert( (y >= first->value && y <= next->value)
1222 || (y <= first->value && y >= next->value) );
1225 const bool past_start = (inclusive ? x >= start : x > start);
1226 if (past_start && x < end) {
1227 /* Move left of cache to this point
1228 * (Optimize for immediate call this cycle within range) */
1229 _search_cache.left = x;
1237 /* No points in the future, so no steps (towards them) in the future */
1244 AutomationList::cut (iterator start, iterator end)
1246 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1249 Glib::Mutex::Lock lm (_lock);
1251 for (iterator x = start; x != end; ) {
1257 nal->_events.push_back (new ControlEvent (**x));
1260 reposition_for_rt_add (0);
1268 maybe_signal_changed ();
1274 AutomationList::cut_copy_clear (double start, double end, int op)
1276 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1278 ControlEvent cp (start, 0.0);
1279 bool changed = false;
1282 Glib::Mutex::Lock lm (_lock);
1284 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1289 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1291 if (op != 2 && (*s)->when != start) {
1292 nal->_events.push_back (new ControlEvent (0, unlocked_eval (start)));
1295 for (iterator x = s; x != e; ) {
1303 /* adjust new points to be relative to start, which
1304 has been set to zero.
1308 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1318 if (op != 2 && nal->_events.back()->when != end - start) {
1319 nal->_events.push_back (new ControlEvent (end - start, unlocked_eval (end)));
1323 reposition_for_rt_add (0);
1329 maybe_signal_changed ();
1336 AutomationList::copy (iterator start, iterator end)
1338 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1341 Glib::Mutex::Lock lm (_lock);
1343 for (iterator x = start; x != end; ) {
1349 nal->_events.push_back (new ControlEvent (**x));
1359 AutomationList::cut (double start, double end)
1361 return cut_copy_clear (start, end, 0);
1365 AutomationList::copy (double start, double end)
1367 return cut_copy_clear (start, end, 1);
1371 AutomationList::clear (double start, double end)
1373 (void) cut_copy_clear (start, end, 2);
1377 AutomationList::paste (AutomationList& alist, double pos, float times)
1379 if (alist._events.empty()) {
1384 Glib::Mutex::Lock lm (_lock);
1388 ControlEvent cp (pos, 0.0);
1390 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1392 for (iterator i = alist.begin();i != alist.end(); ++i) {
1393 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1394 end = (*i)->when + pos;
1398 /* move all points after the insertion along the timeline by
1402 while (where != _events.end()) {
1404 if ((*where)->when <= end) {
1407 _events.erase(where);
1415 reposition_for_rt_add (0);
1419 maybe_signal_changed ();
1424 AutomationList::get_state ()
1426 return state (true);
1430 AutomationList::state (bool full)
1432 XMLNode* root = new XMLNode (X_("AutomationList"));
1434 LocaleGuard lg (X_("POSIX"));
1436 root->add_property ("automation-id", _parameter.to_string());
1438 root->add_property ("id", _id.to_s());
1440 snprintf (buf, sizeof (buf), "%.12g", _default_value);
1441 root->add_property ("default", buf);
1442 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
1443 root->add_property ("min_yval", buf);
1444 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
1445 root->add_property ("max_yval", buf);
1446 snprintf (buf, sizeof (buf), "%.12g", _max_xval);
1447 root->add_property ("max_xval", buf);
1449 root->add_property ("interpolation-style", enum_2_string (_interpolation));
1452 root->add_property ("state", auto_state_to_string (_state));
1454 /* never save anything but Off for automation state to a template */
1455 root->add_property ("state", auto_state_to_string (Off));
1458 root->add_property ("style", auto_style_to_string (_style));
1460 if (!_events.empty()) {
1461 root->add_child_nocopy (serialize_events());
1468 AutomationList::serialize_events ()
1470 XMLNode* node = new XMLNode (X_("events"));
1473 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
1474 str << (double) (*xx)->when;
1476 str <<(double) (*xx)->value;
1480 /* XML is a bit wierd */
1482 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
1483 content_node->set_content (str.str());
1485 node->add_child_nocopy (*content_node);
1491 AutomationList::deserialize_events (const XMLNode& node)
1493 if (node.children().empty()) {
1497 XMLNode* content_node = node.children().front();
1499 if (content_node->content().empty()) {
1506 stringstream str (content_node->content());
1522 fast_simple_add (x, y);
1527 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
1530 reposition_for_rt_add (0);
1531 maybe_signal_changed ();
1540 AutomationList::set_state (const XMLNode& node)
1542 XMLNodeList nlist = node.children();
1544 XMLNodeIterator niter;
1545 const XMLProperty* prop;
1547 if (node.name() == X_("events")) {
1548 /* partial state setting*/
1549 return deserialize_events (node);
1552 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
1554 if ((nsos = node.child (X_("AutomationList")))) {
1555 /* new school in old school clothing */
1556 return set_state (*nsos);
1561 const XMLNodeList& elist = node.children();
1562 XMLNodeConstIterator i;
1570 for (i = elist.begin(); i != elist.end(); ++i) {
1572 if ((prop = (*i)->property ("x")) == 0) {
1573 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
1576 x = atoi (prop->value().c_str());
1578 if ((prop = (*i)->property ("y")) == 0) {
1579 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
1582 y = atof (prop->value().c_str());
1584 fast_simple_add (x, y);
1592 if (node.name() != X_("AutomationList") ) {
1593 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
1597 if ((prop = node.property ("id")) != 0) {
1598 _id = prop->value ();
1599 /* update session AL list */
1600 AutomationListCreated(this);
1603 if ((prop = node.property (X_("automation-id"))) != 0){
1604 _parameter = Parameter(prop->value());
1606 warning << "Legacy session: automation list has no automation-id property.";
1609 if ((prop = node.property (X_("interpolation-style"))) != 0) {
1610 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
1612 _interpolation = Linear;
1615 if ((prop = node.property (X_("default"))) != 0){
1616 _default_value = atof (prop->value().c_str());
1618 _default_value = 0.0;
1621 if ((prop = node.property (X_("style"))) != 0) {
1622 _style = string_to_auto_style (prop->value());
1627 if ((prop = node.property (X_("state"))) != 0) {
1628 _state = string_to_auto_state (prop->value());
1633 if ((prop = node.property (X_("min_yval"))) != 0) {
1634 _min_yval = atof (prop->value ().c_str());
1636 _min_yval = FLT_MIN;
1639 if ((prop = node.property (X_("max_yval"))) != 0) {
1640 _max_yval = atof (prop->value ().c_str());
1642 _max_yval = FLT_MAX;
1645 if ((prop = node.property (X_("max_xval"))) != 0) {
1646 _max_xval = atof (prop->value ().c_str());
1648 _max_xval = 0; // means "no limit ;
1651 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1652 if ((*niter)->name() == X_("events")) {
1653 deserialize_events (*(*niter));