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>
32 using namespace ARDOUR;
36 sigc::signal<void,AutomationList *> AutomationList::AutomationListCreated;
39 static void dumpit (const AutomationList& al, string prefix = "")
41 cerr << prefix << &al << endl;
42 for (AutomationList::const_iterator i = al.const_begin(); i != al.const_end(); ++i) {
43 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
49 AutomationList::AutomationList (double defval, bool with_state)
52 changed_when_thawed = false;
56 no_state = with_state;
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();
67 save_state (_("initial"));
70 AutomationListCreated(this);
73 AutomationList::AutomationList (const AutomationList& other)
76 changed_when_thawed = false;
77 _style = other._style;
78 min_yval = other.min_yval;
79 max_yval = other.max_yval;
80 max_xval = other.max_xval;
81 default_value = other.default_value;
82 _state = other._state;
83 _touching = other._touching;
85 rt_insertion_point = events.end();
86 no_state = other.no_state;
87 lookup_cache.left = -1;
88 lookup_cache.range.first = events.end();
90 for (const_iterator i = other.events.begin(); i != other.events.end(); ++i) {
91 /* we have to use other point_factory() because
92 its virtual and we're in a constructor.
94 events.push_back (other.point_factory (**i));
98 AutomationListCreated(this);
101 AutomationList::AutomationList (const AutomationList& other, double start, double end)
104 changed_when_thawed = false;
105 _style = other._style;
106 min_yval = other.min_yval;
107 max_yval = other.max_yval;
108 max_xval = other.max_xval;
109 default_value = other.default_value;
110 _state = other._state;
111 _touching = other._touching;
113 rt_insertion_point = events.end();
114 no_state = other.no_state;
115 lookup_cache.left = -1;
116 lookup_cache.range.first = events.end();
118 /* now grab the relevant points, and shift them back if necessary */
120 AutomationList* section = const_cast<AutomationList*>(&other)->copy (start, end);
122 if (!section->empty()) {
123 for (AutomationList::iterator i = section->begin(); i != section->end(); ++i) {
124 events.push_back (other.point_factory ((*i)->when, (*i)->value));
131 AutomationListCreated(this);
134 AutomationList::~AutomationList()
136 std::set<ControlEvent*> all_events;
137 AutomationList::State* asp;
141 for (AutomationEventList::iterator x = events.begin(); x != events.end(); ++x) {
142 all_events.insert (*x);
145 for (StateMap::iterator i = states.begin(); i != states.end(); ++i) {
147 if ((asp = dynamic_cast<AutomationList::State*> (*i)) != 0) {
149 for (AutomationEventList::iterator x = asp->events.begin(); x != asp->events.end(); ++x) {
150 all_events.insert (*x);
155 for (std::set<ControlEvent*>::iterator i = all_events.begin(); i != all_events.end(); ++i) {
161 AutomationList::operator== (const AutomationList& other)
163 return events == other.events;
167 AutomationList::operator= (const AutomationList& other)
169 if (this != &other) {
173 for (const_iterator i = other.events.begin(); i != other.events.end(); ++i) {
174 events.push_back (point_factory (**i));
177 min_yval = other.min_yval;
178 max_yval = other.max_yval;
179 max_xval = other.max_xval;
180 default_value = other.default_value;
183 maybe_signal_changed ();
190 AutomationList::maybe_signal_changed ()
195 changed_when_thawed = true;
197 StateChanged (Change (0));
202 AutomationList::set_automation_state (AutoState s)
206 automation_state_changed (); /* EMIT SIGNAL */
211 AutomationList::set_automation_style (AutoStyle s)
215 automation_style_changed (); /* EMIT SIGNAL */
220 AutomationList::start_touch ()
227 AutomationList::stop_touch ()
234 AutomationList::clear ()
237 Glib::Mutex::Lock lm (lock);
240 save_state (_("cleared"));
245 maybe_signal_changed ();
249 AutomationList::x_scale (double factor)
251 Glib::Mutex::Lock lm (lock);
256 AutomationList::extend_to (double when)
258 Glib::Mutex::Lock lm (lock);
259 if (events.empty() || events.back()->when == when) {
262 double factor = when / events.back()->when;
267 void AutomationList::_x_scale (double factor)
269 for (AutomationList::iterator i = events.begin(); i != events.end(); ++i) {
270 (*i)->when = floor ((*i)->when * factor);
273 save_state ("x-scaled");
278 AutomationList::reposition_for_rt_add (double when)
280 rt_insertion_point = events.end();
283 #define last_rt_insertion_point rt_insertion_point
286 AutomationList::rt_add (double when, double value)
288 /* this is for automation recording */
290 if ((_state & Touch) && !_touching) {
294 // cerr << "RT: alist @ " << this << " add " << value << " @ " << when << endl;
297 Glib::Mutex::Lock lm (lock);
301 ControlEvent cp (when, 0.0);
304 if ((last_rt_insertion_point != events.end()) && ((*last_rt_insertion_point)->when < when) ) {
306 /* we have a previous insertion point, so we should delete
307 everything between it and the position where we are going
308 to insert this point.
311 iterator after = last_rt_insertion_point;
313 if (++after != events.end()) {
314 iterator far = after;
316 while (far != events.end()) {
317 if ((*far)->when > when) {
325 last_rt_insertion_point = where;
327 if((*where)->when == when) {
328 (*where)->value = value;
332 where = events.erase (after, far);
341 iterator previous = last_rt_insertion_point;
344 if (last_rt_insertion_point != events.begin() && (*last_rt_insertion_point)->value == value && (*previous)->value == value) {
345 (*last_rt_insertion_point)->when = when;
352 where = lower_bound (events.begin(), events.end(), &cp, cmp);
354 if (where != events.end()) {
355 if ((*where)->when == when) {
356 (*where)->value = value;
363 last_rt_insertion_point = events.insert (where, point_factory (when, value));
370 maybe_signal_changed ();
373 #undef last_rt_insertion_point
376 AutomationList::add (double when, double value, bool for_loading)
378 /* this is for graphical editing and loading data from storage */
381 Glib::Mutex::Lock lm (lock);
383 ControlEvent cp (when, 0.0f);
385 iterator insertion_point;
387 for (insertion_point = lower_bound (events.begin(), events.end(), &cp, cmp); insertion_point != events.end(); ++insertion_point) {
389 /* only one point allowed per time point */
391 if ((*insertion_point)->when == when) {
392 (*insertion_point)->value = value;
397 if ((*insertion_point)->when >= when) {
404 events.insert (insertion_point, point_factory (when, value));
405 reposition_for_rt_add (0);
411 if (!no_state && !for_loading) {
412 save_state (_("added event"));
417 maybe_signal_changed ();
422 AutomationList::erase (AutomationList::iterator i)
425 Glib::Mutex::Lock lm (lock);
427 reposition_for_rt_add (0);
429 save_state (_("removed event"));
433 maybe_signal_changed ();
437 AutomationList::erase (AutomationList::iterator start, AutomationList::iterator end)
440 Glib::Mutex::Lock lm (lock);
441 events.erase (start, end);
442 reposition_for_rt_add (0);
444 save_state (_("removed multiple events"));
448 maybe_signal_changed ();
452 AutomationList::reset_range (double start, double endt)
457 Glib::Mutex::Lock lm (lock);
459 ControlEvent cp (start, 0.0f);
463 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) != events.end()) {
466 e = upper_bound (events.begin(), events.end(), &cp, cmp);
468 for (iterator i = s; i != e; ++i) {
469 (*i)->value = default_value;
475 save_state (_("removed range"));
483 maybe_signal_changed ();
488 AutomationList::erase_range (double start, double endt)
493 Glib::Mutex::Lock lm (lock);
495 ControlEvent cp (start, 0.0f);
499 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) != events.end()) {
501 e = upper_bound (events.begin(), events.end(), &cp, cmp);
503 reposition_for_rt_add (0);
506 save_state (_("removed range"));
514 maybe_signal_changed ();
519 AutomationList::move_range (iterator start, iterator end, double xdelta, double ydelta)
521 /* note: we assume higher level logic is in place to avoid this
522 reordering the time-order of control events in the list. ie. all
523 points after end are later than (end)->when.
527 Glib::Mutex::Lock lm (lock);
529 while (start != end) {
530 (*start)->when += xdelta;
531 (*start)->value += ydelta;
536 save_state (_("event range adjusted"));
542 maybe_signal_changed ();
546 AutomationList::modify (iterator iter, double when, double val)
548 /* note: we assume higher level logic is in place to avoid this
549 reordering the time-order of control events in the list. ie. all
550 points after *iter are later than when.
554 Glib::Mutex::Lock lm (lock);
555 (*iter)->when = when;
556 (*iter)->value = val;
558 save_state (_("event adjusted"));
564 maybe_signal_changed ();
567 std::pair<AutomationList::iterator,AutomationList::iterator>
568 AutomationList::control_points_adjacent (double xval)
570 Glib::Mutex::Lock lm (lock);
573 ControlEvent cp (xval, 0.0f);
574 std::pair<iterator,iterator> ret;
576 ret.first = events.end();
577 ret.second = events.end();
579 for (i = lower_bound (events.begin(), events.end(), &cp, cmp); i != events.end(); ++i) {
581 if (ret.first == events.end()) {
582 if ((*i)->when >= xval) {
583 if (i != events.begin()) {
592 if ((*i)->when > xval) {
602 AutomationList::freeze ()
608 AutomationList::thaw ()
611 if (changed_when_thawed) {
612 StateChanged(Change(0)); /* EMIT SIGNAL */
617 AutomationList::state_factory (std::string why) const
619 State* state = new State (why);
621 for (AutomationEventList::const_iterator x = events.begin(); x != events.end(); ++x) {
622 state->events.push_back (point_factory (**x));
629 AutomationList::restore_state (StateManager::State& state)
632 Glib::Mutex::Lock lm (lock);
633 State* lstate = dynamic_cast<State*> (&state);
636 for (AutomationEventList::const_iterator x = lstate->events.begin(); x != lstate->events.end(); ++x) {
637 events.push_back (point_factory (**x));
645 AutomationList::get_memento () const
647 return sigc::bind (mem_fun (*(const_cast<AutomationList*> (this)), &StateManager::use_state), _current_state_id);
651 AutomationList::set_max_xval (double x)
657 AutomationList::mark_dirty ()
659 lookup_cache.left = -1;
664 AutomationList::truncate_end (double last_coordinate)
667 Glib::Mutex::Lock lm (lock);
668 ControlEvent cp (last_coordinate, 0);
669 list<ControlEvent*>::reverse_iterator i;
672 if (events.empty()) {
673 fatal << _("programming error:")
674 << "AutomationList::truncate_end() called on an empty list"
680 if (last_coordinate == events.back()->when) {
684 if (last_coordinate > events.back()->when) {
689 iterator foo = events.begin();
692 if (foo == events.end()) {
694 } else if (++foo == events.end()) {
701 /* less than 2 points: add a new point */
702 events.push_back (point_factory (last_coordinate, events.back()->value));
705 /* more than 2 points: check to see if the last 2 values
706 are equal. if so, just move the position of the
707 last point. otherwise, add a new point.
710 iterator penultimate = events.end();
711 --penultimate; /* points at last point */
712 --penultimate; /* points at the penultimate point */
714 if (events.back()->value == (*penultimate)->value) {
715 events.back()->when = last_coordinate;
717 events.push_back (point_factory (last_coordinate, events.back()->value));
725 last_val = unlocked_eval (last_coordinate);
726 last_val = max ((double) min_yval, last_val);
727 last_val = min ((double) max_yval, last_val);
731 /* make i point to the last control point */
735 /* now go backwards, removing control points that are
736 beyond the new last coordinate.
739 uint32_t sz = events.size();
741 while (i != events.rend() && sz > 2) {
742 list<ControlEvent*>::reverse_iterator tmp;
747 if ((*i)->when < last_coordinate) {
751 events.erase (i.base());
757 events.back()->when = last_coordinate;
758 events.back()->value = last_val;
761 reposition_for_rt_add (0);
765 maybe_signal_changed ();
769 AutomationList::truncate_start (double overall_length)
772 Glib::Mutex::Lock lm (lock);
773 AutomationList::iterator i;
774 double first_legal_value;
775 double first_legal_coordinate;
777 if (events.empty()) {
778 fatal << _("programming error:")
779 << "AutomationList::truncate_start() called on an empty list"
785 if (overall_length == events.back()->when) {
786 /* no change in overall length */
790 if (overall_length > events.back()->when) {
792 /* growing at front: duplicate first point. shift all others */
794 double shift = overall_length - events.back()->when;
797 for (np = 0, i = events.begin(); i != events.end(); ++i, ++np) {
803 /* less than 2 points: add a new point */
804 events.push_front (point_factory (0, events.front()->value));
808 /* more than 2 points: check to see if the first 2 values
809 are equal. if so, just move the position of the
810 first point. otherwise, add a new point.
813 iterator second = events.begin();
814 ++second; /* points at the second point */
816 if (events.front()->value == (*second)->value) {
817 /* first segment is flat, just move start point back to zero */
818 events.front()->when = 0;
820 /* leave non-flat segment in place, add a new leading point. */
821 events.push_front (point_factory (0, events.front()->value));
827 /* shrinking at front */
829 first_legal_coordinate = events.back()->when - overall_length;
830 first_legal_value = unlocked_eval (first_legal_coordinate);
831 first_legal_value = max (min_yval, first_legal_value);
832 first_legal_value = min (max_yval, first_legal_value);
834 /* remove all events earlier than the new "front" */
838 while (i != events.end() && !events.empty()) {
839 list<ControlEvent*>::iterator tmp;
844 if ((*i)->when > first_legal_coordinate) {
854 /* shift all remaining points left to keep their same
858 for (i = events.begin(); i != events.end(); ++i) {
859 (*i)->when -= first_legal_coordinate;
862 /* add a new point for the interpolated new value */
864 events.push_front (point_factory (0, first_legal_value));
867 reposition_for_rt_add (0);
872 maybe_signal_changed ();
876 AutomationList::unlocked_eval (double x)
878 return shared_eval (x);
882 AutomationList::shared_eval (double x)
884 pair<AutomationEventList::iterator,AutomationEventList::iterator> range;
890 npoints = events.size();
894 return default_value;
897 if (x >= events.front()->when) {
898 return events.front()->value;
900 // return default_value;
901 return events.front()->value;
905 if (x >= events.back()->when) {
906 return events.back()->value;
907 } else if (x == events.front()->when) {
908 return events.front()->value;
909 } else if (x < events.front()->when) {
910 // return default_value;
911 return events.front()->value;
914 lpos = events.front()->when;
915 lval = events.front()->value;
916 upos = events.back()->when;
917 uval = events.back()->value;
919 /* linear interpolation betweeen the two points
922 fraction = (double) (x - lpos) / (double) (upos - lpos);
923 return lval + (fraction * (uval - lval));
927 if (x >= events.back()->when) {
928 return events.back()->value;
929 } else if (x == events.front()->when) {
930 return events.front()->value;
931 } else if (x < events.front()->when) {
932 // return default_value;
933 return events.front()->value;
936 return multipoint_eval (x);
942 AutomationList::multipoint_eval (double x)
944 pair<AutomationList::iterator,AutomationList::iterator> range;
949 /* only do the range lookup if x is in a different range than last time
950 this was called (or if the lookup cache has been marked "dirty" (left<0)
953 if ((lookup_cache.left < 0) ||
954 ((lookup_cache.left > x) ||
955 (lookup_cache.range.first == events.end()) ||
956 ((*lookup_cache.range.second)->when < x))) {
958 ControlEvent cp (x, 0);
961 lookup_cache.range = equal_range (events.begin(), events.end(), &cp, cmp);
964 range = lookup_cache.range;
966 if (range.first == range.second) {
968 /* x does not exist within the list as a control point */
970 lookup_cache.left = x;
972 if (range.first != events.begin()) {
974 lpos = (*range.first)->when;
975 lval = (*range.first)->value;
977 /* we're before the first point */
978 // return default_value;
979 return events.front()->value;
982 if (range.second == events.end()) {
983 /* we're after the last point */
984 return events.back()->value;
987 upos = (*range.second)->when;
988 uval = (*range.second)->value;
990 /* linear interpolation betweeen the two points
994 fraction = (double) (x - lpos) / (double) (upos - lpos);
995 return lval + (fraction * (uval - lval));
999 /* x is a control point in the data */
1000 lookup_cache.left = -1;
1001 return (*range.first)->value;
1005 AutomationList::cut (iterator start, iterator end)
1007 AutomationList* nal = new AutomationList (default_value);
1010 Glib::Mutex::Lock lm (lock);
1012 for (iterator x = start; x != end; ) {
1018 nal->events.push_back (point_factory (**x));
1021 reposition_for_rt_add (0);
1029 maybe_signal_changed ();
1035 AutomationList::cut_copy_clear (double start, double end, int op)
1037 AutomationList* nal = new AutomationList (default_value);
1039 ControlEvent cp (start, 0.0);
1041 bool changed = false;
1044 Glib::Mutex::Lock lm (lock);
1046 if ((s = lower_bound (events.begin(), events.end(), &cp, cmp)) == events.end()) {
1051 e = upper_bound (events.begin(), events.end(), &cp, cmp);
1053 if (op != 2 && (*s)->when != start) {
1054 nal->events.push_back (point_factory (0, unlocked_eval (start)));
1057 for (iterator x = s; x != e; ) {
1065 /* adjust new points to be relative to start, which
1066 has been set to zero.
1070 nal->events.push_back (point_factory ((*x)->when - start, (*x)->value));
1080 if (op != 2 && nal->events.back()->when != end - start) {
1081 nal->events.push_back (point_factory (end - start, unlocked_eval (end)));
1085 reposition_for_rt_add (0);
1087 save_state (_("cut/copy/clear"));
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));
1120 save_state (_("copy"));
1128 AutomationList::cut (double start, double end)
1130 return cut_copy_clear (start, end, 0);
1134 AutomationList::copy (double start, double end)
1136 return cut_copy_clear (start, end, 1);
1140 AutomationList::clear (double start, double end)
1142 (void) cut_copy_clear (start, end, 2);
1146 AutomationList::paste (AutomationList& alist, double pos, float times)
1148 if (alist.events.empty()) {
1153 Glib::Mutex::Lock lm (lock);
1157 ControlEvent cp (pos, 0.0);
1160 where = upper_bound (events.begin(), events.end(), &cp, cmp);
1162 for (iterator i = alist.begin();i != alist.end(); ++i) {
1163 events.insert (where, point_factory( (*i)->when+pos,( *i)->value));
1164 end = (*i)->when + pos;
1168 /* move all points after the insertion along the timeline by
1172 while (where != events.end()) {
1174 if ((*where)->when <= end) {
1177 events.erase(where);
1185 reposition_for_rt_add (0);
1188 save_state (_("paste"));
1194 maybe_signal_changed ();
1199 AutomationList::point_factory (double when, double val) const
1201 return new ControlEvent (when, val);
1205 AutomationList::point_factory (const ControlEvent& other) const
1207 return new ControlEvent (other);
1211 AutomationList::store_state (XMLNode& node) const
1213 LocaleGuard lg (X_("POSIX"));
1215 for (const_iterator i = const_begin(); i != const_end(); ++i) {
1218 XMLNode *pointnode = new XMLNode ("point");
1220 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*i)->when));
1221 pointnode->add_property ("x", buf);
1222 snprintf (buf, sizeof (buf), "%.12g", (*i)->value);
1223 pointnode->add_property ("y", buf);
1225 node.add_child_nocopy (*pointnode);
1230 AutomationList::load_state (const XMLNode& node)
1232 const XMLNodeList& elist = node.children();
1233 XMLNodeConstIterator i;
1240 for (i = elist.begin(); i != elist.end(); ++i) {
1242 if ((prop = (*i)->property ("x")) == 0) {
1243 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
1246 x = atoi (prop->value().c_str());
1248 if ((prop = (*i)->property ("y")) == 0) {
1249 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
1252 y = atof (prop->value().c_str());
1258 XMLNode &AutomationList::get_state ()
1260 XMLNode *node = new XMLNode("AutomationList");
1265 int AutomationList::set_state(const XMLNode &s)