change return type of AutomationControl::actually_set_value() from void to bool,...
[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 bool
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         const double old_value = get_value ();
124
125         Control::set_double (value, _session.transport_frame(), to_list);
126
127         //AutomationType at = (AutomationType) _parameter.type();
128         //std::cerr << "++++ Changed (" << enum_2_string (at) << ", " << enum_2_string (gcd) << ") = " << value
129         //<< " (was " << old_value << ") @ " << this << std::endl;
130
131         if (old_value != value) {
132                 Changed (true, gcd);
133                 return true;
134         }
135
136         return false;
137 }
138
139 void
140 AutomationControl::set_list (boost::shared_ptr<Evoral::ControlList> list)
141 {
142         Control::set_list (list);
143         Changed (true, Controllable::NoGroup);
144 }
145
146 void
147 AutomationControl::set_automation_state (AutoState as)
148 {
149         if (flags() & NotAutomatable) {
150                 return;
151         }
152         if (_list && as != alist()->automation_state()) {
153
154                 const double val = get_value ();
155
156                 alist()->set_automation_state (as);
157                 if (_desc.toggled) {
158                         return;  // No watch for boolean automation
159                 }
160
161                 if (as == Write) {
162                         AutomationWatch::instance().add_automation_watch (shared_from_this());
163                 } else if (as == Touch) {
164                         if (alist()->empty()) {
165                                 Control::set_double (val, _session.current_start_frame (), true);
166                                 Control::set_double (val, _session.current_end_frame (), true);
167                                 Changed (true, Controllable::NoGroup);
168                         }
169                         if (!touching()) {
170                                 AutomationWatch::instance().remove_automation_watch (shared_from_this());
171                         } else {
172                                 /* this seems unlikely, but the combination of
173                                  * a control surface and the mouse could make
174                                  * it possible to put the control into Touch
175                                  * mode *while* touching it.
176                                  */
177                                 AutomationWatch::instance().add_automation_watch (shared_from_this());
178                         }
179                 } else {
180                         AutomationWatch::instance().remove_automation_watch (shared_from_this());
181                 }
182         }
183 }
184
185 void
186 AutomationControl::set_automation_style (AutoStyle as)
187 {
188         if (!_list) return;
189         alist()->set_automation_style (as);
190 }
191
192 void
193 AutomationControl::start_touch(double when)
194 {
195         if (!_list) {
196                 return;
197         }
198
199         if (!touching()) {
200
201                 if (alist()->automation_state() == Touch) {
202                         /* subtle. aligns the user value with the playback */
203                         set_value (get_value (), Controllable::NoGroup);
204                         alist()->start_touch (when);
205                         if (!_desc.toggled) {
206                                 AutomationWatch::instance().add_automation_watch (shared_from_this());
207                         }
208                 }
209                 set_touching (true);
210         }
211 }
212
213 void
214 AutomationControl::stop_touch(bool mark, double when)
215 {
216         if (!_list) return;
217         if (touching()) {
218                 set_touching (false);
219
220                 if (alist()->automation_state() == Touch) {
221                         alist()->stop_touch (mark, when);
222                         if (!_desc.toggled) {
223                                 AutomationWatch::instance().remove_automation_watch (shared_from_this());
224
225                         }
226                 }
227         }
228 }
229
230 void
231 AutomationControl::commit_transaction (bool did_write)
232 {
233         if (did_write) {
234                 if (alist ()->before ()) {
235                         _session.begin_reversible_command (string_compose (_("record %1 automation"), name ()));
236                         _session.commit_reversible_command (new MementoCommand<AutomationList> (*alist ().get (), alist ()->before (), &alist ()->get_state ()));
237                 }
238         } else {
239                 alist ()->clear_history ();
240         }
241 }
242
243 double
244 AutomationControl::internal_to_interface (double val) const
245 {
246         if (_desc.integer_step) {
247                 // both upper and lower are inclusive.
248                 val =  (val - lower()) / (1 + upper() - lower());
249         } else {
250                 val =  (val - lower()) / (upper() - lower());
251         }
252
253         if (_desc.logarithmic) {
254                 if (val > 0) {
255                         val = pow (val, 1./2.0);
256                 } else {
257                         val = 0;
258                 }
259         }
260
261         return val;
262 }
263
264 double
265 AutomationControl::interface_to_internal (double val) const
266 {
267         if (!isfinite_local (val)) {
268                 val = 0;
269         }
270         if (_desc.logarithmic) {
271                 if (val <= 0) {
272                         val = 0;
273                 } else {
274                         val = pow (val, 2.0);
275                 }
276         }
277
278         if (_desc.integer_step) {
279                 val =  lower() + val * (1 + upper() - lower());
280         } else {
281                 val =  lower() + val * (upper() - lower());
282         }
283
284         if (val < lower()) val = lower();
285         if (val > upper()) val = upper();
286
287         return val;
288 }
289
290 void
291 AutomationControl::set_group (boost::shared_ptr<ControlGroup> cg)
292 {
293         /* this method can only be called by a ControlGroup. We do not need
294            to ensure consistency by calling ControlGroup::remove_control(),
295            since we are guaranteed that the ControlGroup will take care of that
296            for us.
297         */
298
299         _group = cg;
300 }
301
302 bool
303 AutomationControl::check_rt (double val, Controllable::GroupControlDisposition gcd)
304 {
305         if (!_session.loading() && (flags() & Controllable::RealTime) && !AudioEngine::instance()->in_process_thread()) {
306                 /* queue change in RT context */
307                 _session.set_control (shared_from_this(), val, gcd);
308                 return true;
309         }
310
311         return false;
312 }