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>
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 fatal << string_compose (_("programming error: %1"), X_("AutomationList::thaw() called while not frozen")) << endmsg;
643 Glib::Mutex::Lock lm (lock);
646 events.sort (sort_events_by_time);
647 sort_pending = false;
651 if (changed_when_thawed) {
652 StateChanged(); /* EMIT SIGNAL */
657 AutomationList::set_max_xval (double x)
663 AutomationList::mark_dirty ()
665 lookup_cache.left = -1;
670 AutomationList::truncate_end (double last_coordinate)
673 Glib::Mutex::Lock lm (lock);
674 ControlEvent cp (last_coordinate, 0);
675 list<ControlEvent*>::reverse_iterator i;
678 if (events.empty()) {
682 if (last_coordinate == events.back()->when) {
686 if (last_coordinate > events.back()->when) {
691 iterator foo = events.begin();
694 if (foo == events.end()) {
696 } else if (++foo == events.end()) {
703 /* less than 2 points: add a new point */
704 events.push_back (point_factory (last_coordinate, events.back()->value));
707 /* more than 2 points: check to see if the last 2 values
708 are equal. if so, just move the position of the
709 last point. otherwise, add a new point.
712 iterator penultimate = events.end();
713 --penultimate; /* points at last point */
714 --penultimate; /* points at the penultimate point */
716 if (events.back()->value == (*penultimate)->value) {
717 events.back()->when = last_coordinate;
719 events.push_back (point_factory (last_coordinate, events.back()->value));
727 last_val = unlocked_eval (last_coordinate);
728 last_val = max ((double) min_yval, last_val);
729 last_val = min ((double) max_yval, last_val);
733 /* make i point to the last control point */
737 /* now go backwards, removing control points that are
738 beyond the new last coordinate.
741 uint32_t sz = events.size();
743 while (i != events.rend() && sz > 2) {
744 list<ControlEvent*>::reverse_iterator tmp;
749 if ((*i)->when < last_coordinate) {
753 events.erase (i.base());
759 events.back()->when = last_coordinate;
760 events.back()->value = last_val;
763 reposition_for_rt_add (0);
767 maybe_signal_changed ();
771 AutomationList::truncate_start (double overall_length)
774 Glib::Mutex::Lock lm (lock);
775 AutomationList::iterator i;
776 double first_legal_value;
777 double first_legal_coordinate;
779 if (events.empty()) {
780 fatal << _("programming error:")
781 << "AutomationList::truncate_start() called on an empty list"
787 if (overall_length == events.back()->when) {
788 /* no change in overall length */
792 if (overall_length > events.back()->when) {
794 /* growing at front: duplicate first point. shift all others */
796 double shift = overall_length - events.back()->when;
799 for (np = 0, i = events.begin(); i != events.end(); ++i, ++np) {
805 /* less than 2 points: add a new point */
806 events.push_front (point_factory (0, events.front()->value));
810 /* more than 2 points: check to see if the first 2 values
811 are equal. if so, just move the position of the
812 first point. otherwise, add a new point.
815 iterator second = events.begin();
816 ++second; /* points at the second point */
818 if (events.front()->value == (*second)->value) {
819 /* first segment is flat, just move start point back to zero */
820 events.front()->when = 0;
822 /* leave non-flat segment in place, add a new leading point. */
823 events.push_front (point_factory (0, events.front()->value));
829 /* shrinking at front */
831 first_legal_coordinate = events.back()->when - overall_length;
832 first_legal_value = unlocked_eval (first_legal_coordinate);
833 first_legal_value = max (min_yval, first_legal_value);
834 first_legal_value = min (max_yval, first_legal_value);
836 /* remove all events earlier than the new "front" */
840 while (i != events.end() && !events.empty()) {
841 list<ControlEvent*>::iterator tmp;
846 if ((*i)->when > first_legal_coordinate) {
856 /* shift all remaining points left to keep their same
860 for (i = events.begin(); i != events.end(); ++i) {
861 (*i)->when -= first_legal_coordinate;
864 /* add a new point for the interpolated new value */
866 events.push_front (point_factory (0, first_legal_value));
869 reposition_for_rt_add (0);
874 maybe_signal_changed ();
878 AutomationList::unlocked_eval (double x)
880 return shared_eval (x);
884 AutomationList::shared_eval (double x)
886 pair<AutomationEventList::iterator,AutomationEventList::iterator> range;
892 npoints = events.size();
896 return default_value;
899 if (x >= events.front()->when) {
900 return events.front()->value;
902 // return default_value;
903 return events.front()->value;
907 if (x >= events.back()->when) {
908 return events.back()->value;
909 } else if (x == events.front()->when) {
910 return events.front()->value;
911 } else if (x < events.front()->when) {
912 // return default_value;
913 return events.front()->value;
916 lpos = events.front()->when;
917 lval = events.front()->value;
918 upos = events.back()->when;
919 uval = events.back()->value;
921 /* linear interpolation betweeen the two points
924 fraction = (double) (x - lpos) / (double) (upos - lpos);
925 return lval + (fraction * (uval - lval));
929 if (x >= events.back()->when) {
930 return events.back()->value;
931 } else if (x == events.front()->when) {
932 return events.front()->value;
933 } else if (x < events.front()->when) {
934 // return default_value;
935 return events.front()->value;
938 return multipoint_eval (x);
944 AutomationList::multipoint_eval (double x)
946 pair<AutomationList::iterator,AutomationList::iterator> range;
951 /* only do the range lookup if x is in a different range than last time
952 this was called (or if the lookup cache has been marked "dirty" (left<0)
955 if ((lookup_cache.left < 0) ||
956 ((lookup_cache.left > x) ||
957 (lookup_cache.range.first == events.end()) ||
958 ((*lookup_cache.range.second)->when < x))) {
960 ControlEvent cp (x, 0);
963 lookup_cache.range = equal_range (events.begin(), events.end(), &cp, cmp);
966 range = lookup_cache.range;
968 if (range.first == range.second) {
970 /* x does not exist within the list as a control point */
972 lookup_cache.left = x;
974 if (range.first != events.begin()) {
976 lpos = (*range.first)->when;
977 lval = (*range.first)->value;
979 /* we're before the first point */
980 // return default_value;
981 return events.front()->value;
984 if (range.second == events.end()) {
985 /* we're after the last point */
986 return events.back()->value;
989 upos = (*range.second)->when;
990 uval = (*range.second)->value;
992 /* linear interpolation betweeen the two points
996 fraction = (double) (x - lpos) / (double) (upos - lpos);
997 return lval + (fraction * (uval - lval));
1001 /* x is a control point in the data */
1002 lookup_cache.left = -1;
1003 return (*range.first)->value;
1007 AutomationList::cut (iterator start, iterator end)
1009 AutomationList* nal = new AutomationList (default_value);
1012 Glib::Mutex::Lock lm (lock);
1014 for (iterator x = start; x != end; ) {
1020 nal->events.push_back (point_factory (**x));
1023 reposition_for_rt_add (0);
1031 maybe_signal_changed ();
1037 AutomationList::cut_copy_clear (double start, double end, int op)
1039 AutomationList* nal = new AutomationList (default_value);
1041 ControlEvent cp (start, 0.0);
1043 bool changed = false;
1046 Glib::Mutex::Lock lm (lock);
1048 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) == events.end()) {
1053 e = upper_bound (events.begin(), events.end(), &cp, cmp);
1055 if (op != 2 && (*s)->when != start) {
1056 nal->events.push_back (point_factory (0, unlocked_eval (start)));
1059 for (iterator x = s; x != e; ) {
1067 /* adjust new points to be relative to start, which
1068 has been set to zero.
1072 nal->events.push_back (point_factory ((*x)->when - start, (*x)->value));
1082 if (op != 2 && nal->events.back()->when != end - start) {
1083 nal->events.push_back (point_factory (end - start, unlocked_eval (end)));
1087 reposition_for_rt_add (0);
1093 maybe_signal_changed ();
1100 AutomationList::copy (iterator start, iterator end)
1102 AutomationList* nal = new AutomationList (default_value);
1105 Glib::Mutex::Lock lm (lock);
1107 for (iterator x = start; x != end; ) {
1113 nal->events.push_back (point_factory (**x));
1123 AutomationList::cut (double start, double end)
1125 return cut_copy_clear (start, end, 0);
1129 AutomationList::copy (double start, double end)
1131 return cut_copy_clear (start, end, 1);
1135 AutomationList::clear (double start, double end)
1137 (void) cut_copy_clear (start, end, 2);
1141 AutomationList::paste (AutomationList& alist, double pos, float times)
1143 if (alist.events.empty()) {
1148 Glib::Mutex::Lock lm (lock);
1152 ControlEvent cp (pos, 0.0);
1155 where = upper_bound (events.begin(), events.end(), &cp, cmp);
1157 for (iterator i = alist.begin();i != alist.end(); ++i) {
1158 events.insert (where, point_factory( (*i)->when+pos,( *i)->value));
1159 end = (*i)->when + pos;
1163 /* move all points after the insertion along the timeline by
1167 while (where != events.end()) {
1169 if ((*where)->when <= end) {
1172 events.erase(where);
1180 reposition_for_rt_add (0);
1184 maybe_signal_changed ();
1189 AutomationList::point_factory (double when, double val) const
1191 return new ControlEvent (when, val);
1195 AutomationList::point_factory (const ControlEvent& other) const
1197 return new ControlEvent (other);
1201 AutomationList::get_state ()
1203 return state (true);
1207 AutomationList::state (bool full)
1209 XMLNode* root = new XMLNode (X_("AutomationList"));
1211 LocaleGuard lg (X_("POSIX"));
1213 root->add_property ("id", _id.to_s());
1215 snprintf (buf, sizeof (buf), "%.12g", default_value);
1216 root->add_property ("default", buf);
1217 snprintf (buf, sizeof (buf), "%.12g", min_yval);
1218 root->add_property ("min_yval", buf);
1219 snprintf (buf, sizeof (buf), "%.12g", max_yval);
1220 root->add_property ("max_yval", buf);
1221 snprintf (buf, sizeof (buf), "%.12g", max_xval);
1222 root->add_property ("max_xval", buf);
1225 root->add_property ("state", auto_state_to_string (_state));
1227 /* never save anything but Off for automation state to a template */
1228 root->add_property ("state", auto_state_to_string (Off));
1231 root->add_property ("style", auto_style_to_string (_style));
1233 if (!events.empty()) {
1234 root->add_child_nocopy (serialize_events());
1241 AutomationList::serialize_events ()
1243 XMLNode* node = new XMLNode (X_("events"));
1246 for (iterator xx = events.begin(); xx != events.end(); ++xx) {
1247 str << (double) (*xx)->when;
1249 str <<(double) (*xx)->value;
1253 /* XML is a bit wierd */
1255 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
1256 content_node->set_content (str.str());
1258 node->add_child_nocopy (*content_node);
1264 AutomationList::deserialize_events (const XMLNode& node)
1266 if (node.children().empty()) {
1270 XMLNode* content_node = node.children().front();
1272 if (content_node->content().empty()) {
1279 stringstream str (content_node->content());
1295 fast_simple_add (x, y);
1300 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
1303 reposition_for_rt_add (0);
1304 maybe_signal_changed ();
1313 AutomationList::set_state (const XMLNode& node)
1315 XMLNodeList nlist = node.children();
1317 XMLNodeIterator niter;
1318 const XMLProperty* prop;
1320 if (node.name() == X_("events")) {
1321 /* partial state setting*/
1322 return deserialize_events (node);
1325 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
1327 if ((nsos = node.child (X_("AutomationList")))) {
1328 /* new school in old school clothing */
1329 return set_state (*nsos);
1334 const XMLNodeList& elist = node.children();
1335 XMLNodeConstIterator i;
1343 for (i = elist.begin(); i != elist.end(); ++i) {
1345 if ((prop = (*i)->property ("x")) == 0) {
1346 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
1349 x = atoi (prop->value().c_str());
1351 if ((prop = (*i)->property ("y")) == 0) {
1352 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
1355 y = atof (prop->value().c_str());
1357 fast_simple_add (x, y);
1365 if (node.name() != X_("AutomationList") ) {
1366 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
1370 if ((prop = node.property ("id")) != 0) {
1371 _id = prop->value ();
1372 /* update session AL list */
1373 AutomationListCreated(this);
1376 if ((prop = node.property (X_("default"))) != 0){
1377 default_value = atof (prop->value());
1379 default_value = 0.0;
1382 if ((prop = node.property (X_("style"))) != 0) {
1383 _style = string_to_auto_style (prop->value());
1388 if ((prop = node.property (X_("state"))) != 0) {
1389 _state = string_to_auto_state (prop->value());
1394 if ((prop = node.property (X_("min_yval"))) != 0) {
1395 min_yval = atof (prop->value ());
1400 if ((prop = node.property (X_("max_yval"))) != 0) {
1401 max_yval = atof (prop->value ());
1406 if ((prop = node.property (X_("max_xval"))) != 0) {
1407 max_xval = atof (prop->value ());
1409 max_xval = 0; // means "no limit ;
1412 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1413 if ((*niter)->name() == X_("events")) {
1414 deserialize_events (*(*niter));