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)
63 g_atomic_int_set (&_touching, 0);
65 create_curve_if_necessary();
67 assert(_parameter.type() != NullAutomation);
68 AutomationListCreated(this);
71 AutomationList::AutomationList (const Evoral::Parameter& id)
72 : ControlList(id, ARDOUR::ParameterDescriptor(id))
77 g_atomic_int_set (&_touching, 0);
79 create_curve_if_necessary();
81 assert(_parameter.type() != NullAutomation);
82 AutomationListCreated(this);
85 AutomationList::AutomationList (const AutomationList& other)
87 , StatefulDestructible()
90 _style = other._style;
91 _state = other._state;
92 g_atomic_int_set (&_touching, other.touching());
94 create_curve_if_necessary();
96 assert(_parameter.type() != NullAutomation);
97 AutomationListCreated(this);
100 AutomationList::AutomationList (const AutomationList& other, double start, double end)
101 : ControlList(other, start, end)
104 _style = other._style;
105 _state = other._state;
106 g_atomic_int_set (&_touching, other.touching());
108 create_curve_if_necessary();
110 assert(_parameter.type() != NullAutomation);
111 AutomationListCreated(this);
114 /** @param id is used for legacy sessions where the type is not present
115 * in or below the AutomationList node. It is used if @param id is non-null.
117 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
118 : ControlList(id, ARDOUR::ParameterDescriptor(id))
121 g_atomic_int_set (&_touching, 0);
125 set_state (node, Stateful::loading_state_version);
131 create_curve_if_necessary();
133 assert(_parameter.type() != NullAutomation);
134 AutomationListCreated(this);
137 AutomationList::~AutomationList()
142 boost::shared_ptr<Evoral::ControlList>
143 AutomationList::create(const Evoral::Parameter& id,
144 const Evoral::ParameterDescriptor& desc)
146 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id, desc));
150 AutomationList::create_curve_if_necessary()
152 switch (_parameter.type()) {
155 case PanAzimuthAutomation:
156 case PanElevationAutomation:
157 case PanWidthAutomation:
158 case FadeInAutomation:
159 case FadeOutAutomation:
160 case EnvelopeAutomation:
169 AutomationList::operator= (const AutomationList& other)
171 if (this != &other) {
174 ControlList::operator= (other);
175 _state = other._state;
176 _style = other._style;
177 _touching = other._touching;
180 maybe_signal_changed ();
187 AutomationList::maybe_signal_changed ()
189 ControlList::maybe_signal_changed ();
191 if (!ControlList::frozen()) {
192 StateChanged (); /* EMIT SIGNAL */
197 AutomationList::set_automation_state (AutoState s)
202 if (s == Write && _desc.toggled) {
203 _before = &get_state ();
207 automation_state_changed (s); /* EMIT SIGNAL */
212 AutomationList::set_automation_style (AutoStyle s)
216 automation_style_changed (); /* EMIT SIGNAL */
221 AutomationList::start_write_pass (double when)
224 if (in_new_write_pass ()) {
225 _before = &get_state ();
229 ControlList::start_write_pass (when);
233 AutomationList::write_pass_finished (double when, double thinning_factor)
235 ControlList::write_pass_finished (when, thinning_factor);
239 AutomationList::start_touch (double when)
241 if (_state == Touch) {
242 start_write_pass (when);
245 g_atomic_int_set (&_touching, 1);
249 AutomationList::stop_touch (bool mark, double)
251 if (g_atomic_int_get (&_touching) == 0) {
252 /* this touch has already been stopped (probably by Automatable::transport_stopped),
253 so we've nothing to do.
258 g_atomic_int_set (&_touching, 0);
260 if (_state == Touch) {
264 /* XXX need to mark the last added point with the
271 /* _before may be owned by the undo stack,
272 * so we have to be careful about doing this.
274 * ::before () transfers ownership, setting _before to 0
277 AutomationList::clear_history ()
284 AutomationList::thaw ()
288 if (_changed_when_thawed) {
289 _changed_when_thawed = false;
290 StateChanged(); /* EMIT SIGNAL */
295 AutomationList::paste (const ControlList& alist, double pos, DoubleBeatsFramesConverter const& bfc)
297 AutomationType src_type = (AutomationType)alist.parameter().type();
298 AutomationType dst_type = (AutomationType)_parameter.type();
300 if (parameter_is_midi (src_type) == parameter_is_midi (dst_type)) {
301 return ControlList::paste (alist, pos);
303 bool to_frame = parameter_is_midi (src_type);
305 ControlList cl (alist);
307 for (const_iterator i = alist.begin ();i != alist.end (); ++i) {
308 double when = (*i)->when;
310 when = bfc.to ((*i)->when);
312 when = bfc.from ((*i)->when);
314 cl.fast_simple_add (when, (*i)->value);
316 return ControlList::paste (cl, pos);
320 AutomationList::memento_command (XMLNode* before, XMLNode* after)
322 return new MementoCommand<AutomationList> (*this, before, after);
326 AutomationList::get_state ()
332 AutomationList::state (bool full)
334 XMLNode* root = new XMLNode (X_("AutomationList"));
337 root->set_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
338 root->set_property ("id", id());
339 root->set_property ("default", _default_value);
340 root->set_property ("min-yval", _min_yval);
341 root->set_property ("max-yval", _max_yval);
342 root->set_property ("interpolation-style", _interpolation);
345 /* never serialize state with Write enabled - too dangerous
348 if (_state != Write) {
349 root->set_property ("state", _state);
351 if (_events.empty ()) {
352 root->set_property ("state", Off);
354 root->set_property ("state", Touch);
358 /* never save anything but Off for automation state to a template */
359 root->set_property ("state", Off);
362 root->set_property ("style", _style);
364 if (!_events.empty()) {
365 root->add_child_nocopy (serialize_events());
372 AutomationList::serialize_events ()
374 XMLNode* node = new XMLNode (X_("events"));
377 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
378 str << PBD::to_string ((*xx)->when);
380 str << PBD::to_string ((*xx)->value);
384 /* XML is a bit wierd */
386 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
387 content_node->set_content (str.str());
389 node->add_child_nocopy (*content_node);
395 AutomationList::deserialize_events (const XMLNode& node)
397 if (node.children().empty()) {
401 XMLNode* content_node = node.children().front();
403 if (content_node->content().empty()) {
407 ControlList::freeze ();
410 stringstream str (content_node->content());
420 if (!str || !PBD::string_to<double> (x_str, x)) {
424 if (!str || !PBD::string_to<double> (y_str, y)) {
428 fast_simple_add (x, y);
433 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
436 maybe_signal_changed ();
445 AutomationList::set_state (const XMLNode& node, int version)
448 XMLNodeList nlist = node.children();
450 XMLNodeIterator niter;
452 if (node.name() == X_("events")) {
453 /* partial state setting*/
454 return deserialize_events (node);
457 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
459 if ((nsos = node.child (X_("AutomationList")))) {
460 /* new school in old school clothing */
461 return set_state (*nsos, version);
466 const XMLNodeList& elist = node.children();
467 XMLNodeConstIterator i;
469 ControlList::freeze ();
472 for (i = elist.begin(); i != elist.end(); ++i) {
475 if (!(*i)->get_property ("x", x)) {
476 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
481 if (!(*i)->get_property ("y", y)) {
482 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
486 fast_simple_add (x, y);
494 if (node.name() != X_("AutomationList") ) {
495 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
500 /* update session AL list */
501 AutomationListCreated(this);
505 if (node.get_property (X_("automation-id"), value)) {
506 _parameter = EventTypeMap::instance().from_symbol(value);
508 warning << "Legacy session: automation list has no automation-id property." << endmsg;
511 if (!node.get_property (X_("interpolation-style"), _interpolation)) {
512 _interpolation = Linear;
515 if (!node.get_property (X_("default"), _default_value)) {
516 _default_value = 0.0;
519 if (!node.get_property (X_("style"), _style)) {
523 if (node.get_property (X_("state"), _state)) {
524 if (_state == Write) {
527 automation_state_changed (_state);
532 if (!node.get_property (X_("min-yval"), _min_yval)) {
536 if (!node.get_property (X_("max-yval"), _max_yval)) {
540 bool have_events = false;
542 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
543 if ((*niter)->name() == X_("events")) {
544 deserialize_events (*(*niter));
550 /* there was no Events child node; clear any current events */
554 maybe_signal_changed ();
562 AutomationList::operator!= (AutomationList const & other) const
565 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
566 _state != other._state ||
567 _style != other._style ||
568 _touching != other._touching
573 AutomationListProperty::clone () const
575 return new AutomationListProperty (
577 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
578 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))