56fd0e64097f4609c1a49a0b1e148124587a5427
[ardour.git] / libs / ardour / automation_control.cc
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 #include <math.h>
22 #include <iostream>
23
24 #include "pbd/memento_command.h"
25 #include "pbd/stacktrace.h"
26
27 #include "ardour/audioengine.h"
28 #include "ardour/automation_control.h"
29 #include "ardour/automation_watch.h"
30 #include "ardour/control_group.h"
31 #include "ardour/event_type_map.h"
32 #include "ardour/session.h"
33
34 #include "pbd/i18n.h"
35
36 #ifdef COMPILER_MSVC
37 #include <float.h>
38 // C99 'isfinite()' is not available in MSVC.
39 #define isfinite_local(val) (bool)_finite((double)val)
40 #else
41 #define isfinite_local isfinite
42 #endif
43
44 using namespace std;
45 using namespace ARDOUR;
46 using namespace PBD;
47
48 AutomationControl::AutomationControl(ARDOUR::Session&                          session,
49                                      const Evoral::Parameter&                  parameter,
50                                      const ParameterDescriptor&                desc,
51                                      boost::shared_ptr<ARDOUR::AutomationList> list,
52                                      const string&                             name,
53                                      Controllable::Flag                        flags)
54
55         : Controllable (name.empty() ? EventTypeMap::instance().to_symbol(parameter) : name, flags)
56         , Evoral::Control(parameter, desc, list)
57         , _session(session)
58         , _desc(desc)
59 {
60         if (_desc.toggled) {
61                 set_flags (Controllable::Toggle);
62         }
63 }
64
65 AutomationControl::~AutomationControl ()
66 {
67         DropReferences (); /* EMIT SIGNAL */
68 }
69
70 bool
71 AutomationControl::writable() const
72 {
73         boost::shared_ptr<AutomationList> al = alist();
74         if (al) {
75                 return al->automation_state() != Play;
76         }
77         return true;
78 }
79
80 /** Get the current effective `user' value based on automation state */
81 double
82 AutomationControl::get_value() const
83 {
84         bool from_list = _list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_playback();
85         return Control::get_double (from_list, _session.transport_frame());
86 }
87
88 void
89 AutomationControl::set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
90 {
91         if (!writable()) {
92                 return;
93         }
94
95         /* enforce strict double/boolean value mapping */
96
97         if (_desc.toggled) {
98                 if (val != 0.0) {
99                         val = 1.0;
100                 }
101         }
102
103         if (check_rt (val, gcd)) {
104                 /* change has been queued to take place in an RT context */
105                 return;
106         }
107
108         if (_group && _group->use_me (gcd)) {
109                 _group->set_group_value (shared_from_this(), val);
110         } else {
111                 actually_set_value (val, gcd);
112         }
113 }
114
115 /** Set the value and do the right thing based on automation state
116  *  (e.g. record if necessary, etc.)
117  *  @param value `user' value
118  */
119 void
120 AutomationControl::actually_set_value (double value, PBD::Controllable::GroupControlDisposition gcd)
121 {
122         bool to_list = _list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_write();
123         /* We cannot use ::get_value() here since that is virtual,
124            and its return value will/may depend on the ordering
125            of a derived class' own implementation of ::actually_set_value().
126
127            The derived classes generally need to set their own internal state
128            before calling this version, which means that ::get_value()
129            would generally return the *NEW* state, and thus lead to
130            Changed() not being emitted below.
131         */
132
133         const double old_value = Control::user_double ();
134
135         Control::set_double (value, _session.transport_frame(), to_list);
136
137         if (old_value != value) {
138                 // AutomationType at = (AutomationType) _parameter.type();
139                 // std::cerr << "++++ Changed (" << enum_2_string (at) << ", " << enum_2_string (gcd) << ") = " << value
140                 // << " (was " << old_value << ") @ " << this << std::endl;
141
142                 Changed (true, gcd);
143                 _session.set_dirty ();
144         }
145 }
146
147 void
148 AutomationControl::set_list (boost::shared_ptr<Evoral::ControlList> list)
149 {
150         Control::set_list (list);
151         Changed (true, Controllable::NoGroup);
152 }
153
154 void
155 AutomationControl::set_automation_state (AutoState as)
156 {
157         if (flags() & NotAutomatable) {
158                 return;
159         }
160         if (_list && as != alist()->automation_state()) {
161
162                 const double val = get_value ();
163
164                 alist()->set_automation_state (as);
165                 if (_desc.toggled) {
166                         return;  // No watch for boolean automation
167                 }
168
169                 if (as == Write) {
170                         AutomationWatch::instance().add_automation_watch (shared_from_this());
171                 } else if (as == Touch) {
172                         if (alist()->empty()) {
173                                 Control::set_double (val, _session.current_start_frame (), true);
174                                 Control::set_double (val, _session.current_end_frame (), true);
175                                 Changed (true, Controllable::NoGroup);
176                         }
177                         if (!touching()) {
178                                 AutomationWatch::instance().remove_automation_watch (shared_from_this());
179                         } else {
180                                 /* this seems unlikely, but the combination of
181                                  * a control surface and the mouse could make
182                                  * it possible to put the control into Touch
183                                  * mode *while* touching it.
184                                  */
185                                 AutomationWatch::instance().add_automation_watch (shared_from_this());
186                         }
187                 } else {
188                         AutomationWatch::instance().remove_automation_watch (shared_from_this());
189                 }
190         }
191 }
192
193 void
194 AutomationControl::set_automation_style (AutoStyle as)
195 {
196         if (!_list) return;
197         alist()->set_automation_style (as);
198 }
199
200 void
201 AutomationControl::start_touch(double when)
202 {
203         if (!_list) {
204                 return;
205         }
206
207         if (!touching()) {
208
209                 if (alist()->automation_state() == Touch) {
210                         /* subtle. aligns the user value with the playback */
211                         set_value (get_value (), Controllable::NoGroup);
212                         alist()->start_touch (when);
213                         if (!_desc.toggled) {
214                                 AutomationWatch::instance().add_automation_watch (shared_from_this());
215                         }
216                 }
217                 set_touching (true);
218         }
219 }
220
221 void
222 AutomationControl::stop_touch(bool mark, double when)
223 {
224         if (!_list) return;
225         if (touching()) {
226                 set_touching (false);
227
228                 if (alist()->automation_state() == Touch) {
229                         alist()->stop_touch (mark, when);
230                         if (!_desc.toggled) {
231                                 AutomationWatch::instance().remove_automation_watch (shared_from_this());
232
233                         }
234                 }
235         }
236 }
237
238 void
239 AutomationControl::commit_transaction (bool did_write)
240 {
241         if (did_write) {
242                 if (alist ()->before ()) {
243                         _session.begin_reversible_command (string_compose (_("record %1 automation"), name ()));
244                         _session.commit_reversible_command (new MementoCommand<AutomationList> (*alist ().get (), alist ()->before (), &alist ()->get_state ()));
245                 }
246         } else {
247                 alist ()->clear_history ();
248         }
249 }
250
251 double
252 AutomationControl::internal_to_interface (double val) const
253 {
254         if (_desc.integer_step) {
255                 // both upper and lower are inclusive.
256                 val =  (val - lower()) / (1 + upper() - lower());
257         } else {
258                 val =  (val - lower()) / (upper() - lower());
259         }
260
261         if (_desc.logarithmic) {
262                 if (val > 0) {
263                         val = pow (val, 1./2.0);
264                 } else {
265                         val = 0;
266                 }
267         }
268
269         return val;
270 }
271
272 double
273 AutomationControl::interface_to_internal (double val) const
274 {
275         if (!isfinite_local (val)) {
276                 val = 0;
277         }
278         if (_desc.logarithmic) {
279                 if (val <= 0) {
280                         val = 0;
281                 } else {
282                         val = pow (val, 2.0);
283                 }
284         }
285
286         if (_desc.integer_step) {
287                 val =  lower() + val * (1 + upper() - lower());
288         } else {
289                 val =  lower() + val * (upper() - lower());
290         }
291
292         if (val < lower()) val = lower();
293         if (val > upper()) val = upper();
294
295         return val;
296 }
297
298 void
299 AutomationControl::set_group (boost::shared_ptr<ControlGroup> cg)
300 {
301         /* this method can only be called by a ControlGroup. We do not need
302            to ensure consistency by calling ControlGroup::remove_control(),
303            since we are guaranteed that the ControlGroup will take care of that
304            for us.
305         */
306
307         _group = cg;
308 }
309
310 bool
311 AutomationControl::check_rt (double val, Controllable::GroupControlDisposition gcd)
312 {
313         if (!_session.loading() && (flags() & Controllable::RealTime) && !AudioEngine::instance()->in_process_thread()) {
314                 /* queue change in RT context */
315                 _session.set_control (shared_from_this(), val, gcd);
316                 return true;
317         }
318
319         return false;
320 }