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 "ardour/automation_list.h"
27 #include "ardour/event_type_map.h"
28 #include "ardour/parameter_descriptor.h"
29 #include "evoral/Curve.hpp"
30 #include "pbd/stacktrace.h"
31 #include "pbd/enumwriter.h"
36 using namespace ARDOUR;
39 PBD::Signal1<void,AutomationList *> AutomationList::AutomationListCreated;
42 static void dumpit (const AutomationList& al, string prefix = "")
44 cerr << prefix << &al << endl;
45 for (AutomationList::const_iterator i = al.begin(); i != al.end(); ++i) {
46 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
51 AutomationList::AutomationList (const Evoral::Parameter& id, const Evoral::ParameterDescriptor& desc)
52 : ControlList(id, desc)
57 g_atomic_int_set (&_touching, 0);
59 create_curve_if_necessary();
61 assert(_parameter.type() != NullAutomation);
62 AutomationListCreated(this);
65 AutomationList::AutomationList (const Evoral::Parameter& id)
66 : ControlList(id, ARDOUR::ParameterDescriptor(id))
71 g_atomic_int_set (&_touching, 0);
73 create_curve_if_necessary();
75 assert(_parameter.type() != NullAutomation);
76 AutomationListCreated(this);
79 AutomationList::AutomationList (const AutomationList& other)
80 : StatefulDestructible()
84 _style = other._style;
85 _state = other._state;
86 g_atomic_int_set (&_touching, other.touching());
88 create_curve_if_necessary();
90 assert(_parameter.type() != NullAutomation);
91 AutomationListCreated(this);
94 AutomationList::AutomationList (const AutomationList& other, double start, double end)
95 : ControlList(other, start, end)
98 _style = other._style;
99 _state = other._state;
100 g_atomic_int_set (&_touching, other.touching());
102 create_curve_if_necessary();
104 assert(_parameter.type() != NullAutomation);
105 AutomationListCreated(this);
108 /** @param id is used for legacy sessions where the type is not present
109 * in or below the AutomationList node. It is used if @param id is non-null.
111 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
112 : ControlList(id, ARDOUR::ParameterDescriptor(id))
115 g_atomic_int_set (&_touching, 0);
119 set_state (node, Stateful::loading_state_version);
125 create_curve_if_necessary();
127 assert(_parameter.type() != NullAutomation);
128 AutomationListCreated(this);
131 AutomationList::~AutomationList()
135 boost::shared_ptr<Evoral::ControlList>
136 AutomationList::create(const Evoral::Parameter& id,
137 const Evoral::ParameterDescriptor& desc)
139 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id, desc));
143 AutomationList::create_curve_if_necessary()
145 switch (_parameter.type()) {
148 case PanAzimuthAutomation:
149 case PanElevationAutomation:
150 case PanWidthAutomation:
151 case FadeInAutomation:
152 case FadeOutAutomation:
153 case EnvelopeAutomation:
162 AutomationList::operator= (const AutomationList& other)
164 if (this != &other) {
167 ControlList::operator= (other);
168 _state = other._state;
169 _style = other._style;
170 _touching = other._touching;
173 maybe_signal_changed ();
180 AutomationList::maybe_signal_changed ()
182 ControlList::maybe_signal_changed ();
184 if (!ControlList::frozen()) {
185 StateChanged (); /* EMIT SIGNAL */
190 AutomationList::set_automation_state (AutoState s)
195 _before = &get_state ();
197 automation_state_changed (s); /* EMIT SIGNAL */
202 AutomationList::set_automation_style (AutoStyle s)
206 automation_style_changed (); /* EMIT SIGNAL */
211 AutomationList::start_write_pass (double when)
213 if (in_new_write_pass ()) {
214 _before = &get_state ();
216 ControlList::start_write_pass (when);
220 AutomationList::write_pass_finished (double when, double thinning_factor)
222 ControlList::write_pass_finished (when, thinning_factor);
223 /* automation control has deleted this or it is now owned by the session undo stack */
228 AutomationList::start_touch (double when)
230 if (_state == Touch) {
231 start_write_pass (when);
234 g_atomic_int_set (&_touching, 1);
238 AutomationList::stop_touch (bool mark, double)
240 if (g_atomic_int_get (&_touching) == 0) {
241 /* this touch has already been stopped (probably by Automatable::transport_stopped),
242 so we've nothing to do.
247 g_atomic_int_set (&_touching, 0);
249 if (_state == Touch) {
253 /* XXX need to mark the last added point with the
260 /* _before may be owned by the undo stack,
261 * so we have to be careful about doing this.
264 AutomationList::clear_history ()
271 AutomationList::thaw ()
275 if (_changed_when_thawed) {
276 _changed_when_thawed = false;
277 StateChanged(); /* EMIT SIGNAL */
282 AutomationList::get_state ()
288 AutomationList::state (bool full)
290 XMLNode* root = new XMLNode (X_("AutomationList"));
292 LocaleGuard lg (X_("C"));
294 root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
296 root->add_property ("id", id().to_s());
298 snprintf (buf, sizeof (buf), "%.12g", _default_value);
299 root->add_property ("default", buf);
300 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
301 root->add_property ("min-yval", buf);
302 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
303 root->add_property ("max-yval", buf);
305 root->add_property ("interpolation-style", enum_2_string (_interpolation));
308 /* never serialize state with Write enabled - too dangerous
311 if (_state != Write) {
312 root->add_property ("state", auto_state_to_string (_state));
314 if (_events.empty ()) {
315 root->add_property ("state", auto_state_to_string (Off));
317 root->add_property ("state", auto_state_to_string (Touch));
321 /* never save anything but Off for automation state to a template */
322 root->add_property ("state", auto_state_to_string (Off));
325 root->add_property ("style", auto_style_to_string (_style));
327 if (!_events.empty()) {
328 root->add_child_nocopy (serialize_events());
335 AutomationList::serialize_events ()
337 XMLNode* node = new XMLNode (X_("events"));
340 str.precision(15); //10 digits is enough digits for 24 hours at 96kHz
342 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
343 str << (double) (*xx)->when;
345 str <<(double) (*xx)->value;
349 /* XML is a bit wierd */
351 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
352 content_node->set_content (str.str());
354 node->add_child_nocopy (*content_node);
360 AutomationList::deserialize_events (const XMLNode& node)
362 if (node.children().empty()) {
366 XMLNode* content_node = node.children().front();
368 if (content_node->content().empty()) {
372 ControlList::freeze ();
375 stringstream str (content_node->content());
391 fast_simple_add (x, y);
396 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
399 maybe_signal_changed ();
408 AutomationList::set_state (const XMLNode& node, int version)
410 LocaleGuard lg (X_("C"));
411 XMLNodeList nlist = node.children();
413 XMLNodeIterator niter;
414 const XMLProperty* prop;
416 if (node.name() == X_("events")) {
417 /* partial state setting*/
418 return deserialize_events (node);
421 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
423 if ((nsos = node.child (X_("AutomationList")))) {
424 /* new school in old school clothing */
425 return set_state (*nsos, version);
430 const XMLNodeList& elist = node.children();
431 XMLNodeConstIterator i;
436 ControlList::freeze ();
439 for (i = elist.begin(); i != elist.end(); ++i) {
441 if ((prop = (*i)->property ("x")) == 0) {
442 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
445 x = atoi (prop->value().c_str());
447 if ((prop = (*i)->property ("y")) == 0) {
448 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
451 y = atof (prop->value().c_str());
453 fast_simple_add (x, y);
461 if (node.name() != X_("AutomationList") ) {
462 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
467 /* update session AL list */
468 AutomationListCreated(this);
471 if ((prop = node.property (X_("automation-id"))) != 0){
472 _parameter = EventTypeMap::instance().from_symbol(prop->value());
474 warning << "Legacy session: automation list has no automation-id property." << endmsg;
477 if ((prop = node.property (X_("interpolation-style"))) != 0) {
478 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
480 _interpolation = Linear;
483 if ((prop = node.property (X_("default"))) != 0){
484 _default_value = atof (prop->value().c_str());
486 _default_value = 0.0;
489 if ((prop = node.property (X_("style"))) != 0) {
490 _style = string_to_auto_style (prop->value());
495 if ((prop = node.property (X_("state"))) != 0) {
496 _state = string_to_auto_state (prop->value());
497 if (_state == Write) {
500 automation_state_changed(_state);
505 if ((prop = node.property (X_("min-yval"))) != 0) {
506 _min_yval = atof (prop->value ().c_str());
511 if ((prop = node.property (X_("max-yval"))) != 0) {
512 _max_yval = atof (prop->value ().c_str());
517 bool have_events = false;
519 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
520 if ((*niter)->name() == X_("events")) {
521 deserialize_events (*(*niter));
527 /* there was no Events child node; clear any current events */
531 maybe_signal_changed ();
539 AutomationList::operator!= (AutomationList const & other) const
542 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
543 _state != other._state ||
544 _style != other._style ||
545 _touching != other._touching
550 AutomationListProperty::clone () const
552 return new AutomationListProperty (
554 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
555 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))