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/automation_event.h>
28 #include <pbd/stacktrace.h>
33 using namespace ARDOUR;
37 sigc::signal<void,AutomationList *> AutomationList::AutomationListCreated;
39 static bool sort_events_by_time (ControlEvent* a, ControlEvent* b)
41 return a->when < b->when;
45 static void dumpit (const AutomationList& al, string prefix = "")
47 cerr << prefix << &al << endl;
48 for (AutomationList::const_iterator i = al.const_begin(); i != al.const_end(); ++i) {
49 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
55 AutomationList::AutomationList (double defval)
58 changed_when_thawed = false;
64 max_xval = 0; // means "no limit"
65 default_value = defval;
67 rt_insertion_point = events.end();
68 lookup_cache.left = -1;
69 lookup_cache.range.first = events.end();
72 AutomationListCreated(this);
75 AutomationList::AutomationList (const AutomationList& other)
78 changed_when_thawed = false;
79 _style = other._style;
80 min_yval = other.min_yval;
81 max_yval = other.max_yval;
82 max_xval = other.max_xval;
83 default_value = other.default_value;
84 _state = other._state;
85 _touching = other._touching;
87 rt_insertion_point = events.end();
88 lookup_cache.left = -1;
89 lookup_cache.range.first = events.end();
92 for (const_iterator i = other.events.begin(); i != other.events.end(); ++i) {
93 /* we have to use other point_factory() because
94 its virtual and we're in a constructor.
96 events.push_back (other.point_factory (**i));
100 AutomationListCreated(this);
103 AutomationList::AutomationList (const AutomationList& other, double start, double end)
106 changed_when_thawed = false;
107 _style = other._style;
108 min_yval = other.min_yval;
109 max_yval = other.max_yval;
110 max_xval = other.max_xval;
111 default_value = other.default_value;
112 _state = other._state;
113 _touching = other._touching;
115 rt_insertion_point = events.end();
116 lookup_cache.left = -1;
117 lookup_cache.range.first = events.end();
118 sort_pending = false;
120 /* now grab the relevant points, and shift them back if necessary */
122 AutomationList* section = const_cast<AutomationList*>(&other)->copy (start, end);
124 if (!section->empty()) {
125 for (AutomationList::iterator i = section->begin(); i != section->end(); ++i) {
126 events.push_back (other.point_factory ((*i)->when, (*i)->value));
134 AutomationListCreated(this);
137 AutomationList::AutomationList (const XMLNode& node)
140 changed_when_thawed = false;
144 max_xval = 0; // means "no limit"
148 rt_insertion_point = events.end();
149 lookup_cache.left = -1;
150 lookup_cache.range.first = events.end();
151 sort_pending = false;
155 AutomationListCreated(this);
158 AutomationList::~AutomationList()
162 for (AutomationEventList::iterator x = events.begin(); x != events.end(); ++x) {
168 AutomationList::operator== (const AutomationList& other)
170 return events == other.events;
174 AutomationList::operator= (const AutomationList& other)
176 if (this != &other) {
180 for (const_iterator i = other.events.begin(); i != other.events.end(); ++i) {
181 events.push_back (point_factory (**i));
184 min_yval = other.min_yval;
185 max_yval = other.max_yval;
186 max_xval = other.max_xval;
187 default_value = other.default_value;
190 maybe_signal_changed ();
197 AutomationList::maybe_signal_changed ()
202 changed_when_thawed = true;
209 AutomationList::set_automation_state (AutoState s)
213 automation_state_changed (); /* EMIT SIGNAL */
218 AutomationList::set_automation_style (AutoStyle s)
222 automation_style_changed (); /* EMIT SIGNAL */
227 AutomationList::start_touch ()
234 AutomationList::stop_touch ()
241 AutomationList::clear ()
244 Glib::Mutex::Lock lm (lock);
249 maybe_signal_changed ();
253 AutomationList::x_scale (double factor)
255 Glib::Mutex::Lock lm (lock);
260 AutomationList::extend_to (double when)
262 Glib::Mutex::Lock lm (lock);
263 if (events.empty() || events.back()->when == when) {
266 double factor = when / events.back()->when;
271 void AutomationList::_x_scale (double factor)
273 for (AutomationList::iterator i = events.begin(); i != events.end(); ++i) {
274 (*i)->when = floor ((*i)->when * factor);
281 AutomationList::reposition_for_rt_add (double when)
283 rt_insertion_point = events.end();
286 #define last_rt_insertion_point rt_insertion_point
289 AutomationList::rt_add (double when, double value)
291 /* this is for automation recording */
293 if ((_state & Touch) && !_touching) {
297 // cerr << "RT: alist @ " << this << " add " << value << " @ " << when << endl;
300 Glib::Mutex::Lock lm (lock);
304 ControlEvent cp (when, 0.0);
307 if ((last_rt_insertion_point != events.end()) && ((*last_rt_insertion_point)->when < when) ) {
309 /* we have a previous insertion point, so we should delete
310 everything between it and the position where we are going
311 to insert this point.
314 iterator after = last_rt_insertion_point;
316 if (++after != events.end()) {
317 iterator far = after;
319 while (far != events.end()) {
320 if ((*far)->when > when) {
328 last_rt_insertion_point = where;
330 if((*where)->when == when) {
331 (*where)->value = value;
335 where = events.erase (after, far);
344 iterator previous = last_rt_insertion_point;
347 if (last_rt_insertion_point != events.begin() && (*last_rt_insertion_point)->value == value && (*previous)->value == value) {
348 (*last_rt_insertion_point)->when = when;
355 where = lower_bound (events.begin(), events.end(), &cp, cmp);
357 if (where != events.end()) {
358 if ((*where)->when == when) {
359 (*where)->value = value;
366 last_rt_insertion_point = events.insert (where, point_factory (when, value));
373 maybe_signal_changed ();
377 AutomationList::fast_simple_add (double when, double value)
379 /* to be used only for loading pre-sorted data from saved state */
380 events.insert (events.end(), point_factory (when, value));
383 #undef last_rt_insertion_point
386 AutomationList::add (double when, double value)
388 /* this is for graphical editing */
391 Glib::Mutex::Lock lm (lock);
393 ControlEvent cp (when, 0.0f);
395 iterator insertion_point;
397 for (insertion_point = lower_bound (events.begin(), events.end(), &cp, cmp); insertion_point != events.end(); ++insertion_point) {
399 /* only one point allowed per time point */
401 if ((*insertion_point)->when == when) {
402 (*insertion_point)->value = value;
407 if ((*insertion_point)->when >= when) {
414 events.insert (insertion_point, point_factory (when, value));
415 reposition_for_rt_add (0);
422 maybe_signal_changed ();
426 AutomationList::erase (AutomationList::iterator i)
429 Glib::Mutex::Lock lm (lock);
431 reposition_for_rt_add (0);
434 maybe_signal_changed ();
438 AutomationList::erase (AutomationList::iterator start, AutomationList::iterator end)
441 Glib::Mutex::Lock lm (lock);
442 events.erase (start, end);
443 reposition_for_rt_add (0);
446 maybe_signal_changed ();
450 AutomationList::reset_range (double start, double endt)
455 Glib::Mutex::Lock lm (lock);
457 ControlEvent cp (start, 0.0f);
461 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) != events.end()) {
464 e = upper_bound (events.begin(), events.end(), &cp, cmp);
466 for (iterator i = s; i != e; ++i) {
467 (*i)->value = default_value;
477 maybe_signal_changed ();
482 AutomationList::erase_range (double start, double endt)
487 Glib::Mutex::Lock lm (lock);
489 ControlEvent cp (start, 0.0f);
493 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) != events.end()) {
495 e = upper_bound (events.begin(), events.end(), &cp, cmp);
497 reposition_for_rt_add (0);
505 maybe_signal_changed ();
510 AutomationList::move_range (iterator start, iterator end, double xdelta, double ydelta)
512 /* note: we assume higher level logic is in place to avoid this
513 reordering the time-order of control events in the list. ie. all
514 points after end are later than (end)->when.
518 Glib::Mutex::Lock lm (lock);
520 while (start != end) {
521 (*start)->when += xdelta;
522 (*start)->value += ydelta;
523 if (isnan ((*start)->value)) {
530 events.sort (sort_events_by_time);
538 maybe_signal_changed ();
542 AutomationList::slide (iterator before, double distance)
545 Glib::Mutex::Lock lm (lock);
547 if (before == events.end()) {
551 while (before != events.end()) {
552 (*before)->when += distance;
557 maybe_signal_changed ();
561 AutomationList::modify (iterator iter, double when, double val)
563 /* note: we assume higher level logic is in place to avoid this
564 reordering the time-order of control events in the list. ie. all
565 points after *iter are later than when.
569 Glib::Mutex::Lock lm (lock);
571 (*iter)->when = when;
572 (*iter)->value = val;
579 events.sort (sort_events_by_time);
587 maybe_signal_changed ();
590 std::pair<AutomationList::iterator,AutomationList::iterator>
591 AutomationList::control_points_adjacent (double xval)
593 Glib::Mutex::Lock lm (lock);
596 ControlEvent cp (xval, 0.0f);
597 std::pair<iterator,iterator> ret;
599 ret.first = events.end();
600 ret.second = events.end();
602 for (i = lower_bound (events.begin(), events.end(), &cp, cmp); i != events.end(); ++i) {
604 if (ret.first == events.end()) {
605 if ((*i)->when >= xval) {
606 if (i != events.begin()) {
615 if ((*i)->when > xval) {
625 AutomationList::freeze ()
631 AutomationList::thaw ()
634 PBD::stacktrace (cerr);
635 fatal << string_compose (_("programming error: %1"), X_("AutomationList::thaw() called while not frozen")) << endmsg;
644 Glib::Mutex::Lock lm (lock);
647 events.sort (sort_events_by_time);
648 sort_pending = false;
652 if (changed_when_thawed) {
653 StateChanged(); /* EMIT SIGNAL */
658 AutomationList::set_max_xval (double x)
664 AutomationList::mark_dirty ()
666 lookup_cache.left = -1;
671 AutomationList::truncate_end (double last_coordinate)
674 Glib::Mutex::Lock lm (lock);
675 ControlEvent cp (last_coordinate, 0);
676 list<ControlEvent*>::reverse_iterator i;
679 if (events.empty()) {
683 if (last_coordinate == events.back()->when) {
687 if (last_coordinate > events.back()->when) {
692 iterator foo = events.begin();
695 if (foo == events.end()) {
697 } else if (++foo == events.end()) {
704 /* less than 2 points: add a new point */
705 events.push_back (point_factory (last_coordinate, events.back()->value));
708 /* more than 2 points: check to see if the last 2 values
709 are equal. if so, just move the position of the
710 last point. otherwise, add a new point.
713 iterator penultimate = events.end();
714 --penultimate; /* points at last point */
715 --penultimate; /* points at the penultimate point */
717 if (events.back()->value == (*penultimate)->value) {
718 events.back()->when = last_coordinate;
720 events.push_back (point_factory (last_coordinate, events.back()->value));
728 last_val = unlocked_eval (last_coordinate);
729 last_val = max ((double) min_yval, last_val);
730 last_val = min ((double) max_yval, last_val);
734 /* make i point to the last control point */
738 /* now go backwards, removing control points that are
739 beyond the new last coordinate.
742 uint32_t sz = events.size();
744 while (i != events.rend() && sz > 2) {
745 list<ControlEvent*>::reverse_iterator tmp;
750 if ((*i)->when < last_coordinate) {
754 events.erase (i.base());
760 events.back()->when = last_coordinate;
761 events.back()->value = last_val;
764 reposition_for_rt_add (0);
768 maybe_signal_changed ();
772 AutomationList::truncate_start (double overall_length)
775 Glib::Mutex::Lock lm (lock);
776 AutomationList::iterator i;
777 double first_legal_value;
778 double first_legal_coordinate;
780 if (events.empty()) {
781 fatal << _("programming error:")
782 << "AutomationList::truncate_start() called on an empty list"
788 if (overall_length == events.back()->when) {
789 /* no change in overall length */
793 if (overall_length > events.back()->when) {
795 /* growing at front: duplicate first point. shift all others */
797 double shift = overall_length - events.back()->when;
800 for (np = 0, i = events.begin(); i != events.end(); ++i, ++np) {
806 /* less than 2 points: add a new point */
807 events.push_front (point_factory (0, events.front()->value));
811 /* more than 2 points: check to see if the first 2 values
812 are equal. if so, just move the position of the
813 first point. otherwise, add a new point.
816 iterator second = events.begin();
817 ++second; /* points at the second point */
819 if (events.front()->value == (*second)->value) {
820 /* first segment is flat, just move start point back to zero */
821 events.front()->when = 0;
823 /* leave non-flat segment in place, add a new leading point. */
824 events.push_front (point_factory (0, events.front()->value));
830 /* shrinking at front */
832 first_legal_coordinate = events.back()->when - overall_length;
833 first_legal_value = unlocked_eval (first_legal_coordinate);
834 first_legal_value = max (min_yval, first_legal_value);
835 first_legal_value = min (max_yval, first_legal_value);
837 /* remove all events earlier than the new "front" */
841 while (i != events.end() && !events.empty()) {
842 list<ControlEvent*>::iterator tmp;
847 if ((*i)->when > first_legal_coordinate) {
857 /* shift all remaining points left to keep their same
861 for (i = events.begin(); i != events.end(); ++i) {
862 (*i)->when -= first_legal_coordinate;
865 /* add a new point for the interpolated new value */
867 events.push_front (point_factory (0, first_legal_value));
870 reposition_for_rt_add (0);
875 maybe_signal_changed ();
879 AutomationList::unlocked_eval (double x)
881 return shared_eval (x);
885 AutomationList::shared_eval (double x)
887 pair<AutomationEventList::iterator,AutomationEventList::iterator> range;
893 npoints = events.size();
897 return default_value;
900 if (x >= events.front()->when) {
901 return events.front()->value;
903 // return default_value;
904 return events.front()->value;
908 if (x >= events.back()->when) {
909 return events.back()->value;
910 } else if (x == events.front()->when) {
911 return events.front()->value;
912 } else if (x < events.front()->when) {
913 // return default_value;
914 return events.front()->value;
917 lpos = events.front()->when;
918 lval = events.front()->value;
919 upos = events.back()->when;
920 uval = events.back()->value;
922 /* linear interpolation betweeen the two points
925 fraction = (double) (x - lpos) / (double) (upos - lpos);
926 return lval + (fraction * (uval - lval));
930 if (x >= events.back()->when) {
931 return events.back()->value;
932 } else if (x == events.front()->when) {
933 return events.front()->value;
934 } else if (x < events.front()->when) {
935 // return default_value;
936 return events.front()->value;
939 return multipoint_eval (x);
945 AutomationList::multipoint_eval (double x)
947 pair<AutomationList::iterator,AutomationList::iterator> range;
952 /* only do the range lookup if x is in a different range than last time
953 this was called (or if the lookup cache has been marked "dirty" (left<0)
956 if ((lookup_cache.left < 0) ||
957 ((lookup_cache.left > x) ||
958 (lookup_cache.range.first == events.end()) ||
959 ((*lookup_cache.range.second)->when < x))) {
961 ControlEvent cp (x, 0);
964 lookup_cache.range = equal_range (events.begin(), events.end(), &cp, cmp);
967 range = lookup_cache.range;
969 if (range.first == range.second) {
971 /* x does not exist within the list as a control point */
973 lookup_cache.left = x;
975 if (range.first != events.begin()) {
977 lpos = (*range.first)->when;
978 lval = (*range.first)->value;
980 /* we're before the first point */
981 // return default_value;
982 return events.front()->value;
985 if (range.second == events.end()) {
986 /* we're after the last point */
987 return events.back()->value;
990 upos = (*range.second)->when;
991 uval = (*range.second)->value;
993 /* linear interpolation betweeen the two points
997 fraction = (double) (x - lpos) / (double) (upos - lpos);
998 return lval + (fraction * (uval - lval));
1002 /* x is a control point in the data */
1003 lookup_cache.left = -1;
1004 return (*range.first)->value;
1008 AutomationList::cut (iterator start, iterator end)
1010 AutomationList* nal = new AutomationList (default_value);
1013 Glib::Mutex::Lock lm (lock);
1015 for (iterator x = start; x != end; ) {
1021 nal->events.push_back (point_factory (**x));
1024 reposition_for_rt_add (0);
1032 maybe_signal_changed ();
1038 AutomationList::cut_copy_clear (double start, double end, int op)
1040 AutomationList* nal = new AutomationList (default_value);
1042 ControlEvent cp (start, 0.0);
1044 bool changed = false;
1047 Glib::Mutex::Lock lm (lock);
1049 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) == events.end()) {
1054 e = upper_bound (events.begin(), events.end(), &cp, cmp);
1056 if (op != 2 && (*s)->when != start) {
1057 nal->events.push_back (point_factory (0, unlocked_eval (start)));
1060 for (iterator x = s; x != e; ) {
1068 /* adjust new points to be relative to start, which
1069 has been set to zero.
1073 nal->events.push_back (point_factory ((*x)->when - start, (*x)->value));
1083 if (op != 2 && nal->events.back()->when != end - start) {
1084 nal->events.push_back (point_factory (end - start, unlocked_eval (end)));
1088 reposition_for_rt_add (0);
1094 maybe_signal_changed ();
1101 AutomationList::copy (iterator start, iterator end)
1103 AutomationList* nal = new AutomationList (default_value);
1106 Glib::Mutex::Lock lm (lock);
1108 for (iterator x = start; x != end; ) {
1114 nal->events.push_back (point_factory (**x));
1124 AutomationList::cut (double start, double end)
1126 return cut_copy_clear (start, end, 0);
1130 AutomationList::copy (double start, double end)
1132 return cut_copy_clear (start, end, 1);
1136 AutomationList::clear (double start, double end)
1138 (void) cut_copy_clear (start, end, 2);
1142 AutomationList::paste (AutomationList& alist, double pos, float times)
1144 if (alist.events.empty()) {
1149 Glib::Mutex::Lock lm (lock);
1153 ControlEvent cp (pos, 0.0);
1156 where = upper_bound (events.begin(), events.end(), &cp, cmp);
1158 for (iterator i = alist.begin();i != alist.end(); ++i) {
1159 events.insert (where, point_factory( (*i)->when+pos,( *i)->value));
1160 end = (*i)->when + pos;
1164 /* move all points after the insertion along the timeline by
1168 while (where != events.end()) {
1170 if ((*where)->when <= end) {
1173 events.erase(where);
1181 reposition_for_rt_add (0);
1185 maybe_signal_changed ();
1190 AutomationList::point_factory (double when, double val) const
1192 return new ControlEvent (when, val);
1196 AutomationList::point_factory (const ControlEvent& other) const
1198 return new ControlEvent (other);
1202 AutomationList::get_state ()
1204 return state (true);
1208 AutomationList::state (bool full)
1210 XMLNode* root = new XMLNode (X_("AutomationList"));
1212 LocaleGuard lg (X_("POSIX"));
1214 root->add_property ("id", _id.to_s());
1216 snprintf (buf, sizeof (buf), "%.12g", default_value);
1217 root->add_property ("default", buf);
1218 snprintf (buf, sizeof (buf), "%.12g", min_yval);
1219 root->add_property ("min_yval", buf);
1220 snprintf (buf, sizeof (buf), "%.12g", max_yval);
1221 root->add_property ("max_yval", buf);
1222 snprintf (buf, sizeof (buf), "%.12g", max_xval);
1223 root->add_property ("max_xval", buf);
1226 root->add_property ("state", auto_state_to_string (_state));
1228 /* never save anything but Off for automation state to a template */
1229 root->add_property ("state", auto_state_to_string (Off));
1232 root->add_property ("style", auto_style_to_string (_style));
1234 if (!events.empty()) {
1235 root->add_child_nocopy (serialize_events());
1242 AutomationList::serialize_events ()
1244 XMLNode* node = new XMLNode (X_("events"));
1247 for (iterator xx = events.begin(); xx != events.end(); ++xx) {
1248 str << (double) (*xx)->when;
1250 str <<(double) (*xx)->value;
1254 /* XML is a bit wierd */
1256 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
1257 content_node->set_content (str.str());
1259 node->add_child_nocopy (*content_node);
1265 AutomationList::deserialize_events (const XMLNode& node)
1267 if (node.children().empty()) {
1271 XMLNode* content_node = node.children().front();
1273 if (content_node->content().empty()) {
1280 stringstream str (content_node->content());
1296 fast_simple_add (x, y);
1301 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
1304 reposition_for_rt_add (0);
1305 maybe_signal_changed ();
1314 AutomationList::set_state (const XMLNode& node)
1316 XMLNodeList nlist = node.children();
1318 XMLNodeIterator niter;
1319 const XMLProperty* prop;
1321 if (node.name() == X_("events")) {
1322 /* partial state setting*/
1323 return deserialize_events (node);
1326 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
1328 if ((nsos = node.child (X_("AutomationList")))) {
1329 /* new school in old school clothing */
1330 return set_state (*nsos);
1335 const XMLNodeList& elist = node.children();
1336 XMLNodeConstIterator i;
1344 for (i = elist.begin(); i != elist.end(); ++i) {
1346 if ((prop = (*i)->property ("x")) == 0) {
1347 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
1350 x = atoi (prop->value().c_str());
1352 if ((prop = (*i)->property ("y")) == 0) {
1353 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
1356 y = atof (prop->value().c_str());
1358 fast_simple_add (x, y);
1366 if (node.name() != X_("AutomationList") ) {
1367 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
1371 if ((prop = node.property ("id")) != 0) {
1372 _id = prop->value ();
1373 /* update session AL list */
1374 AutomationListCreated(this);
1377 if ((prop = node.property (X_("default"))) != 0){
1378 default_value = atof (prop->value());
1380 default_value = 0.0;
1383 if ((prop = node.property (X_("style"))) != 0) {
1384 _style = string_to_auto_style (prop->value());
1389 if ((prop = node.property (X_("state"))) != 0) {
1390 _state = string_to_auto_state (prop->value());
1395 if ((prop = node.property (X_("min_yval"))) != 0) {
1396 min_yval = atof (prop->value ());
1401 if ((prop = node.property (X_("max_yval"))) != 0) {
1402 max_yval = atof (prop->value ());
1407 if ((prop = node.property (X_("max_xval"))) != 0) {
1408 max_xval = atof (prop->value ());
1410 max_xval = 0; // means "no limit ;
1413 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1414 if ((*niter)->name() == X_("events")) {
1415 deserialize_events (*(*niter));