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)
56 g_atomic_int_set (&_touching, 0);
58 create_curve_if_necessary();
60 assert(_parameter.type() != NullAutomation);
61 AutomationListCreated(this);
64 AutomationList::AutomationList (const Evoral::Parameter& id)
65 : ControlList(id, ARDOUR::ParameterDescriptor(id))
69 g_atomic_int_set (&_touching, 0);
71 create_curve_if_necessary();
73 assert(_parameter.type() != NullAutomation);
74 AutomationListCreated(this);
77 AutomationList::AutomationList (const AutomationList& other)
78 : StatefulDestructible()
81 _style = other._style;
82 _state = other._state;
83 g_atomic_int_set (&_touching, other.touching());
85 create_curve_if_necessary();
87 assert(_parameter.type() != NullAutomation);
88 AutomationListCreated(this);
91 AutomationList::AutomationList (const AutomationList& other, double start, double end)
92 : ControlList(other, start, end)
94 _style = other._style;
95 _state = other._state;
96 g_atomic_int_set (&_touching, other.touching());
98 create_curve_if_necessary();
100 assert(_parameter.type() != NullAutomation);
101 AutomationListCreated(this);
104 /** @param id is used for legacy sessions where the type is not present
105 * in or below the AutomationList node. It is used if @param id is non-null.
107 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
108 : ControlList(id, ARDOUR::ParameterDescriptor(id))
110 g_atomic_int_set (&_touching, 0);
114 set_state (node, Stateful::loading_state_version);
120 create_curve_if_necessary();
122 assert(_parameter.type() != NullAutomation);
123 AutomationListCreated(this);
126 AutomationList::~AutomationList()
130 boost::shared_ptr<Evoral::ControlList>
131 AutomationList::create(const Evoral::Parameter& id,
132 const Evoral::ParameterDescriptor& desc)
134 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id, desc));
138 AutomationList::create_curve_if_necessary()
140 switch (_parameter.type()) {
143 case PanAzimuthAutomation:
144 case PanElevationAutomation:
145 case PanWidthAutomation:
146 case FadeInAutomation:
147 case FadeOutAutomation:
148 case EnvelopeAutomation:
157 AutomationList::operator= (const AutomationList& other)
159 if (this != &other) {
162 ControlList::operator= (other);
163 _state = other._state;
164 _style = other._style;
165 _touching = other._touching;
168 maybe_signal_changed ();
175 AutomationList::maybe_signal_changed ()
177 ControlList::maybe_signal_changed ();
179 if (!ControlList::frozen()) {
180 StateChanged (); /* EMIT SIGNAL */
185 AutomationList::set_automation_state (AutoState s)
189 automation_state_changed (s); /* EMIT SIGNAL */
194 AutomationList::set_automation_style (AutoStyle s)
198 automation_style_changed (); /* EMIT SIGNAL */
203 AutomationList::start_touch (double when)
205 if (_state == Touch) {
206 start_write_pass (when);
209 g_atomic_int_set (&_touching, 1);
213 AutomationList::stop_touch (bool mark, double)
215 if (g_atomic_int_get (&_touching) == 0) {
216 /* this touch has already been stopped (probably by Automatable::transport_stopped),
217 so we've nothing to do.
222 g_atomic_int_set (&_touching, 0);
224 if (_state == Touch) {
228 /* XXX need to mark the last added point with the
236 AutomationList::thaw ()
240 if (_changed_when_thawed) {
241 _changed_when_thawed = false;
242 StateChanged(); /* EMIT SIGNAL */
247 AutomationList::get_state ()
253 AutomationList::state (bool full)
255 XMLNode* root = new XMLNode (X_("AutomationList"));
257 LocaleGuard lg (X_("C"));
259 root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
261 root->add_property ("id", id().to_s());
263 snprintf (buf, sizeof (buf), "%.12g", _default_value);
264 root->add_property ("default", buf);
265 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
266 root->add_property ("min-yval", buf);
267 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
268 root->add_property ("max-yval", buf);
270 root->add_property ("interpolation-style", enum_2_string (_interpolation));
273 /* never serialize state with Write enabled - too dangerous
276 if (_state != Write) {
277 root->add_property ("state", auto_state_to_string (_state));
279 root->add_property ("state", auto_state_to_string (Off));
282 /* never save anything but Off for automation state to a template */
283 root->add_property ("state", auto_state_to_string (Off));
286 root->add_property ("style", auto_style_to_string (_style));
288 if (!_events.empty()) {
289 root->add_child_nocopy (serialize_events());
296 AutomationList::serialize_events ()
298 XMLNode* node = new XMLNode (X_("events"));
301 str.precision(15); //10 digits is enough digits for 24 hours at 96kHz
303 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
304 str << (double) (*xx)->when;
306 str <<(double) (*xx)->value;
310 /* XML is a bit wierd */
312 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
313 content_node->set_content (str.str());
315 node->add_child_nocopy (*content_node);
321 AutomationList::deserialize_events (const XMLNode& node)
323 if (node.children().empty()) {
327 XMLNode* content_node = node.children().front();
329 if (content_node->content().empty()) {
333 ControlList::freeze ();
336 stringstream str (content_node->content());
352 fast_simple_add (x, y);
357 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
360 maybe_signal_changed ();
369 AutomationList::set_state (const XMLNode& node, int version)
371 LocaleGuard lg (X_("C"));
372 XMLNodeList nlist = node.children();
374 XMLNodeIterator niter;
375 const XMLProperty* prop;
377 if (node.name() == X_("events")) {
378 /* partial state setting*/
379 return deserialize_events (node);
382 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
384 if ((nsos = node.child (X_("AutomationList")))) {
385 /* new school in old school clothing */
386 return set_state (*nsos, version);
391 const XMLNodeList& elist = node.children();
392 XMLNodeConstIterator i;
397 ControlList::freeze ();
400 for (i = elist.begin(); i != elist.end(); ++i) {
402 if ((prop = (*i)->property ("x")) == 0) {
403 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
406 x = atoi (prop->value().c_str());
408 if ((prop = (*i)->property ("y")) == 0) {
409 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
412 y = atof (prop->value().c_str());
414 fast_simple_add (x, y);
422 if (node.name() != X_("AutomationList") ) {
423 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
428 /* update session AL list */
429 AutomationListCreated(this);
432 if ((prop = node.property (X_("automation-id"))) != 0){
433 _parameter = EventTypeMap::instance().from_symbol(prop->value());
435 warning << "Legacy session: automation list has no automation-id property." << endmsg;
438 if ((prop = node.property (X_("interpolation-style"))) != 0) {
439 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
441 _interpolation = Linear;
444 if ((prop = node.property (X_("default"))) != 0){
445 _default_value = atof (prop->value().c_str());
447 _default_value = 0.0;
450 if ((prop = node.property (X_("style"))) != 0) {
451 _style = string_to_auto_style (prop->value());
456 if ((prop = node.property (X_("state"))) != 0) {
457 _state = string_to_auto_state (prop->value());
458 if (_state == Write) {
461 automation_state_changed(_state);
466 if ((prop = node.property (X_("min-yval"))) != 0) {
467 _min_yval = atof (prop->value ().c_str());
472 if ((prop = node.property (X_("max-yval"))) != 0) {
473 _max_yval = atof (prop->value ().c_str());
478 bool have_events = false;
480 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
481 if ((*niter)->name() == X_("events")) {
482 deserialize_events (*(*niter));
488 /* there was no Events child node; clear any current events */
492 maybe_signal_changed ();
500 AutomationList::operator!= (AutomationList const & other) const
503 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
504 _state != other._state ||
505 _style != other._style ||
506 _touching != other._touching
511 AutomationListProperty::clone () const
513 return new AutomationListProperty (
515 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
516 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))