245c70b915ffed83c9633c84ae83fb7e43eeb2d3
[ardour.git] / libs / ardour / ardour / automation_control.h
1 /*
2  * Copyright (C) 2007-2015 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015 Nick Mainsbridge <mainsbridge@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifndef __ardour_automation_control_h__
24 #define __ardour_automation_control_h__
25
26 #include <map>
27
28 #include <glibmm/threads.h>
29
30 #include <boost/shared_ptr.hpp>
31
32 #include "pbd/controllable.h"
33
34 #include "evoral/types.hpp"
35 #include "evoral/Control.hpp"
36
37 #include "ardour/automation_list.h"
38 #include "ardour/control_group_member.h"
39 #include "ardour/parameter_descriptor.h"
40 #include "ardour/session_handle.h"
41
42 #include "ardour/libardour_visibility.h"
43
44 namespace ARDOUR {
45
46 class Session;
47 class Automatable;
48 class ControlGroup;
49
50 /** A PBD::Controllable with associated automation data (AutomationList)
51  */
52 class LIBARDOUR_API AutomationControl
53         : public PBD::Controllable
54         , public Evoral::Control
55         , public ControlGroupMember
56         , public SessionHandleRef
57 {
58 public:
59         AutomationControl(ARDOUR::Session&,
60                           const Evoral::Parameter&                  parameter,
61                           const ParameterDescriptor&                desc,
62                           boost::shared_ptr<ARDOUR::AutomationList> l=boost::shared_ptr<ARDOUR::AutomationList>(),
63                           const std::string&                        name="",
64                           PBD::Controllable::Flag                   flags=PBD::Controllable::Flag (0)
65                 );
66
67         virtual ~AutomationControl ();
68
69         boost::shared_ptr<AutomationList> alist() const {
70                 return boost::dynamic_pointer_cast<AutomationList>(_list);
71         }
72
73         void set_list (boost::shared_ptr<Evoral::ControlList>);
74
75         inline bool automation_playback() const {
76                 return alist() ? alist()->automation_playback() : false;
77         }
78
79         inline bool automation_write() const {
80                 return alist() ? alist()->automation_write() : false;
81         }
82
83         inline AutoState automation_state() const {
84                 return alist() ? alist()->automation_state() : Off;
85         }
86
87         void set_automation_state(AutoState as);
88         void start_touch(double when);
89         void stop_touch(double when);
90
91         /* inherited from PBD::Controllable. */
92         virtual double get_value () const;
93         virtual double get_save_value () const;
94
95         /* inherited from PBD::Controllable.
96          * Derived classes MUST call ::writable() to verify
97          * that writing to the parameter is legal at that time.
98          */
99         void set_value (double value, PBD::Controllable::GroupControlDisposition group_override);
100         /* automation related value setting */
101         virtual bool writable () const;
102         /* Call to ::set_value() with no test for writable() because
103          * this is only used by automation playback.
104          */
105         void set_value_unchecked (double val) {
106                 actually_set_value (val, PBD::Controllable::NoGroup);
107         }
108
109         virtual void automation_run (samplepos_t start, pframes_t nframes);
110
111         double lower()   const { return _desc.lower; }
112         double upper()   const { return _desc.upper; }
113         double normal()  const { return _desc.normal; }
114         bool   toggled() const { return _desc.toggled; }
115
116         double internal_to_interface (double i) const;
117         double interface_to_internal (double i) const;
118
119         virtual std::string get_user_string() const;
120
121         const ParameterDescriptor& desc() const { return _desc; }
122
123         const ARDOUR::Session& session() const { return _session; }
124         void commit_transaction (bool did_write);
125
126         ControlList grouped_controls () const;
127
128 protected:
129         boost::shared_ptr<ControlGroup> _group;
130
131         const ParameterDescriptor _desc;
132
133         bool check_rt (double val, Controllable::GroupControlDisposition gcd);
134
135         /* derived classes may reimplement this, but should either
136            call this explicitly inside their version OR make sure that the
137            Controllable::Changed signal is emitted when necessary.
138         */
139
140         virtual void actually_set_value (double value, PBD::Controllable::GroupControlDisposition);
141
142         /* Session needs to call this method before it queues up the real
143            change for execution in a realtime context. C++ access control sucks.
144         */
145         friend class Session;
146         /* this is what the session invokes */
147         void pre_realtime_queue_stuff (double new_value, PBD::Controllable::GroupControlDisposition);
148         /* this will be invoked in turn on behalf of the group or the control by itself */
149         virtual void do_pre_realtime_queue_stuff (double new_value) {}
150
151         void session_going_away ();
152
153 private:
154         /* I am unclear on why we have to make ControlGroup a friend in order
155            to get access to the ::set_group() method when it is already
156            declared to be a friend in ControlGroupMember. Oh well.
157         */
158         friend class ControlGroup;
159         void set_group (boost::shared_ptr<ControlGroup>);
160         PBD::ScopedConnection _state_changed_connection;
161         bool _no_session;
162 };
163
164
165 } // namespace ARDOUR
166
167 #endif /* __ardour_automation_control_h__ */