clean up mess in Route/Track controllables caused by not understanding the significan...
[ardour.git] / libs / ardour / route_controls.cc
1 /*
2     Copyright (C) 2000 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include "ardour/automation_control.h"
25 #include "ardour/parameter_descriptor.h"
26 #include "ardour/route.h"
27 #include "ardour/session.h"
28
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 void
36 Route::set_control (AutomationType type, double val, PBD::Controllable::GroupControlDisposition group_override)
37 {
38         boost::shared_ptr<RouteList> rl;
39
40         switch (type) {
41         case GainAutomation:
42                 /* route must mediate group control */
43                 set_gain (val, group_override);
44                 break;
45
46         case TrimAutomation:
47                 /* route must mediate group control */
48                 set_trim (val, group_override);
49                 break;
50
51         case RecEnableAutomation:
52                 /* session must mediate group control */
53                 rl.reset (new RouteList);
54                 rl->push_back (shared_from_this());
55                 _session.set_record_enabled (rl, val >= 0.5 ? true : false, Session::rt_cleanup, group_override);
56                 break;
57
58         case SoloAutomation:
59                 /* session must mediate group control */
60                 rl.reset (new RouteList);
61                 rl->push_back (shared_from_this());
62                 if (Config->get_solo_control_is_listen_control()) {
63                         _session.set_listen (rl, val >= 0.5 ? true : false, Session::rt_cleanup, group_override);
64                 } else {
65                         _session.set_solo (rl, val >= 0.5 ? true : false, Session::rt_cleanup, group_override);
66                 }
67                 break;
68
69         case MuteAutomation:
70                 /* session must mediate group control */
71                 rl.reset (new RouteList);
72                 rl->push_back (shared_from_this());
73                 _session.set_mute (rl, !muted(), Session::rt_cleanup, group_override);
74                 return;
75                 break;
76
77         default:
78                 /* Not a route automation control */
79                 fatal << string_compose (_("programming error: %1%2\n"), X_("illegal type of route automation control passed to Route::set_control(): "), enum_2_string(type)) << endmsg;
80                 /*NOTREACHED*/
81                 return;
82         }
83 }
84
85
86 Route::RouteAutomationControl::RouteAutomationControl (const std::string& name,
87                                                        AutomationType atype,
88                                                        boost::shared_ptr<AutomationList> alist,
89                                                        boost::shared_ptr<Route> r)
90         : AutomationControl (r->session(), Evoral::Parameter (atype),
91                              ParameterDescriptor (Evoral::Parameter (atype)),
92                              alist, name)
93         , _route (r)
94 {
95 }
96
97 Route::GainControllable::GainControllable (Session& s, AutomationType atype, boost::shared_ptr<Route> r)
98         : GainControl (s, Evoral::Parameter(atype))
99         , _route (r)
100 {
101
102 }
103
104 Route::SoloControllable::SoloControllable (std::string name, boost::shared_ptr<Route> r)
105         : RouteAutomationControl (name, SoloAutomation, boost::shared_ptr<AutomationList>(), r)
106 {
107         boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(SoloAutomation)));
108         gl->set_interpolation(Evoral::ControlList::Discrete);
109         set_list (gl);
110 }
111
112 void
113 Route::SoloControllable::set_value (double val, PBD::Controllable::GroupControlDisposition group_override)
114 {
115         if (writable()) {
116                 _set_value (val, group_override);
117         }
118 }
119
120 void
121 Route::SoloControllable::_set_value (double val, PBD::Controllable::GroupControlDisposition group_override)
122 {
123         boost::shared_ptr<Route> r = _route.lock ();
124         if (!r) {
125                 return;
126         }
127         r->set_control (SoloAutomation, val, group_override);
128 }
129
130 void
131 Route::SoloControllable::set_value_unchecked (double val)
132 {
133         /* Used only by automation playback */
134
135         _set_value (val, Controllable::NoGroup);
136 }
137
138 double
139 Route::SoloControllable::get_value () const
140 {
141         boost::shared_ptr<Route> r = _route.lock ();
142         if (!r) {
143                 return 0;
144         }
145
146         if (Config->get_solo_control_is_listen_control()) {
147                 return r->listening_via_monitor() ? GAIN_COEFF_UNITY : GAIN_COEFF_ZERO;
148         } else {
149                 return r->self_soloed() ? GAIN_COEFF_UNITY : GAIN_COEFF_ZERO;
150         }
151 }
152
153 Route::MuteControllable::MuteControllable (std::string name, boost::shared_ptr<Route> r)
154         : RouteAutomationControl (name, MuteAutomation, boost::shared_ptr<AutomationList>(), r)
155         , _route (r)
156 {
157         boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(MuteAutomation)));
158         gl->set_interpolation(Evoral::ControlList::Discrete);
159         set_list (gl);
160 }
161
162 void
163 Route::MuteControllable::set_superficial_value(bool muted)
164 {
165         /* Note we can not use AutomationControl::set_value here since it will emit
166            Changed(), but the value will not be correct to the observer. */
167
168         const bool to_list = _list && ((AutomationList*)_list.get ())->automation_write ();
169         const double where = _session.audible_frame ();
170         if (to_list) {
171                 /* Note that we really need this:
172                  *  if (as == Touch && _list->in_new_write_pass ()) {
173                  *       alist->start_write_pass (_session.audible_frame ());
174                  *  }
175                  * here in the case of the user calling from a GUI or whatever.
176                  * Without the ability to distinguish between user and
177                  * automation-initiated changes, we lose the "touch mute"
178                  * behaviour we have in AutomationController::toggled ().
179                  */
180                 _list->set_in_write_pass (true, false, where);
181         }
182
183         Control::set_double (muted, where, to_list);
184 }
185
186 void
187 Route::MuteControllable::set_value (double val, PBD::Controllable::GroupControlDisposition group_override)
188 {
189         if (writable()) {
190                 _set_value (val, group_override);
191         }
192 }
193
194 void
195 Route::MuteControllable::set_value_unchecked (double val)
196 {
197         /* used only automation playback */
198         _set_value (val, Controllable::NoGroup);
199 }
200
201 void
202 Route::MuteControllable::_set_value (double val, Controllable::GroupControlDisposition group_override)
203 {
204         boost::shared_ptr<Route> r = _route.lock ();
205
206         if (!r) {
207                 return;
208         }
209
210         if (_list && ((AutomationList*)_list.get())->automation_playback()) {
211                 // Set superficial/automation value to drive controller (and possibly record)
212                 const bool bval = ((val >= 0.5) ? true : false);
213                 set_superficial_value (bval);
214                 // Playing back automation, set route mute directly
215                 r->set_mute (bval, Controllable::NoGroup);
216         } else {
217                 r->set_control (MuteAutomation, val, group_override);
218         }
219 }
220
221 double
222 Route::MuteControllable::get_value () const
223 {
224         if (_list && ((AutomationList*)_list.get())->automation_playback()) {
225                 // Playing back automation, get the value from the list
226                 return AutomationControl::get_value();
227         }
228
229         // Not playing back automation, get the actual route mute value
230         boost::shared_ptr<Route> r = _route.lock ();
231         return (r && r->muted()) ? GAIN_COEFF_UNITY : GAIN_COEFF_ZERO;
232 }
233
234 Route::PhaseControllable::PhaseControllable (std::string name, boost::shared_ptr<Route> r)
235         : RouteAutomationControl (name, PhaseAutomation, boost::shared_ptr<AutomationList>(), r)
236 {
237         boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(PhaseAutomation)));
238         gl->set_interpolation(Evoral::ControlList::Discrete);
239         set_list (gl);
240 }
241
242 void
243 Route::PhaseControllable::set_value (double v, PBD::Controllable::GroupControlDisposition /* group_override */)
244 {
245         boost::shared_ptr<Route> r = _route.lock ();
246         if (r->phase_invert().size()) {
247                 if (v == 0 || (v < 1 && v > 0.9) ) {
248                         r->set_phase_invert (_current_phase, false);
249                 } else {
250                         r->set_phase_invert (_current_phase, true);
251                 }
252         }
253 }
254
255 double
256 Route::PhaseControllable::get_value () const
257 {
258         boost::shared_ptr<Route> r = _route.lock ();
259         if (!r) {
260                 return 0.0;
261         }
262         return (double) r->phase_invert (_current_phase);
263 }
264
265 void
266 Route::PhaseControllable::set_channel (uint32_t c)
267 {
268         _current_phase = c;
269 }
270
271 uint32_t
272 Route::PhaseControllable::channel () const
273 {
274         return _current_phase;
275 }
276
277 Route::SoloIsolateControllable::SoloIsolateControllable (std::string name, boost::shared_ptr<Route> r)
278         : RouteAutomationControl (name, SoloIsolateAutomation, boost::shared_ptr<AutomationList>(), r)
279 {
280         boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(SoloIsolateAutomation)));
281         gl->set_interpolation(Evoral::ControlList::Discrete);
282         set_list (gl);
283 }
284
285
286 double
287 Route::SoloIsolateControllable::get_value () const
288 {
289         boost::shared_ptr<Route> r = _route.lock ();
290         if (!r) {
291                 return 0.0; /* "false" */
292         }
293
294         return r->solo_isolated() ? 1.0 : 0.0;
295 }
296
297 void
298 Route::SoloIsolateControllable::set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
299 {
300         _set_value (val, gcd);
301 }
302
303 void
304 Route::SoloIsolateControllable::_set_value (double val, PBD::Controllable::GroupControlDisposition)
305 {
306         boost::shared_ptr<Route> r = _route.lock ();
307         if (!r) {
308                 return;
309         }
310
311         /* no group semantics yet */
312         r->set_solo_isolated (val >= 0.5 ? true : false);
313 }
314
315 Route::SoloSafeControllable::SoloSafeControllable (std::string name, boost::shared_ptr<Route> r)
316         : RouteAutomationControl (name, SoloSafeAutomation, boost::shared_ptr<AutomationList>(), r)
317 {
318         boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(SoloSafeAutomation)));
319         gl->set_interpolation(Evoral::ControlList::Discrete);
320         set_list (gl);
321 }
322
323 void
324 Route::SoloSafeControllable::set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
325 {
326         _set_value (val, gcd);
327 }
328
329 void
330 Route::SoloSafeControllable::_set_value (double val, PBD::Controllable::GroupControlDisposition)
331 {
332         boost::shared_ptr<Route> r = _route.lock ();
333         if (!r) {
334                 return;
335         }
336
337         /* no group semantics yet */
338         r->set_solo_safe (val >= 0.5 ? true : false);
339 }
340
341 double
342 Route::SoloSafeControllable::get_value () const
343 {
344         boost::shared_ptr<Route> r = _route.lock ();
345         if (!r) {
346                 return 0.0; /* "false" */
347         }
348
349         return r->solo_safe() ? 1.0 : 0.0;
350 }
351