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_list.h"
28 #include "ardour/event_type_map.h"
29 #include "evoral/Curve.hpp"
30 #include "pbd/stacktrace.h"
31 #include "pbd/enumwriter.h"
36 using namespace ARDOUR;
40 sigc::signal<void,AutomationList *> AutomationList::AutomationListCreated;
43 static void dumpit (const AutomationList& al, string prefix = "")
45 cerr << prefix << &al << endl;
46 for (AutomationList::const_iterator i = al.const_begin(); i != al.const_end(); ++i) {
47 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
53 AutomationList::AutomationList (Evoral::Parameter id)
60 create_curve_if_necessary();
62 assert(_parameter.type() != NullAutomation);
63 AutomationListCreated(this);
66 AutomationList::AutomationList (const AutomationList& other)
67 : StatefulDestructible()
70 _style = other._style;
71 _state = other._state;
72 _touching = other._touching;
74 create_curve_if_necessary();
76 assert(_parameter.type() != NullAutomation);
77 AutomationListCreated(this);
80 AutomationList::AutomationList (const AutomationList& other, double start, double end)
81 : ControlList(other, start, end)
83 _style = other._style;
84 _state = other._state;
85 _touching = other._touching;
87 create_curve_if_necessary();
89 assert(_parameter.type() != NullAutomation);
90 AutomationListCreated(this);
93 /** \a id is used for legacy sessions where the type is not present
94 * in or below the <AutomationList> node. It is used if \a id is non-null.
96 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
109 create_curve_if_necessary();
111 assert(_parameter.type() != NullAutomation);
112 AutomationListCreated(this);
115 AutomationList::~AutomationList()
120 boost::shared_ptr<Evoral::ControlList>
121 AutomationList::create(Evoral::Parameter id)
123 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id));
127 AutomationList::create_curve_if_necessary()
129 switch (_parameter.type()) {
132 case FadeInAutomation:
133 case FadeOutAutomation:
134 case EnvelopeAutomation:
143 AutomationList::operator== (const AutomationList& other)
145 return _events == other._events;
149 AutomationList::operator= (const AutomationList& other)
151 if (this != &other) {
155 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
156 _events.push_back (new Evoral::ControlEvent (**i));
159 _min_yval = other._min_yval;
160 _max_yval = other._max_yval;
161 _max_xval = other._max_xval;
162 _default_value = other._default_value;
165 maybe_signal_changed ();
172 AutomationList::maybe_signal_changed ()
174 ControlList::maybe_signal_changed ();
177 StateChanged (); /* EMIT SIGNAL */
182 AutomationList::set_automation_state (AutoState s)
186 automation_state_changed (); /* EMIT SIGNAL */
191 AutomationList::set_automation_style (AutoStyle s)
195 automation_style_changed (); /* EMIT SIGNAL */
200 AutomationList::start_touch ()
207 AutomationList::stop_touch ()
214 AutomationList::freeze ()
220 AutomationList::thaw ()
224 if (_changed_when_thawed) {
225 StateChanged(); /* EMIT SIGNAL */
230 AutomationList::mark_dirty () const
232 ControlList::mark_dirty ();
233 Dirty (); /* EMIT SIGNAL */
237 AutomationList::get_state ()
243 AutomationList::state (bool full)
245 XMLNode* root = new XMLNode (X_("AutomationList"));
247 LocaleGuard lg (X_("POSIX"));
249 root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
251 root->add_property ("id", _id.to_s());
253 snprintf (buf, sizeof (buf), "%.12g", _default_value);
254 root->add_property ("default", buf);
255 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
256 root->add_property ("min-yval", buf);
257 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
258 root->add_property ("max-yval", buf);
259 snprintf (buf, sizeof (buf), "%.12g", _max_xval);
260 root->add_property ("max-xval", buf);
262 root->add_property ("interpolation-style", enum_2_string (_interpolation));
265 root->add_property ("state", auto_state_to_string (_state));
267 /* never save anything but Off for automation state to a template */
268 root->add_property ("state", auto_state_to_string (Off));
271 root->add_property ("style", auto_style_to_string (_style));
273 if (!_events.empty()) {
274 root->add_child_nocopy (serialize_events());
281 AutomationList::serialize_events ()
283 XMLNode* node = new XMLNode (X_("events"));
286 str.precision(15); //10 digits is enough digits for 24 hours at 96kHz
288 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
289 str << (double) (*xx)->when;
291 str <<(double) (*xx)->value;
295 /* XML is a bit wierd */
297 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
298 content_node->set_content (str.str());
300 node->add_child_nocopy (*content_node);
306 AutomationList::deserialize_events (const XMLNode& node)
308 if (node.children().empty()) {
312 XMLNode* content_node = node.children().front();
314 if (content_node->content().empty()) {
321 stringstream str (content_node->content());
337 fast_simple_add (x, y);
342 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
345 reposition_for_rt_add (0);
346 maybe_signal_changed ();
355 AutomationList::set_state (const XMLNode& node)
357 XMLNodeList nlist = node.children();
359 XMLNodeIterator niter;
360 const XMLProperty* prop;
362 if (node.name() == X_("events")) {
363 /* partial state setting*/
364 return deserialize_events (node);
367 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
369 if ((nsos = node.child (X_("AutomationList")))) {
370 /* new school in old school clothing */
371 return set_state (*nsos);
376 const XMLNodeList& elist = node.children();
377 XMLNodeConstIterator i;
385 for (i = elist.begin(); i != elist.end(); ++i) {
387 if ((prop = (*i)->property ("x")) == 0) {
388 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
391 x = atoi (prop->value().c_str());
393 if ((prop = (*i)->property ("y")) == 0) {
394 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
397 y = atof (prop->value().c_str());
399 fast_simple_add (x, y);
407 if (node.name() != X_("AutomationList") ) {
408 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
412 if ((prop = node.property ("id")) != 0) {
413 _id = prop->value ();
414 /* update session AL list */
415 AutomationListCreated(this);
418 if ((prop = node.property (X_("automation-id"))) != 0){
419 _parameter = EventTypeMap::instance().new_parameter(prop->value());
421 warning << "Legacy session: automation list has no automation-id property.";
424 if ((prop = node.property (X_("interpolation-style"))) != 0) {
425 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
427 _interpolation = Linear;
430 if ((prop = node.property (X_("default"))) != 0){
431 _default_value = atof (prop->value().c_str());
433 _default_value = 0.0;
436 if ((prop = node.property (X_("style"))) != 0) {
437 _style = string_to_auto_style (prop->value());
442 if ((prop = node.property (X_("state"))) != 0) {
443 _state = string_to_auto_state (prop->value());
448 if ((prop = node.property (X_("min_yval"))) != 0) {
449 _min_yval = atof (prop->value ().c_str());
454 if ((prop = node.property (X_("max_yval"))) != 0) {
455 _max_yval = atof (prop->value ().c_str());
460 if ((prop = node.property (X_("max_xval"))) != 0) {
461 _max_xval = atof (prop->value ().c_str());
463 _max_xval = 0; // means "no limit ;
466 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
467 if ((*niter)->name() == X_("events")) {
468 deserialize_events (*(*niter));