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;
40 static void dumpit (const AutomationList& al, string prefix = "")
42 cerr << prefix << &al << endl;
43 for (AutomationList::const_iterator i = al.const_begin(); i != al.const_end(); ++i) {
44 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
50 AutomationList::AutomationList (double defval)
53 changed_when_thawed = false;
59 max_xval = 0; // means "no limit"
60 default_value = defval;
62 rt_insertion_point = events.end();
63 lookup_cache.left = -1;
64 lookup_cache.range.first = events.end();
66 AutomationListCreated(this);
69 AutomationList::AutomationList (const AutomationList& other)
72 changed_when_thawed = false;
73 _style = other._style;
74 min_yval = other.min_yval;
75 max_yval = other.max_yval;
76 max_xval = other.max_xval;
77 default_value = other.default_value;
78 _state = other._state;
79 _touching = other._touching;
81 rt_insertion_point = events.end();
82 lookup_cache.left = -1;
83 lookup_cache.range.first = events.end();
85 for (const_iterator i = other.events.begin(); i != other.events.end(); ++i) {
86 /* we have to use other point_factory() because
87 its virtual and we're in a constructor.
89 events.push_back (other.point_factory (**i));
93 AutomationListCreated(this);
96 AutomationList::AutomationList (const AutomationList& other, double start, double end)
99 changed_when_thawed = false;
100 _style = other._style;
101 min_yval = other.min_yval;
102 max_yval = other.max_yval;
103 max_xval = other.max_xval;
104 default_value = other.default_value;
105 _state = other._state;
106 _touching = other._touching;
108 rt_insertion_point = events.end();
109 lookup_cache.left = -1;
110 lookup_cache.range.first = events.end();
112 /* now grab the relevant points, and shift them back if necessary */
114 AutomationList* section = const_cast<AutomationList*>(&other)->copy (start, end);
116 if (!section->empty()) {
117 for (AutomationList::iterator i = section->begin(); i != section->end(); ++i) {
118 events.push_back (other.point_factory ((*i)->when, (*i)->value));
126 AutomationListCreated(this);
129 AutomationList::AutomationList (const XMLNode& node)
132 changed_when_thawed = false;
136 max_xval = 0; // means "no limit"
140 rt_insertion_point = events.end();
141 lookup_cache.left = -1;
142 lookup_cache.range.first = events.end();
146 AutomationListCreated(this);
149 AutomationList::~AutomationList()
153 for (AutomationEventList::iterator x = events.begin(); x != events.end(); ++x) {
159 AutomationList::operator== (const AutomationList& other)
161 return events == other.events;
165 AutomationList::operator= (const AutomationList& other)
167 if (this != &other) {
171 for (const_iterator i = other.events.begin(); i != other.events.end(); ++i) {
172 events.push_back (point_factory (**i));
175 min_yval = other.min_yval;
176 max_yval = other.max_yval;
177 max_xval = other.max_xval;
178 default_value = other.default_value;
181 maybe_signal_changed ();
188 AutomationList::maybe_signal_changed ()
193 changed_when_thawed = true;
195 StateChanged (Change (0));
200 AutomationList::set_automation_state (AutoState s)
204 automation_state_changed (); /* EMIT SIGNAL */
209 AutomationList::set_automation_style (AutoStyle s)
213 automation_style_changed (); /* EMIT SIGNAL */
218 AutomationList::start_touch ()
225 AutomationList::stop_touch ()
232 AutomationList::clear ()
235 Glib::Mutex::Lock lm (lock);
240 maybe_signal_changed ();
244 AutomationList::x_scale (double factor)
246 Glib::Mutex::Lock lm (lock);
251 AutomationList::extend_to (double when)
253 Glib::Mutex::Lock lm (lock);
254 if (events.empty() || events.back()->when == when) {
257 double factor = when / events.back()->when;
262 void AutomationList::_x_scale (double factor)
264 for (AutomationList::iterator i = events.begin(); i != events.end(); ++i) {
265 (*i)->when = floor ((*i)->when * factor);
272 AutomationList::reposition_for_rt_add (double when)
274 rt_insertion_point = events.end();
277 #define last_rt_insertion_point rt_insertion_point
280 AutomationList::rt_add (double when, double value)
282 /* this is for automation recording */
284 if ((_state & Touch) && !_touching) {
288 // cerr << "RT: alist @ " << this << " add " << value << " @ " << when << endl;
291 Glib::Mutex::Lock lm (lock);
295 ControlEvent cp (when, 0.0);
298 if ((last_rt_insertion_point != events.end()) && ((*last_rt_insertion_point)->when < when) ) {
300 /* we have a previous insertion point, so we should delete
301 everything between it and the position where we are going
302 to insert this point.
305 iterator after = last_rt_insertion_point;
307 if (++after != events.end()) {
308 iterator far = after;
310 while (far != events.end()) {
311 if ((*far)->when > when) {
319 last_rt_insertion_point = where;
321 if((*where)->when == when) {
322 (*where)->value = value;
326 where = events.erase (after, far);
335 iterator previous = last_rt_insertion_point;
338 if (last_rt_insertion_point != events.begin() && (*last_rt_insertion_point)->value == value && (*previous)->value == value) {
339 (*last_rt_insertion_point)->when = when;
346 where = lower_bound (events.begin(), events.end(), &cp, cmp);
348 if (where != events.end()) {
349 if ((*where)->when == when) {
350 (*where)->value = value;
357 last_rt_insertion_point = events.insert (where, point_factory (when, value));
364 maybe_signal_changed ();
368 AutomationList::fast_simple_add (double when, double value)
370 /* to be used only for loading pre-sorted data from saved state */
371 events.insert (events.end(), point_factory (when, value));
374 #undef last_rt_insertion_point
377 AutomationList::add (double when, double value)
379 /* this is for graphical editing */
382 Glib::Mutex::Lock lm (lock);
384 ControlEvent cp (when, 0.0f);
386 iterator insertion_point;
388 for (insertion_point = lower_bound (events.begin(), events.end(), &cp, cmp); insertion_point != events.end(); ++insertion_point) {
390 /* only one point allowed per time point */
392 if ((*insertion_point)->when == when) {
393 (*insertion_point)->value = value;
398 if ((*insertion_point)->when >= when) {
405 events.insert (insertion_point, point_factory (when, value));
406 reposition_for_rt_add (0);
413 maybe_signal_changed ();
417 AutomationList::erase (AutomationList::iterator i)
420 Glib::Mutex::Lock lm (lock);
422 reposition_for_rt_add (0);
425 maybe_signal_changed ();
429 AutomationList::erase (AutomationList::iterator start, AutomationList::iterator end)
432 Glib::Mutex::Lock lm (lock);
433 events.erase (start, end);
434 reposition_for_rt_add (0);
437 maybe_signal_changed ();
441 AutomationList::reset_range (double start, double endt)
446 Glib::Mutex::Lock lm (lock);
448 ControlEvent cp (start, 0.0f);
452 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) != events.end()) {
455 e = upper_bound (events.begin(), events.end(), &cp, cmp);
457 for (iterator i = s; i != e; ++i) {
458 (*i)->value = default_value;
468 maybe_signal_changed ();
473 AutomationList::erase_range (double start, double endt)
478 Glib::Mutex::Lock lm (lock);
480 ControlEvent cp (start, 0.0f);
484 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) != events.end()) {
486 e = upper_bound (events.begin(), events.end(), &cp, cmp);
488 reposition_for_rt_add (0);
496 maybe_signal_changed ();
501 AutomationList::move_range (iterator start, iterator end, double xdelta, double ydelta)
503 /* note: we assume higher level logic is in place to avoid this
504 reordering the time-order of control events in the list. ie. all
505 points after end are later than (end)->when.
509 Glib::Mutex::Lock lm (lock);
511 while (start != end) {
512 (*start)->when += xdelta;
513 (*start)->value += ydelta;
520 maybe_signal_changed ();
524 AutomationList::modify (iterator iter, double when, double val)
526 /* note: we assume higher level logic is in place to avoid this
527 reordering the time-order of control events in the list. ie. all
528 points after *iter are later than when.
532 Glib::Mutex::Lock lm (lock);
533 (*iter)->when = when;
534 (*iter)->value = val;
538 maybe_signal_changed ();
541 std::pair<AutomationList::iterator,AutomationList::iterator>
542 AutomationList::control_points_adjacent (double xval)
544 Glib::Mutex::Lock lm (lock);
547 ControlEvent cp (xval, 0.0f);
548 std::pair<iterator,iterator> ret;
550 ret.first = events.end();
551 ret.second = events.end();
553 for (i = lower_bound (events.begin(), events.end(), &cp, cmp); i != events.end(); ++i) {
555 if (ret.first == events.end()) {
556 if ((*i)->when >= xval) {
557 if (i != events.begin()) {
566 if ((*i)->when > xval) {
576 AutomationList::freeze ()
582 AutomationList::thaw ()
585 if (changed_when_thawed) {
586 StateChanged(Change(0)); /* EMIT SIGNAL */
591 AutomationList::set_max_xval (double x)
597 AutomationList::mark_dirty ()
599 lookup_cache.left = -1;
604 AutomationList::truncate_end (double last_coordinate)
607 Glib::Mutex::Lock lm (lock);
608 ControlEvent cp (last_coordinate, 0);
609 list<ControlEvent*>::reverse_iterator i;
612 if (events.empty()) {
616 if (last_coordinate == events.back()->when) {
620 if (last_coordinate > events.back()->when) {
625 iterator foo = events.begin();
628 if (foo == events.end()) {
630 } else if (++foo == events.end()) {
637 /* less than 2 points: add a new point */
638 events.push_back (point_factory (last_coordinate, events.back()->value));
641 /* more than 2 points: check to see if the last 2 values
642 are equal. if so, just move the position of the
643 last point. otherwise, add a new point.
646 iterator penultimate = events.end();
647 --penultimate; /* points at last point */
648 --penultimate; /* points at the penultimate point */
650 if (events.back()->value == (*penultimate)->value) {
651 events.back()->when = last_coordinate;
653 events.push_back (point_factory (last_coordinate, events.back()->value));
661 last_val = unlocked_eval (last_coordinate);
662 last_val = max ((double) min_yval, last_val);
663 last_val = min ((double) max_yval, last_val);
667 /* make i point to the last control point */
671 /* now go backwards, removing control points that are
672 beyond the new last coordinate.
675 uint32_t sz = events.size();
677 while (i != events.rend() && sz > 2) {
678 list<ControlEvent*>::reverse_iterator tmp;
683 if ((*i)->when < last_coordinate) {
687 events.erase (i.base());
693 events.back()->when = last_coordinate;
694 events.back()->value = last_val;
697 reposition_for_rt_add (0);
701 maybe_signal_changed ();
705 AutomationList::truncate_start (double overall_length)
708 Glib::Mutex::Lock lm (lock);
709 AutomationList::iterator i;
710 double first_legal_value;
711 double first_legal_coordinate;
713 if (events.empty()) {
714 fatal << _("programming error:")
715 << "AutomationList::truncate_start() called on an empty list"
721 if (overall_length == events.back()->when) {
722 /* no change in overall length */
726 if (overall_length > events.back()->when) {
728 /* growing at front: duplicate first point. shift all others */
730 double shift = overall_length - events.back()->when;
733 for (np = 0, i = events.begin(); i != events.end(); ++i, ++np) {
739 /* less than 2 points: add a new point */
740 events.push_front (point_factory (0, events.front()->value));
744 /* more than 2 points: check to see if the first 2 values
745 are equal. if so, just move the position of the
746 first point. otherwise, add a new point.
749 iterator second = events.begin();
750 ++second; /* points at the second point */
752 if (events.front()->value == (*second)->value) {
753 /* first segment is flat, just move start point back to zero */
754 events.front()->when = 0;
756 /* leave non-flat segment in place, add a new leading point. */
757 events.push_front (point_factory (0, events.front()->value));
763 /* shrinking at front */
765 first_legal_coordinate = events.back()->when - overall_length;
766 first_legal_value = unlocked_eval (first_legal_coordinate);
767 first_legal_value = max (min_yval, first_legal_value);
768 first_legal_value = min (max_yval, first_legal_value);
770 /* remove all events earlier than the new "front" */
774 while (i != events.end() && !events.empty()) {
775 list<ControlEvent*>::iterator tmp;
780 if ((*i)->when > first_legal_coordinate) {
790 /* shift all remaining points left to keep their same
794 for (i = events.begin(); i != events.end(); ++i) {
795 (*i)->when -= first_legal_coordinate;
798 /* add a new point for the interpolated new value */
800 events.push_front (point_factory (0, first_legal_value));
803 reposition_for_rt_add (0);
808 maybe_signal_changed ();
812 AutomationList::unlocked_eval (double x)
814 return shared_eval (x);
818 AutomationList::shared_eval (double x)
820 pair<AutomationEventList::iterator,AutomationEventList::iterator> range;
826 npoints = events.size();
830 return default_value;
833 if (x >= events.front()->when) {
834 return events.front()->value;
836 // return default_value;
837 return events.front()->value;
841 if (x >= events.back()->when) {
842 return events.back()->value;
843 } else if (x == events.front()->when) {
844 return events.front()->value;
845 } else if (x < events.front()->when) {
846 // return default_value;
847 return events.front()->value;
850 lpos = events.front()->when;
851 lval = events.front()->value;
852 upos = events.back()->when;
853 uval = events.back()->value;
855 /* linear interpolation betweeen the two points
858 fraction = (double) (x - lpos) / (double) (upos - lpos);
859 return lval + (fraction * (uval - lval));
863 if (x >= events.back()->when) {
864 return events.back()->value;
865 } else if (x == events.front()->when) {
866 return events.front()->value;
867 } else if (x < events.front()->when) {
868 // return default_value;
869 return events.front()->value;
872 return multipoint_eval (x);
878 AutomationList::multipoint_eval (double x)
880 pair<AutomationList::iterator,AutomationList::iterator> range;
885 /* only do the range lookup if x is in a different range than last time
886 this was called (or if the lookup cache has been marked "dirty" (left<0)
889 if ((lookup_cache.left < 0) ||
890 ((lookup_cache.left > x) ||
891 (lookup_cache.range.first == events.end()) ||
892 ((*lookup_cache.range.second)->when < x))) {
894 ControlEvent cp (x, 0);
897 lookup_cache.range = equal_range (events.begin(), events.end(), &cp, cmp);
900 range = lookup_cache.range;
902 if (range.first == range.second) {
904 /* x does not exist within the list as a control point */
906 lookup_cache.left = x;
908 if (range.first != events.begin()) {
910 lpos = (*range.first)->when;
911 lval = (*range.first)->value;
913 /* we're before the first point */
914 // return default_value;
915 return events.front()->value;
918 if (range.second == events.end()) {
919 /* we're after the last point */
920 return events.back()->value;
923 upos = (*range.second)->when;
924 uval = (*range.second)->value;
926 /* linear interpolation betweeen the two points
930 fraction = (double) (x - lpos) / (double) (upos - lpos);
931 return lval + (fraction * (uval - lval));
935 /* x is a control point in the data */
936 lookup_cache.left = -1;
937 return (*range.first)->value;
941 AutomationList::cut (iterator start, iterator end)
943 AutomationList* nal = new AutomationList (default_value);
946 Glib::Mutex::Lock lm (lock);
948 for (iterator x = start; x != end; ) {
954 nal->events.push_back (point_factory (**x));
957 reposition_for_rt_add (0);
965 maybe_signal_changed ();
971 AutomationList::cut_copy_clear (double start, double end, int op)
973 AutomationList* nal = new AutomationList (default_value);
975 ControlEvent cp (start, 0.0);
977 bool changed = false;
980 Glib::Mutex::Lock lm (lock);
982 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) == events.end()) {
987 e = upper_bound (events.begin(), events.end(), &cp, cmp);
989 if (op != 2 && (*s)->when != start) {
990 nal->events.push_back (point_factory (0, unlocked_eval (start)));
993 for (iterator x = s; x != e; ) {
1001 /* adjust new points to be relative to start, which
1002 has been set to zero.
1006 nal->events.push_back (point_factory ((*x)->when - start, (*x)->value));
1016 if (op != 2 && nal->events.back()->when != end - start) {
1017 nal->events.push_back (point_factory (end - start, unlocked_eval (end)));
1021 reposition_for_rt_add (0);
1027 maybe_signal_changed ();
1034 AutomationList::copy (iterator start, iterator end)
1036 AutomationList* nal = new AutomationList (default_value);
1039 Glib::Mutex::Lock lm (lock);
1041 for (iterator x = start; x != end; ) {
1047 nal->events.push_back (point_factory (**x));
1057 AutomationList::cut (double start, double end)
1059 return cut_copy_clear (start, end, 0);
1063 AutomationList::copy (double start, double end)
1065 return cut_copy_clear (start, end, 1);
1069 AutomationList::clear (double start, double end)
1071 (void) cut_copy_clear (start, end, 2);
1075 AutomationList::paste (AutomationList& alist, double pos, float times)
1077 if (alist.events.empty()) {
1082 Glib::Mutex::Lock lm (lock);
1086 ControlEvent cp (pos, 0.0);
1089 where = upper_bound (events.begin(), events.end(), &cp, cmp);
1091 for (iterator i = alist.begin();i != alist.end(); ++i) {
1092 events.insert (where, point_factory( (*i)->when+pos,( *i)->value));
1093 end = (*i)->when + pos;
1097 /* move all points after the insertion along the timeline by
1101 while (where != events.end()) {
1103 if ((*where)->when <= end) {
1106 events.erase(where);
1114 reposition_for_rt_add (0);
1118 maybe_signal_changed ();
1123 AutomationList::point_factory (double when, double val) const
1125 return new ControlEvent (when, val);
1129 AutomationList::point_factory (const ControlEvent& other) const
1131 return new ControlEvent (other);
1135 AutomationList::get_state ()
1137 return state (true);
1141 AutomationList::state (bool full)
1143 XMLNode* root = new XMLNode (X_("AutomationList"));
1145 LocaleGuard lg (X_("POSIX"));
1147 root->add_property ("id", _id.to_s());
1149 snprintf (buf, sizeof (buf), "%.12g", default_value);
1150 root->add_property ("default", buf);
1151 snprintf (buf, sizeof (buf), "%.12g", min_yval);
1152 root->add_property ("min_yval", buf);
1153 snprintf (buf, sizeof (buf), "%.12g", max_yval);
1154 root->add_property ("max_yval", buf);
1155 snprintf (buf, sizeof (buf), "%.12g", max_xval);
1156 root->add_property ("max_xval", buf);
1159 root->add_property ("state", auto_state_to_string (_state));
1161 /* never save anything but Off for automation state to a template */
1162 root->add_property ("state", auto_state_to_string (Off));
1165 root->add_property ("style", auto_style_to_string (_style));
1167 if (!events.empty()) {
1168 root->add_child_nocopy (serialize_events());
1175 AutomationList::serialize_events ()
1177 XMLNode* node = new XMLNode (X_("events"));
1180 for (iterator xx = events.begin(); xx != events.end(); ++xx) {
1181 str << (double) (*xx)->when;
1183 str <<(double) (*xx)->value;
1187 /* XML is a bit wierd */
1189 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
1190 content_node->set_content (str.str());
1192 node->add_child_nocopy (*content_node);
1198 AutomationList::deserialize_events (const XMLNode& node)
1200 if (node.children().empty()) {
1204 XMLNode* content_node = node.children().front();
1206 if (content_node->content().empty()) {
1213 stringstream str (content_node->content());
1229 fast_simple_add (x, y);
1234 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
1237 reposition_for_rt_add (0);
1238 maybe_signal_changed ();
1246 AutomationList::set_state (const XMLNode& node)
1248 XMLNodeList nlist = node.children();
1249 XMLNodeIterator niter;
1250 const XMLProperty* prop;
1252 if (node.name() == X_("events")) {
1253 /* partial state setting*/
1254 return deserialize_events (node);
1257 if (node.name() == X_("Envelope")) {
1261 const XMLNodeList& elist = node.children();
1262 XMLNodeConstIterator i;
1269 for (i = elist.begin(); i != elist.end(); ++i) {
1271 if ((prop = (*i)->property ("x")) == 0) {
1272 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
1275 x = atoi (prop->value().c_str());
1277 if ((prop = (*i)->property ("y")) == 0) {
1278 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
1281 y = atof (prop->value().c_str());
1289 if (node.name() != X_("AutomationList") ) {
1290 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
1294 if ((prop = node.property ("id")) != 0) {
1295 _id = prop->value ();
1296 /* update session AL list */
1297 AutomationListCreated(this);
1300 if ((prop = node.property (X_("default"))) != 0){
1301 default_value = atof (prop->value());
1303 default_value = 0.0;
1306 if ((prop = node.property (X_("style"))) != 0) {
1307 _style = string_to_auto_style (prop->value());
1312 if ((prop = node.property (X_("state"))) != 0) {
1313 _state = string_to_auto_state (prop->value());
1318 if ((prop = node.property (X_("min_yval"))) != 0) {
1319 min_yval = atof (prop->value ());
1324 if ((prop = node.property (X_("max_yval"))) != 0) {
1325 max_yval = atof (prop->value ());
1330 if ((prop = node.property (X_("max_xval"))) != 0) {
1331 max_xval = atof (prop->value ());
1333 max_xval = 0; // means "no limit ;
1336 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1337 if ((*niter)->name() == X_("events")) {
1338 deserialize_events (*(*niter));