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()
136 boost::shared_ptr<Evoral::ControlList>
137 AutomationList::create(const Evoral::Parameter& id,
138 const Evoral::ParameterDescriptor& desc)
140 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id, desc));
144 AutomationList::create_curve_if_necessary()
146 switch (_parameter.type()) {
149 case PanAzimuthAutomation:
150 case PanElevationAutomation:
151 case PanWidthAutomation:
152 case FadeInAutomation:
153 case FadeOutAutomation:
154 case EnvelopeAutomation:
163 AutomationList::operator= (const AutomationList& other)
165 if (this != &other) {
168 ControlList::operator= (other);
169 _state = other._state;
170 _style = other._style;
171 _touching = other._touching;
174 maybe_signal_changed ();
181 AutomationList::maybe_signal_changed ()
183 ControlList::maybe_signal_changed ();
185 if (!ControlList::frozen()) {
186 StateChanged (); /* EMIT SIGNAL */
191 AutomationList::set_automation_state (AutoState s)
195 if (s == Write && _desc.toggled) {
196 _before = &get_state ();
198 automation_state_changed (s); /* EMIT SIGNAL */
203 AutomationList::set_automation_style (AutoStyle s)
207 automation_style_changed (); /* EMIT SIGNAL */
212 AutomationList::start_write_pass (double when)
214 if (in_new_write_pass ()) {
215 _before = &get_state ();
217 ControlList::start_write_pass (when);
221 AutomationList::write_pass_finished (double when, double thinning_factor)
223 ControlList::write_pass_finished (when, thinning_factor);
224 /* automation control has deleted this or it is now owned by the session undo stack */
229 AutomationList::start_touch (double when)
231 if (_state == Touch) {
232 start_write_pass (when);
235 g_atomic_int_set (&_touching, 1);
239 AutomationList::stop_touch (bool mark, double)
241 if (g_atomic_int_get (&_touching) == 0) {
242 /* this touch has already been stopped (probably by Automatable::transport_stopped),
243 so we've nothing to do.
248 g_atomic_int_set (&_touching, 0);
250 if (_state == Touch) {
254 /* XXX need to mark the last added point with the
261 /* _before may be owned by the undo stack,
262 * so we have to be careful about doing this.
265 AutomationList::clear_history ()
272 AutomationList::thaw ()
276 if (_changed_when_thawed) {
277 _changed_when_thawed = false;
278 StateChanged(); /* EMIT SIGNAL */
283 AutomationList::memento_command (XMLNode* before, XMLNode* after)
285 return new MementoCommand<AutomationList> (*this, before, after);
289 AutomationList::get_state ()
295 AutomationList::state (bool full)
297 XMLNode* root = new XMLNode (X_("AutomationList"));
299 LocaleGuard lg (X_("C"));
301 root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
303 root->add_property ("id", id().to_s());
305 snprintf (buf, sizeof (buf), "%.12g", _default_value);
306 root->add_property ("default", buf);
307 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
308 root->add_property ("min-yval", buf);
309 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
310 root->add_property ("max-yval", buf);
312 root->add_property ("interpolation-style", enum_2_string (_interpolation));
315 /* never serialize state with Write enabled - too dangerous
318 if (_state != Write) {
319 root->add_property ("state", auto_state_to_string (_state));
321 if (_events.empty ()) {
322 root->add_property ("state", auto_state_to_string (Off));
324 root->add_property ("state", auto_state_to_string (Touch));
328 /* never save anything but Off for automation state to a template */
329 root->add_property ("state", auto_state_to_string (Off));
332 root->add_property ("style", auto_style_to_string (_style));
334 if (!_events.empty()) {
335 root->add_child_nocopy (serialize_events());
342 AutomationList::serialize_events ()
344 XMLNode* node = new XMLNode (X_("events"));
347 str.precision(15); //10 digits is enough digits for 24 hours at 96kHz
349 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
350 str << (double) (*xx)->when;
352 str <<(double) (*xx)->value;
356 /* XML is a bit wierd */
358 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
359 content_node->set_content (str.str());
361 node->add_child_nocopy (*content_node);
367 AutomationList::deserialize_events (const XMLNode& node)
369 if (node.children().empty()) {
373 XMLNode* content_node = node.children().front();
375 if (content_node->content().empty()) {
379 ControlList::freeze ();
382 stringstream str (content_node->content());
398 fast_simple_add (x, y);
403 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
406 maybe_signal_changed ();
415 AutomationList::set_state (const XMLNode& node, int version)
417 LocaleGuard lg (X_("C"));
418 XMLNodeList nlist = node.children();
420 XMLNodeIterator niter;
421 const XMLProperty* prop;
423 if (node.name() == X_("events")) {
424 /* partial state setting*/
425 return deserialize_events (node);
428 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
430 if ((nsos = node.child (X_("AutomationList")))) {
431 /* new school in old school clothing */
432 return set_state (*nsos, version);
437 const XMLNodeList& elist = node.children();
438 XMLNodeConstIterator i;
443 ControlList::freeze ();
446 for (i = elist.begin(); i != elist.end(); ++i) {
448 if ((prop = (*i)->property ("x")) == 0) {
449 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
452 x = atoi (prop->value().c_str());
454 if ((prop = (*i)->property ("y")) == 0) {
455 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
458 y = atof (prop->value().c_str());
460 fast_simple_add (x, y);
468 if (node.name() != X_("AutomationList") ) {
469 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
474 /* update session AL list */
475 AutomationListCreated(this);
478 if ((prop = node.property (X_("automation-id"))) != 0){
479 _parameter = EventTypeMap::instance().from_symbol(prop->value());
481 warning << "Legacy session: automation list has no automation-id property." << endmsg;
484 if ((prop = node.property (X_("interpolation-style"))) != 0) {
485 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
487 _interpolation = Linear;
490 if ((prop = node.property (X_("default"))) != 0){
491 _default_value = atof (prop->value().c_str());
493 _default_value = 0.0;
496 if ((prop = node.property (X_("style"))) != 0) {
497 _style = string_to_auto_style (prop->value());
502 if ((prop = node.property (X_("state"))) != 0) {
503 _state = string_to_auto_state (prop->value());
504 if (_state == Write) {
507 automation_state_changed(_state);
512 if ((prop = node.property (X_("min-yval"))) != 0) {
513 _min_yval = atof (prop->value ().c_str());
518 if ((prop = node.property (X_("max-yval"))) != 0) {
519 _max_yval = atof (prop->value ().c_str());
524 bool have_events = false;
526 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
527 if ((*niter)->name() == X_("events")) {
528 deserialize_events (*(*niter));
534 /* there was no Events child node; clear any current events */
538 maybe_signal_changed ();
546 AutomationList::operator!= (AutomationList const & other) const
549 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
550 _state != other._state ||
551 _style != other._style ||
552 _touching != other._touching
557 AutomationListProperty::clone () const
559 return new AutomationListProperty (
561 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
562 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))