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