d84f0a091fc8573af4922fc99c06faee074b5290
[ardour.git] / libs / ardour / ardour / automation_control.h
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __ardour_automation_control_h__
22 #define __ardour_automation_control_h__
23
24 #include <map>
25
26 #include <glibmm/threads.h>
27
28 #include <boost/shared_ptr.hpp>
29 #include <boost/enable_shared_from_this.hpp>
30
31 #include "pbd/controllable.h"
32
33 #include "evoral/types.hpp"
34 #include "evoral/Control.hpp"
35
36 #include "ardour/libardour_visibility.h"
37 #include "ardour/automation_list.h"
38 #include "ardour/parameter_descriptor.h"
39
40 namespace ARDOUR {
41
42 class Session;
43 class Automatable;
44 class ControlGroup;
45
46 /** A PBD::Controllable with associated automation data (AutomationList)
47  */
48 class LIBARDOUR_API AutomationControl
49         : public PBD::Controllable
50         , public Evoral::Control
51         , public boost::enable_shared_from_this<AutomationControl>
52 {
53     public:
54         AutomationControl(ARDOUR::Session&,
55                           const Evoral::Parameter&                  parameter,
56                           const ParameterDescriptor&                desc,
57                           boost::shared_ptr<ARDOUR::AutomationList> l=boost::shared_ptr<ARDOUR::AutomationList>(),
58                           const std::string&                        name="");
59
60         ~AutomationControl ();
61
62         boost::shared_ptr<AutomationList> alist() const {
63                 return boost::dynamic_pointer_cast<AutomationList>(_list);
64         }
65
66         void set_list (boost::shared_ptr<Evoral::ControlList>);
67
68         inline bool automation_playback() const {
69                 return alist() ? alist()->automation_playback() : false;
70         }
71
72         inline bool automation_write() const {
73                 return alist() ? alist()->automation_write() : false;
74         }
75
76         inline AutoState automation_state() const {
77                 return alist() ? alist()->automation_state() : Off;
78         }
79
80         inline AutoStyle automation_style() const {
81                 return alist() ? alist()->automation_style() : Absolute;
82         }
83
84         void set_automation_state(AutoState as);
85         void set_automation_style(AutoStyle as);
86         void start_touch(double when);
87         void stop_touch(bool mark, double when);
88
89         /* inherited from PBD::Controllable.
90          */
91         double get_value () const;
92         /* inherited from PBD::Controllable.
93          * Derived classes MUST call ::writable() to verify
94          * that writing to the parameter is legal at that time.
95          */
96         void set_value (double value, PBD::Controllable::GroupControlDisposition group_override);
97         /* automation related value setting */
98         virtual bool writable () const;
99         /* Call to ::set_value() with no test for writable() because
100          * this is only used by automation playback.
101          */
102         void set_value_unchecked (double val) {
103                 actually_set_value (val, PBD::Controllable::NoGroup);
104         }
105
106         double lower()   const { return _desc.lower; }
107         double upper()   const { return _desc.upper; }
108         double normal()  const { return _desc.normal; }
109         bool   toggled() const { return _desc.toggled; }
110
111         double internal_to_interface (double i) const;
112         double interface_to_internal (double i) const;
113
114         const ParameterDescriptor& desc() const { return _desc; }
115
116         const ARDOUR::Session& session() const { return _session; }
117         void commit_transaction (bool did_write);
118
119         void set_group (boost::shared_ptr<ControlGroup>);
120
121   protected:
122         ARDOUR::Session& _session;
123         boost::shared_ptr<ControlGroup> _group;
124
125         const ParameterDescriptor _desc;
126
127         bool check_rt (double val, Controllable::GroupControlDisposition gcd);
128
129         /* derived classes may reimplement this, but should either
130            call this explicitly inside their version OR make sure that the
131            Controllable::Changed signal is emitted when necessary.
132         */
133
134         virtual void actually_set_value (double value, PBD::Controllable::GroupControlDisposition);
135 };
136
137 class SlavableAutomationControl : public AutomationControl
138 {
139     public:
140         SlavableAutomationControl(ARDOUR::Session&,
141                           const Evoral::Parameter&                  parameter,
142                           const ParameterDescriptor&                desc,
143                           boost::shared_ptr<ARDOUR::AutomationList> l=boost::shared_ptr<ARDOUR::AutomationList>(),
144                           const std::string&                        name="");
145
146         ~SlavableAutomationControl ();
147
148         double get_value () const;
149
150         void add_master (boost::shared_ptr<AutomationControl>);
151         void remove_master (boost::shared_ptr<AutomationControl>);
152         void clear_masters ();
153         bool slaved_to (boost::shared_ptr<AutomationControl>) const;
154         bool slaved () const;
155         std::vector<PBD::ID> masters () const;
156
157         PBD::Signal0<void> MasterStatusChange;
158
159     protected:
160
161         class MasterRecord {
162           public:
163                 MasterRecord (boost::shared_ptr<AutomationControl> gc, double r)
164                         : _master (gc)
165                         , _ratio (r)
166                 {}
167
168                 boost::shared_ptr<AutomationControl> master() const { return _master; }
169                 double ratio () const { return _ratio; }
170                 void reset_ratio (double r) { _ratio = r; }
171
172                 PBD::ScopedConnection connection;
173
174          private:
175                 boost::shared_ptr<AutomationControl> _master;
176                 double _ratio;
177
178         };
179
180         mutable Glib::Threads::RWLock master_lock;
181         typedef std::map<PBD::ID,MasterRecord> Masters;
182         Masters _masters;
183         PBD::ScopedConnectionList masters_connections;
184         virtual void master_changed (bool from_self, GroupControlDisposition gcd);
185         void master_going_away (boost::weak_ptr<AutomationControl>);
186         virtual void recompute_masters_ratios (double val) { /* do nothing by default */}
187         virtual double get_masters_value_locked () const;
188         double get_value_locked() const;
189
190 };
191
192
193 } // namespace ARDOUR
194
195 #endif /* __ardour_automation_control_h__ */