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/event_type_map.h"
28 #include "evoral/Curve.hpp"
29 #include "pbd/stacktrace.h"
30 #include "pbd/enumwriter.h"
35 using namespace ARDOUR;
38 PBD::Signal1<void,AutomationList *> AutomationList::AutomationListCreated;
41 static void dumpit (const AutomationList& al, string prefix = "")
43 cerr << prefix << &al << endl;
44 for (AutomationList::const_iterator i = al.begin(); i != al.end(); ++i) {
45 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
50 AutomationList::AutomationList (Evoral::Parameter id)
55 g_atomic_int_set (&_touching, 0);
57 create_curve_if_necessary();
59 assert(_parameter.type() != NullAutomation);
60 AutomationListCreated(this);
63 AutomationList::AutomationList (const AutomationList& other)
64 : StatefulDestructible()
67 _style = other._style;
68 _state = other._state;
69 g_atomic_int_set (&_touching, other.touching());
71 create_curve_if_necessary();
73 assert(_parameter.type() != NullAutomation);
74 AutomationListCreated(this);
77 AutomationList::AutomationList (const AutomationList& other, double start, double end)
78 : ControlList(other, start, end)
80 _style = other._style;
81 _state = other._state;
82 g_atomic_int_set (&_touching, other.touching());
84 create_curve_if_necessary();
86 assert(_parameter.type() != NullAutomation);
87 AutomationListCreated(this);
90 /** @param id is used for legacy sessions where the type is not present
91 * in or below the AutomationList node. It is used if @param id is non-null.
93 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
96 g_atomic_int_set (&_touching, 0);
100 set_state (node, Stateful::loading_state_version);
106 create_curve_if_necessary();
108 assert(_parameter.type() != NullAutomation);
109 AutomationListCreated(this);
112 AutomationList::~AutomationList()
116 boost::shared_ptr<Evoral::ControlList>
117 AutomationList::create(Evoral::Parameter id)
119 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id));
123 AutomationList::create_curve_if_necessary()
125 switch (_parameter.type()) {
128 case FadeInAutomation:
129 case FadeOutAutomation:
130 case EnvelopeAutomation:
139 AutomationList::operator== (const AutomationList& other)
141 return _events == other._events;
145 AutomationList::operator= (const AutomationList& other)
147 if (this != &other) {
151 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
152 _events.push_back (new Evoral::ControlEvent (**i));
155 _min_yval = other._min_yval;
156 _max_yval = other._max_yval;
157 _max_xval = other._max_xval;
158 _default_value = other._default_value;
161 maybe_signal_changed ();
168 AutomationList::maybe_signal_changed ()
170 ControlList::maybe_signal_changed ();
172 if (!ControlList::frozen()) {
173 StateChanged (); /* EMIT SIGNAL */
178 AutomationList::set_automation_state (AutoState s)
183 if (_state == Write) {
184 Glib::Mutex::Lock lm (ControlList::_lock);
185 nascent.push_back (new NascentInfo (false));
188 automation_state_changed (s); /* EMIT SIGNAL */
193 AutomationList::set_automation_style (AutoStyle s)
197 automation_style_changed (); /* EMIT SIGNAL */
202 AutomationList::start_touch (double when)
204 if (_state == Touch) {
205 Glib::Mutex::Lock lm (ControlList::_lock);
206 nascent.push_back (new NascentInfo (true, when));
209 g_atomic_int_set (&_touching, 1);
213 AutomationList::stop_touch (bool mark, double when)
215 g_atomic_int_set (&_touching, 0);
217 if (_state == Touch) {
218 Glib::Mutex::Lock lm (ControlList::_lock);
221 nascent.back()->end_time = when;
225 /* nascent info created in start touch but never used. just get rid of it.
228 NascentInfo* ninfo = nascent.back ();
229 nascent.erase (nascent.begin());
236 AutomationList::thaw ()
240 if (_changed_when_thawed) {
241 _changed_when_thawed = false;
242 StateChanged(); /* EMIT SIGNAL */
247 AutomationList::get_state ()
253 AutomationList::state (bool full)
255 XMLNode* root = new XMLNode (X_("AutomationList"));
257 LocaleGuard lg (X_("POSIX"));
259 root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
261 root->add_property ("id", _id.to_s());
263 snprintf (buf, sizeof (buf), "%.12g", _default_value);
264 root->add_property ("default", buf);
265 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
266 root->add_property ("min-yval", buf);
267 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
268 root->add_property ("max-yval", buf);
269 snprintf (buf, sizeof (buf), "%.12g", _max_xval);
270 root->add_property ("max-xval", buf);
272 root->add_property ("interpolation-style", enum_2_string (_interpolation));
275 /* never serialize state with Write enabled - too dangerous
278 if (_state != Write) {
279 root->add_property ("state", auto_state_to_string (_state));
281 root->add_property ("state", auto_state_to_string (Off));
284 /* never save anything but Off for automation state to a template */
285 root->add_property ("state", auto_state_to_string (Off));
288 root->add_property ("style", auto_style_to_string (_style));
290 if (!_events.empty()) {
291 root->add_child_nocopy (serialize_events());
298 AutomationList::serialize_events ()
300 XMLNode* node = new XMLNode (X_("events"));
303 str.precision(15); //10 digits is enough digits for 24 hours at 96kHz
305 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
306 str << (double) (*xx)->when;
308 str <<(double) (*xx)->value;
312 /* XML is a bit wierd */
314 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
315 content_node->set_content (str.str());
317 node->add_child_nocopy (*content_node);
323 AutomationList::deserialize_events (const XMLNode& node)
325 if (node.children().empty()) {
329 XMLNode* content_node = node.children().front();
331 if (content_node->content().empty()) {
335 ControlList::freeze ();
338 stringstream str (content_node->content());
354 fast_simple_add (x, y);
359 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
362 maybe_signal_changed ();
371 AutomationList::set_state (const XMLNode& node, int version)
373 XMLNodeList nlist = node.children();
375 XMLNodeIterator niter;
376 const XMLProperty* prop;
378 if (node.name() == X_("events")) {
379 /* partial state setting*/
380 return deserialize_events (node);
383 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
385 if ((nsos = node.child (X_("AutomationList")))) {
386 /* new school in old school clothing */
387 return set_state (*nsos, version);
392 const XMLNodeList& elist = node.children();
393 XMLNodeConstIterator i;
398 ControlList::freeze ();
401 for (i = elist.begin(); i != elist.end(); ++i) {
403 if ((prop = (*i)->property ("x")) == 0) {
404 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
407 x = atoi (prop->value().c_str());
409 if ((prop = (*i)->property ("y")) == 0) {
410 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
413 y = atof (prop->value().c_str());
415 fast_simple_add (x, y);
423 if (node.name() != X_("AutomationList") ) {
424 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
428 if ((prop = node.property ("id")) != 0) {
429 _id = prop->value ();
430 /* update session AL list */
431 AutomationListCreated(this);
434 if ((prop = node.property (X_("automation-id"))) != 0){
435 _parameter = EventTypeMap::instance().new_parameter(prop->value());
437 warning << "Legacy session: automation list has no automation-id property.";
440 if ((prop = node.property (X_("interpolation-style"))) != 0) {
441 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
443 _interpolation = Linear;
446 if ((prop = node.property (X_("default"))) != 0){
447 _default_value = atof (prop->value().c_str());
449 _default_value = 0.0;
452 if ((prop = node.property (X_("style"))) != 0) {
453 _style = string_to_auto_style (prop->value());
458 if ((prop = node.property (X_("state"))) != 0) {
459 _state = string_to_auto_state (prop->value());
460 if (_state == Write) {
467 if ((prop = node.property (X_("min-yval"))) != 0) {
468 _min_yval = atof (prop->value ().c_str());
473 if ((prop = node.property (X_("max-yval"))) != 0) {
474 _max_yval = atof (prop->value ().c_str());
479 if ((prop = node.property (X_("max-xval"))) != 0) {
480 _max_xval = atof (prop->value ().c_str());
482 _max_xval = 0; // means "no limit ;
485 bool have_events = false;
487 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
488 if ((*niter)->name() == X_("events")) {
489 deserialize_events (*(*niter));
495 /* there was no Events child node; clear any current events */
499 maybe_signal_changed ();