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);
63 _interpolation = default_interpolation ();
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))
76 g_atomic_int_set (&_touching, 0);
77 _interpolation = default_interpolation ();
79 create_curve_if_necessary();
81 assert(_parameter.type() != NullAutomation);
82 AutomationListCreated(this);
85 AutomationList::AutomationList (const AutomationList& other)
87 , StatefulDestructible()
90 _state = other._state;
91 g_atomic_int_set (&_touching, other.touching());
93 create_curve_if_necessary();
95 assert(_parameter.type() != NullAutomation);
96 AutomationListCreated(this);
99 AutomationList::AutomationList (const AutomationList& other, double start, double end)
100 : ControlList(other, start, end)
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);
120 _interpolation = default_interpolation ();
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:
165 WritePassStarted.connect_same_thread (_writepass_connection, boost::bind (&AutomationList::snapshot_history, this, false));
169 AutomationList::operator= (const AutomationList& other)
171 if (this != &other) {
172 ControlList::freeze ();
173 /* ControlList::operator= calls copy_events() which calls
174 * mark_dirty() and maybe_signal_changed()
176 ControlList::operator= (other);
177 _state = other._state;
178 _touching = other._touching;
179 ControlList::thaw ();
186 AutomationList::maybe_signal_changed ()
188 ControlList::maybe_signal_changed ();
190 if (!ControlList::frozen()) {
191 StateChanged (); /* EMIT SIGNAL */
196 AutomationList::automation_state() const
198 Glib::Threads::RWLock::ReaderLock lm (Evoral::ControlList::_lock);
203 AutomationList::set_automation_state (AutoState s)
206 Glib::Threads::RWLock::ReaderLock lm (Evoral::ControlList::_lock);
212 if (s == Write && _desc.toggled) {
213 snapshot_history (true);
217 automation_state_changed (s); /* EMIT SIGNAL */
220 Evoral::ControlList::InterpolationStyle
221 AutomationList::default_interpolation () const
223 switch (_parameter.type()) {
226 case EnvelopeAutomation:
227 #ifndef XXX_NEW_INTERPOLATON__BREAK_SESSION_FORMAT_XXX
228 /* use old, wrong linear gain interpolation */
229 return ControlList::Linear;
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, DoubleBeatsFramesConverter 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_frame = 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 full, 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());
358 #ifndef XXX_NEW_INTERPOLATON__BREAK_SESSION_FORMAT_XXX
359 /* force new enums to existing ones in session-file */
360 Evoral::ControlList::InterpolationStyle is = _interpolation;
362 case ControlList::Exponential:
363 case ControlList::Logarithmic:
364 is = ControlList::Linear;
369 root->set_property ("interpolation-style", is);
371 root->set_property ("interpolation-style", _interpolation);
375 /* never serialize state with Write enabled - too dangerous
378 if (_state != Write) {
379 root->set_property ("state", _state);
381 if (_events.empty ()) {
382 root->set_property ("state", Off);
384 root->set_property ("state", Touch);
388 /* never save anything but Off for automation state to a template */
389 root->set_property ("state", Off);
392 if (!_events.empty()) {
393 root->add_child_nocopy (serialize_events (need_lock));
400 AutomationList::serialize_events (bool need_lock)
402 XMLNode* node = new XMLNode (X_("events"));
405 Glib::Threads::RWLock::ReaderLock lm (Evoral::ControlList::_lock, Glib::Threads::NOT_LOCK);
409 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
410 str << PBD::to_string ((*xx)->when);
412 str << PBD::to_string ((*xx)->value);
416 /* XML is a bit wierd */
418 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
419 content_node->set_content (str.str());
421 node->add_child_nocopy (*content_node);
427 AutomationList::deserialize_events (const XMLNode& node)
429 if (node.children().empty()) {
433 XMLNode* content_node = node.children().front();
435 if (content_node->content().empty()) {
439 ControlList::freeze ();
442 stringstream str (content_node->content());
452 if (!str || !PBD::string_to<double> (x_str, x)) {
456 if (!str || !PBD::string_to<double> (y_str, y)) {
460 y = std::min ((double)_desc.upper, std::max ((double)_desc.lower, y));
461 fast_simple_add (x, y);
466 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
469 maybe_signal_changed ();
478 AutomationList::set_state (const XMLNode& node, int version)
480 XMLNodeList nlist = node.children();
482 XMLNodeIterator niter;
484 if (node.name() == X_("events")) {
485 /* partial state setting*/
486 return deserialize_events (node);
489 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
491 if ((nsos = node.child (X_("AutomationList")))) {
492 /* new school in old school clothing */
493 return set_state (*nsos, version);
498 const XMLNodeList& elist = node.children();
499 XMLNodeConstIterator i;
501 ControlList::freeze ();
504 for (i = elist.begin(); i != elist.end(); ++i) {
507 if (!(*i)->get_property ("x", x)) {
508 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
513 if (!(*i)->get_property ("y", y)) {
514 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
518 y = std::min ((double)_desc.upper, std::max ((double)_desc.lower, y));
519 fast_simple_add (x, y);
527 if (node.name() != X_("AutomationList") ) {
528 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
533 /* update session AL list */
534 AutomationListCreated(this);
538 if (node.get_property (X_("automation-id"), value)) {
539 _parameter = EventTypeMap::instance().from_symbol(value);
541 warning << "Legacy session: automation list has no automation-id property." << endmsg;
544 if (!node.get_property (X_("interpolation-style"), _interpolation)) {
545 _interpolation = default_interpolation ();
547 #ifndef XXX_NEW_INTERPOLATON__BREAK_SESSION_FORMAT_XXX
548 /* internally force logarithmic and Trim params to use Log-scale */
549 if (_desc.logarithmic || _parameter.type() == TrimAutomation) {
550 _interpolation = ControlList::Logarithmic;
554 if (node.get_property (X_("state"), _state)) {
555 if (_state == Write) {
558 automation_state_changed (_state);
563 bool have_events = false;
565 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
566 if ((*niter)->name() == X_("events")) {
567 deserialize_events (*(*niter));
573 /* there was no Events child node; clear any current events */
577 maybe_signal_changed ();
585 AutomationList::operator!= (AutomationList const & other) const
588 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
589 _state != other._state ||
590 _touching != other._touching
595 AutomationListProperty::clone () const
597 return new AutomationListProperty (
599 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
600 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))