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 ("interpolation-style", _interpolation);
327 /* never serialize state with Write enabled - too dangerous
330 if (_state != Write) {
331 root->set_property ("state", _state);
333 if (_events.empty ()) {
334 root->set_property ("state", Off);
336 root->set_property ("state", Touch);
340 /* never save anything but Off for automation state to a template */
341 root->set_property ("state", Off);
344 if (!_events.empty()) {
345 root->add_child_nocopy (serialize_events());
352 AutomationList::serialize_events ()
354 XMLNode* node = new XMLNode (X_("events"));
357 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
358 str << PBD::to_string ((*xx)->when);
360 str << PBD::to_string ((*xx)->value);
364 /* XML is a bit wierd */
366 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
367 content_node->set_content (str.str());
369 node->add_child_nocopy (*content_node);
375 AutomationList::deserialize_events (const XMLNode& node)
377 if (node.children().empty()) {
381 XMLNode* content_node = node.children().front();
383 if (content_node->content().empty()) {
387 ControlList::freeze ();
390 stringstream str (content_node->content());
400 if (!str || !PBD::string_to<double> (x_str, x)) {
404 if (!str || !PBD::string_to<double> (y_str, y)) {
408 fast_simple_add (x, y);
413 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
416 maybe_signal_changed ();
425 AutomationList::set_state (const XMLNode& node, int version)
428 XMLNodeList nlist = node.children();
430 XMLNodeIterator niter;
432 if (node.name() == X_("events")) {
433 /* partial state setting*/
434 return deserialize_events (node);
437 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
439 if ((nsos = node.child (X_("AutomationList")))) {
440 /* new school in old school clothing */
441 return set_state (*nsos, version);
446 const XMLNodeList& elist = node.children();
447 XMLNodeConstIterator i;
449 ControlList::freeze ();
452 for (i = elist.begin(); i != elist.end(); ++i) {
455 if (!(*i)->get_property ("x", x)) {
456 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
461 if (!(*i)->get_property ("y", y)) {
462 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
466 fast_simple_add (x, y);
474 if (node.name() != X_("AutomationList") ) {
475 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
480 /* update session AL list */
481 AutomationListCreated(this);
485 if (node.get_property (X_("automation-id"), value)) {
486 _parameter = EventTypeMap::instance().from_symbol(value);
488 warning << "Legacy session: automation list has no automation-id property." << endmsg;
491 if (!node.get_property (X_("interpolation-style"), _interpolation)) {
492 _interpolation = Linear;
495 if (node.get_property (X_("state"), _state)) {
496 if (_state == Write) {
499 automation_state_changed (_state);
504 bool have_events = false;
506 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
507 if ((*niter)->name() == X_("events")) {
508 deserialize_events (*(*niter));
514 /* there was no Events child node; clear any current events */
518 maybe_signal_changed ();
526 AutomationList::operator!= (AutomationList const & other) const
529 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
530 _state != other._state ||
531 _touching != other._touching
536 AutomationListProperty::clone () const
538 return new AutomationListProperty (
540 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
541 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))