Remove ambiguous API implementation
[ardour.git] / libs / ardour / solo_control.cc
1 /*
2     Copyright (C) 2016 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "ardour/debug.h"
20 #include "ardour/mute_master.h"
21 #include "ardour/session.h"
22 #include "ardour/solo_control.h"
23
24 #include "pbd/i18n.h"
25
26 using namespace ARDOUR;
27 using namespace std;
28 using namespace PBD;
29
30 SoloControl::SoloControl (Session& session, std::string const & name, Soloable& s, Muteable& m)
31         : SlavableAutomationControl (session, SoloAutomation, ParameterDescriptor (SoloAutomation),
32                                      boost::shared_ptr<AutomationList>(new AutomationList(Evoral::Parameter(SoloAutomation))),
33                                      name)
34         , _soloable (s)
35         , _muteable (m)
36         , _self_solo (false)
37         , _soloed_by_others_upstream (0)
38         , _soloed_by_others_downstream (0)
39         , _transition_into_solo (false)
40 {
41         _list->set_interpolation (Evoral::ControlList::Discrete);
42         /* solo changes must be synchronized by the process cycle */
43         set_flags (Controllable::Flag (flags() | Controllable::RealTime));
44 }
45
46 void
47 SoloControl::set_self_solo (bool yn)
48 {
49         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1: set SELF solo => %2\n", name(), yn));
50         _self_solo = yn;
51         set_mute_master_solo ();
52
53         _transition_into_solo = 0;
54
55         if (yn) {
56                 if (get_masters_value() == 0) {
57                         _transition_into_solo = 1;
58                 }
59         } else {
60                 if (get_masters_value() == 0) {
61                         _transition_into_solo = -1;
62                 }
63         }
64 }
65
66 void
67 SoloControl::set_mute_master_solo ()
68 {
69         _muteable.mute_master()->set_soloed_by_self (self_soloed() || get_masters_value());
70
71         if (Config->get_solo_control_is_listen_control()) {
72                 _muteable.mute_master()->set_soloed_by_others (false);
73         } else {
74                 _muteable.mute_master()->set_soloed_by_others (soloed_by_others_downstream() || soloed_by_others_upstream() || get_masters_value());
75         }
76 }
77
78 void
79 SoloControl::mod_solo_by_others_downstream (int32_t delta)
80 {
81         if (_soloable.is_safe() || !_soloable.can_solo()) {
82                 return;
83         }
84
85         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 mod solo-by-downstream by %2, current up = %3 down = %4\n",
86                                                   name(), delta, _soloed_by_others_upstream, _soloed_by_others_downstream));
87
88         if (delta < 0) {
89                 if (_soloed_by_others_downstream >= (uint32_t) abs (delta)) {
90                         _soloed_by_others_downstream += delta;
91                 } else {
92                         _soloed_by_others_downstream = 0;
93                 }
94         } else {
95                 _soloed_by_others_downstream += delta;
96         }
97
98         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 SbD delta %2 = %3\n", name(), delta, _soloed_by_others_downstream));
99
100         set_mute_master_solo ();
101         _transition_into_solo = 0;
102         Changed (false, Controllable::UseGroup); /* EMIT SIGNAL */
103 }
104
105 void
106 SoloControl::mod_solo_by_others_upstream (int32_t delta)
107 {
108         if (_soloable.is_safe() || !_soloable.can_solo()) {
109                 return;
110         }
111
112         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 mod solo-by-upstream by %2, current up = %3 down = %4\n",
113                                                   name(), delta, _soloed_by_others_upstream, _soloed_by_others_downstream));
114
115         uint32_t old_sbu = _soloed_by_others_upstream;
116
117         if (delta < 0) {
118                 if (_soloed_by_others_upstream >= (uint32_t) abs (delta)) {
119                         _soloed_by_others_upstream += delta;
120                 } else {
121                         _soloed_by_others_upstream = 0;
122                 }
123         } else {
124                 _soloed_by_others_upstream += delta;
125         }
126
127         DEBUG_TRACE (DEBUG::Solo, string_compose (
128                              "%1 SbU delta %2 = %3 old = %4 sbd %5 ss %6 exclusive %7\n",
129                              name(), delta, _soloed_by_others_upstream, old_sbu,
130                              _soloed_by_others_downstream, _self_solo, Config->get_exclusive_solo()));
131
132
133         /* push the inverse solo change to everything that feeds us.
134
135            This is important for solo-within-group. When we solo 1 track out of N that
136            feed a bus, that track will cause mod_solo_by_upstream (+1) to be called
137            on the bus. The bus then needs to call mod_solo_by_downstream (-1) on all
138            tracks that feed it. This will silence them if they were audible because
139            of a bus solo, but the newly soloed track will still be audible (because
140            it is self-soloed).
141
142            but .. do this only when we are being told to solo-by-upstream (i.e delta = +1),
143                    not in reverse.
144         */
145
146         if ((_self_solo || _soloed_by_others_downstream) &&
147             ((old_sbu == 0 && _soloed_by_others_upstream > 0) ||
148              (old_sbu > 0 && _soloed_by_others_upstream == 0))) {
149
150                 if (delta > 0 || !Config->get_exclusive_solo()) {
151                         _soloable.push_solo_upstream (delta);
152                 }
153         }
154
155         set_mute_master_solo ();
156         _transition_into_solo = 0;
157         Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
158 }
159
160 void
161 SoloControl::actually_set_value (double val, PBD::Controllable::GroupControlDisposition group_override)
162 {
163         if (_soloable.is_safe() || !_soloable.can_solo()) {
164                 return;
165         }
166
167         set_self_solo (val == 1.0);
168
169         /* this sets the Evoral::Control::_user_value for us, which will
170            be retrieved by AutomationControl::get_value (), and emits Changed
171         */
172
173         SlavableAutomationControl::actually_set_value (val, group_override);
174 }
175
176 double
177 SoloControl::get_value () const
178 {
179         if (slaved()) {
180                 return self_soloed() || get_masters_value ();
181         }
182
183         if (_list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_playback()) {
184                 // Playing back automation, get the value from the list
185                 return AutomationControl::get_value();
186         }
187
188         return soloed();
189 }
190
191 void
192 SoloControl::clear_all_solo_state ()
193 {
194         bool change = false;
195
196         if (self_soloed()) {
197                 PBD::info << string_compose (_("Cleared Explicit solo: %1\n"), name()) << endmsg;
198                 actually_set_value (0.0, Controllable::NoGroup);
199                 change = true;
200         }
201
202         if (_soloed_by_others_upstream) {
203                 PBD::info << string_compose (_("Cleared upstream solo: %1 up:%2\n"), name(), _soloed_by_others_upstream)
204                           << endmsg;
205                 _soloed_by_others_upstream = 0;
206                 change = true;
207         }
208
209         if (_soloed_by_others_downstream) {
210                 PBD::info << string_compose (_("Cleared downstream solo: %1 down:%2\n"), name(), _soloed_by_others_downstream)
211                           << endmsg;
212                 _soloed_by_others_downstream = 0;
213                 change = true;
214         }
215
216         _transition_into_solo = 0; /* Session does not need to propagate */
217
218         if (change) {
219                 Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
220         }
221 }
222
223 int
224 SoloControl::set_state (XMLNode const & node, int version)
225 {
226         if (SlavableAutomationControl::set_state(node, version)) {
227                 return -1;
228         }
229
230         bool yn;
231         if (node.get_property ("self-solo", yn)) {
232                 set_self_solo (yn);
233         }
234
235         uint32_t val;
236         if (node.get_property ("soloed-by-upstream", val)) {
237                 _soloed_by_others_upstream = 0; // needed for mod_.... () to work
238                 mod_solo_by_others_upstream (val);
239         }
240
241         if (node.get_property ("soloed-by-downstream", val)) {
242                 _soloed_by_others_downstream = 0; // needed for mod_.... () to work
243                 mod_solo_by_others_downstream (val);
244         }
245
246         return 0;
247 }
248
249 XMLNode&
250 SoloControl::get_state ()
251 {
252         XMLNode& node (SlavableAutomationControl::get_state());
253
254         node.set_property (X_("self-solo"), _self_solo);
255         node.set_property (X_("soloed-by-upstream"), _soloed_by_others_upstream);
256         node.set_property (X_("soloed-by-downstream"), _soloed_by_others_downstream);
257
258         return node;
259 }
260
261 void
262 SoloControl::master_changed (bool /*from self*/, GroupControlDisposition, boost::weak_ptr<AutomationControl> wm)
263 {
264         boost::shared_ptr<AutomationControl> m = wm.lock ();
265         assert (m);
266         bool send_signal = false;
267
268         _transition_into_solo = 0;
269
270         /* Notice that we call get_boolean_masters() BEFORE we call
271          * update_boolean_masters_records(), in order to know what
272          * our master state was BEFORE it gets changed.
273          */
274
275
276         if (m->get_value()) {
277                 /* this master is now enabled */
278                 if (!self_soloed() && get_boolean_masters() == 0) {
279                         /* not self-soloed, wasn't soloed by masters before */
280                         send_signal = true;
281                         _transition_into_solo = 1;
282                 }
283         } else {
284                 if (!self_soloed() && get_boolean_masters() == 1) {
285                         /* not self-soloed, soloed by just 1 master before */
286                         _transition_into_solo = -1;
287                         send_signal = true;
288                 }
289         }
290
291         update_boolean_masters_records (m);
292
293         if (send_signal) {
294                 set_mute_master_solo ();
295                 Changed (false, Controllable::UseGroup);
296         }
297
298 }
299
300 void
301 SoloControl::post_add_master (boost::shared_ptr<AutomationControl> m)
302 {
303         if (m->get_value()) {
304
305                 /* boolean masters records are not updated until AFTER
306                  * ::post_add_master() is called, so we can use them to check
307                  * on whether any master was already enabled before the new
308                  * one was added.
309                  */
310
311                 if (!self_soloed() && !get_boolean_masters()) {
312                         _transition_into_solo = 1;
313                         Changed (false, Controllable::NoGroup);
314                 }
315         }
316 }
317
318 void
319 SoloControl::pre_remove_master (boost::shared_ptr<AutomationControl> m)
320 {
321         if (!m) {
322                 /* null control ptr means we're removing all masters. Nothing
323                  * to do. Changed will be emitted in
324                  * SlavableAutomationControl::clear_masters()
325                  */
326                 return;
327         }
328
329         if (m->get_value()) {
330                 if (!self_soloed() && (get_boolean_masters() == 1)) {
331                         /* we're not self-soloed, this master is, and we're
332                            removing
333                            it. SlavableAutomationControl::remove_master() will
334                            ensure that we reset our own value after actually
335                            removing the master, so that our state does not
336                            change (this is a precondition of the
337                            SlavableAutomationControl API). This will emit
338                            Changed(), and we need to make sure that any
339                            listener knows that there has been no transition.
340                         */
341                         _transition_into_solo = 0;
342                 } else {
343                         _transition_into_solo = 1;
344                 }
345         } else {
346                 _transition_into_solo = 0;
347         }
348 }
349
350 bool
351 SoloControl::can_solo () const
352 {
353         return _soloable.can_solo ();
354 }