2 * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2008-2014 David Robillard <d@drobilla.net>
4 * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2012-2017 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2015 Nick Mainsbridge <mainsbridge@gmail.com>
7 * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "ardour/automation_list.h"
31 #include "ardour/beats_samples_converter.h"
32 #include "ardour/event_type_map.h"
33 #include "ardour/parameter_descriptor.h"
34 #include "ardour/parameter_types.h"
35 #include "ardour/evoral_types_convert.h"
36 #include "ardour/types_convert.h"
37 #include "evoral/Curve.hpp"
38 #include "pbd/memento_command.h"
39 #include "pbd/stacktrace.h"
40 #include "pbd/enumwriter.h"
41 #include "pbd/types_convert.h"
46 using namespace ARDOUR;
49 PBD::Signal1<void,AutomationList *> AutomationList::AutomationListCreated;
52 static void dumpit (const AutomationList& al, string prefix = "")
54 cerr << prefix << &al << endl;
55 for (AutomationList::const_iterator i = al.begin(); i != al.end(); ++i) {
56 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
61 AutomationList::AutomationList (const Evoral::Parameter& id, const Evoral::ParameterDescriptor& desc)
62 : ControlList(id, desc)
66 g_atomic_int_set (&_touching, 0);
67 _interpolation = default_interpolation ();
69 create_curve_if_necessary();
71 assert(_parameter.type() != NullAutomation);
72 AutomationListCreated(this);
75 AutomationList::AutomationList (const Evoral::Parameter& id)
76 : ControlList(id, ARDOUR::ParameterDescriptor(id))
80 g_atomic_int_set (&_touching, 0);
81 _interpolation = default_interpolation ();
83 create_curve_if_necessary();
85 assert(_parameter.type() != NullAutomation);
86 AutomationListCreated(this);
89 AutomationList::AutomationList (const AutomationList& other)
91 , StatefulDestructible()
94 _state = other._state;
95 g_atomic_int_set (&_touching, other.touching());
97 create_curve_if_necessary();
99 assert(_parameter.type() != NullAutomation);
100 AutomationListCreated(this);
103 AutomationList::AutomationList (const AutomationList& other, double start, double end)
104 : ControlList(other, start, end)
107 _state = other._state;
108 g_atomic_int_set (&_touching, other.touching());
110 create_curve_if_necessary();
112 assert(_parameter.type() != NullAutomation);
113 AutomationListCreated(this);
116 /** @param id is used for legacy sessions where the type is not present
117 * in or below the AutomationList node. It is used if @param id is non-null.
119 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
120 : ControlList(id, ARDOUR::ParameterDescriptor(id))
123 g_atomic_int_set (&_touching, 0);
124 _interpolation = default_interpolation ();
127 set_state (node, Stateful::loading_state_version);
133 create_curve_if_necessary();
135 assert(_parameter.type() != NullAutomation);
136 AutomationListCreated(this);
139 AutomationList::~AutomationList()
144 boost::shared_ptr<Evoral::ControlList>
145 AutomationList::create(const Evoral::Parameter& id,
146 const Evoral::ParameterDescriptor& desc)
148 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id, desc));
152 AutomationList::create_curve_if_necessary()
154 switch (_parameter.type()) {
157 case PanAzimuthAutomation:
158 case PanElevationAutomation:
159 case PanWidthAutomation:
160 case FadeInAutomation:
161 case FadeOutAutomation:
162 case EnvelopeAutomation:
169 WritePassStarted.connect_same_thread (_writepass_connection, boost::bind (&AutomationList::snapshot_history, this, false));
173 AutomationList::operator= (const AutomationList& other)
175 if (this != &other) {
176 ControlList::freeze ();
177 /* ControlList::operator= calls copy_events() which calls
178 * mark_dirty() and maybe_signal_changed()
180 ControlList::operator= (other);
181 _state = other._state;
182 _touching = other._touching;
183 ControlList::thaw ();
190 AutomationList::maybe_signal_changed ()
192 ControlList::maybe_signal_changed ();
194 if (!ControlList::frozen()) {
195 StateChanged (); /* EMIT SIGNAL */
200 AutomationList::automation_state() const
202 Glib::Threads::RWLock::ReaderLock lm (Evoral::ControlList::_lock);
207 AutomationList::set_automation_state (AutoState s)
210 Glib::Threads::RWLock::ReaderLock lm (Evoral::ControlList::_lock);
216 if (s == Write && _desc.toggled) {
217 snapshot_history (true);
221 automation_state_changed (s); /* EMIT SIGNAL */
224 Evoral::ControlList::InterpolationStyle
225 AutomationList::default_interpolation () const
227 switch (_parameter.type()) {
230 case EnvelopeAutomation:
231 return ControlList::Exponential;
234 return ControlList::Logarithmic;
239 /* based on Evoral::ParameterDescriptor log,toggle,.. */
240 return ControlList::default_interpolation ();
244 AutomationList::start_write_pass (double when)
246 snapshot_history (true);
247 ControlList::start_write_pass (when);
251 AutomationList::write_pass_finished (double when, double thinning_factor)
253 ControlList::write_pass_finished (when, thinning_factor);
257 AutomationList::start_touch (double when)
259 if (_state == Touch) {
260 start_write_pass (when);
263 g_atomic_int_set (&_touching, 1);
267 AutomationList::stop_touch (double)
269 if (g_atomic_int_get (&_touching) == 0) {
270 /* this touch has already been stopped (probably by Automatable::transport_stopped),
271 so we've nothing to do.
276 g_atomic_int_set (&_touching, 0);
279 /* _before may be owned by the undo stack,
280 * so we have to be careful about doing this.
282 * ::before () transfers ownership, setting _before to 0
285 AutomationList::clear_history ()
292 AutomationList::snapshot_history (bool need_lock)
294 if (!in_new_write_pass ()) {
298 _before = &state (true, need_lock);
303 AutomationList::thaw ()
307 if (_changed_when_thawed) {
308 _changed_when_thawed = false;
309 StateChanged(); /* EMIT SIGNAL */
314 AutomationList::paste (const ControlList& alist, double pos, DoubleBeatsSamplesConverter const& bfc)
316 AutomationType src_type = (AutomationType)alist.parameter().type();
317 AutomationType dst_type = (AutomationType)_parameter.type();
319 if (parameter_is_midi (src_type) == parameter_is_midi (dst_type)) {
320 return ControlList::paste (alist, pos);
322 bool to_sample = parameter_is_midi (src_type);
324 ControlList cl (alist);
326 for (const_iterator i = alist.begin ();i != alist.end (); ++i) {
327 double when = (*i)->when;
329 when = bfc.to ((*i)->when);
331 when = bfc.from ((*i)->when);
333 cl.fast_simple_add (when, (*i)->value);
335 return ControlList::paste (cl, pos);
339 AutomationList::memento_command (XMLNode* before, XMLNode* after)
341 return new MementoCommand<AutomationList> (*this, before, after);
345 AutomationList::get_state ()
347 return state (true, true);
351 AutomationList::state (bool save_auto_state, bool need_lock)
353 XMLNode* root = new XMLNode (X_("AutomationList"));
355 root->set_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
356 root->set_property ("id", id());
357 root->set_property ("interpolation-style", _interpolation);
359 if (save_auto_state) {
360 /* never serialize state with Write enabled - too dangerous
363 if (_state != Write) {
364 root->set_property ("state", _state);
366 if (_events.empty ()) {
367 root->set_property ("state", Off);
369 root->set_property ("state", Touch);
373 /* never save anything but Off for automation state to a template */
374 root->set_property ("state", Off);
377 if (!_events.empty()) {
378 root->add_child_nocopy (serialize_events (need_lock));
385 AutomationList::serialize_events (bool need_lock)
387 XMLNode* node = new XMLNode (X_("events"));
390 Glib::Threads::RWLock::ReaderLock lm (Evoral::ControlList::_lock, Glib::Threads::NOT_LOCK);
394 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
395 str << PBD::to_string ((*xx)->when);
397 str << PBD::to_string ((*xx)->value);
401 /* XML is a bit wierd */
403 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
404 content_node->set_content (str.str());
406 node->add_child_nocopy (*content_node);
412 AutomationList::deserialize_events (const XMLNode& node)
414 if (node.children().empty()) {
418 XMLNode* content_node = node.children().front();
420 if (content_node->content().empty()) {
424 ControlList::freeze ();
427 stringstream str (content_node->content());
437 if (!str || !PBD::string_to<double> (x_str, x)) {
441 if (!str || !PBD::string_to<double> (y_str, y)) {
445 y = std::min ((double)_desc.upper, std::max ((double)_desc.lower, y));
446 fast_simple_add (x, y);
451 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
454 maybe_signal_changed ();
463 AutomationList::set_state (const XMLNode& node, int version)
465 XMLNodeList nlist = node.children();
467 XMLNodeIterator niter;
469 if (node.name() == X_("events")) {
470 /* partial state setting*/
471 return deserialize_events (node);
474 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
476 if ((nsos = node.child (X_("AutomationList")))) {
477 /* new school in old school clothing */
478 return set_state (*nsos, version);
483 const XMLNodeList& elist = node.children();
484 XMLNodeConstIterator i;
486 ControlList::freeze ();
489 for (i = elist.begin(); i != elist.end(); ++i) {
492 if (!(*i)->get_property ("x", x)) {
493 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
498 if (!(*i)->get_property ("y", y)) {
499 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
503 y = std::min ((double)_desc.upper, std::max ((double)_desc.lower, y));
504 fast_simple_add (x, y);
512 if (node.name() != X_("AutomationList") ) {
513 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
518 /* update session AL list */
519 AutomationListCreated(this);
523 if (node.get_property (X_("automation-id"), value)) {
524 _parameter = EventTypeMap::instance().from_symbol(value);
526 warning << "Legacy session: automation list has no automation-id property." << endmsg;
529 if (!node.get_property (X_("interpolation-style"), _interpolation)) {
530 _interpolation = default_interpolation ();
533 if (node.get_property (X_("state"), _state)) {
534 if (_state == Write) {
537 automation_state_changed (_state);
542 bool have_events = false;
544 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
545 if ((*niter)->name() == X_("events")) {
546 deserialize_events (*(*niter));
552 /* there was no Events child node; clear any current events */
556 maybe_signal_changed ();
564 AutomationList::operator!= (AutomationList const & other) const
567 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
568 _state != other._state ||
569 _touching != other._touching
574 AutomationListProperty::clone () const
576 return new AutomationListProperty (
578 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
579 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))