e4d551168b7f1437ae1bc2162dfbfd5b95667150
[ardour.git] / libs / ardour / solo_isolate_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_isolate_control.h"
23
24 #include "pbd/i18n.h"
25
26 using namespace ARDOUR;
27 using namespace std;
28 using namespace PBD;
29
30 SoloIsolateControl::SoloIsolateControl (Session& session, std::string const & name, Soloable& s, Muteable& m)
31         : SlavableAutomationControl (session, SoloIsolateAutomation, ParameterDescriptor (SoloIsolateAutomation),
32                                      boost::shared_ptr<AutomationList>(new AutomationList(Evoral::Parameter(SoloIsolateAutomation))),
33                                      name)
34         , _soloable (s)
35         , _muteable (m)
36         , _solo_isolated (false)
37         , _solo_isolated_by_upstream (0)
38 {
39         _list->set_interpolation (Evoral::ControlList::Discrete);
40         /* isolate changes must be synchronized by the process cycle */
41         set_flags (Controllable::Flag (flags() | Controllable::RealTime));
42 }
43
44 void
45 SoloIsolateControl::master_changed (bool from_self, PBD::Controllable::GroupControlDisposition gcd, boost::shared_ptr<AutomationControl>)
46 {
47         if (!_soloable.can_solo()) {
48                 return;
49         }
50
51         bool master_soloed;
52
53         {
54                 Glib::Threads::RWLock::ReaderLock lm (master_lock);
55                 master_soloed = (bool) get_masters_value_locked ();
56         }
57
58         /* Master is considered equivalent to an upstream solo control, not
59          * direct control over self-soloed.
60          */
61
62         mod_solo_isolated_by_upstream (master_soloed ? 1 : -1);
63
64         /* no need to call AutomationControl::master_changed() since it just
65            emits Changed() which we already did in mod_solo_by_others_upstream()
66         */
67 }
68
69 void
70 SoloIsolateControl::mod_solo_isolated_by_upstream (int32_t delta)
71 {
72         bool old = solo_isolated ();
73         DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 mod_solo_isolated_by_upstream cur: %2 d: %3\n",
74                                                   name(), _solo_isolated_by_upstream, delta));
75
76         if (delta < 0) {
77                 if (_solo_isolated_by_upstream >= (uint32_t) abs(delta)) {
78                         _solo_isolated_by_upstream += delta;
79                 } else {
80                         _solo_isolated_by_upstream = 0;
81                 }
82         } else {
83                 _solo_isolated_by_upstream += delta;
84         }
85
86         if (solo_isolated() != old) {
87                 /* solo isolated status changed */
88                 _muteable.mute_master()->set_solo_ignore (solo_isolated());
89                 Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
90         }
91 }
92
93 void
94 SoloIsolateControl::actually_set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
95 {
96         if (!_soloable.can_solo()) {
97                 return;
98         }
99
100         set_solo_isolated (val, gcd);
101
102         /* this sets the Evoral::Control::_user_value for us, which will
103            be retrieved by AutomationControl::get_value (), and emits Changed
104         */
105
106         AutomationControl::actually_set_value (val, gcd);
107         _session.set_dirty ();
108 }
109
110 void
111 SoloIsolateControl::set_solo_isolated (bool yn, Controllable::GroupControlDisposition group_override)
112 {
113         if (!_soloable.can_solo()) {
114                 return;
115         }
116
117         bool changed = false;
118
119         if (yn) {
120                 if (_solo_isolated == false) {
121                         _muteable.mute_master()->set_solo_ignore (true);
122                         changed = true;
123                 }
124                 _solo_isolated = true;
125         } else {
126                 if (_solo_isolated == true) {
127                         _solo_isolated = false;
128                         _muteable.mute_master()->set_solo_ignore (false);
129                         changed = true;
130                 }
131         }
132
133         if (!changed) {
134                 return;
135         }
136
137         _soloable.push_solo_isolate_upstream (yn ? 1 : -1);
138
139         /* XXX should we back-propagate as well? (April 2010: myself and chris goddard think not) */
140
141         Changed (true, group_override); /* EMIT SIGNAL */
142 }
143
144
145 double
146 SoloIsolateControl::get_value () const
147 {
148         if (slaved()) {
149                 return solo_isolated() || get_masters_value ();
150         }
151
152         if (_list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_playback()) {
153                 // Playing back automation, get the value from the list
154                 return AutomationControl::get_value();
155         }
156
157         return solo_isolated ();
158 }
159
160 int
161 SoloIsolateControl::set_state (XMLNode const & node, int)
162 {
163         XMLProperty const * prop;
164
165         if ((prop = node.property ("solo-isolated")) != 0) {
166                 _solo_isolated = string_is_affirmative (prop->value());
167         }
168
169         return 0;
170 }
171
172 XMLNode&
173 SoloIsolateControl::get_state ()
174 {
175         XMLNode& node (SlavableAutomationControl::get_state());
176         node.add_property (X_("solo-isolated"), _solo_isolated ? X_("yes") : X_("no"));
177         return node;
178 }