2 Copyright (C) 2016 Paul Davis
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 675 Mass Ave, Cambridge, MA 02139, USA.
19 #ifndef __libardour_slavable_automation_control_h__
20 #define __libardour_slavable_automation_control_h__
22 #include "pbd/enumwriter.h"
23 #include "pbd/error.h"
24 #include "pbd/memento_command.h"
25 #include "pbd/types_convert.h"
28 #include "evoral/Curve.hpp"
30 #include "ardour/audioengine.h"
31 #include "ardour/runtime_functions.h"
32 #include "ardour/slavable_automation_control.h"
33 #include "ardour/session.h"
36 using namespace ARDOUR;
39 SlavableAutomationControl::SlavableAutomationControl(ARDOUR::Session& s,
40 const Evoral::Parameter& parameter,
41 const ParameterDescriptor& desc,
42 boost::shared_ptr<ARDOUR::AutomationList> l,
43 const std::string& name,
44 Controllable::Flag flags)
45 : AutomationControl (s, parameter, desc, l, name, flags)
50 SlavableAutomationControl::~SlavableAutomationControl ()
59 SlavableAutomationControl::get_masters_value_locked () const
62 for (Masters::const_iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
63 if (mr->second.master()->get_value()) {
70 double v = 1.0; /* the masters function as a scaling factor */
72 for (Masters::const_iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
73 v *= mr->second.master_ratio ();
81 SlavableAutomationControl::get_value_locked() const
83 /* read or write masters lock must be held */
85 if (_masters.empty()) {
86 return Control::get_double (false, _session.transport_frame());
90 /* for boolean/toggle controls, if this slave OR any master is
91 * enabled, this slave is enabled. So check our own value
92 * first, because if we are enabled, we can return immediately.
94 if (Control::get_double (false, _session.transport_frame())) {
99 return Control::get_double() * get_masters_value_locked ();
102 /** Get the current effective `user' value based on automation state */
104 SlavableAutomationControl::get_value() const
106 bool from_list = _list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_playback();
108 Glib::Threads::RWLock::ReaderLock lm (master_lock);
110 if (!_masters.empty() && automation_write ()) {
111 /* writing automation takes the fader value as-is, factor out the master */
112 return Control::user_double ();
114 return get_value_locked ();
116 return Control::get_double (true, _session.transport_frame()) * get_masters_value_locked();
121 SlavableAutomationControl::get_masters_curve_locked (framepos_t, framepos_t, float*, framecnt_t) const
123 /* Every AutomationControl needs to implement this as-needed.
125 * This class also provides some convenient methods which
126 * could be used as defaults here (depending on AutomationType)
127 * e.g. masters_curve_multiply()
133 SlavableAutomationControl::masters_curve_multiply (framepos_t start, framepos_t end, float* vec, framecnt_t veclen) const
135 gain_t* scratch = _session.scratch_automation_buffer ();
136 bool from_list = _list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_playback();
137 bool rv = from_list && list()->curve().rt_safe_get_vector (start, end, scratch, veclen);
139 for (framecnt_t i = 0; i < veclen; ++i) {
140 vec[i] *= scratch[i];
143 apply_gain_to_buffer (vec, veclen, Control::get_double ());
145 if (_masters.empty()) {
149 for (Masters::const_iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
150 boost::shared_ptr<SlavableAutomationControl> sc
151 = boost::dynamic_pointer_cast<SlavableAutomationControl>(mr->second.master());
153 rv |= sc->masters_curve_multiply (start, end, vec, veclen);
154 apply_gain_to_buffer (vec, veclen, mr->second.val_master_inv ());
160 SlavableAutomationControl::reduce_by_masters_locked (double value, bool ignore_automation_state) const
162 if (!_desc.toggled) {
163 Glib::Threads::RWLock::ReaderLock lm (master_lock);
164 if (!_masters.empty() && (ignore_automation_state || !automation_write ())) {
165 /* need to scale given value by current master's scaling */
166 const double masters_value = get_masters_value_locked();
167 if (masters_value == 0.0) {
170 value /= masters_value;
171 value = std::max (lower(), std::min(upper(), value));
179 SlavableAutomationControl::actually_set_value (double value, PBD::Controllable::GroupControlDisposition gcd)
181 value = reduce_by_masters (value);
182 /* this will call Control::set_double() and emit Changed signals as appropriate */
183 AutomationControl::actually_set_value (value, gcd);
187 SlavableAutomationControl::add_master (boost::shared_ptr<AutomationControl> m)
189 std::pair<Masters::iterator,bool> res;
192 const double master_value = m->get_value();
193 Glib::Threads::RWLock::WriterLock lm (master_lock);
195 pair<PBD::ID,MasterRecord> newpair (m->id(), MasterRecord (boost::weak_ptr<AutomationControl> (m), get_value_locked(), master_value));
196 res = _masters.insert (newpair);
200 /* note that we bind @param m as a weak_ptr<AutomationControl>, thus
201 avoiding holding a reference to the control in the binding
204 m->DropReferences.connect_same_thread (res.first->second.dropped_connection, boost::bind (&SlavableAutomationControl::master_going_away, this, boost::weak_ptr<AutomationControl>(m)));
206 /* Store the connection inside the MasterRecord, so
207 that when we destroy it, the connection is destroyed
208 and we no longer hear about changes to the
211 Note that this also makes it safe to store a
212 boost::shared_ptr<AutomationControl> in the functor,
213 since we know we will destroy the functor when the
214 connection is destroyed, which happens when we
215 disconnect from the master (for any reason).
217 Note that we fix the "from_self" argument that will
218 be given to our own Changed signal to "false",
219 because the change came from the master.
222 m->Changed.connect_same_thread (res.first->second.changed_connection, boost::bind (&SlavableAutomationControl::master_changed, this, _1, _2, boost::weak_ptr<AutomationControl>(m)));
227 /* this will notify everyone that we're now slaved to the master */
228 MasterStatusChange (); /* EMIT SIGNAL */
233 update_boolean_masters_records (m);
237 SlavableAutomationControl::get_boolean_masters () const
242 Glib::Threads::RWLock::ReaderLock lm (master_lock);
243 for (Masters::const_iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
244 if (mr->second.yn()) {
254 SlavableAutomationControl::update_boolean_masters_records (boost::shared_ptr<AutomationControl> m)
257 /* We may modify a MasterRecord, but we not modify the master
258 * map, so we use a ReaderLock
260 Glib::Threads::RWLock::ReaderLock lm (master_lock);
261 Masters::iterator mi = _masters.find (m->id());
262 if (mi != _masters.end()) {
263 /* update MasterRecord to show whether the master is
264 on/off. We need to store this because the master
265 may change (in the sense of emitting Changed())
266 several times without actually changing the result
267 of ::get_value(). This is a feature of
268 AutomationControls (or even just Controllables,
269 really) which have more than a simple scalar
270 value. For example, the master may be a mute control
271 which can be muted_by_self() and/or
272 muted_by_masters(). When either of those two
273 conditions changes, Changed() will be emitted, even
274 though ::get_value() will return the same value each
275 time (1.0 if either are true, 0.0 if neither is).
277 This provides a way for derived types to check
278 the last known state of a Master when the Master
279 changes. We update it after calling
280 ::master_changed() (though derived types must do
283 mi->second.set_yn (m->get_value());
289 SlavableAutomationControl::master_changed (bool /*from_self*/, GroupControlDisposition gcd, boost::weak_ptr<AutomationControl> wm)
291 boost::shared_ptr<AutomationControl> m = wm.lock ();
293 Glib::Threads::RWLock::ReaderLock lm (master_lock);
294 bool send_signal = handle_master_change (m);
295 lm.release (); // update_boolean_masters_records() takes lock
297 update_boolean_masters_records (m);
299 Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
304 SlavableAutomationControl::master_going_away (boost::weak_ptr<AutomationControl> wm)
306 boost::shared_ptr<AutomationControl> m = wm.lock();
313 SlavableAutomationControl::scale_automation_callback (double value, double ratio) const
315 /* derived classes can override this and e.g. add/subtract. */
317 value = std::max (lower(), std::min(upper(), value));
322 SlavableAutomationControl::remove_master (boost::shared_ptr<AutomationControl> m)
324 if (_session.deletion_in_progress()) {
325 /* no reason to care about new values or sending signals */
329 pre_remove_master (m);
331 const double old_val = AutomationControl::get_double();
333 bool update_value = false;
334 double master_ratio = 0;
335 double list_ratio = 1;
337 boost::shared_ptr<AutomationControl> master;
340 Glib::Threads::RWLock::WriterLock lm (master_lock);
342 Masters::const_iterator mi = _masters.find (m->id ());
344 if (mi != _masters.end()) {
345 master_ratio = mi->second.master_ratio ();
347 master = mi->second.master();
348 list_ratio *= mi->second.val_master_inv ();
351 if (!_masters.erase (m->id())) {
357 /* when un-assigning we apply the master-value permanently */
358 double new_val = old_val * master_ratio;
360 if (old_val != new_val) {
361 AutomationControl::set_double (new_val, Controllable::NoGroup);
364 /* ..and update automation */
366 XMLNode* before = &alist ()->get_state ();
367 if (master->automation_playback () && master->list()) {
368 _list->list_merge (*master->list().get(), boost::bind (&SlavableAutomationControl::scale_automation_callback, this, _1, _2));
369 _list->y_transform (boost::bind (&SlavableAutomationControl::scale_automation_callback, this, _1, list_ratio));
371 // do we need to freeze/thaw the list? probably no: iterators & positions don't change
372 _list->y_transform (boost::bind (&SlavableAutomationControl::scale_automation_callback, this, _1, master_ratio));
374 XMLNode* after = &alist ()->get_state ();
375 if (*before != *after) {
376 _session.begin_reversible_command (string_compose (_("Merge VCA automation into %1"), name ()));
377 _session.commit_reversible_command (alist()->memento_command (before, after));
382 MasterStatusChange (); /* EMIT SIGNAL */
384 /* no need to update boolean masters records, since the MR will have
385 * been removed already.
390 SlavableAutomationControl::clear_masters ()
392 if (_session.deletion_in_progress()) {
393 /* no reason to care about new values or sending signals */
397 const double old_val = AutomationControl::get_double();
400 bool update_value = false;
401 double master_ratio = 0;
402 double list_ratio = 1;
404 /* null ptr means "all masters */
405 pre_remove_master (boost::shared_ptr<AutomationControl>());
408 Glib::Threads::RWLock::WriterLock lm (master_lock);
409 if (_masters.empty()) {
413 for (Masters::const_iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
414 boost::shared_ptr<AutomationControl> master = mr->second.master();
415 if (master->automation_playback () && master->list()) {
416 masters.push_back (mr->second.master());
417 list_ratio *= mr->second.val_master_inv ();
419 list_ratio *= mr->second.master_ratio ();
423 master_ratio = get_masters_value_locked ();
429 /* permanently apply masters value */
430 double new_val = old_val * master_ratio;
432 if (old_val != new_val) {
433 AutomationControl::set_double (new_val, Controllable::NoGroup);
436 /* ..and update automation */
438 XMLNode* before = &alist ()->get_state ();
439 if (!masters.empty()) {
440 for (ControlList::const_iterator m = masters.begin(); m != masters.end(); ++m) {
441 _list->list_merge (*(*m)->list().get(), boost::bind (&SlavableAutomationControl::scale_automation_callback, this, _1, _2));
443 _list->y_transform (boost::bind (&SlavableAutomationControl::scale_automation_callback, this, _1, list_ratio));
445 _list->y_transform (boost::bind (&SlavableAutomationControl::scale_automation_callback, this, _1, master_ratio));
447 XMLNode* after = &alist ()->get_state ();
448 if (*before != *after) {
449 _session.begin_reversible_command (string_compose (_("Merge VCA automation into %1"), name ()));
450 _session.commit_reversible_command (alist()->memento_command (before, after));
455 MasterStatusChange (); /* EMIT SIGNAL */
457 /* no need to update boolean masters records, since all MRs will have
458 * been removed already.
463 SlavableAutomationControl::find_next_event_locked (double now, double end, Evoral::ControlEvent& next_event) const
465 if (_masters.empty()) {
469 /* iterate over all masters check their automation lists
470 * for any event between "now" and "end" which is earlier than
471 * next_event.when. If found, set next_event.when and return true.
472 * (see also Automatable::find_next_event)
474 for (Masters::const_iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
475 boost::shared_ptr<AutomationControl> ac (mr->second.master());
477 boost::shared_ptr<SlavableAutomationControl> sc
478 = boost::dynamic_pointer_cast<SlavableAutomationControl>(ac);
480 if (sc && sc->find_next_event_locked (now, end, next_event)) {
484 Evoral::ControlList::const_iterator i;
485 boost::shared_ptr<const Evoral::ControlList> alist (ac->list());
486 Evoral::ControlEvent cp (now, 0.0f);
491 for (i = lower_bound (alist->begin(), alist->end(), &cp, Evoral::ControlList::time_comparator);
492 i != alist->end() && (*i)->when < end; ++i) {
493 if ((*i)->when > now) {
498 if (i != alist->end() && (*i)->when < end) {
499 if ((*i)->when < next_event.when) {
500 next_event.when = (*i)->when;
510 SlavableAutomationControl::handle_master_change (boost::shared_ptr<AutomationControl>)
512 /* Derived classes can implement this for special cases (e.g. mute).
513 * This method is called with a ReaderLock (master_lock) held.
515 * return true if the changed master value resulted
516 * in a change of the control itself. */
517 return true; // emit Changed
521 SlavableAutomationControl::automation_run (framepos_t start, pframes_t nframes)
523 if (!automation_playback ()) {
529 double val = _list->rt_safe_eval (start, valid);
534 const double thresh = .5 * (_desc.upper - _desc.lower);
535 bool on = (val >= thresh) || (get_masters_value () >= thresh);
536 set_value_unchecked (on ? _desc.upper : _desc.lower);
538 set_value_unchecked (val * get_masters_value ());
543 SlavableAutomationControl::boolean_automation_run_locked (framepos_t start, pframes_t len)
546 if (!_desc.toggled) {
549 for (Masters::iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
550 boost::shared_ptr<AutomationControl> ac (mr->second.master());
551 if (!ac->automation_playback ()) {
554 if (!ac->toggled ()) {
557 boost::shared_ptr<SlavableAutomationControl> sc = boost::dynamic_pointer_cast<MuteControl>(ac);
559 rv |= sc->boolean_automation_run (start, len);
561 boost::shared_ptr<const Evoral::ControlList> alist (ac->list());
563 const bool yn = alist->rt_safe_eval (start, valid) >= 0.5;
567 /* ideally we'd call just master_changed() which calls update_boolean_masters_records()
568 * but that takes the master_lock, which is already locked */
569 if (mr->second.yn() != yn) {
570 rv |= handle_master_change (ac);
571 mr->second.set_yn (yn);
578 SlavableAutomationControl::boolean_automation_run (framepos_t start, pframes_t len)
582 Glib::Threads::RWLock::ReaderLock lm (master_lock);
583 change = boolean_automation_run_locked (start, len);
586 Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
592 SlavableAutomationControl::slaved_to (boost::shared_ptr<AutomationControl> m) const
594 Glib::Threads::RWLock::ReaderLock lm (master_lock);
595 return _masters.find (m->id()) != _masters.end();
599 SlavableAutomationControl::slaved () const
601 Glib::Threads::RWLock::ReaderLock lm (master_lock);
602 return !_masters.empty();
606 SlavableAutomationControl::MasterRecord::set_state (XMLNode const& n, int)
608 n.get_property (X_("yn"), _yn);
609 n.get_property (X_("val-ctrl"), _val_ctrl);
610 n.get_property (X_("val-master"), _val_master);
615 SlavableAutomationControl::use_saved_master_ratios ()
617 if (!_masters_node) {
621 Glib::Threads::RWLock::ReaderLock lm (master_lock);
623 XMLNodeList nlist = _masters_node->children();
624 XMLNodeIterator niter;
626 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
628 if (!(*niter)->get_property (X_("id"), id_val)) {
631 Masters::iterator mi = _masters.find (id_val);
632 if (mi == _masters.end()) {
635 mi->second.set_state (**niter, Stateful::loading_state_version);
638 delete _masters_node;
646 SlavableAutomationControl::get_state ()
648 XMLNode& node (AutomationControl::get_state());
650 /* store VCA master ratios */
653 Glib::Threads::RWLock::ReaderLock lm (master_lock);
654 if (!_masters.empty()) {
655 XMLNode* masters_node = new XMLNode (X_("masters"));
656 for (Masters::iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
657 XMLNode* mnode = new XMLNode (X_("master"));
658 mnode->set_property (X_("id"), mr->second.master()->id());
661 mnode->set_property (X_("yn"), mr->second.yn());
663 mnode->set_property (X_("val-ctrl"), mr->second.val_ctrl());
664 mnode->set_property (X_("val-master"), mr->second.val_master());
666 masters_node->add_child_nocopy (*mnode);
668 node.add_child_nocopy (*masters_node);
676 SlavableAutomationControl::set_state (XMLNode const& node, int version)
678 XMLNodeList nlist = node.children();
679 XMLNodeIterator niter;
681 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
682 if ((*niter)->name() == X_("masters")) {
683 _masters_node = new XMLNode (**niter);
687 return AutomationControl::set_state (node, version);
691 #endif /* __libardour_slavable_automation_control_h__ */