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 "ardour/evoral_types_convert.h"
30 #include "ardour/types_convert.h"
31 #include "evoral/Curve.hpp"
32 #include "pbd/memento_command.h"
33 #include "pbd/stacktrace.h"
34 #include "pbd/enumwriter.h"
35 #include "pbd/types_convert.h"
40 using namespace ARDOUR;
43 PBD::Signal1<void,AutomationList *> AutomationList::AutomationListCreated;
46 static void dumpit (const AutomationList& al, string prefix = "")
48 cerr << prefix << &al << endl;
49 for (AutomationList::const_iterator i = al.begin(); i != al.end(); ++i) {
50 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
55 AutomationList::AutomationList (const Evoral::Parameter& id, const Evoral::ParameterDescriptor& desc)
56 : ControlList(id, desc)
61 g_atomic_int_set (&_touching, 0);
63 create_curve_if_necessary();
65 assert(_parameter.type() != NullAutomation);
66 AutomationListCreated(this);
69 AutomationList::AutomationList (const Evoral::Parameter& id)
70 : ControlList(id, ARDOUR::ParameterDescriptor(id))
75 g_atomic_int_set (&_touching, 0);
77 create_curve_if_necessary();
79 assert(_parameter.type() != NullAutomation);
80 AutomationListCreated(this);
83 AutomationList::AutomationList (const AutomationList& other)
84 : StatefulDestructible()
88 _style = other._style;
89 _state = other._state;
90 g_atomic_int_set (&_touching, other.touching());
92 create_curve_if_necessary();
94 assert(_parameter.type() != NullAutomation);
95 AutomationListCreated(this);
98 AutomationList::AutomationList (const AutomationList& other, double start, double end)
99 : ControlList(other, start, end)
102 _style = other._style;
103 _state = other._state;
104 g_atomic_int_set (&_touching, other.touching());
106 create_curve_if_necessary();
108 assert(_parameter.type() != NullAutomation);
109 AutomationListCreated(this);
112 /** @param id is used for legacy sessions where the type is not present
113 * in or below the AutomationList node. It is used if @param id is non-null.
115 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
116 : ControlList(id, ARDOUR::ParameterDescriptor(id))
119 g_atomic_int_set (&_touching, 0);
123 set_state (node, Stateful::loading_state_version);
129 create_curve_if_necessary();
131 assert(_parameter.type() != NullAutomation);
132 AutomationListCreated(this);
135 AutomationList::~AutomationList()
140 boost::shared_ptr<Evoral::ControlList>
141 AutomationList::create(const Evoral::Parameter& id,
142 const Evoral::ParameterDescriptor& desc)
144 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id, desc));
148 AutomationList::create_curve_if_necessary()
150 switch (_parameter.type()) {
153 case PanAzimuthAutomation:
154 case PanElevationAutomation:
155 case PanWidthAutomation:
156 case FadeInAutomation:
157 case FadeOutAutomation:
158 case EnvelopeAutomation:
167 AutomationList::operator= (const AutomationList& other)
169 if (this != &other) {
172 ControlList::operator= (other);
173 _state = other._state;
174 _style = other._style;
175 _touching = other._touching;
178 maybe_signal_changed ();
185 AutomationList::maybe_signal_changed ()
187 ControlList::maybe_signal_changed ();
189 if (!ControlList::frozen()) {
190 StateChanged (); /* EMIT SIGNAL */
195 AutomationList::set_automation_state (AutoState s)
200 if (s == Write && _desc.toggled) {
201 _before = &get_state ();
205 automation_state_changed (s); /* EMIT SIGNAL */
210 AutomationList::set_automation_style (AutoStyle s)
214 automation_style_changed (); /* EMIT SIGNAL */
219 AutomationList::start_write_pass (double when)
222 if (in_new_write_pass ()) {
223 _before = &get_state ();
227 ControlList::start_write_pass (when);
231 AutomationList::write_pass_finished (double when, double thinning_factor)
233 ControlList::write_pass_finished (when, thinning_factor);
237 AutomationList::start_touch (double when)
239 if (_state == Touch) {
240 start_write_pass (when);
243 g_atomic_int_set (&_touching, 1);
247 AutomationList::stop_touch (bool mark, double)
249 if (g_atomic_int_get (&_touching) == 0) {
250 /* this touch has already been stopped (probably by Automatable::transport_stopped),
251 so we've nothing to do.
256 g_atomic_int_set (&_touching, 0);
258 if (_state == Touch) {
262 /* XXX need to mark the last added point with the
269 /* _before may be owned by the undo stack,
270 * so we have to be careful about doing this.
272 * ::before () transfers ownership, setting _before to 0
275 AutomationList::clear_history ()
282 AutomationList::thaw ()
286 if (_changed_when_thawed) {
287 _changed_when_thawed = false;
288 StateChanged(); /* EMIT SIGNAL */
293 AutomationList::memento_command (XMLNode* before, XMLNode* after)
295 return new MementoCommand<AutomationList> (*this, before, after);
299 AutomationList::get_state ()
305 AutomationList::state (bool full)
307 XMLNode* root = new XMLNode (X_("AutomationList"));
310 root->set_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
311 root->set_property ("id", id());
312 root->set_property ("default", _default_value);
313 root->set_property ("min-yval", _min_yval);
314 root->set_property ("max-yval", _max_yval);
315 root->set_property ("interpolation-style", _interpolation);
318 /* never serialize state with Write enabled - too dangerous
321 if (_state != Write) {
322 root->set_property ("state", _state);
324 if (_events.empty ()) {
325 root->set_property ("state", Off);
327 root->set_property ("state", Touch);
331 /* never save anything but Off for automation state to a template */
332 root->set_property ("state", Off);
335 root->set_property ("style", _style);
337 if (!_events.empty()) {
338 root->add_child_nocopy (serialize_events());
345 AutomationList::serialize_events ()
347 XMLNode* node = new XMLNode (X_("events"));
350 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
351 str << PBD::to_string ((*xx)->when);
353 str << PBD::to_string ((*xx)->value);
357 /* XML is a bit wierd */
359 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
360 content_node->set_content (str.str());
362 node->add_child_nocopy (*content_node);
368 AutomationList::deserialize_events (const XMLNode& node)
370 if (node.children().empty()) {
374 XMLNode* content_node = node.children().front();
376 if (content_node->content().empty()) {
380 ControlList::freeze ();
383 stringstream str (content_node->content());
393 if (!str || !PBD::string_to<double> (x_str, x)) {
397 if (!str || !PBD::string_to<double> (y_str, y)) {
401 fast_simple_add (x, y);
406 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
409 maybe_signal_changed ();
418 AutomationList::set_state (const XMLNode& node, int version)
421 XMLNodeList nlist = node.children();
423 XMLNodeIterator niter;
425 if (node.name() == X_("events")) {
426 /* partial state setting*/
427 return deserialize_events (node);
430 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
432 if ((nsos = node.child (X_("AutomationList")))) {
433 /* new school in old school clothing */
434 return set_state (*nsos, version);
439 const XMLNodeList& elist = node.children();
440 XMLNodeConstIterator i;
442 ControlList::freeze ();
445 for (i = elist.begin(); i != elist.end(); ++i) {
448 if (!(*i)->get_property ("x", x)) {
449 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
454 if (!(*i)->get_property ("y", y)) {
455 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
459 fast_simple_add (x, y);
467 if (node.name() != X_("AutomationList") ) {
468 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
473 /* update session AL list */
474 AutomationListCreated(this);
478 if (node.get_property (X_("automation-id"), value)) {
479 _parameter = EventTypeMap::instance().from_symbol(value);
481 warning << "Legacy session: automation list has no automation-id property." << endmsg;
484 if (!node.get_property (X_("interpolation-style"), _interpolation)) {
485 _interpolation = Linear;
488 if (!node.get_property (X_("default"), _default_value)) {
489 _default_value = 0.0;
492 if (!node.get_property (X_("style"), _style)) {
496 if (node.get_property (X_("state"), _state)) {
497 if (_state == Write) {
500 automation_state_changed (_state);
505 if (!node.get_property (X_("min-yval"), _min_yval)) {
509 if (!node.get_property (X_("max-yval"), _max_yval)) {
513 bool have_events = false;
515 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
516 if ((*niter)->name() == X_("events")) {
517 deserialize_events (*(*niter));
523 /* there was no Events child node; clear any current events */
527 maybe_signal_changed ();
535 AutomationList::operator!= (AutomationList const & other) const
538 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
539 _state != other._state ||
540 _style != other._style ||
541 _touching != other._touching
546 AutomationListProperty::clone () const
548 return new AutomationListProperty (
550 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
551 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))