Make AutomationLists clear their events when their state is set to an AutomationList...
[ArdourMidi.git] / libs / ardour / automation_list.cc
blob17f346db1da3833e5551c3bc4dd61ffcaa521f1c
1 /*
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.
20 #include <set>
21 #include <climits>
22 #include <float.h>
23 #include <cmath>
24 #include <sstream>
25 #include <algorithm>
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"
32 #include "i18n.h"
34 using namespace std;
35 using namespace ARDOUR;
36 using namespace PBD;
38 PBD::Signal1<void,AutomationList *> AutomationList::AutomationListCreated;
40 #if 0
41 static void dumpit (const AutomationList& al, string prefix = "")
43 cerr << prefix << &al << endl;
44 for (AutomationList::const_iterator i = al.const_begin(); i != al.const_end(); ++i) {
45 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
47 cerr << "\n";
49 #endif
51 AutomationList::AutomationList (Evoral::Parameter id)
52 : ControlList(id)
54 _state = Off;
55 _style = Absolute;
56 _touching = false;
58 create_curve_if_necessary();
60 assert(_parameter.type() != NullAutomation);
61 AutomationListCreated(this);
64 AutomationList::AutomationList (const AutomationList& other)
65 : StatefulDestructible()
66 , ControlList(other)
68 _style = other._style;
69 _state = other._state;
70 _touching = other._touching;
72 create_curve_if_necessary();
74 assert(_parameter.type() != NullAutomation);
75 AutomationListCreated(this);
78 AutomationList::AutomationList (const AutomationList& other, double start, double end)
79 : ControlList(other, start, end)
81 _style = other._style;
82 _state = other._state;
83 _touching = other._touching;
85 create_curve_if_necessary();
87 assert(_parameter.type() != NullAutomation);
88 AutomationListCreated(this);
91 /** @param id is used for legacy sessions where the type is not present
92 * in or below the AutomationList node. It is used if @param id is non-null.
94 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
95 : ControlList(id)
97 _touching = false;
98 _state = Off;
99 _style = Absolute;
101 set_state (node, Stateful::loading_state_version);
103 if (id) {
104 _parameter = id;
107 create_curve_if_necessary();
109 assert(_parameter.type() != NullAutomation);
110 AutomationListCreated(this);
113 AutomationList::~AutomationList()
117 boost::shared_ptr<Evoral::ControlList>
118 AutomationList::create(Evoral::Parameter id)
120 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id));
123 void
124 AutomationList::create_curve_if_necessary()
126 switch (_parameter.type()) {
127 case GainAutomation:
128 case PanAutomation:
129 case FadeInAutomation:
130 case FadeOutAutomation:
131 case EnvelopeAutomation:
132 create_curve();
133 break;
134 default:
135 break;
139 bool
140 AutomationList::operator== (const AutomationList& other)
142 return _events == other._events;
145 AutomationList&
146 AutomationList::operator= (const AutomationList& other)
148 if (this != &other) {
150 _events.clear ();
152 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
153 _events.push_back (new Evoral::ControlEvent (**i));
156 _min_yval = other._min_yval;
157 _max_yval = other._max_yval;
158 _max_xval = other._max_xval;
159 _default_value = other._default_value;
161 mark_dirty ();
162 maybe_signal_changed ();
165 return *this;
168 void
169 AutomationList::maybe_signal_changed ()
171 ControlList::maybe_signal_changed ();
173 if (!ControlList::frozen()) {
174 StateChanged (); /* EMIT SIGNAL */
178 void
179 AutomationList::set_automation_state (AutoState s)
181 if (s != _state) {
182 _state = s;
183 automation_state_changed (); /* EMIT SIGNAL */
187 void
188 AutomationList::set_automation_style (AutoStyle s)
190 if (s != _style) {
191 _style = s;
192 automation_style_changed (); /* EMIT SIGNAL */
196 void
197 AutomationList::start_touch ()
199 _touching = true;
200 _new_value = true;
203 void
204 AutomationList::stop_touch ()
206 _touching = false;
207 _new_value = false;
210 void
211 AutomationList::thaw ()
213 ControlList::thaw();
215 if (_changed_when_thawed) {
216 _changed_when_thawed = false;
217 StateChanged(); /* EMIT SIGNAL */
221 XMLNode&
222 AutomationList::get_state ()
224 return state (true);
227 XMLNode&
228 AutomationList::state (bool full)
230 XMLNode* root = new XMLNode (X_("AutomationList"));
231 char buf[64];
232 LocaleGuard lg (X_("POSIX"));
234 root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
236 root->add_property ("id", _id.to_s());
238 snprintf (buf, sizeof (buf), "%.12g", _default_value);
239 root->add_property ("default", buf);
240 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
241 root->add_property ("min-yval", buf);
242 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
243 root->add_property ("max-yval", buf);
244 snprintf (buf, sizeof (buf), "%.12g", _max_xval);
245 root->add_property ("max-xval", buf);
247 root->add_property ("interpolation-style", enum_2_string (_interpolation));
249 if (full) {
250 root->add_property ("state", auto_state_to_string (_state));
251 } else {
252 /* never save anything but Off for automation state to a template */
253 root->add_property ("state", auto_state_to_string (Off));
256 root->add_property ("style", auto_style_to_string (_style));
258 if (!_events.empty()) {
259 root->add_child_nocopy (serialize_events());
262 return *root;
265 XMLNode&
266 AutomationList::serialize_events ()
268 XMLNode* node = new XMLNode (X_("events"));
269 stringstream str;
271 str.precision(15); //10 digits is enough digits for 24 hours at 96kHz
273 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
274 str << (double) (*xx)->when;
275 str << ' ';
276 str <<(double) (*xx)->value;
277 str << '\n';
280 /* XML is a bit wierd */
282 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
283 content_node->set_content (str.str());
285 node->add_child_nocopy (*content_node);
287 return *node;
291 AutomationList::deserialize_events (const XMLNode& node)
293 if (node.children().empty()) {
294 return -1;
297 XMLNode* content_node = node.children().front();
299 if (content_node->content().empty()) {
300 return -1;
303 ControlList::freeze ();
304 clear ();
306 stringstream str (content_node->content());
308 double x;
309 double y;
310 bool ok = true;
312 while (str) {
313 str >> x;
314 if (!str) {
315 break;
317 str >> y;
318 if (!str) {
319 ok = false;
320 break;
322 fast_simple_add (x, y);
325 if (!ok) {
326 clear ();
327 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
328 } else {
329 mark_dirty ();
330 reposition_for_rt_add (0);
331 maybe_signal_changed ();
334 thaw ();
336 return 0;
340 AutomationList::set_state (const XMLNode& node, int version)
342 XMLNodeList nlist = node.children();
343 XMLNode* nsos;
344 XMLNodeIterator niter;
345 const XMLProperty* prop;
347 if (node.name() == X_("events")) {
348 /* partial state setting*/
349 return deserialize_events (node);
352 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
354 if ((nsos = node.child (X_("AutomationList")))) {
355 /* new school in old school clothing */
356 return set_state (*nsos, version);
359 /* old school */
361 const XMLNodeList& elist = node.children();
362 XMLNodeConstIterator i;
363 XMLProperty* prop;
364 nframes_t x;
365 double y;
367 ControlList::freeze ();
368 clear ();
370 for (i = elist.begin(); i != elist.end(); ++i) {
372 if ((prop = (*i)->property ("x")) == 0) {
373 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
374 continue;
376 x = atoi (prop->value().c_str());
378 if ((prop = (*i)->property ("y")) == 0) {
379 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
380 continue;
382 y = atof (prop->value().c_str());
384 fast_simple_add (x, y);
387 thaw ();
389 return 0;
392 if (node.name() != X_("AutomationList") ) {
393 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
394 return -1;
397 if ((prop = node.property ("id")) != 0) {
398 _id = prop->value ();
399 /* update session AL list */
400 AutomationListCreated(this);
403 if ((prop = node.property (X_("automation-id"))) != 0){
404 _parameter = EventTypeMap::instance().new_parameter(prop->value());
405 } else {
406 warning << "Legacy session: automation list has no automation-id property.";
409 if ((prop = node.property (X_("interpolation-style"))) != 0) {
410 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
411 } else {
412 _interpolation = Linear;
415 if ((prop = node.property (X_("default"))) != 0){
416 _default_value = atof (prop->value().c_str());
417 } else {
418 _default_value = 0.0;
421 if ((prop = node.property (X_("style"))) != 0) {
422 _style = string_to_auto_style (prop->value());
423 } else {
424 _style = Absolute;
427 if ((prop = node.property (X_("state"))) != 0) {
428 _state = string_to_auto_state (prop->value());
429 } else {
430 _state = Off;
433 if ((prop = node.property (X_("min_yval"))) != 0) {
434 _min_yval = atof (prop->value ().c_str());
435 } else {
436 _min_yval = FLT_MIN;
439 if ((prop = node.property (X_("max_yval"))) != 0) {
440 _max_yval = atof (prop->value ().c_str());
441 } else {
442 _max_yval = FLT_MAX;
445 if ((prop = node.property (X_("max_xval"))) != 0) {
446 _max_xval = atof (prop->value ().c_str());
447 } else {
448 _max_xval = 0; // means "no limit ;
451 bool have_events = false;
453 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
454 if ((*niter)->name() == X_("events")) {
455 deserialize_events (*(*niter));
456 have_events = true;
460 if (!have_events) {
461 /* there was no Events child node; clear any current events */
462 freeze ();
463 clear ();
464 mark_dirty ();
465 reposition_for_rt_add (0);
466 maybe_signal_changed ();
467 thaw ();
470 return 0;