fix crash when copy'ing latent plugins
[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         // ideally this function will never do anything, it only exists to forestall Murphy
195
196 #ifndef NDEBUG
197         // these are really debug messages, but of possible interest.
198         if (self_soloed()) {
199                 PBD::info << string_compose (_("Cleared Explicit solo: %1\n"), name());
200         }
201         if (_soloed_by_others_upstream || _soloed_by_others_downstream) {
202                 PBD::info << string_compose (_("Cleared Implicit solo: %1 up:%2 down:%3\n"),
203                                 name(), _soloed_by_others_upstream, _soloed_by_others_downstream);
204         }
205 #endif
206
207         _soloed_by_others_upstream = 0;
208         _soloed_by_others_downstream = 0;
209
210         set_self_solo (false);
211         _transition_into_solo = 0; /* Session does not need to propagate */
212         Changed (false, Controllable::UseGroup); /* EMIT SIGNAL */
213 }
214
215 int
216 SoloControl::set_state (XMLNode const & node, int)
217 {
218         XMLProperty const * prop;
219
220         if ((prop = node.property ("self-solo")) != 0) {
221                 set_self_solo (string_is_affirmative (prop->value()));
222         }
223
224         if ((prop = node.property ("soloed-by-upstream")) != 0) {
225                 _soloed_by_others_upstream = 0; // needed for mod_.... () to work
226                 mod_solo_by_others_upstream (atoi (prop->value()));
227         }
228
229         if ((prop = node.property ("soloed-by-downstream")) != 0) {
230                 _soloed_by_others_downstream = 0; // needed for mod_.... () to work
231                 mod_solo_by_others_downstream (atoi (prop->value()));
232         }
233
234         return 0;
235 }
236
237 XMLNode&
238 SoloControl::get_state ()
239 {
240         XMLNode& node (SlavableAutomationControl::get_state());
241
242         node.add_property (X_("self-solo"), _self_solo ? X_("yes") : X_("no"));
243         char buf[32];
244         snprintf (buf, sizeof(buf), "%d",  _soloed_by_others_upstream);
245         node.add_property (X_("soloed-by-upstream"), buf);
246         snprintf (buf, sizeof(buf), "%d",  _soloed_by_others_downstream);
247         node.add_property (X_("soloed-by-downstream"), buf);
248
249         return node;
250 }
251
252 void
253 SoloControl::master_changed (bool /*from self*/, GroupControlDisposition, boost::shared_ptr<AutomationControl> m)
254 {
255         bool send_signal = false;
256
257         _transition_into_solo = 0;
258
259         /* Notice that we call get_boolean_masters() BEFORE we call
260          * update_boolean_masters_records(), in order to know what
261          * our master state was BEFORE it gets changed.
262          */
263
264
265         if (m->get_value()) {
266                 /* this master is now enabled */
267                 if (!self_soloed() && get_boolean_masters() == 0) {
268                         /* not self-soloed, wasn't soloed by masters before */
269                         send_signal = true;
270                         _transition_into_solo = 1;
271                 }
272         } else {
273                 if (!self_soloed() && get_boolean_masters() == 1) {
274                         /* not self-soloed, soloed by just 1 master before */
275                         _transition_into_solo = -1;
276                         send_signal = true;
277                 }
278         }
279
280         update_boolean_masters_records (m);
281
282         if (send_signal) {
283                 set_mute_master_solo ();
284                 Changed (false, Controllable::UseGroup);
285         }
286
287 }
288
289 void
290 SoloControl::post_add_master (boost::shared_ptr<AutomationControl> m)
291 {
292         if (m->get_value()) {
293
294                 /* boolean masters records are not updated until AFTER
295                  * ::post_add_master() is called, so we can use them to check
296                  * on whether any master was already enabled before the new
297                  * one was added.
298                  */
299
300                 if (!self_soloed() && !get_boolean_masters()) {
301                         _transition_into_solo = 1;
302                         Changed (false, Controllable::NoGroup);
303                 }
304         }
305 }
306
307 void
308 SoloControl::pre_remove_master (boost::shared_ptr<AutomationControl> m)
309 {
310         if (!m) {
311                 /* null control ptr means we're removing all masters. Nothing
312                  * to do. Changed will be emitted in
313                  * SlavableAutomationControl::clear_masters()
314                  */
315                 return;
316         }
317
318         if (m->get_value()) {
319                 if (!self_soloed() && (get_boolean_masters() == 1)) {
320                         /* we're not self-soloed, this master is, and we're
321                            removing
322                            it. SlavableAutomationControl::remove_master() will
323                            ensure that we reset our own value after actually
324                            removing the master, so that our state does not
325                            change (this is a precondition of the
326                            SlavableAutomationControl API). This will emit
327                            Changed(), and we need to make sure that any
328                            listener knows that there has been no transition.
329                         */
330                         _transition_into_solo = 0;
331                 } else {
332                         _transition_into_solo = 1;
333                 }
334         } else {
335                 _transition_into_solo = 0;
336         }
337 }
338
339 bool
340 SoloControl::can_solo () const
341 {
342         return _soloable.can_solo ();
343 }