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