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.
27 #include <sigc++/bind.h>
28 #include <ardour/automation_event.h>
29 #include <pbd/stacktrace.h>
34 using namespace ARDOUR;
38 sigc::signal<void,AutomationList *> AutomationList::AutomationListCreated;
40 static bool sort_events_by_time (ControlEvent* a, ControlEvent* b)
42 return a->when < b->when;
46 static void dumpit (const AutomationList& al, string prefix = "")
48 cerr << prefix << &al << endl;
49 for (AutomationList::const_iterator i = al.const_begin(); i != al.const_end(); ++i) {
50 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
56 AutomationList::AutomationList (double defval)
59 changed_when_thawed = false;
65 max_xval = 0; // means "no limit"
66 default_value = defval;
68 rt_insertion_point = events.end();
69 lookup_cache.left = -1;
70 lookup_cache.range.first = events.end();
73 AutomationListCreated(this);
76 AutomationList::AutomationList (const AutomationList& other)
79 changed_when_thawed = false;
80 _style = other._style;
81 min_yval = other.min_yval;
82 max_yval = other.max_yval;
83 max_xval = other.max_xval;
84 default_value = other.default_value;
85 _state = other._state;
86 _touching = other._touching;
88 rt_insertion_point = events.end();
89 lookup_cache.left = -1;
90 lookup_cache.range.first = events.end();
93 for (const_iterator i = other.events.begin(); i != other.events.end(); ++i) {
94 /* we have to use other point_factory() because
95 its virtual and we're in a constructor.
97 events.push_back (other.point_factory (**i));
101 AutomationListCreated(this);
104 AutomationList::AutomationList (const AutomationList& other, double start, double end)
107 changed_when_thawed = false;
108 _style = other._style;
109 min_yval = other.min_yval;
110 max_yval = other.max_yval;
111 max_xval = other.max_xval;
112 default_value = other.default_value;
113 _state = other._state;
114 _touching = other._touching;
116 rt_insertion_point = events.end();
117 lookup_cache.left = -1;
118 lookup_cache.range.first = events.end();
119 sort_pending = false;
121 /* now grab the relevant points, and shift them back if necessary */
123 AutomationList* section = const_cast<AutomationList*>(&other)->copy (start, end);
125 if (!section->empty()) {
126 for (AutomationList::iterator i = section->begin(); i != section->end(); ++i) {
127 events.push_back (other.point_factory ((*i)->when, (*i)->value));
135 AutomationListCreated(this);
138 AutomationList::AutomationList (const XMLNode& node)
141 changed_when_thawed = false;
145 max_xval = 0; // means "no limit"
149 rt_insertion_point = events.end();
150 lookup_cache.left = -1;
151 lookup_cache.range.first = events.end();
152 sort_pending = false;
156 AutomationListCreated(this);
159 AutomationList::~AutomationList()
163 for (AutomationEventList::iterator x = events.begin(); x != events.end(); ++x) {
169 AutomationList::operator== (const AutomationList& other)
171 return events == other.events;
175 AutomationList::operator= (const AutomationList& other)
177 if (this != &other) {
181 for (const_iterator i = other.events.begin(); i != other.events.end(); ++i) {
182 events.push_back (point_factory (**i));
185 min_yval = other.min_yval;
186 max_yval = other.max_yval;
187 max_xval = other.max_xval;
188 default_value = other.default_value;
191 maybe_signal_changed ();
198 AutomationList::maybe_signal_changed ()
203 changed_when_thawed = true;
210 AutomationList::set_automation_state (AutoState s)
214 automation_state_changed (); /* EMIT SIGNAL */
219 AutomationList::set_automation_style (AutoStyle s)
223 automation_style_changed (); /* EMIT SIGNAL */
228 AutomationList::start_touch ()
235 AutomationList::stop_touch ()
242 AutomationList::clear ()
245 Glib::Mutex::Lock lm (lock);
250 maybe_signal_changed ();
254 AutomationList::x_scale (double factor)
256 Glib::Mutex::Lock lm (lock);
261 AutomationList::extend_to (double when)
263 Glib::Mutex::Lock lm (lock);
264 if (events.empty() || events.back()->when == when) {
267 double factor = when / events.back()->when;
272 void AutomationList::_x_scale (double factor)
274 for (AutomationList::iterator i = events.begin(); i != events.end(); ++i) {
275 (*i)->when = floor ((*i)->when * factor);
282 AutomationList::reposition_for_rt_add (double when)
284 rt_insertion_point = events.end();
287 #define last_rt_insertion_point rt_insertion_point
290 AutomationList::rt_add (double when, double value)
292 /* this is for automation recording */
294 if ((_state & Touch) && !_touching) {
298 // cerr << "RT: alist @ " << this << " add " << value << " @ " << when << endl;
301 Glib::Mutex::Lock lm (lock);
305 ControlEvent cp (when, 0.0);
308 if ((last_rt_insertion_point != events.end()) && ((*last_rt_insertion_point)->when < when) ) {
310 /* we have a previous insertion point, so we should delete
311 everything between it and the position where we are going
312 to insert this point.
315 iterator after = last_rt_insertion_point;
317 if (++after != events.end()) {
318 iterator far = after;
320 while (far != events.end()) {
321 if ((*far)->when > when) {
329 last_rt_insertion_point = where;
331 if((*where)->when == when) {
332 (*where)->value = value;
336 where = events.erase (after, far);
345 iterator previous = last_rt_insertion_point;
348 if (last_rt_insertion_point != events.begin() && (*last_rt_insertion_point)->value == value && (*previous)->value == value) {
349 (*last_rt_insertion_point)->when = when;
356 where = lower_bound (events.begin(), events.end(), &cp, cmp);
358 if (where != events.end()) {
359 if ((*where)->when == when) {
360 (*where)->value = value;
367 last_rt_insertion_point = events.insert (where, point_factory (when, value));
374 maybe_signal_changed ();
378 AutomationList::fast_simple_add (double when, double value)
380 /* to be used only for loading pre-sorted data from saved state */
381 events.insert (events.end(), point_factory (when, value));
384 #undef last_rt_insertion_point
387 AutomationList::add (double when, double value)
389 /* this is for graphical editing */
392 Glib::Mutex::Lock lm (lock);
394 ControlEvent cp (when, 0.0f);
396 iterator insertion_point;
398 for (insertion_point = lower_bound (events.begin(), events.end(), &cp, cmp); insertion_point != events.end(); ++insertion_point) {
400 /* only one point allowed per time point */
402 if ((*insertion_point)->when == when) {
403 (*insertion_point)->value = value;
408 if ((*insertion_point)->when >= when) {
415 events.insert (insertion_point, point_factory (when, value));
416 reposition_for_rt_add (0);
423 maybe_signal_changed ();
427 AutomationList::erase (AutomationList::iterator i)
430 Glib::Mutex::Lock lm (lock);
432 reposition_for_rt_add (0);
435 maybe_signal_changed ();
439 AutomationList::erase (AutomationList::iterator start, AutomationList::iterator end)
442 Glib::Mutex::Lock lm (lock);
443 events.erase (start, end);
444 reposition_for_rt_add (0);
447 maybe_signal_changed ();
451 AutomationList::reset_range (double start, double endt)
456 Glib::Mutex::Lock lm (lock);
458 ControlEvent cp (start, 0.0f);
462 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) != events.end()) {
465 e = upper_bound (events.begin(), events.end(), &cp, cmp);
467 for (iterator i = s; i != e; ++i) {
468 (*i)->value = default_value;
478 maybe_signal_changed ();
483 AutomationList::erase_range (double start, double endt)
488 Glib::Mutex::Lock lm (lock);
490 ControlEvent cp (start, 0.0f);
494 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) != events.end()) {
496 e = upper_bound (events.begin(), events.end(), &cp, cmp);
498 reposition_for_rt_add (0);
506 maybe_signal_changed ();
511 AutomationList::move_range (iterator start, iterator end, double xdelta, double ydelta)
513 /* note: we assume higher level logic is in place to avoid this
514 reordering the time-order of control events in the list. ie. all
515 points after end are later than (end)->when.
519 Glib::Mutex::Lock lm (lock);
521 while (start != end) {
522 (*start)->when += xdelta;
523 (*start)->value += ydelta;
524 if (isnan ((*start)->value)) {
531 events.sort (sort_events_by_time);
539 maybe_signal_changed ();
543 AutomationList::slide (iterator before, double distance)
546 Glib::Mutex::Lock lm (lock);
548 if (before == events.end()) {
552 while (before != events.end()) {
553 (*before)->when += distance;
558 maybe_signal_changed ();
562 AutomationList::modify (iterator iter, double when, double val)
564 /* note: we assume higher level logic is in place to avoid this
565 reordering the time-order of control events in the list. ie. all
566 points after *iter are later than when.
570 Glib::Mutex::Lock lm (lock);
572 (*iter)->when = when;
573 (*iter)->value = val;
580 events.sort (sort_events_by_time);
588 maybe_signal_changed ();
591 std::pair<AutomationList::iterator,AutomationList::iterator>
592 AutomationList::control_points_adjacent (double xval)
594 Glib::Mutex::Lock lm (lock);
597 ControlEvent cp (xval, 0.0f);
598 std::pair<iterator,iterator> ret;
600 ret.first = events.end();
601 ret.second = events.end();
603 for (i = lower_bound (events.begin(), events.end(), &cp, cmp); i != events.end(); ++i) {
605 if (ret.first == events.end()) {
606 if ((*i)->when >= xval) {
607 if (i != events.begin()) {
616 if ((*i)->when > xval) {
626 AutomationList::freeze ()
632 AutomationList::thaw ()
635 PBD::stacktrace (cerr);
636 fatal << string_compose (_("programming error: %1"), X_("AutomationList::thaw() called while not frozen")) << endmsg;
645 Glib::Mutex::Lock lm (lock);
648 events.sort (sort_events_by_time);
649 sort_pending = false;
653 if (changed_when_thawed) {
654 StateChanged(); /* EMIT SIGNAL */
659 AutomationList::set_max_xval (double x)
665 AutomationList::mark_dirty ()
667 lookup_cache.left = -1;
672 AutomationList::truncate_end (double last_coordinate)
675 Glib::Mutex::Lock lm (lock);
676 ControlEvent cp (last_coordinate, 0);
677 list<ControlEvent*>::reverse_iterator i;
680 if (events.empty()) {
684 if (last_coordinate == events.back()->when) {
688 if (last_coordinate > events.back()->when) {
693 iterator foo = events.begin();
696 if (foo == events.end()) {
698 } else if (++foo == events.end()) {
705 /* less than 2 points: add a new point */
706 events.push_back (point_factory (last_coordinate, events.back()->value));
709 /* more than 2 points: check to see if the last 2 values
710 are equal. if so, just move the position of the
711 last point. otherwise, add a new point.
714 iterator penultimate = events.end();
715 --penultimate; /* points at last point */
716 --penultimate; /* points at the penultimate point */
718 if (events.back()->value == (*penultimate)->value) {
719 events.back()->when = last_coordinate;
721 events.push_back (point_factory (last_coordinate, events.back()->value));
729 last_val = unlocked_eval (last_coordinate);
730 last_val = max ((double) min_yval, last_val);
731 last_val = min ((double) max_yval, last_val);
735 /* make i point to the last control point */
739 /* now go backwards, removing control points that are
740 beyond the new last coordinate.
743 uint32_t sz = events.size();
745 while (i != events.rend() && sz > 2) {
746 list<ControlEvent*>::reverse_iterator tmp;
751 if ((*i)->when < last_coordinate) {
755 events.erase (i.base());
761 events.back()->when = last_coordinate;
762 events.back()->value = last_val;
765 reposition_for_rt_add (0);
769 maybe_signal_changed ();
773 AutomationList::truncate_start (double overall_length)
776 Glib::Mutex::Lock lm (lock);
777 AutomationList::iterator i;
778 double first_legal_value;
779 double first_legal_coordinate;
781 if (events.empty()) {
782 fatal << _("programming error:")
783 << "AutomationList::truncate_start() called on an empty list"
789 if (overall_length == events.back()->when) {
790 /* no change in overall length */
794 if (overall_length > events.back()->when) {
796 /* growing at front: duplicate first point. shift all others */
798 double shift = overall_length - events.back()->when;
801 for (np = 0, i = events.begin(); i != events.end(); ++i, ++np) {
807 /* less than 2 points: add a new point */
808 events.push_front (point_factory (0, events.front()->value));
812 /* more than 2 points: check to see if the first 2 values
813 are equal. if so, just move the position of the
814 first point. otherwise, add a new point.
817 iterator second = events.begin();
818 ++second; /* points at the second point */
820 if (events.front()->value == (*second)->value) {
821 /* first segment is flat, just move start point back to zero */
822 events.front()->when = 0;
824 /* leave non-flat segment in place, add a new leading point. */
825 events.push_front (point_factory (0, events.front()->value));
831 /* shrinking at front */
833 first_legal_coordinate = events.back()->when - overall_length;
834 first_legal_value = unlocked_eval (first_legal_coordinate);
835 first_legal_value = max (min_yval, first_legal_value);
836 first_legal_value = min (max_yval, first_legal_value);
838 /* remove all events earlier than the new "front" */
842 while (i != events.end() && !events.empty()) {
843 list<ControlEvent*>::iterator tmp;
848 if ((*i)->when > first_legal_coordinate) {
858 /* shift all remaining points left to keep their same
862 for (i = events.begin(); i != events.end(); ++i) {
863 (*i)->when -= first_legal_coordinate;
866 /* add a new point for the interpolated new value */
868 events.push_front (point_factory (0, first_legal_value));
871 reposition_for_rt_add (0);
876 maybe_signal_changed ();
880 AutomationList::unlocked_eval (double x)
882 return shared_eval (x);
886 AutomationList::shared_eval (double x)
888 pair<AutomationEventList::iterator,AutomationEventList::iterator> range;
894 npoints = events.size();
898 return default_value;
901 if (x >= events.front()->when) {
902 return events.front()->value;
904 // return default_value;
905 return events.front()->value;
909 if (x >= events.back()->when) {
910 return events.back()->value;
911 } else if (x == events.front()->when) {
912 return events.front()->value;
913 } else if (x < events.front()->when) {
914 // return default_value;
915 return events.front()->value;
918 lpos = events.front()->when;
919 lval = events.front()->value;
920 upos = events.back()->when;
921 uval = events.back()->value;
923 /* linear interpolation betweeen the two points
926 fraction = (double) (x - lpos) / (double) (upos - lpos);
927 return lval + (fraction * (uval - lval));
931 if (x >= events.back()->when) {
932 return events.back()->value;
933 } else if (x == events.front()->when) {
934 return events.front()->value;
935 } else if (x < events.front()->when) {
936 // return default_value;
937 return events.front()->value;
940 return multipoint_eval (x);
946 AutomationList::multipoint_eval (double x)
948 pair<AutomationList::iterator,AutomationList::iterator> range;
953 /* only do the range lookup if x is in a different range than last time
954 this was called (or if the lookup cache has been marked "dirty" (left<0)
957 if ((lookup_cache.left < 0) ||
958 ((lookup_cache.left > x) ||
959 (lookup_cache.range.first == events.end()) ||
960 ((*lookup_cache.range.second)->when < x))) {
962 ControlEvent cp (x, 0);
965 lookup_cache.range = equal_range (events.begin(), events.end(), &cp, cmp);
968 range = lookup_cache.range;
970 if (range.first == range.second) {
972 /* x does not exist within the list as a control point */
974 lookup_cache.left = x;
976 if (range.first != events.begin()) {
978 lpos = (*range.first)->when;
979 lval = (*range.first)->value;
981 /* we're before the first point */
982 // return default_value;
983 return events.front()->value;
986 if (range.second == events.end()) {
987 /* we're after the last point */
988 return events.back()->value;
991 upos = (*range.second)->when;
992 uval = (*range.second)->value;
994 /* linear interpolation betweeen the two points
998 fraction = (double) (x - lpos) / (double) (upos - lpos);
999 return lval + (fraction * (uval - lval));
1003 /* x is a control point in the data */
1004 lookup_cache.left = -1;
1005 return (*range.first)->value;
1009 AutomationList::cut (iterator start, iterator end)
1011 AutomationList* nal = new AutomationList (default_value);
1014 Glib::Mutex::Lock lm (lock);
1016 for (iterator x = start; x != end; ) {
1022 nal->events.push_back (point_factory (**x));
1025 reposition_for_rt_add (0);
1033 maybe_signal_changed ();
1039 AutomationList::cut_copy_clear (double start, double end, int op)
1041 AutomationList* nal = new AutomationList (default_value);
1043 ControlEvent cp (start, 0.0);
1045 bool changed = false;
1048 Glib::Mutex::Lock lm (lock);
1050 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) == events.end()) {
1055 e = upper_bound (events.begin(), events.end(), &cp, cmp);
1057 if (op != 2 && (*s)->when != start) {
1058 nal->events.push_back (point_factory (0, unlocked_eval (start)));
1061 for (iterator x = s; x != e; ) {
1069 /* adjust new points to be relative to start, which
1070 has been set to zero.
1074 nal->events.push_back (point_factory ((*x)->when - start, (*x)->value));
1084 if (op != 2 && nal->events.back()->when != end - start) {
1085 nal->events.push_back (point_factory (end - start, unlocked_eval (end)));
1089 reposition_for_rt_add (0);
1095 maybe_signal_changed ();
1102 AutomationList::copy (iterator start, iterator end)
1104 AutomationList* nal = new AutomationList (default_value);
1107 Glib::Mutex::Lock lm (lock);
1109 for (iterator x = start; x != end; ) {
1115 nal->events.push_back (point_factory (**x));
1125 AutomationList::cut (double start, double end)
1127 return cut_copy_clear (start, end, 0);
1131 AutomationList::copy (double start, double end)
1133 return cut_copy_clear (start, end, 1);
1137 AutomationList::clear (double start, double end)
1139 (void) cut_copy_clear (start, end, 2);
1143 AutomationList::paste (AutomationList& alist, double pos, float times)
1145 if (alist.events.empty()) {
1150 Glib::Mutex::Lock lm (lock);
1154 ControlEvent cp (pos, 0.0);
1157 where = upper_bound (events.begin(), events.end(), &cp, cmp);
1159 for (iterator i = alist.begin();i != alist.end(); ++i) {
1160 events.insert (where, point_factory( (*i)->when+pos,( *i)->value));
1161 end = (*i)->when + pos;
1165 /* move all points after the insertion along the timeline by
1169 while (where != events.end()) {
1171 if ((*where)->when <= end) {
1174 events.erase(where);
1182 reposition_for_rt_add (0);
1186 maybe_signal_changed ();
1191 AutomationList::point_factory (double when, double val) const
1193 return new ControlEvent (when, val);
1197 AutomationList::point_factory (const ControlEvent& other) const
1199 return new ControlEvent (other);
1203 AutomationList::get_state ()
1205 return state (true);
1209 AutomationList::state (bool full)
1211 XMLNode* root = new XMLNode (X_("AutomationList"));
1213 LocaleGuard lg (X_("POSIX"));
1215 root->add_property ("id", _id.to_s());
1217 snprintf (buf, sizeof (buf), "%.12g", default_value);
1218 root->add_property ("default", buf);
1219 snprintf (buf, sizeof (buf), "%.12g", min_yval);
1220 root->add_property ("min_yval", buf);
1221 snprintf (buf, sizeof (buf), "%.12g", max_yval);
1222 root->add_property ("max_yval", buf);
1223 snprintf (buf, sizeof (buf), "%.12g", max_xval);
1224 root->add_property ("max_xval", buf);
1227 root->add_property ("state", auto_state_to_string (_state));
1229 /* never save anything but Off for automation state to a template */
1230 root->add_property ("state", auto_state_to_string (Off));
1233 root->add_property ("style", auto_style_to_string (_style));
1235 if (!events.empty()) {
1236 root->add_child_nocopy (serialize_events());
1243 AutomationList::serialize_events ()
1245 XMLNode* node = new XMLNode (X_("events"));
1248 for (iterator xx = events.begin(); xx != events.end(); ++xx) {
1249 str << (double) (*xx)->when;
1251 str <<(double) (*xx)->value;
1255 /* XML is a bit wierd */
1257 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
1258 content_node->set_content (str.str());
1260 node->add_child_nocopy (*content_node);
1266 AutomationList::deserialize_events (const XMLNode& node)
1268 if (node.children().empty()) {
1272 XMLNode* content_node = node.children().front();
1274 if (content_node->content().empty()) {
1281 stringstream str (content_node->content());
1297 fast_simple_add (x, y);
1302 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
1305 reposition_for_rt_add (0);
1306 maybe_signal_changed ();
1315 AutomationList::set_state (const XMLNode& node)
1317 XMLNodeList nlist = node.children();
1319 XMLNodeIterator niter;
1320 const XMLProperty* prop;
1322 if (node.name() == X_("events")) {
1323 /* partial state setting*/
1324 return deserialize_events (node);
1327 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
1329 if ((nsos = node.child (X_("AutomationList")))) {
1330 /* new school in old school clothing */
1331 return set_state (*nsos);
1336 const XMLNodeList& elist = node.children();
1337 XMLNodeConstIterator i;
1345 for (i = elist.begin(); i != elist.end(); ++i) {
1347 if ((prop = (*i)->property ("x")) == 0) {
1348 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
1351 x = atoi (prop->value().c_str());
1353 if ((prop = (*i)->property ("y")) == 0) {
1354 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
1357 y = atof (prop->value().c_str());
1359 fast_simple_add (x, y);
1367 if (node.name() != X_("AutomationList") ) {
1368 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
1372 if ((prop = node.property ("id")) != 0) {
1373 _id = prop->value ();
1374 /* update session AL list */
1375 AutomationListCreated(this);
1378 if ((prop = node.property (X_("default"))) != 0){
1379 default_value = atof (prop->value());
1381 default_value = 0.0;
1384 if ((prop = node.property (X_("style"))) != 0) {
1385 _style = string_to_auto_style (prop->value());
1390 if ((prop = node.property (X_("state"))) != 0) {
1391 _state = string_to_auto_state (prop->value());
1396 if ((prop = node.property (X_("min_yval"))) != 0) {
1397 min_yval = atof (prop->value ());
1402 if ((prop = node.property (X_("max_yval"))) != 0) {
1403 max_yval = atof (prop->value ());
1408 if ((prop = node.property (X_("max_xval"))) != 0) {
1409 max_xval = atof (prop->value ());
1411 max_xval = 0; // means "no limit ;
1414 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1415 if ((*niter)->name() == X_("events")) {
1416 deserialize_events (*(*niter));