98745e025d091a317d3461db5a5cc957e6800739
[ardour.git] / libs / ardour / ardour / slavable_automation_control.h
1 /*
2     Copyright (C) 2016 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 #ifndef __ardour_slavable_automation_control_h__
21 #define __ardour_slavable_automation_control_h__
22
23 #include "ardour/automation_control.h"
24
25 namespace ARDOUR {
26
27 class SlavableAutomationControl : public AutomationControl
28 {
29     public:
30         SlavableAutomationControl(ARDOUR::Session&,
31                           const Evoral::Parameter&                  parameter,
32                           const ParameterDescriptor&                desc,
33                           boost::shared_ptr<ARDOUR::AutomationList> l=boost::shared_ptr<ARDOUR::AutomationList>(),
34                           const std::string&                        name="");
35
36         double get_value () const;
37
38         void add_master (boost::shared_ptr<AutomationControl>);
39         void remove_master (boost::shared_ptr<AutomationControl>);
40         void clear_masters ();
41         bool slaved_to (boost::shared_ptr<AutomationControl>) const;
42         bool slaved () const;
43         double get_masters_value () const {
44                 Glib::Threads::RWLock::ReaderLock lm (master_lock);
45                 return get_masters_value_locked ();
46         }
47
48         /* for toggled/boolean controls, returns a count of the number of
49            masters currently enabled. For other controls, returns zero.
50         */
51         int32_t   get_boolean_masters () const;
52
53         std::vector<PBD::ID> masters () const;
54
55         PBD::Signal0<void> MasterStatusChange;
56
57     protected:
58
59         class MasterRecord {
60           public:
61                 MasterRecord (boost::shared_ptr<AutomationControl> gc, double r)
62                         : _master (gc)
63                         , _ratio (r)
64                 {}
65
66                 boost::shared_ptr<AutomationControl> master() const { return _master; }
67
68                 /* for boolean/toggled controls, we store a boolean value to
69                  * indicate if this master returned true/false (1.0/0.0) from
70                  * ::get_value() after its most recent change.
71                  */
72
73                 bool yn() const { return _yn; }
74                 void set_yn (bool yn) { _yn = yn; }
75
76                 /* for non-boolean/non-toggled controls, we store a ratio that
77                  * connects the value of the master with the value of this
78                  * slave. See comments in the source for more details on how
79                  * this is computed and used.
80                  */
81
82                 double ratio () const { return _ratio; }
83                 void reset_ratio (double r) { _ratio = r; }
84
85                 PBD::ScopedConnection connection;
86
87          private:
88                 boost::shared_ptr<AutomationControl> _master;
89                 union {
90                         double _ratio;
91                         bool   _yn;
92                 };
93         };
94
95         mutable Glib::Threads::RWLock master_lock;
96         typedef std::map<PBD::ID,MasterRecord> Masters;
97         Masters _masters;
98         PBD::ScopedConnectionList masters_connections;
99
100         void   master_going_away (boost::weak_ptr<AutomationControl>);
101         double get_value_locked() const;
102         void   actually_set_value (double val, PBD::Controllable::GroupControlDisposition group_override);
103         void   update_boolean_masters_records (boost::shared_ptr<AutomationControl>);
104
105         virtual void   master_changed (bool from_self, GroupControlDisposition gcd, boost::shared_ptr<AutomationControl>);
106         virtual void   recompute_masters_ratios (double val) { /* do nothing by default */}
107         virtual double get_masters_value_locked () const;
108         virtual void   pre_remove_master (boost::shared_ptr<AutomationControl>) {}
109         virtual void   post_add_master (boost::shared_ptr<AutomationControl>) {}
110
111
112 };
113
114 } // namespace ARDOUR
115
116 #endif /* __ardour_slavable_automation_control_h__ */