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.
1053 * \return true if event is found (and \a x and \a y are valid).
1056 AutomationList::rt_safe_earliest_event(double start, double end, double& x, double& y) const
1058 if (_interpolation == Discrete)
1059 return rt_safe_earliest_event_discrete(start, end, x, y);
1061 return rt_safe_earliest_event_linear(start, end, x, y);
1064 /** Get the earliest event between \a start and \a end (Discrete (lack of) interpolation)
1066 * If an event is found, \a x and \a y are set to its coordinates.
1067 * \return true if event is found (and \a x and \a y are valid).
1070 AutomationList::rt_safe_earliest_event_discrete (double start, double end, double& x, double& y) const
1072 // FIXME: It would be nice if this was unnecessary..
1073 Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
1078 build_search_cache_if_necessary(start, end);
1080 pair<const_iterator,const_iterator> range = _search_cache.range;
1082 if (range.first != _events.end()) {
1083 const ControlEvent* const first = *range.first;
1085 /* Earliest points is in range, return it */
1086 if (first->when >= start && first->when < end) {
1091 /* Move left of cache to this point
1092 * (Optimize for immediate call this cycle within range) */
1093 _search_cache.left = x;
1094 ++_search_cache.range.first;
1105 /* No points in range */
1111 /** Get the earliest time the line crosses an integer (Linear interpolation).
1113 * If an event is found, \a x and \a y are set to its coordinates.
1114 * \return true if event is found (and \a x and \a y are valid).
1117 AutomationList::rt_safe_earliest_event_linear (double start, double end, double& x, double& y) const
1119 // FIXME: It would be nice if this was unnecessary..
1120 Glib::Mutex::Lock lm(_lock, Glib::TRY_LOCK);
1125 if (_events.size() < 2)
1128 build_search_cache_if_necessary(start, end);
1130 pair<const_iterator,const_iterator> range = _search_cache.range;
1132 if (range.first != _events.end()) {
1134 const ControlEvent* first = NULL;
1135 const ControlEvent* next = NULL;
1137 /* Step is after first */
1138 if (range.first == _events.begin() || (*range.first)->when == start) {
1139 first = *range.first;
1140 next = *(++range.first);
1142 /* Step is before first */
1144 const_iterator prev = range.first;
1147 next = *range.first;
1150 if (first->when == start) {
1153 /* Move left of cache to this point
1154 * (Optimize for immediate call this cycle within range) */
1155 _search_cache.left = x;
1156 ++_search_cache.range.first;
1160 /*cerr << first->value << " @ " << first->when << " ---> "
1161 << next->value << " @ " << next->when << endl;*/
1163 const double slope = (next->value - first->value) / (double)(next->when - first->when);
1165 const double start_y = first->value + (slope * (start - first->when));
1166 //cerr << "start y: " << start_y << endl;
1168 if (start_y >= first->value) {
1169 x = first->when + ((ceil(start_y) - first->value) / (double)slope);
1172 x = first->when + ((floor(start_y) - first->value) / (double)slope);
1176 if (x >= start && x < end) {
1177 //cerr << y << " @ " << x << endl;
1181 /* Move left of cache to this point
1182 * (Optimize for immediate call this cycle within range) */
1183 _search_cache.left = x;
1185 if (x >= next->when)
1186 ++_search_cache.range.first;
1191 //cerr << "\tNo: " << start_y << ", " << x << endl;
1195 /* No points in the future, so no steps (towards them) in the future */
1202 AutomationList::cut (iterator start, iterator end)
1204 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1207 Glib::Mutex::Lock lm (_lock);
1209 for (iterator x = start; x != end; ) {
1215 nal->_events.push_back (new ControlEvent (**x));
1218 reposition_for_rt_add (0);
1226 maybe_signal_changed ();
1232 AutomationList::cut_copy_clear (double start, double end, int op)
1234 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1236 ControlEvent cp (start, 0.0);
1237 bool changed = false;
1240 Glib::Mutex::Lock lm (_lock);
1242 if ((s = lower_bound (_events.begin(), _events.end(), &cp, time_comparator)) == _events.end()) {
1247 e = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1249 if (op != 2 && (*s)->when != start) {
1250 nal->_events.push_back (new ControlEvent (0, unlocked_eval (start)));
1253 for (iterator x = s; x != e; ) {
1261 /* adjust new points to be relative to start, which
1262 has been set to zero.
1266 nal->_events.push_back (new ControlEvent ((*x)->when - start, (*x)->value));
1276 if (op != 2 && nal->_events.back()->when != end - start) {
1277 nal->_events.push_back (new ControlEvent (end - start, unlocked_eval (end)));
1281 reposition_for_rt_add (0);
1287 maybe_signal_changed ();
1294 AutomationList::copy (iterator start, iterator end)
1296 AutomationList* nal = new AutomationList (_parameter, _min_yval, _max_yval, _default_value);
1299 Glib::Mutex::Lock lm (_lock);
1301 for (iterator x = start; x != end; ) {
1307 nal->_events.push_back (new ControlEvent (**x));
1317 AutomationList::cut (double start, double end)
1319 return cut_copy_clear (start, end, 0);
1323 AutomationList::copy (double start, double end)
1325 return cut_copy_clear (start, end, 1);
1329 AutomationList::clear (double start, double end)
1331 (void) cut_copy_clear (start, end, 2);
1335 AutomationList::paste (AutomationList& alist, double pos, float times)
1337 if (alist._events.empty()) {
1342 Glib::Mutex::Lock lm (_lock);
1346 ControlEvent cp (pos, 0.0);
1348 where = upper_bound (_events.begin(), _events.end(), &cp, time_comparator);
1350 for (iterator i = alist.begin();i != alist.end(); ++i) {
1351 _events.insert (where, new ControlEvent( (*i)->when+pos,( *i)->value));
1352 end = (*i)->when + pos;
1356 /* move all points after the insertion along the timeline by
1360 while (where != _events.end()) {
1362 if ((*where)->when <= end) {
1365 _events.erase(where);
1373 reposition_for_rt_add (0);
1377 maybe_signal_changed ();
1382 AutomationList::get_state ()
1384 return state (true);
1388 AutomationList::state (bool full)
1390 XMLNode* root = new XMLNode (X_("AutomationList"));
1392 LocaleGuard lg (X_("POSIX"));
1394 root->add_property ("automation-id", _parameter.to_string());
1396 root->add_property ("id", _id.to_s());
1398 snprintf (buf, sizeof (buf), "%.12g", _default_value);
1399 root->add_property ("default", buf);
1400 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
1401 root->add_property ("min_yval", buf);
1402 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
1403 root->add_property ("max_yval", buf);
1404 snprintf (buf, sizeof (buf), "%.12g", _max_xval);
1405 root->add_property ("max_xval", buf);
1407 root->add_property ("interpolation-style", enum_2_string (_interpolation));
1410 root->add_property ("state", auto_state_to_string (_state));
1412 /* never save anything but Off for automation state to a template */
1413 root->add_property ("state", auto_state_to_string (Off));
1416 root->add_property ("style", auto_style_to_string (_style));
1418 if (!_events.empty()) {
1419 root->add_child_nocopy (serialize_events());
1426 AutomationList::serialize_events ()
1428 XMLNode* node = new XMLNode (X_("events"));
1431 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
1432 str << (double) (*xx)->when;
1434 str <<(double) (*xx)->value;
1438 /* XML is a bit wierd */
1440 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
1441 content_node->set_content (str.str());
1443 node->add_child_nocopy (*content_node);
1449 AutomationList::deserialize_events (const XMLNode& node)
1451 if (node.children().empty()) {
1455 XMLNode* content_node = node.children().front();
1457 if (content_node->content().empty()) {
1464 stringstream str (content_node->content());
1480 fast_simple_add (x, y);
1485 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
1488 reposition_for_rt_add (0);
1489 maybe_signal_changed ();
1498 AutomationList::set_state (const XMLNode& node)
1500 XMLNodeList nlist = node.children();
1502 XMLNodeIterator niter;
1503 const XMLProperty* prop;
1505 if (node.name() == X_("events")) {
1506 /* partial state setting*/
1507 return deserialize_events (node);
1510 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
1512 if ((nsos = node.child (X_("AutomationList")))) {
1513 /* new school in old school clothing */
1514 return set_state (*nsos);
1519 const XMLNodeList& elist = node.children();
1520 XMLNodeConstIterator i;
1528 for (i = elist.begin(); i != elist.end(); ++i) {
1530 if ((prop = (*i)->property ("x")) == 0) {
1531 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
1534 x = atoi (prop->value().c_str());
1536 if ((prop = (*i)->property ("y")) == 0) {
1537 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
1540 y = atof (prop->value().c_str());
1542 fast_simple_add (x, y);
1550 if (node.name() != X_("AutomationList") ) {
1551 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
1555 if ((prop = node.property ("id")) != 0) {
1556 _id = prop->value ();
1557 /* update session AL list */
1558 AutomationListCreated(this);
1561 if ((prop = node.property (X_("automation-id"))) != 0){
1562 _parameter = Parameter(prop->value());
1564 warning << "Legacy session: automation list has no automation-id property.";
1567 if ((prop = node.property (X_("interpolation-style"))) != 0) {
1568 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
1570 _interpolation = Linear;
1573 if ((prop = node.property (X_("default"))) != 0){
1574 _default_value = atof (prop->value().c_str());
1576 _default_value = 0.0;
1579 if ((prop = node.property (X_("style"))) != 0) {
1580 _style = string_to_auto_style (prop->value());
1585 if ((prop = node.property (X_("state"))) != 0) {
1586 _state = string_to_auto_state (prop->value());
1591 if ((prop = node.property (X_("min_yval"))) != 0) {
1592 _min_yval = atof (prop->value ().c_str());
1594 _min_yval = FLT_MIN;
1597 if ((prop = node.property (X_("max_yval"))) != 0) {
1598 _max_yval = atof (prop->value ().c_str());
1600 _max_yval = FLT_MAX;
1603 if ((prop = node.property (X_("max_xval"))) != 0) {
1604 _max_xval = atof (prop->value ().c_str());
1606 _max_xval = 0; // means "no limit ;
1609 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1610 if ((*niter)->name() == X_("events")) {
1611 deserialize_events (*(*niter));