Optimize plugin-processing for non-automated params
[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_samples_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         WritePassStarted.connect_same_thread (_writepass_connection, boost::bind (&AutomationList::snapshot_history, this, false));
166 }
167
168 AutomationList&
169 AutomationList::operator= (const AutomationList& other)
170 {
171         if (this != &other) {
172                 ControlList::freeze ();
173                 /* ControlList::operator= calls copy_events() which calls
174                  * mark_dirty() and maybe_signal_changed()
175                  */
176                 ControlList::operator= (other);
177                 _state = other._state;
178                 _touching = other._touching;
179                 ControlList::thaw ();
180         }
181
182         return *this;
183 }
184
185 void
186 AutomationList::maybe_signal_changed ()
187 {
188         ControlList::maybe_signal_changed ();
189
190         if (!ControlList::frozen()) {
191                 StateChanged (); /* EMIT SIGNAL */
192         }
193 }
194
195 AutoState
196 AutomationList::automation_state() const
197 {
198         Glib::Threads::RWLock::ReaderLock lm (Evoral::ControlList::_lock);
199         return _state;
200 }
201
202 void
203 AutomationList::set_automation_state (AutoState s)
204 {
205         {
206                 Glib::Threads::RWLock::ReaderLock lm (Evoral::ControlList::_lock);
207
208                 if (s == _state) {
209                         return;
210                 }
211                 _state = s;
212                 if (s == Write && _desc.toggled) {
213                         snapshot_history (true);
214                 }
215         }
216
217         automation_state_changed (s); /* EMIT SIGNAL */
218 }
219
220 Evoral::ControlList::InterpolationStyle
221 AutomationList::default_interpolation () const
222 {
223         switch (_parameter.type()) {
224                 case GainAutomation:
225                 case BusSendLevel:
226                 case EnvelopeAutomation:
227                         return ControlList::Exponential;
228                         break;
229                 case TrimAutomation:
230                         return ControlList::Logarithmic;
231                         break;
232                 default:
233                         break;
234         }
235         /* based on Evoral::ParameterDescriptor log,toggle,.. */
236         return ControlList::default_interpolation ();
237 }
238
239 void
240 AutomationList::start_write_pass (double when)
241 {
242         snapshot_history (true);
243         ControlList::start_write_pass (when);
244 }
245
246 void
247 AutomationList::write_pass_finished (double when, double thinning_factor)
248 {
249         ControlList::write_pass_finished (when, thinning_factor);
250 }
251
252 void
253 AutomationList::start_touch (double when)
254 {
255         if (_state == Touch) {
256                 start_write_pass (when);
257         }
258
259         g_atomic_int_set (&_touching, 1);
260 }
261
262 void
263 AutomationList::stop_touch (double)
264 {
265         if (g_atomic_int_get (&_touching) == 0) {
266                 /* this touch has already been stopped (probably by Automatable::transport_stopped),
267                    so we've nothing to do.
268                 */
269                 return;
270         }
271
272         g_atomic_int_set (&_touching, 0);
273 }
274
275 /* _before may be owned by the undo stack,
276  * so we have to be careful about doing this.
277  *
278  * ::before () transfers ownership, setting _before to 0
279  */
280 void
281 AutomationList::clear_history ()
282 {
283         delete _before;
284         _before = 0;
285 }
286
287 void
288 AutomationList::snapshot_history (bool need_lock)
289 {
290         if (!in_new_write_pass ()) {
291                 return;
292         }
293         delete _before;
294         _before = &state (true, need_lock);
295 }
296
297
298 void
299 AutomationList::thaw ()
300 {
301         ControlList::thaw();
302
303         if (_changed_when_thawed) {
304                 _changed_when_thawed = false;
305                 StateChanged(); /* EMIT SIGNAL */
306         }
307 }
308
309 bool
310 AutomationList::paste (const ControlList& alist, double pos, DoubleBeatsSamplesConverter const& bfc)
311 {
312         AutomationType src_type = (AutomationType)alist.parameter().type();
313         AutomationType dst_type = (AutomationType)_parameter.type();
314
315         if (parameter_is_midi (src_type) == parameter_is_midi (dst_type)) {
316                 return ControlList::paste (alist, pos);
317         }
318         bool to_sample = parameter_is_midi (src_type);
319
320         ControlList cl (alist);
321         cl.clear ();
322         for (const_iterator i = alist.begin ();i != alist.end (); ++i) {
323                 double when = (*i)->when;
324                 if (to_sample) {
325                         when = bfc.to ((*i)->when);
326                 } else {
327                         when = bfc.from ((*i)->when);
328                 }
329                 cl.fast_simple_add (when, (*i)->value);
330         }
331         return ControlList::paste (cl, pos);
332 }
333
334 Command*
335 AutomationList::memento_command (XMLNode* before, XMLNode* after)
336 {
337         return new MementoCommand<AutomationList> (*this, before, after);
338 }
339
340 XMLNode&
341 AutomationList::get_state ()
342 {
343         return state (true, true);
344 }
345
346 XMLNode&
347 AutomationList::state (bool save_auto_state, bool need_lock)
348 {
349         XMLNode* root = new XMLNode (X_("AutomationList"));
350
351         root->set_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
352         root->set_property ("id", id());
353         root->set_property ("interpolation-style", _interpolation);
354
355         if (save_auto_state) {
356                 /* never serialize state with Write enabled - too dangerous
357                    for the user's data
358                 */
359                 if (_state != Write) {
360                         root->set_property ("state", _state);
361                 } else {
362                         if (_events.empty ()) {
363                                 root->set_property ("state", Off);
364                         } else {
365                                 root->set_property ("state", Touch);
366                         }
367                 }
368         } else {
369                 /* never save anything but Off for automation state to a template */
370                 root->set_property ("state", Off);
371         }
372
373         if (!_events.empty()) {
374                 root->add_child_nocopy (serialize_events (need_lock));
375         }
376
377         return *root;
378 }
379
380 XMLNode&
381 AutomationList::serialize_events (bool need_lock)
382 {
383         XMLNode* node = new XMLNode (X_("events"));
384         stringstream str;
385
386         Glib::Threads::RWLock::ReaderLock lm (Evoral::ControlList::_lock, Glib::Threads::NOT_LOCK);
387         if (need_lock) {
388                 lm.acquire ();
389         }
390         for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
391                 str << PBD::to_string ((*xx)->when);
392                 str << ' ';
393                 str << PBD::to_string ((*xx)->value);
394                 str << '\n';
395         }
396
397         /* XML is a bit wierd */
398
399         XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
400         content_node->set_content (str.str());
401
402         node->add_child_nocopy (*content_node);
403
404         return *node;
405 }
406
407 int
408 AutomationList::deserialize_events (const XMLNode& node)
409 {
410         if (node.children().empty()) {
411                 return -1;
412         }
413
414         XMLNode* content_node = node.children().front();
415
416         if (content_node->content().empty()) {
417                 return -1;
418         }
419
420         ControlList::freeze ();
421         clear ();
422
423         stringstream str (content_node->content());
424
425         std::string x_str;
426         std::string y_str;
427         double x;
428         double y;
429         bool ok = true;
430
431         while (str) {
432                 str >> x_str;
433                 if (!str || !PBD::string_to<double> (x_str, x)) {
434                         break;
435                 }
436                 str >> y_str;
437                 if (!str || !PBD::string_to<double> (y_str, y)) {
438                         ok = false;
439                         break;
440                 }
441                 y = std::min ((double)_desc.upper, std::max ((double)_desc.lower, y));
442                 fast_simple_add (x, y);
443         }
444
445         if (!ok) {
446                 clear ();
447                 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
448         } else {
449                 mark_dirty ();
450                 maybe_signal_changed ();
451         }
452
453         thaw ();
454
455         return 0;
456 }
457
458 int
459 AutomationList::set_state (const XMLNode& node, int version)
460 {
461         XMLNodeList nlist = node.children();
462         XMLNode* nsos;
463         XMLNodeIterator niter;
464
465         if (node.name() == X_("events")) {
466                 /* partial state setting*/
467                 return deserialize_events (node);
468         }
469
470         if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
471
472                 if ((nsos = node.child (X_("AutomationList")))) {
473                         /* new school in old school clothing */
474                         return set_state (*nsos, version);
475                 }
476
477                 /* old school */
478
479                 const XMLNodeList& elist = node.children();
480                 XMLNodeConstIterator i;
481
482                 ControlList::freeze ();
483                 clear ();
484
485                 for (i = elist.begin(); i != elist.end(); ++i) {
486
487                         pframes_t x;
488                         if (!(*i)->get_property ("x", x)) {
489                                 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
490                                 continue;
491                         }
492
493                         double y;
494                         if (!(*i)->get_property ("y", y)) {
495                                 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
496                                 continue;
497                         }
498
499                         y = std::min ((double)_desc.upper, std::max ((double)_desc.lower, y));
500                         fast_simple_add (x, y);
501                 }
502
503                 thaw ();
504
505                 return 0;
506         }
507
508         if (node.name() != X_("AutomationList") ) {
509                 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
510                 return -1;
511         }
512
513         if (set_id (node)) {
514                 /* update session AL list */
515                 AutomationListCreated(this);
516         }
517
518         std::string value;
519         if (node.get_property (X_("automation-id"), value)) {
520                 _parameter = EventTypeMap::instance().from_symbol(value);
521         } else {
522                 warning << "Legacy session: automation list has no automation-id property." << endmsg;
523         }
524
525         if (!node.get_property (X_("interpolation-style"), _interpolation)) {
526                 _interpolation = default_interpolation ();
527         }
528
529         if (node.get_property (X_("state"), _state)) {
530                 if (_state == Write) {
531                         _state = Off;
532                 }
533                 automation_state_changed (_state);
534         } else {
535                 _state = Off;
536         }
537
538         bool have_events = false;
539
540         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
541                 if ((*niter)->name() == X_("events")) {
542                         deserialize_events (*(*niter));
543                         have_events = true;
544                 }
545         }
546
547         if (!have_events) {
548                 /* there was no Events child node; clear any current events */
549                 freeze ();
550                 clear ();
551                 mark_dirty ();
552                 maybe_signal_changed ();
553                 thaw ();
554         }
555
556         return 0;
557 }
558
559 bool
560 AutomationList::operator!= (AutomationList const & other) const
561 {
562         return (
563                 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
564                 _state != other._state ||
565                 _touching != other._touching
566                 );
567 }
568
569 PBD::PropertyBase *
570 AutomationListProperty::clone () const
571 {
572         return new AutomationListProperty (
573                 this->property_id(),
574                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
575                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))
576                 );
577 }