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);
227 AutomationList::start_touch (double when)
229 if (_state == Touch) {
230 start_write_pass (when);
233 g_atomic_int_set (&_touching, 1);
237 AutomationList::stop_touch (bool mark, double)
239 if (g_atomic_int_get (&_touching) == 0) {
240 /* this touch has already been stopped (probably by Automatable::transport_stopped),
241 so we've nothing to do.
246 g_atomic_int_set (&_touching, 0);
248 if (_state == Touch) {
252 /* XXX need to mark the last added point with the
260 AutomationList::thaw ()
264 if (_changed_when_thawed) {
265 _changed_when_thawed = false;
266 StateChanged(); /* EMIT SIGNAL */
271 AutomationList::get_state ()
277 AutomationList::state (bool full)
279 XMLNode* root = new XMLNode (X_("AutomationList"));
281 LocaleGuard lg (X_("C"));
283 root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
285 root->add_property ("id", id().to_s());
287 snprintf (buf, sizeof (buf), "%.12g", _default_value);
288 root->add_property ("default", buf);
289 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
290 root->add_property ("min-yval", buf);
291 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
292 root->add_property ("max-yval", buf);
294 root->add_property ("interpolation-style", enum_2_string (_interpolation));
297 /* never serialize state with Write enabled - too dangerous
300 if (_state != Write) {
301 root->add_property ("state", auto_state_to_string (_state));
303 if (_events.empty ()) {
304 root->add_property ("state", auto_state_to_string (Off));
306 root->add_property ("state", auto_state_to_string (Touch));
310 /* never save anything but Off for automation state to a template */
311 root->add_property ("state", auto_state_to_string (Off));
314 root->add_property ("style", auto_style_to_string (_style));
316 if (!_events.empty()) {
317 root->add_child_nocopy (serialize_events());
324 AutomationList::serialize_events ()
326 XMLNode* node = new XMLNode (X_("events"));
329 str.precision(15); //10 digits is enough digits for 24 hours at 96kHz
331 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
332 str << (double) (*xx)->when;
334 str <<(double) (*xx)->value;
338 /* XML is a bit wierd */
340 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
341 content_node->set_content (str.str());
343 node->add_child_nocopy (*content_node);
349 AutomationList::deserialize_events (const XMLNode& node)
351 if (node.children().empty()) {
355 XMLNode* content_node = node.children().front();
357 if (content_node->content().empty()) {
361 ControlList::freeze ();
364 stringstream str (content_node->content());
380 fast_simple_add (x, y);
385 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
388 maybe_signal_changed ();
397 AutomationList::set_state (const XMLNode& node, int version)
399 LocaleGuard lg (X_("C"));
400 XMLNodeList nlist = node.children();
402 XMLNodeIterator niter;
403 const XMLProperty* prop;
405 if (node.name() == X_("events")) {
406 /* partial state setting*/
407 return deserialize_events (node);
410 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
412 if ((nsos = node.child (X_("AutomationList")))) {
413 /* new school in old school clothing */
414 return set_state (*nsos, version);
419 const XMLNodeList& elist = node.children();
420 XMLNodeConstIterator i;
425 ControlList::freeze ();
428 for (i = elist.begin(); i != elist.end(); ++i) {
430 if ((prop = (*i)->property ("x")) == 0) {
431 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
434 x = atoi (prop->value().c_str());
436 if ((prop = (*i)->property ("y")) == 0) {
437 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
440 y = atof (prop->value().c_str());
442 fast_simple_add (x, y);
450 if (node.name() != X_("AutomationList") ) {
451 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
456 /* update session AL list */
457 AutomationListCreated(this);
460 if ((prop = node.property (X_("automation-id"))) != 0){
461 _parameter = EventTypeMap::instance().from_symbol(prop->value());
463 warning << "Legacy session: automation list has no automation-id property." << endmsg;
466 if ((prop = node.property (X_("interpolation-style"))) != 0) {
467 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
469 _interpolation = Linear;
472 if ((prop = node.property (X_("default"))) != 0){
473 _default_value = atof (prop->value().c_str());
475 _default_value = 0.0;
478 if ((prop = node.property (X_("style"))) != 0) {
479 _style = string_to_auto_style (prop->value());
484 if ((prop = node.property (X_("state"))) != 0) {
485 _state = string_to_auto_state (prop->value());
486 if (_state == Write) {
489 automation_state_changed(_state);
494 if ((prop = node.property (X_("min-yval"))) != 0) {
495 _min_yval = atof (prop->value ().c_str());
500 if ((prop = node.property (X_("max-yval"))) != 0) {
501 _max_yval = atof (prop->value ().c_str());
506 bool have_events = false;
508 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
509 if ((*niter)->name() == X_("events")) {
510 deserialize_events (*(*niter));
516 /* there was no Events child node; clear any current events */
520 maybe_signal_changed ();
528 AutomationList::operator!= (AutomationList const & other) const
531 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
532 _state != other._state ||
533 _style != other._style ||
534 _touching != other._touching
539 AutomationListProperty::clone () const
541 return new AutomationListProperty (
543 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
544 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))