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