summaryrefslogtreecommitdiff
path: root/libs/ardour/solo_control.cc
blob: 9d898493dc0b1b8e198785437543023cb12389c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
    Copyright (C) 2016 Paul Davis

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the Free
    Software Foundation; either version 2 of the License, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "ardour/debug.h"
#include "ardour/mute_master.h"
#include "ardour/session.h"
#include "ardour/solo_control.h"

#include "pbd/i18n.h"

using namespace ARDOUR;
using namespace std;
using namespace PBD;

SoloControl::SoloControl (Session& session, std::string const & name, Soloable& s, Muteable& m)
	: SlavableAutomationControl (session, SoloAutomation, ParameterDescriptor (SoloAutomation),
	                             boost::shared_ptr<AutomationList>(new AutomationList(Evoral::Parameter(SoloAutomation))),
	                             name)
	, _soloable (s)
	, _muteable (m)
	, _self_solo (false)
	, _soloed_by_others_upstream (0)
	, _soloed_by_others_downstream (0)
	, _transition_into_solo (false)
{
	_list->set_interpolation (Evoral::ControlList::Discrete);
	/* solo changes must be synchronized by the process cycle */
	set_flags (Controllable::Flag (flags() | Controllable::RealTime));
}

void
SoloControl::set_self_solo (bool yn)
{
	DEBUG_TRACE (DEBUG::Solo, string_compose ("%1: set SELF solo => %2\n", name(), yn));
	_self_solo = yn;
	set_mute_master_solo ();

	_transition_into_solo = 0;

	if (yn) {
		if (get_masters_value() == 0) {
			_transition_into_solo = 1;
		}
	} else {
		if (get_masters_value() == 0) {
			_transition_into_solo = -1;
		}
	}
}

void
SoloControl::set_mute_master_solo ()
{
	_muteable.mute_master()->set_soloed_by_self (self_soloed() || get_masters_value());

	if (Config->get_solo_control_is_listen_control()) {
		_muteable.mute_master()->set_soloed_by_others (false);
	} else {
		_muteable.mute_master()->set_soloed_by_others (soloed_by_others_downstream() || soloed_by_others_upstream() || get_masters_value());
	}
}

void
SoloControl::mod_solo_by_others_downstream (int32_t delta)
{
	if (_soloable.is_safe() || !_soloable.can_solo()) {
		return;
	}

	DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 mod solo-by-downstream by %2, current up = %3 down = %4\n",
						  name(), delta, _soloed_by_others_upstream, _soloed_by_others_downstream));

	if (delta < 0) {
		if (_soloed_by_others_downstream >= (uint32_t) abs (delta)) {
			_soloed_by_others_downstream += delta;
		} else {
			_soloed_by_others_downstream = 0;
		}
	} else {
		_soloed_by_others_downstream += delta;
	}

	DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 SbD delta %2 = %3\n", name(), delta, _soloed_by_others_downstream));

	set_mute_master_solo ();
	_transition_into_solo = 0;
	Changed (false, Controllable::UseGroup); /* EMIT SIGNAL */
}

void
SoloControl::mod_solo_by_others_upstream (int32_t delta)
{
	if (_soloable.is_safe() || !_soloable.can_solo()) {
		return;
	}

	DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 mod solo-by-upstream by %2, current up = %3 down = %4\n",
	                                          name(), delta, _soloed_by_others_upstream, _soloed_by_others_downstream));

	uint32_t old_sbu = _soloed_by_others_upstream;

	if (delta < 0) {
		if (_soloed_by_others_upstream >= (uint32_t) abs (delta)) {
			_soloed_by_others_upstream += delta;
		} else {
			_soloed_by_others_upstream = 0;
		}
	} else {
		_soloed_by_others_upstream += delta;
	}

	DEBUG_TRACE (DEBUG::Solo, string_compose (
		             "%1 SbU delta %2 = %3 old = %4 sbd %5 ss %6 exclusive %7\n",
		             name(), delta, _soloed_by_others_upstream, old_sbu,
		             _soloed_by_others_downstream, _self_solo, Config->get_exclusive_solo()));


	/* push the inverse solo change to everything that feeds us.

	   This is important for solo-within-group. When we solo 1 track out of N that
	   feed a bus, that track will cause mod_solo_by_upstream (+1) to be called
	   on the bus. The bus then needs to call mod_solo_by_downstream (-1) on all
	   tracks that feed it. This will silence them if they were audible because
	   of a bus solo, but the newly soloed track will still be audible (because
	   it is self-soloed).

	   but .. do this only when we are being told to solo-by-upstream (i.e delta = +1),
		   not in reverse.
	*/

	if ((_self_solo || _soloed_by_others_downstream) &&
	    ((old_sbu == 0 && _soloed_by_others_upstream > 0) ||
	     (old_sbu > 0 && _soloed_by_others_upstream == 0))) {

		if (delta > 0 || !Config->get_exclusive_solo()) {
			_soloable.push_solo_upstream (delta);
		}
	}

	set_mute_master_solo ();
	_transition_into_solo = 0;
	Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
}

void
SoloControl::actually_set_value (double val, PBD::Controllable::GroupControlDisposition group_override)
{
	if (_soloable.is_safe() || !_soloable.can_solo()) {
		return;
	}

	set_self_solo (val == 1.0);

	/* this sets the Evoral::Control::_user_value for us, which will
	   be retrieved by AutomationControl::get_value (), and emits Changed
	*/

	SlavableAutomationControl::actually_set_value (val, group_override);
}

double
SoloControl::get_value () const
{
	if (slaved()) {
		return self_soloed() || get_masters_value ();
	}

	if (_list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_playback()) {
		// Playing back automation, get the value from the list
		return AutomationControl::get_value();
	}

	return soloed();
}

void
SoloControl::clear_all_solo_state ()
{
	bool change = false;

	if (self_soloed()) {
		PBD::info << string_compose (_("Cleared Explicit solo: %1\n"), name()) << endmsg;
		actually_set_value (0.0, Controllable::NoGroup);
		change = true;
	}

	if (_soloed_by_others_upstream) {
		PBD::info << string_compose (_("Cleared upstream solo: %1 up:%2\n"), name(), _soloed_by_others_upstream)
		          << endmsg;
		_soloed_by_others_upstream = 0;
		change = true;
	}

	if (_soloed_by_others_downstream) {
		PBD::info << string_compose (_("Cleared downstream solo: %1 down:%2\n"), name(), _soloed_by_others_downstream)
		          << endmsg;
		_soloed_by_others_downstream = 0;
		change = true;
	}

	_transition_into_solo = 0; /* Session does not need to propagate */

	if (change) {
		Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
	}
}

int
SoloControl::set_state (XMLNode const & node, int version)
{
	if (SlavableAutomationControl::set_state(node, version)) {
		return -1;
	}

	bool yn;
	if (node.get_property ("self-solo", yn)) {
		set_self_solo (yn);
	}

	uint32_t val;
	if (node.get_property ("soloed-by-upstream", val)) {
		_soloed_by_others_upstream = 0; // needed for mod_.... () to work
		mod_solo_by_others_upstream (val);
	}

	if (node.get_property ("soloed-by-downstream", val)) {
		_soloed_by_others_downstream = 0; // needed for mod_.... () to work
		mod_solo_by_others_downstream (val);
	}

	return 0;
}

XMLNode&
SoloControl::get_state ()
{
	XMLNode& node (SlavableAutomationControl::get_state());

	node.set_property (X_("self-solo"), _self_solo);
	node.set_property (X_("soloed-by-upstream"), _soloed_by_others_upstream);
	node.set_property (X_("soloed-by-downstream"), _soloed_by_others_downstream);

	return node;
}

void
SoloControl::master_changed (bool /*from self*/, GroupControlDisposition, boost::weak_ptr<AutomationControl> wm)
{
	boost::shared_ptr<AutomationControl> m = wm.lock ();
	assert (m);
	bool send_signal = false;

	_transition_into_solo = 0;

	/* Notice that we call get_boolean_masters() BEFORE we call
	 * update_boolean_masters_records(), in order to know what
	 * our master state was BEFORE it gets changed.
	 */


	if (m->get_value()) {
		/* this master is now enabled */
		if (!self_soloed() && get_boolean_masters() == 0) {
			/* not self-soloed, wasn't soloed by masters before */
			send_signal = true;
			_transition_into_solo = 1;
		}
	} else {
		if (!self_soloed() && get_boolean_masters() == 1) {
			/* not self-soloed, soloed by just 1 master before */
			_transition_into_solo = -1;
			send_signal = true;
		}
	}

	update_boolean_masters_records (m);

	if (send_signal) {
		set_mute_master_solo ();
		Changed (false, Controllable::UseGroup);
	}

}

void
SoloControl::post_add_master (boost::shared_ptr<AutomationControl> m)
{
	if (m->get_value()) {

		/* boolean masters records are not updated until AFTER
		 * ::post_add_master() is called, so we can use them to check
		 * on whether any master was already enabled before the new
		 * one was added.
		 */

		if (!self_soloed() && !get_boolean_masters()) {
			_transition_into_solo = 1;
			Changed (false, Controllable::NoGroup);
		}
	}
}

void
SoloControl::pre_remove_master (boost::shared_ptr<AutomationControl> m)
{
	if (!m) {
		/* null control ptr means we're removing all masters. Nothing
		 * to do. Changed will be emitted in
		 * SlavableAutomationControl::clear_masters()
		 */
		return;
	}

	if (m->get_value()) {
		if (!self_soloed() && (get_boolean_masters() == 1)) {
			/* we're not self-soloed, this master is, and we're
			   removing
			   it. SlavableAutomationControl::remove_master() will
			   ensure that we reset our own value after actually
			   removing the master, so that our state does not
			   change (this is a precondition of the
			   SlavableAutomationControl API). This will emit
			   Changed(), and we need to make sure that any
			   listener knows that there has been no transition.
			*/
			_transition_into_solo = 0;
		} else {
			_transition_into_solo = 1;
		}
	} else {
		_transition_into_solo = 0;
	}
}

bool
SoloControl::can_solo () const
{
	return _soloable.can_solo ();
}