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/beats_frames_converter.h"
28 #include "ardour/event_type_map.h"
29 #include "ardour/parameter_descriptor.h"
30 #include "ardour/parameter_types.h"
31 #include "ardour/evoral_types_convert.h"
32 #include "ardour/types_convert.h"
33 #include "evoral/Curve.hpp"
34 #include "pbd/memento_command.h"
35 #include "pbd/stacktrace.h"
36 #include "pbd/enumwriter.h"
37 #include "pbd/types_convert.h"
42 using namespace ARDOUR;
45 PBD::Signal1<void,AutomationList *> AutomationList::AutomationListCreated;
48 static void dumpit (const AutomationList& al, string prefix = "")
50 cerr << prefix << &al << endl;
51 for (AutomationList::const_iterator i = al.begin(); i != al.end(); ++i) {
52 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
57 AutomationList::AutomationList (const Evoral::Parameter& id, const Evoral::ParameterDescriptor& desc)
58 : ControlList(id, desc)
62 g_atomic_int_set (&_touching, 0);
64 create_curve_if_necessary();
66 assert(_parameter.type() != NullAutomation);
67 AutomationListCreated(this);
70 AutomationList::AutomationList (const Evoral::Parameter& id)
71 : 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)
85 , StatefulDestructible()
88 _state = other._state;
89 g_atomic_int_set (&_touching, other.touching());
91 create_curve_if_necessary();
93 assert(_parameter.type() != NullAutomation);
94 AutomationListCreated(this);
97 AutomationList::AutomationList (const AutomationList& other, double start, double end)
98 : ControlList(other, start, end)
101 _state = other._state;
102 g_atomic_int_set (&_touching, other.touching());
104 create_curve_if_necessary();
106 assert(_parameter.type() != NullAutomation);
107 AutomationListCreated(this);
110 /** @param id is used for legacy sessions where the type is not present
111 * in or below the AutomationList node. It is used if @param id is non-null.
113 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
114 : ControlList(id, ARDOUR::ParameterDescriptor(id))
117 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()
137 boost::shared_ptr<Evoral::ControlList>
138 AutomationList::create(const Evoral::Parameter& id,
139 const Evoral::ParameterDescriptor& desc)
141 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id, desc));
145 AutomationList::create_curve_if_necessary()
147 switch (_parameter.type()) {
150 case PanAzimuthAutomation:
151 case PanElevationAutomation:
152 case PanWidthAutomation:
153 case FadeInAutomation:
154 case FadeOutAutomation:
155 case EnvelopeAutomation:
164 AutomationList::operator= (const AutomationList& other)
166 if (this != &other) {
169 ControlList::operator= (other);
170 _state = other._state;
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)
196 if (s == Write && _desc.toggled) {
197 _before = &get_state ();
201 automation_state_changed (s); /* EMIT SIGNAL */
206 AutomationList::start_write_pass (double when)
209 if (in_new_write_pass ()) {
210 _before = &get_state ();
214 ControlList::start_write_pass (when);
218 AutomationList::write_pass_finished (double when, double thinning_factor)
220 ControlList::write_pass_finished (when, thinning_factor);
224 AutomationList::start_touch (double when)
226 if (_state == Touch) {
227 start_write_pass (when);
230 g_atomic_int_set (&_touching, 1);
234 AutomationList::stop_touch (bool mark, double)
236 if (g_atomic_int_get (&_touching) == 0) {
237 /* this touch has already been stopped (probably by Automatable::transport_stopped),
238 so we've nothing to do.
243 g_atomic_int_set (&_touching, 0);
245 if (_state == Touch) {
249 /* XXX need to mark the last added point with the
256 /* _before may be owned by the undo stack,
257 * so we have to be careful about doing this.
259 * ::before () transfers ownership, setting _before to 0
262 AutomationList::clear_history ()
269 AutomationList::thaw ()
273 if (_changed_when_thawed) {
274 _changed_when_thawed = false;
275 StateChanged(); /* EMIT SIGNAL */
280 AutomationList::paste (const ControlList& alist, double pos, DoubleBeatsFramesConverter const& bfc)
282 AutomationType src_type = (AutomationType)alist.parameter().type();
283 AutomationType dst_type = (AutomationType)_parameter.type();
285 if (parameter_is_midi (src_type) == parameter_is_midi (dst_type)) {
286 return ControlList::paste (alist, pos);
288 bool to_frame = parameter_is_midi (src_type);
290 ControlList cl (alist);
292 for (const_iterator i = alist.begin ();i != alist.end (); ++i) {
293 double when = (*i)->when;
295 when = bfc.to ((*i)->when);
297 when = bfc.from ((*i)->when);
299 cl.fast_simple_add (when, (*i)->value);
301 return ControlList::paste (cl, pos);
305 AutomationList::memento_command (XMLNode* before, XMLNode* after)
307 return new MementoCommand<AutomationList> (*this, before, after);
311 AutomationList::get_state ()
317 AutomationList::state (bool full)
319 XMLNode* root = new XMLNode (X_("AutomationList"));
322 root->set_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
323 root->set_property ("id", id());
324 root->set_property ("default", _default_value);
325 root->set_property ("min-yval", _min_yval);
326 root->set_property ("max-yval", _max_yval);
327 root->set_property ("interpolation-style", _interpolation);
330 /* never serialize state with Write enabled - too dangerous
333 if (_state != Write) {
334 root->set_property ("state", _state);
336 if (_events.empty ()) {
337 root->set_property ("state", Off);
339 root->set_property ("state", Touch);
343 /* never save anything but Off for automation state to a template */
344 root->set_property ("state", Off);
347 if (!_events.empty()) {
348 root->add_child_nocopy (serialize_events());
355 AutomationList::serialize_events ()
357 XMLNode* node = new XMLNode (X_("events"));
360 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
361 str << PBD::to_string ((*xx)->when);
363 str << PBD::to_string ((*xx)->value);
367 /* XML is a bit wierd */
369 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
370 content_node->set_content (str.str());
372 node->add_child_nocopy (*content_node);
378 AutomationList::deserialize_events (const XMLNode& node)
380 if (node.children().empty()) {
384 XMLNode* content_node = node.children().front();
386 if (content_node->content().empty()) {
390 ControlList::freeze ();
393 stringstream str (content_node->content());
403 if (!str || !PBD::string_to<double> (x_str, x)) {
407 if (!str || !PBD::string_to<double> (y_str, y)) {
411 fast_simple_add (x, y);
416 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
419 maybe_signal_changed ();
428 AutomationList::set_state (const XMLNode& node, int version)
431 XMLNodeList nlist = node.children();
433 XMLNodeIterator niter;
435 if (node.name() == X_("events")) {
436 /* partial state setting*/
437 return deserialize_events (node);
440 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
442 if ((nsos = node.child (X_("AutomationList")))) {
443 /* new school in old school clothing */
444 return set_state (*nsos, version);
449 const XMLNodeList& elist = node.children();
450 XMLNodeConstIterator i;
452 ControlList::freeze ();
455 for (i = elist.begin(); i != elist.end(); ++i) {
458 if (!(*i)->get_property ("x", x)) {
459 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
464 if (!(*i)->get_property ("y", y)) {
465 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
469 fast_simple_add (x, y);
477 if (node.name() != X_("AutomationList") ) {
478 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
483 /* update session AL list */
484 AutomationListCreated(this);
488 if (node.get_property (X_("automation-id"), value)) {
489 _parameter = EventTypeMap::instance().from_symbol(value);
491 warning << "Legacy session: automation list has no automation-id property." << endmsg;
494 if (!node.get_property (X_("interpolation-style"), _interpolation)) {
495 _interpolation = Linear;
498 if (!node.get_property (X_("default"), _default_value)) {
499 _default_value = 0.0;
502 if (node.get_property (X_("state"), _state)) {
503 if (_state == Write) {
506 automation_state_changed (_state);
511 if (!node.get_property (X_("min-yval"), _min_yval)) {
515 if (!node.get_property (X_("max-yval"), _max_yval)) {
519 bool have_events = false;
521 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
522 if ((*niter)->name() == X_("events")) {
523 deserialize_events (*(*niter));
529 /* there was no Events child node; clear any current events */
533 maybe_signal_changed ();
541 AutomationList::operator!= (AutomationList const & other) const
544 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
545 _state != other._state ||
546 _touching != other._touching
551 AutomationListProperty::clone () const
553 return new AutomationListProperty (
555 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
556 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))