Remove separate ControlList min/max/default, use ParameterDescriptor.
[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 ("interpolation-style", _interpolation);
325
326         if (full) {
327                 /* never serialize state with Write enabled - too dangerous
328                    for the user's data
329                 */
330                 if (_state != Write) {
331                         root->set_property ("state", _state);
332                 } else {
333                         if (_events.empty ()) {
334                                 root->set_property ("state", Off);
335                         } else {
336                                 root->set_property ("state", Touch);
337                         }
338                 }
339         } else {
340                 /* never save anything but Off for automation state to a template */
341                 root->set_property ("state", Off);
342         }
343
344         if (!_events.empty()) {
345                 root->add_child_nocopy (serialize_events());
346         }
347
348         return *root;
349 }
350
351 XMLNode&
352 AutomationList::serialize_events ()
353 {
354         XMLNode* node = new XMLNode (X_("events"));
355         stringstream str;
356
357         for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
358                 str << PBD::to_string ((*xx)->when);
359                 str << ' ';
360                 str << PBD::to_string ((*xx)->value);
361                 str << '\n';
362         }
363
364         /* XML is a bit wierd */
365
366         XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
367         content_node->set_content (str.str());
368
369         node->add_child_nocopy (*content_node);
370
371         return *node;
372 }
373
374 int
375 AutomationList::deserialize_events (const XMLNode& node)
376 {
377         if (node.children().empty()) {
378                 return -1;
379         }
380
381         XMLNode* content_node = node.children().front();
382
383         if (content_node->content().empty()) {
384                 return -1;
385         }
386
387         ControlList::freeze ();
388         clear ();
389
390         stringstream str (content_node->content());
391
392         std::string x_str;
393         std::string y_str;
394         double x;
395         double y;
396         bool ok = true;
397
398         while (str) {
399                 str >> x_str;
400                 if (!str || !PBD::string_to<double> (x_str, x)) {
401                         break;
402                 }
403                 str >> y_str;
404                 if (!str || !PBD::string_to<double> (y_str, y)) {
405                         ok = false;
406                         break;
407                 }
408                 fast_simple_add (x, y);
409         }
410
411         if (!ok) {
412                 clear ();
413                 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
414         } else {
415                 mark_dirty ();
416                 maybe_signal_changed ();
417         }
418
419         thaw ();
420
421         return 0;
422 }
423
424 int
425 AutomationList::set_state (const XMLNode& node, int version)
426 {
427         LocaleGuard lg;
428         XMLNodeList nlist = node.children();
429         XMLNode* nsos;
430         XMLNodeIterator niter;
431
432         if (node.name() == X_("events")) {
433                 /* partial state setting*/
434                 return deserialize_events (node);
435         }
436
437         if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
438
439                 if ((nsos = node.child (X_("AutomationList")))) {
440                         /* new school in old school clothing */
441                         return set_state (*nsos, version);
442                 }
443
444                 /* old school */
445
446                 const XMLNodeList& elist = node.children();
447                 XMLNodeConstIterator i;
448
449                 ControlList::freeze ();
450                 clear ();
451
452                 for (i = elist.begin(); i != elist.end(); ++i) {
453
454                         pframes_t x;
455                         if (!(*i)->get_property ("x", x)) {
456                                 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
457                                 continue;
458                         }
459
460                         double y;
461                         if (!(*i)->get_property ("y", y)) {
462                                 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
463                                 continue;
464                         }
465
466                         fast_simple_add (x, y);
467                 }
468
469                 thaw ();
470
471                 return 0;
472         }
473
474         if (node.name() != X_("AutomationList") ) {
475                 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
476                 return -1;
477         }
478
479         if (set_id (node)) {
480                 /* update session AL list */
481                 AutomationListCreated(this);
482         }
483
484         std::string value;
485         if (node.get_property (X_("automation-id"), value)) {
486                 _parameter = EventTypeMap::instance().from_symbol(value);
487         } else {
488                 warning << "Legacy session: automation list has no automation-id property." << endmsg;
489         }
490
491         if (!node.get_property (X_("interpolation-style"), _interpolation)) {
492                 _interpolation = Linear;
493         }
494
495         if (node.get_property (X_("state"), _state)) {
496                 if (_state == Write) {
497                         _state = Off;
498                 }
499                 automation_state_changed (_state);
500         } else {
501                 _state = Off;
502         }
503
504         bool have_events = false;
505
506         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
507                 if ((*niter)->name() == X_("events")) {
508                         deserialize_events (*(*niter));
509                         have_events = true;
510                 }
511         }
512
513         if (!have_events) {
514                 /* there was no Events child node; clear any current events */
515                 freeze ();
516                 clear ();
517                 mark_dirty ();
518                 maybe_signal_changed ();
519                 thaw ();
520         }
521
522         return 0;
523 }
524
525 bool
526 AutomationList::operator!= (AutomationList const & other) const
527 {
528         return (
529                 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
530                 _state != other._state ||
531                 _touching != other._touching
532                 );
533 }
534
535 PBD::PropertyBase *
536 AutomationListProperty::clone () const
537 {
538         return new AutomationListProperty (
539                 this->property_id(),
540                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
541                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))
542                 );
543 }
544