Clean up _before XMLNode in AutomationList if no automation write occurred.
[ardour.git] / libs / ardour / automation_list.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
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 "ardour/parameter_descriptor.h"
29 #include "evoral/Curve.hpp"
30 #include "pbd/stacktrace.h"
31 #include "pbd/enumwriter.h"
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 PBD::Signal1<void,AutomationList *> AutomationList::AutomationListCreated;
40
41 #if 0
42 static void dumpit (const AutomationList& al, string prefix = "")
43 {
44         cerr << prefix << &al << endl;
45         for (AutomationList::const_iterator i = al.begin(); i != al.end(); ++i) {
46                 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
47         }
48         cerr << "\n";
49 }
50 #endif
51 AutomationList::AutomationList (const Evoral::Parameter& id, const Evoral::ParameterDescriptor& desc)
52         : ControlList(id, desc)
53         , _before (0)
54 {
55         _state = Off;
56         _style = Absolute;
57         g_atomic_int_set (&_touching, 0);
58
59         create_curve_if_necessary();
60
61         assert(_parameter.type() != NullAutomation);
62         AutomationListCreated(this);
63 }
64
65 AutomationList::AutomationList (const Evoral::Parameter& id)
66         : ControlList(id, ARDOUR::ParameterDescriptor(id))
67         , _before (0)
68 {
69         _state = Off;
70         _style = Absolute;
71         g_atomic_int_set (&_touching, 0);
72
73         create_curve_if_necessary();
74
75         assert(_parameter.type() != NullAutomation);
76         AutomationListCreated(this);
77 }
78
79 AutomationList::AutomationList (const AutomationList& other)
80         : StatefulDestructible()
81         , ControlList(other)
82         , _before (0)
83 {
84         _style = other._style;
85         _state = other._state;
86         g_atomic_int_set (&_touching, other.touching());
87
88         create_curve_if_necessary();
89
90         assert(_parameter.type() != NullAutomation);
91         AutomationListCreated(this);
92 }
93
94 AutomationList::AutomationList (const AutomationList& other, double start, double end)
95         : ControlList(other, start, end)
96         , _before (0)
97 {
98         _style = other._style;
99         _state = other._state;
100         g_atomic_int_set (&_touching, other.touching());
101
102         create_curve_if_necessary();
103
104         assert(_parameter.type() != NullAutomation);
105         AutomationListCreated(this);
106 }
107
108 /** @param id is used for legacy sessions where the type is not present
109  * in or below the AutomationList node.  It is used if @param id is non-null.
110  */
111 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
112         : ControlList(id, ARDOUR::ParameterDescriptor(id))
113         , _before (0)
114 {
115         g_atomic_int_set (&_touching, 0);
116         _state = Off;
117         _style = Absolute;
118
119         set_state (node, Stateful::loading_state_version);
120
121         if (id) {
122                 _parameter = id;
123         }
124
125         create_curve_if_necessary();
126
127         assert(_parameter.type() != NullAutomation);
128         AutomationListCreated(this);
129 }
130
131 AutomationList::~AutomationList()
132 {
133 }
134
135 boost::shared_ptr<Evoral::ControlList>
136 AutomationList::create(const Evoral::Parameter&           id,
137                        const Evoral::ParameterDescriptor& desc)
138 {
139         return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id, desc));
140 }
141
142 void
143 AutomationList::create_curve_if_necessary()
144 {
145         switch (_parameter.type()) {
146         case GainAutomation:
147         case TrimAutomation:
148         case PanAzimuthAutomation:
149         case PanElevationAutomation:
150         case PanWidthAutomation:
151         case FadeInAutomation:
152         case FadeOutAutomation:
153         case EnvelopeAutomation:
154                 create_curve();
155                 break;
156         default:
157                 break;
158         }
159 }
160
161 AutomationList&
162 AutomationList::operator= (const AutomationList& other)
163 {
164         if (this != &other) {
165
166
167                 ControlList::operator= (other);
168                 _state = other._state;
169                 _style = other._style;
170                 _touching = other._touching;
171
172                 mark_dirty ();
173                 maybe_signal_changed ();
174         }
175
176         return *this;
177 }
178
179 void
180 AutomationList::maybe_signal_changed ()
181 {
182         ControlList::maybe_signal_changed ();
183
184         if (!ControlList::frozen()) {
185                 StateChanged (); /* EMIT SIGNAL */
186         }
187 }
188
189 void
190 AutomationList::set_automation_state (AutoState s)
191 {
192         if (s != _state) {
193                 _state = s;
194                 automation_state_changed (s); /* EMIT SIGNAL */
195         }
196 }
197
198 void
199 AutomationList::set_automation_style (AutoStyle s)
200 {
201         if (s != _style) {
202                 _style = s;
203                 automation_style_changed (); /* EMIT SIGNAL */
204         }
205 }
206
207 void
208 AutomationList::start_write_pass (double when)
209 {
210         if (in_new_write_pass ()) {
211                 _before = &get_state ();
212         }
213         ControlList::start_write_pass (when);
214 }
215
216 void
217 AutomationList::write_pass_finished (double when, double thinning_factor)
218 {
219         ControlList::write_pass_finished (when, thinning_factor);
220         /* automation control has deleted this or it is now owned by the session undo stack */
221         _before = 0;
222 }
223
224 void
225 AutomationList::start_touch (double when)
226 {
227         if (_state == Touch) {
228                 start_write_pass (when);
229         }
230
231         g_atomic_int_set (&_touching, 1);
232 }
233
234 void
235 AutomationList::stop_touch (bool mark, double)
236 {
237         if (g_atomic_int_get (&_touching) == 0) {
238                 /* this touch has already been stopped (probably by Automatable::transport_stopped),
239                    so we've nothing to do.
240                 */
241                 return;
242         }
243
244         g_atomic_int_set (&_touching, 0);
245
246         if (_state == Touch) {
247
248                 if (mark) {
249
250                         /* XXX need to mark the last added point with the
251                          * current time
252                          */
253                 }
254         }
255 }
256
257 /* _before may be owned by the undo stack,
258  * so we have to be careful about doing this.
259 */
260 void
261 AutomationList::clear_history ()
262 {
263         delete _before;
264         _before = 0;
265 }
266
267 void
268 AutomationList::thaw ()
269 {
270         ControlList::thaw();
271
272         if (_changed_when_thawed) {
273                 _changed_when_thawed = false;
274                 StateChanged(); /* EMIT SIGNAL */
275         }
276 }
277
278 XMLNode&
279 AutomationList::get_state ()
280 {
281         return state (true);
282 }
283
284 XMLNode&
285 AutomationList::state (bool full)
286 {
287         XMLNode* root = new XMLNode (X_("AutomationList"));
288         char buf[64];
289         LocaleGuard lg (X_("C"));
290
291         root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
292
293         root->add_property ("id", id().to_s());
294
295         snprintf (buf, sizeof (buf), "%.12g", _default_value);
296         root->add_property ("default", buf);
297         snprintf (buf, sizeof (buf), "%.12g", _min_yval);
298         root->add_property ("min-yval", buf);
299         snprintf (buf, sizeof (buf), "%.12g", _max_yval);
300         root->add_property ("max-yval", buf);
301
302         root->add_property ("interpolation-style", enum_2_string (_interpolation));
303
304         if (full) {
305                 /* never serialize state with Write enabled - too dangerous
306                    for the user's data
307                 */
308                 if (_state != Write) {
309                         root->add_property ("state", auto_state_to_string (_state));
310                 } else {
311                         if (_events.empty ()) {
312                                 root->add_property ("state", auto_state_to_string (Off));
313                         } else {
314                                 root->add_property ("state", auto_state_to_string (Touch));
315                         }
316                 }
317         } else {
318                 /* never save anything but Off for automation state to a template */
319                 root->add_property ("state", auto_state_to_string (Off));
320         }
321
322         root->add_property ("style", auto_style_to_string (_style));
323
324         if (!_events.empty()) {
325                 root->add_child_nocopy (serialize_events());
326         }
327
328         return *root;
329 }
330
331 XMLNode&
332 AutomationList::serialize_events ()
333 {
334         XMLNode* node = new XMLNode (X_("events"));
335         stringstream str;
336
337         str.precision(15);  //10 digits is enough digits for 24 hours at 96kHz
338
339         for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
340                 str << (double) (*xx)->when;
341                 str << ' ';
342                 str <<(double) (*xx)->value;
343                 str << '\n';
344         }
345
346         /* XML is a bit wierd */
347
348         XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
349         content_node->set_content (str.str());
350
351         node->add_child_nocopy (*content_node);
352
353         return *node;
354 }
355
356 int
357 AutomationList::deserialize_events (const XMLNode& node)
358 {
359         if (node.children().empty()) {
360                 return -1;
361         }
362
363         XMLNode* content_node = node.children().front();
364
365         if (content_node->content().empty()) {
366                 return -1;
367         }
368
369         ControlList::freeze ();
370         clear ();
371
372         stringstream str (content_node->content());
373
374         double x;
375         double y;
376         bool ok = true;
377
378         while (str) {
379                 str >> x;
380                 if (!str) {
381                         break;
382                 }
383                 str >> y;
384                 if (!str) {
385                         ok = false;
386                         break;
387                 }
388                 fast_simple_add (x, y);
389         }
390
391         if (!ok) {
392                 clear ();
393                 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
394         } else {
395                 mark_dirty ();
396                 maybe_signal_changed ();
397         }
398
399         thaw ();
400
401         return 0;
402 }
403
404 int
405 AutomationList::set_state (const XMLNode& node, int version)
406 {
407         LocaleGuard lg (X_("C"));
408         XMLNodeList nlist = node.children();
409         XMLNode* nsos;
410         XMLNodeIterator niter;
411         const XMLProperty* prop;
412
413         if (node.name() == X_("events")) {
414                 /* partial state setting*/
415                 return deserialize_events (node);
416         }
417
418         if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
419
420                 if ((nsos = node.child (X_("AutomationList")))) {
421                         /* new school in old school clothing */
422                         return set_state (*nsos, version);
423                 }
424
425                 /* old school */
426
427                 const XMLNodeList& elist = node.children();
428                 XMLNodeConstIterator i;
429                 XMLProperty* prop;
430                 pframes_t x;
431                 double y;
432
433                 ControlList::freeze ();
434                 clear ();
435
436                 for (i = elist.begin(); i != elist.end(); ++i) {
437
438                         if ((prop = (*i)->property ("x")) == 0) {
439                                 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
440                                 continue;
441                         }
442                         x = atoi (prop->value().c_str());
443
444                         if ((prop = (*i)->property ("y")) == 0) {
445                                 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
446                                 continue;
447                         }
448                         y = atof (prop->value().c_str());
449
450                         fast_simple_add (x, y);
451                 }
452
453                 thaw ();
454
455                 return 0;
456         }
457
458         if (node.name() != X_("AutomationList") ) {
459                 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
460                 return -1;
461         }
462
463         if (set_id (node)) {
464                 /* update session AL list */
465                 AutomationListCreated(this);
466         }
467
468         if ((prop = node.property (X_("automation-id"))) != 0){
469                 _parameter = EventTypeMap::instance().from_symbol(prop->value());
470         } else {
471                 warning << "Legacy session: automation list has no automation-id property." << endmsg;
472         }
473
474         if ((prop = node.property (X_("interpolation-style"))) != 0) {
475                 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
476         } else {
477                 _interpolation = Linear;
478         }
479
480         if ((prop = node.property (X_("default"))) != 0){
481                 _default_value = atof (prop->value().c_str());
482         } else {
483                 _default_value = 0.0;
484         }
485
486         if ((prop = node.property (X_("style"))) != 0) {
487                 _style = string_to_auto_style (prop->value());
488         } else {
489                 _style = Absolute;
490         }
491
492         if ((prop = node.property (X_("state"))) != 0) {
493                 _state = string_to_auto_state (prop->value());
494                 if (_state == Write) {
495                         _state = Off;
496                 }
497                 automation_state_changed(_state);
498         } else {
499                 _state = Off;
500         }
501
502         if ((prop = node.property (X_("min-yval"))) != 0) {
503                 _min_yval = atof (prop->value ().c_str());
504         } else {
505                 _min_yval = FLT_MIN;
506         }
507
508         if ((prop = node.property (X_("max-yval"))) != 0) {
509                 _max_yval = atof (prop->value ().c_str());
510         } else {
511                 _max_yval = FLT_MAX;
512         }
513
514         bool have_events = false;
515
516         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
517                 if ((*niter)->name() == X_("events")) {
518                         deserialize_events (*(*niter));
519                         have_events = true;
520                 }
521         }
522
523         if (!have_events) {
524                 /* there was no Events child node; clear any current events */
525                 freeze ();
526                 clear ();
527                 mark_dirty ();
528                 maybe_signal_changed ();
529                 thaw ();
530         }
531
532         return 0;
533 }
534
535 bool
536 AutomationList::operator!= (AutomationList const & other) const
537 {
538         return (
539                 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
540                 _state != other._state ||
541                 _style != other._style ||
542                 _touching != other._touching
543                 );
544 }
545
546 PBD::PropertyBase *
547 AutomationListProperty::clone () const
548 {
549         return new AutomationListProperty (
550                 this->property_id(),
551                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
552                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))
553                 );
554 }
555