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