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/memento_command.h"
31 #include "pbd/stacktrace.h"
32 #include "pbd/enumwriter.h"
37 using namespace ARDOUR;
40 PBD::Signal1<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.begin(); i != al.end(); ++i) {
47 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
52 AutomationList::AutomationList (const Evoral::Parameter& id, const Evoral::ParameterDescriptor& desc)
53 : ControlList(id, desc)
58 g_atomic_int_set (&_touching, 0);
60 create_curve_if_necessary();
62 assert(_parameter.type() != NullAutomation);
63 AutomationListCreated(this);
66 AutomationList::AutomationList (const Evoral::Parameter& id)
67 : ControlList(id, ARDOUR::ParameterDescriptor(id))
72 g_atomic_int_set (&_touching, 0);
74 create_curve_if_necessary();
76 assert(_parameter.type() != NullAutomation);
77 AutomationListCreated(this);
80 AutomationList::AutomationList (const AutomationList& other)
81 : StatefulDestructible()
85 _style = other._style;
86 _state = other._state;
87 g_atomic_int_set (&_touching, other.touching());
89 create_curve_if_necessary();
91 assert(_parameter.type() != NullAutomation);
92 AutomationListCreated(this);
95 AutomationList::AutomationList (const AutomationList& other, double start, double end)
96 : ControlList(other, start, end)
99 _style = other._style;
100 _state = other._state;
101 g_atomic_int_set (&_touching, other.touching());
103 create_curve_if_necessary();
105 assert(_parameter.type() != NullAutomation);
106 AutomationListCreated(this);
109 /** @param id is used for legacy sessions where the type is not present
110 * in or below the AutomationList node. It is used if @param id is non-null.
112 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
113 : ControlList(id, ARDOUR::ParameterDescriptor(id))
116 g_atomic_int_set (&_touching, 0);
120 set_state (node, Stateful::loading_state_version);
126 create_curve_if_necessary();
128 assert(_parameter.type() != NullAutomation);
129 AutomationListCreated(this);
132 AutomationList::~AutomationList()
137 boost::shared_ptr<Evoral::ControlList>
138 AutomationList::create(const Evoral::Parameter& id,
139 const Evoral::ParameterDescriptor& desc)
141 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id, desc));
145 AutomationList::create_curve_if_necessary()
147 switch (_parameter.type()) {
150 case PanAzimuthAutomation:
151 case PanElevationAutomation:
152 case PanWidthAutomation:
153 case FadeInAutomation:
154 case FadeOutAutomation:
155 case EnvelopeAutomation:
164 AutomationList::operator= (const AutomationList& other)
166 if (this != &other) {
169 ControlList::operator= (other);
170 _state = other._state;
171 _style = other._style;
172 _touching = other._touching;
175 maybe_signal_changed ();
182 AutomationList::maybe_signal_changed ()
184 ControlList::maybe_signal_changed ();
186 if (!ControlList::frozen()) {
187 StateChanged (); /* EMIT SIGNAL */
192 AutomationList::set_automation_state (AutoState s)
197 if (s == Write && _desc.toggled) {
198 _before = &get_state ();
202 automation_state_changed (s); /* EMIT SIGNAL */
207 AutomationList::set_automation_style (AutoStyle s)
211 automation_style_changed (); /* EMIT SIGNAL */
216 AutomationList::start_write_pass (double when)
219 if (in_new_write_pass ()) {
220 _before = &get_state ();
224 ControlList::start_write_pass (when);
228 AutomationList::write_pass_finished (double when, double thinning_factor)
230 ControlList::write_pass_finished (when, thinning_factor);
234 AutomationList::start_touch (double when)
236 if (_state == Touch) {
237 start_write_pass (when);
240 g_atomic_int_set (&_touching, 1);
244 AutomationList::stop_touch (bool mark, double)
246 if (g_atomic_int_get (&_touching) == 0) {
247 /* this touch has already been stopped (probably by Automatable::transport_stopped),
248 so we've nothing to do.
253 g_atomic_int_set (&_touching, 0);
255 if (_state == Touch) {
259 /* XXX need to mark the last added point with the
266 /* _before may be owned by the undo stack,
267 * so we have to be careful about doing this.
269 * ::before () transfers ownership, setting _before to 0
272 AutomationList::clear_history ()
279 AutomationList::thaw ()
283 if (_changed_when_thawed) {
284 _changed_when_thawed = false;
285 StateChanged(); /* EMIT SIGNAL */
290 AutomationList::memento_command (XMLNode* before, XMLNode* after)
292 return new MementoCommand<AutomationList> (*this, before, after);
296 AutomationList::get_state ()
302 AutomationList::state (bool full)
304 XMLNode* root = new XMLNode (X_("AutomationList"));
308 root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
310 root->add_property ("id", id().to_s());
312 snprintf (buf, sizeof (buf), "%.12g", _default_value);
313 root->add_property ("default", buf);
314 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
315 root->add_property ("min-yval", buf);
316 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
317 root->add_property ("max-yval", buf);
319 root->add_property ("interpolation-style", enum_2_string (_interpolation));
322 /* never serialize state with Write enabled - too dangerous
325 if (_state != Write) {
326 root->add_property ("state", auto_state_to_string (_state));
328 if (_events.empty ()) {
329 root->add_property ("state", auto_state_to_string (Off));
331 root->add_property ("state", auto_state_to_string (Touch));
335 /* never save anything but Off for automation state to a template */
336 root->add_property ("state", auto_state_to_string (Off));
339 root->add_property ("style", auto_style_to_string (_style));
341 if (!_events.empty()) {
342 root->add_child_nocopy (serialize_events());
349 AutomationList::serialize_events ()
351 XMLNode* node = new XMLNode (X_("events"));
354 str.precision(15); //10 digits is enough digits for 24 hours at 96kHz
356 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
357 str << (double) (*xx)->when;
359 str <<(double) (*xx)->value;
363 /* XML is a bit wierd */
365 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
366 content_node->set_content (str.str());
368 node->add_child_nocopy (*content_node);
374 AutomationList::deserialize_events (const XMLNode& node)
376 if (node.children().empty()) {
380 XMLNode* content_node = node.children().front();
382 if (content_node->content().empty()) {
386 ControlList::freeze ();
389 stringstream str (content_node->content());
405 fast_simple_add (x, y);
410 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
413 maybe_signal_changed ();
422 AutomationList::set_state (const XMLNode& node, int version)
425 XMLNodeList nlist = node.children();
427 XMLNodeIterator niter;
428 XMLProperty const * prop;
430 if (node.name() == X_("events")) {
431 /* partial state setting*/
432 return deserialize_events (node);
435 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
437 if ((nsos = node.child (X_("AutomationList")))) {
438 /* new school in old school clothing */
439 return set_state (*nsos, version);
444 const XMLNodeList& elist = node.children();
445 XMLNodeConstIterator i;
446 XMLProperty const * prop;
450 ControlList::freeze ();
453 for (i = elist.begin(); i != elist.end(); ++i) {
455 if ((prop = (*i)->property ("x")) == 0) {
456 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
459 x = atoi (prop->value().c_str());
461 if ((prop = (*i)->property ("y")) == 0) {
462 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
465 y = atof (prop->value().c_str());
467 fast_simple_add (x, y);
475 if (node.name() != X_("AutomationList") ) {
476 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
481 /* update session AL list */
482 AutomationListCreated(this);
485 if ((prop = node.property (X_("automation-id"))) != 0){
486 _parameter = EventTypeMap::instance().from_symbol(prop->value());
488 warning << "Legacy session: automation list has no automation-id property." << endmsg;
491 if ((prop = node.property (X_("interpolation-style"))) != 0) {
492 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
494 _interpolation = Linear;
497 if ((prop = node.property (X_("default"))) != 0){
498 _default_value = atof (prop->value().c_str());
500 _default_value = 0.0;
503 if ((prop = node.property (X_("style"))) != 0) {
504 _style = string_to_auto_style (prop->value());
509 if ((prop = node.property (X_("state"))) != 0) {
510 _state = string_to_auto_state (prop->value());
511 if (_state == Write) {
514 automation_state_changed(_state);
519 if ((prop = node.property (X_("min-yval"))) != 0) {
520 _min_yval = atof (prop->value ().c_str());
525 if ((prop = node.property (X_("max-yval"))) != 0) {
526 _max_yval = atof (prop->value ().c_str());
531 bool have_events = false;
533 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
534 if ((*niter)->name() == X_("events")) {
535 deserialize_events (*(*niter));
541 /* there was no Events child node; clear any current events */
545 maybe_signal_changed ();
553 AutomationList::operator!= (AutomationList const & other) const
556 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
557 _state != other._state ||
558 _style != other._style ||
559 _touching != other._touching
564 AutomationListProperty::clone () const
566 return new AutomationListProperty (
568 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
569 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))