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