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