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