Fix missing undo for record mute automation.
[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                 if (s == Write) {
195                         _before = &get_state ();
196                 }
197                 automation_state_changed (s); /* EMIT SIGNAL */
198         }
199 }
200
201 void
202 AutomationList::set_automation_style (AutoStyle s)
203 {
204         if (s != _style) {
205                 _style = s;
206                 automation_style_changed (); /* EMIT SIGNAL */
207         }
208 }
209
210 void
211 AutomationList::start_write_pass (double when)
212 {
213         if (in_new_write_pass ()) {
214                 _before = &get_state ();
215         }
216         ControlList::start_write_pass (when);
217 }
218
219 void
220 AutomationList::write_pass_finished (double when, double thinning_factor)
221 {
222         ControlList::write_pass_finished (when, thinning_factor);
223         /* automation control has deleted this or it is now owned by the session undo stack */
224         _before = 0;
225 }
226
227 void
228 AutomationList::start_touch (double when)
229 {
230         if (_state == Touch) {
231                 start_write_pass (when);
232         }
233
234         g_atomic_int_set (&_touching, 1);
235 }
236
237 void
238 AutomationList::stop_touch (bool mark, double)
239 {
240         if (g_atomic_int_get (&_touching) == 0) {
241                 /* this touch has already been stopped (probably by Automatable::transport_stopped),
242                    so we've nothing to do.
243                 */
244                 return;
245         }
246
247         g_atomic_int_set (&_touching, 0);
248
249         if (_state == Touch) {
250
251                 if (mark) {
252
253                         /* XXX need to mark the last added point with the
254                          * current time
255                          */
256                 }
257         }
258 }
259
260 /* _before may be owned by the undo stack,
261  * so we have to be careful about doing this.
262 */
263 void
264 AutomationList::clear_history ()
265 {
266         delete _before;
267         _before = 0;
268 }
269
270 void
271 AutomationList::thaw ()
272 {
273         ControlList::thaw();
274
275         if (_changed_when_thawed) {
276                 _changed_when_thawed = false;
277                 StateChanged(); /* EMIT SIGNAL */
278         }
279 }
280
281 XMLNode&
282 AutomationList::get_state ()
283 {
284         return state (true);
285 }
286
287 XMLNode&
288 AutomationList::state (bool full)
289 {
290         XMLNode* root = new XMLNode (X_("AutomationList"));
291         char buf[64];
292         LocaleGuard lg (X_("C"));
293
294         root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
295
296         root->add_property ("id", id().to_s());
297
298         snprintf (buf, sizeof (buf), "%.12g", _default_value);
299         root->add_property ("default", buf);
300         snprintf (buf, sizeof (buf), "%.12g", _min_yval);
301         root->add_property ("min-yval", buf);
302         snprintf (buf, sizeof (buf), "%.12g", _max_yval);
303         root->add_property ("max-yval", buf);
304
305         root->add_property ("interpolation-style", enum_2_string (_interpolation));
306
307         if (full) {
308                 /* never serialize state with Write enabled - too dangerous
309                    for the user's data
310                 */
311                 if (_state != Write) {
312                         root->add_property ("state", auto_state_to_string (_state));
313                 } else {
314                         if (_events.empty ()) {
315                                 root->add_property ("state", auto_state_to_string (Off));
316                         } else {
317                                 root->add_property ("state", auto_state_to_string (Touch));
318                         }
319                 }
320         } else {
321                 /* never save anything but Off for automation state to a template */
322                 root->add_property ("state", auto_state_to_string (Off));
323         }
324
325         root->add_property ("style", auto_style_to_string (_style));
326
327         if (!_events.empty()) {
328                 root->add_child_nocopy (serialize_events());
329         }
330
331         return *root;
332 }
333
334 XMLNode&
335 AutomationList::serialize_events ()
336 {
337         XMLNode* node = new XMLNode (X_("events"));
338         stringstream str;
339
340         str.precision(15);  //10 digits is enough digits for 24 hours at 96kHz
341
342         for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
343                 str << (double) (*xx)->when;
344                 str << ' ';
345                 str <<(double) (*xx)->value;
346                 str << '\n';
347         }
348
349         /* XML is a bit wierd */
350
351         XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
352         content_node->set_content (str.str());
353
354         node->add_child_nocopy (*content_node);
355
356         return *node;
357 }
358
359 int
360 AutomationList::deserialize_events (const XMLNode& node)
361 {
362         if (node.children().empty()) {
363                 return -1;
364         }
365
366         XMLNode* content_node = node.children().front();
367
368         if (content_node->content().empty()) {
369                 return -1;
370         }
371
372         ControlList::freeze ();
373         clear ();
374
375         stringstream str (content_node->content());
376
377         double x;
378         double y;
379         bool ok = true;
380
381         while (str) {
382                 str >> x;
383                 if (!str) {
384                         break;
385                 }
386                 str >> y;
387                 if (!str) {
388                         ok = false;
389                         break;
390                 }
391                 fast_simple_add (x, y);
392         }
393
394         if (!ok) {
395                 clear ();
396                 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
397         } else {
398                 mark_dirty ();
399                 maybe_signal_changed ();
400         }
401
402         thaw ();
403
404         return 0;
405 }
406
407 int
408 AutomationList::set_state (const XMLNode& node, int version)
409 {
410         LocaleGuard lg (X_("C"));
411         XMLNodeList nlist = node.children();
412         XMLNode* nsos;
413         XMLNodeIterator niter;
414         const XMLProperty* prop;
415
416         if (node.name() == X_("events")) {
417                 /* partial state setting*/
418                 return deserialize_events (node);
419         }
420
421         if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
422
423                 if ((nsos = node.child (X_("AutomationList")))) {
424                         /* new school in old school clothing */
425                         return set_state (*nsos, version);
426                 }
427
428                 /* old school */
429
430                 const XMLNodeList& elist = node.children();
431                 XMLNodeConstIterator i;
432                 XMLProperty* prop;
433                 pframes_t x;
434                 double y;
435
436                 ControlList::freeze ();
437                 clear ();
438
439                 for (i = elist.begin(); i != elist.end(); ++i) {
440
441                         if ((prop = (*i)->property ("x")) == 0) {
442                                 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
443                                 continue;
444                         }
445                         x = atoi (prop->value().c_str());
446
447                         if ((prop = (*i)->property ("y")) == 0) {
448                                 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
449                                 continue;
450                         }
451                         y = atof (prop->value().c_str());
452
453                         fast_simple_add (x, y);
454                 }
455
456                 thaw ();
457
458                 return 0;
459         }
460
461         if (node.name() != X_("AutomationList") ) {
462                 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
463                 return -1;
464         }
465
466         if (set_id (node)) {
467                 /* update session AL list */
468                 AutomationListCreated(this);
469         }
470
471         if ((prop = node.property (X_("automation-id"))) != 0){
472                 _parameter = EventTypeMap::instance().from_symbol(prop->value());
473         } else {
474                 warning << "Legacy session: automation list has no automation-id property." << endmsg;
475         }
476
477         if ((prop = node.property (X_("interpolation-style"))) != 0) {
478                 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
479         } else {
480                 _interpolation = Linear;
481         }
482
483         if ((prop = node.property (X_("default"))) != 0){
484                 _default_value = atof (prop->value().c_str());
485         } else {
486                 _default_value = 0.0;
487         }
488
489         if ((prop = node.property (X_("style"))) != 0) {
490                 _style = string_to_auto_style (prop->value());
491         } else {
492                 _style = Absolute;
493         }
494
495         if ((prop = node.property (X_("state"))) != 0) {
496                 _state = string_to_auto_state (prop->value());
497                 if (_state == Write) {
498                         _state = Off;
499                 }
500                 automation_state_changed(_state);
501         } else {
502                 _state = Off;
503         }
504
505         if ((prop = node.property (X_("min-yval"))) != 0) {
506                 _min_yval = atof (prop->value ().c_str());
507         } else {
508                 _min_yval = FLT_MIN;
509         }
510
511         if ((prop = node.property (X_("max-yval"))) != 0) {
512                 _max_yval = atof (prop->value ().c_str());
513         } else {
514                 _max_yval = FLT_MAX;
515         }
516
517         bool have_events = false;
518
519         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
520                 if ((*niter)->name() == X_("events")) {
521                         deserialize_events (*(*niter));
522                         have_events = true;
523                 }
524         }
525
526         if (!have_events) {
527                 /* there was no Events child node; clear any current events */
528                 freeze ();
529                 clear ();
530                 mark_dirty ();
531                 maybe_signal_changed ();
532                 thaw ();
533         }
534
535         return 0;
536 }
537
538 bool
539 AutomationList::operator!= (AutomationList const & other) const
540 {
541         return (
542                 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
543                 _state != other._state ||
544                 _style != other._style ||
545                 _touching != other._touching
546                 );
547 }
548
549 PBD::PropertyBase *
550 AutomationListProperty::clone () const
551 {
552         return new AutomationListProperty (
553                 this->property_id(),
554                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
555                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))
556                 );
557 }
558