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