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