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 "evoral/Curve.hpp"
29 #include "pbd/stacktrace.h"
30 #include "pbd/enumwriter.h"
35 using namespace ARDOUR;
38 PBD::Signal1<void,AutomationList *> AutomationList::AutomationListCreated;
41 static void dumpit (const AutomationList& al, string prefix = "")
43 cerr << prefix << &al << endl;
44 for (AutomationList::const_iterator i = al.begin(); i != al.end(); ++i) {
45 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
50 AutomationList::AutomationList (Evoral::Parameter id)
55 g_atomic_int_set (&_touching, 0);
57 create_curve_if_necessary();
59 assert(_parameter.type() != NullAutomation);
60 AutomationListCreated(this);
63 AutomationList::AutomationList (const AutomationList& other)
64 : StatefulDestructible()
67 _style = other._style;
68 _state = other._state;
69 g_atomic_int_set (&_touching, other.touching());
71 create_curve_if_necessary();
73 assert(_parameter.type() != NullAutomation);
74 AutomationListCreated(this);
77 AutomationList::AutomationList (const AutomationList& other, double start, double end)
78 : ControlList(other, start, end)
80 _style = other._style;
81 _state = other._state;
82 g_atomic_int_set (&_touching, other.touching());
84 create_curve_if_necessary();
86 assert(_parameter.type() != NullAutomation);
87 AutomationListCreated(this);
90 /** @param id is used for legacy sessions where the type is not present
91 * in or below the AutomationList node. It is used if @param id is non-null.
93 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
96 g_atomic_int_set (&_touching, 0);
100 set_state (node, Stateful::loading_state_version);
106 create_curve_if_necessary();
108 assert(_parameter.type() != NullAutomation);
109 AutomationListCreated(this);
112 AutomationList::~AutomationList()
116 boost::shared_ptr<Evoral::ControlList>
117 AutomationList::create(Evoral::Parameter id)
119 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id));
123 AutomationList::create_curve_if_necessary()
125 switch (_parameter.type()) {
127 case PanAzimuthAutomation:
128 case PanElevationAutomation:
129 case PanWidthAutomation:
130 case FadeInAutomation:
131 case FadeOutAutomation:
132 case EnvelopeAutomation:
141 AutomationList::operator= (const AutomationList& other)
143 if (this != &other) {
146 ControlList::operator= (other);
147 _state = other._state;
148 _style = other._style;
149 _touching = other._touching;
152 maybe_signal_changed ();
159 AutomationList::maybe_signal_changed ()
161 ControlList::maybe_signal_changed ();
163 if (!ControlList::frozen()) {
164 StateChanged (); /* EMIT SIGNAL */
169 AutomationList::set_automation_state (AutoState s)
173 automation_state_changed (s); /* EMIT SIGNAL */
178 AutomationList::set_automation_style (AutoStyle s)
182 automation_style_changed (); /* EMIT SIGNAL */
187 AutomationList::start_touch (double when)
189 if (_state == Touch) {
190 start_write_pass (when);
193 g_atomic_int_set (&_touching, 1);
197 AutomationList::stop_touch (bool mark, double)
199 if (g_atomic_int_get (&_touching) == 0) {
200 /* this touch has already been stopped (probably by Automatable::transport_stopped),
201 so we've nothing to do.
206 g_atomic_int_set (&_touching, 0);
208 if (_state == Touch) {
212 /* XXX need to mark the last added point with the
220 AutomationList::thaw ()
224 if (_changed_when_thawed) {
225 _changed_when_thawed = false;
226 StateChanged(); /* EMIT SIGNAL */
231 AutomationList::get_state ()
237 AutomationList::state (bool full)
239 XMLNode* root = new XMLNode (X_("AutomationList"));
241 LocaleGuard lg (X_("POSIX"));
243 root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
245 root->add_property ("id", id().to_s());
247 snprintf (buf, sizeof (buf), "%.12g", _default_value);
248 root->add_property ("default", buf);
249 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
250 root->add_property ("min-yval", buf);
251 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
252 root->add_property ("max-yval", buf);
254 root->add_property ("interpolation-style", enum_2_string (_interpolation));
257 /* never serialize state with Write enabled - too dangerous
260 if (_state != Write) {
261 root->add_property ("state", auto_state_to_string (_state));
263 root->add_property ("state", auto_state_to_string (Off));
266 /* never save anything but Off for automation state to a template */
267 root->add_property ("state", auto_state_to_string (Off));
270 root->add_property ("style", auto_style_to_string (_style));
272 if (!_events.empty()) {
273 root->add_child_nocopy (serialize_events());
280 AutomationList::serialize_events ()
282 XMLNode* node = new XMLNode (X_("events"));
285 str.precision(15); //10 digits is enough digits for 24 hours at 96kHz
287 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
288 str << (double) (*xx)->when;
290 str <<(double) (*xx)->value;
294 /* XML is a bit wierd */
296 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
297 content_node->set_content (str.str());
299 node->add_child_nocopy (*content_node);
305 AutomationList::deserialize_events (const XMLNode& node)
307 if (node.children().empty()) {
311 XMLNode* content_node = node.children().front();
313 if (content_node->content().empty()) {
317 ControlList::freeze ();
320 stringstream str (content_node->content());
336 fast_simple_add (x, y);
341 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
344 maybe_signal_changed ();
353 AutomationList::set_state (const XMLNode& node, int version)
355 LocaleGuard lg (X_("POSIX"));
356 XMLNodeList nlist = node.children();
358 XMLNodeIterator niter;
359 const XMLProperty* prop;
361 if (node.name() == X_("events")) {
362 /* partial state setting*/
363 return deserialize_events (node);
366 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
368 if ((nsos = node.child (X_("AutomationList")))) {
369 /* new school in old school clothing */
370 return set_state (*nsos, version);
375 const XMLNodeList& elist = node.children();
376 XMLNodeConstIterator i;
381 ControlList::freeze ();
384 for (i = elist.begin(); i != elist.end(); ++i) {
386 if ((prop = (*i)->property ("x")) == 0) {
387 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
390 x = atoi (prop->value().c_str());
392 if ((prop = (*i)->property ("y")) == 0) {
393 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
396 y = atof (prop->value().c_str());
398 fast_simple_add (x, y);
406 if (node.name() != X_("AutomationList") ) {
407 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
412 /* update session AL list */
413 AutomationListCreated(this);
416 if ((prop = node.property (X_("automation-id"))) != 0){
417 _parameter = EventTypeMap::instance().new_parameter(prop->value());
419 warning << "Legacy session: automation list has no automation-id property." << endmsg;
422 if ((prop = node.property (X_("interpolation-style"))) != 0) {
423 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
425 _interpolation = Linear;
428 if ((prop = node.property (X_("default"))) != 0){
429 _default_value = atof (prop->value().c_str());
431 _default_value = 0.0;
434 if ((prop = node.property (X_("style"))) != 0) {
435 _style = string_to_auto_style (prop->value());
440 if ((prop = node.property (X_("state"))) != 0) {
441 _state = string_to_auto_state (prop->value());
442 if (_state == Write) {
445 automation_state_changed(_state);
450 if ((prop = node.property (X_("min-yval"))) != 0) {
451 _min_yval = atof (prop->value ().c_str());
456 if ((prop = node.property (X_("max-yval"))) != 0) {
457 _max_yval = atof (prop->value ().c_str());
462 bool have_events = false;
464 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
465 if ((*niter)->name() == X_("events")) {
466 deserialize_events (*(*niter));
472 /* there was no Events child node; clear any current events */
476 maybe_signal_changed ();
484 AutomationList::operator!= (AutomationList const & other) const
487 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
488 _state != other._state ||
489 _style != other._style ||
490 _touching != other._touching
495 AutomationListProperty::clone () const
497 return new AutomationListProperty (
499 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
500 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))