abort if configuration fails
[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 #include "ardour/automation_control.h"
24 #include "ardour/automation_watch.h"
25 #include "ardour/event_type_map.h"
26 #include "ardour/session.h"
27
28 #include "pbd/memento_command.h"
29 #include "pbd/stacktrace.h"
30
31 #include "i18n.h"
32
33 #ifdef COMPILER_MSVC
34 #include <float.h>
35 // C99 'isfinite()' is not available in MSVC.
36 #define isfinite_local(val) (bool)_finite((double)val)
37 #else
38 #define isfinite_local isfinite
39 #endif
40
41 using namespace std;
42 using namespace ARDOUR;
43 using namespace PBD;
44
45 AutomationControl::AutomationControl(ARDOUR::Session&                          session,
46                                      const Evoral::Parameter&                  parameter,
47                                      const ParameterDescriptor&                desc,
48                                      boost::shared_ptr<ARDOUR::AutomationList> list,
49                                      const string&                             name)
50         : Controllable (name.empty() ? EventTypeMap::instance().to_symbol(parameter) : name)
51         , Evoral::Control(parameter, desc, list)
52         , _session(session)
53         , _desc(desc)
54 {
55 }
56
57 AutomationControl::~AutomationControl ()
58 {
59 }
60
61 bool
62 AutomationControl::writable() const
63 {
64         boost::shared_ptr<AutomationList> al = alist();
65         if (al) {
66                 return al->automation_state() != Play;
67         }
68         return true;
69 }
70
71 /** Get the current effective `user' value based on automation state */
72 double
73 AutomationControl::get_value() const
74 {
75         bool from_list = _list && ((AutomationList*)_list.get())->automation_playback();
76         return Control::get_double (from_list, _session.transport_frame());
77 }
78
79 /** Set the value and do the right thing based on automation state
80  *  (e.g. record if necessary, etc.)
81  *  @param value `user' value
82  */
83 void
84 AutomationControl::set_value (double value, PBD::Controllable::GroupControlDisposition /* group_override */)
85 {
86         bool to_list = _list && ((AutomationList*)_list.get())->automation_write();
87
88         Control::set_double (value, _session.transport_frame(), to_list);
89
90         Changed(); /* EMIT SIGNAL */
91 }
92
93 void
94 AutomationControl::set_list (boost::shared_ptr<Evoral::ControlList> list)
95 {
96         Control::set_list (list);
97         Changed();  /* EMIT SIGNAL */
98 }
99
100 void
101 AutomationControl::set_automation_state (AutoState as)
102 {
103         if (_list && as != alist()->automation_state()) {
104
105                 alist()->set_automation_state (as);
106                 if (_desc.toggled) {
107                         return;  // No watch for boolean automation
108                 }
109
110                 if (as == Write) {
111                         AutomationWatch::instance().add_automation_watch (shared_from_this());
112                 } else if (as == Touch) {
113                         if (!touching()) {
114                                 AutomationWatch::instance().remove_automation_watch (shared_from_this());
115                         } else {
116                                 /* this seems unlikely, but the combination of
117                                  * a control surface and the mouse could make
118                                  * it possible to put the control into Touch
119                                  * mode *while* touching it.
120                                  */
121                                 AutomationWatch::instance().add_automation_watch (shared_from_this());
122                         }
123                 } else {
124                         AutomationWatch::instance().remove_automation_watch (shared_from_this());
125                 }
126         }
127 }
128
129 void
130 AutomationControl::set_automation_style (AutoStyle as)
131 {
132         if (!_list) return;
133         alist()->set_automation_style (as);
134 }
135
136 void
137 AutomationControl::start_touch(double when)
138 {
139         if (!_list) {
140                 return;
141         }
142
143         if (!touching()) {
144
145                 if (alist()->automation_state() == Touch) {
146                         /* subtle. aligns the user value with the playback */
147                         set_value (get_value (), Controllable::NoGroup);
148                         alist()->start_touch (when);
149                         if (!_desc.toggled) {
150                                 AutomationWatch::instance().add_automation_watch (shared_from_this());
151                         }
152                 }
153                 set_touching (true);
154         }
155 }
156
157 void
158 AutomationControl::stop_touch(bool mark, double when)
159 {
160         if (!_list) return;
161         if (touching()) {
162                 set_touching (false);
163
164                 if (alist()->automation_state() == Touch) {
165                         alist()->stop_touch (mark, when);
166                         if (!_desc.toggled) {
167                                 AutomationWatch::instance().remove_automation_watch (shared_from_this());
168
169                         }
170                 }
171         }
172 }
173
174 void
175 AutomationControl::commit_transaction (bool did_write)
176 {
177         if (did_write) {
178                 if (alist ()->before ()) {
179                         _session.begin_reversible_command (string_compose (_("record %1 automation"), name ()));
180                         _session.commit_reversible_command (new MementoCommand<AutomationList> (*alist ().get (), alist ()->before (), &alist ()->get_state ()));
181                 }
182         } else {
183                 alist ()->clear_history ();
184         }
185 }
186
187 double
188 AutomationControl::internal_to_interface (double val) const
189 {
190         if (_desc.integer_step) {
191                 // both upper and lower are inclusive.
192                 val =  (val - lower()) / (1 + upper() - lower());
193         } else {
194                 val =  (val - lower()) / (upper() - lower());
195         }
196
197         if (_desc.logarithmic) {
198                 if (val > 0) {
199                         val = pow (val, 1./2.0);
200                 } else {
201                         val = 0;
202                 }
203         }
204
205         return val;
206 }
207
208 double
209 AutomationControl::interface_to_internal (double val) const
210 {
211         if (!isfinite_local (val)) {
212                 val = 0;
213         }
214         if (_desc.logarithmic) {
215                 if (val <= 0) {
216                         val = 0;
217                 } else {
218                         val = pow (val, 2.0);
219                 }
220         }
221
222         if (_desc.integer_step) {
223                 val =  lower() + val * (1 + upper() - lower());
224         } else {
225                 val =  lower() + val * (upper() - lower());
226         }
227
228         if (val < lower()) val = lower();
229         if (val > upper()) val = upper();
230
231         return val;
232 }
233
234