move vca assignment up to the Route level (from GainControl)
[ardour.git] / libs / ardour / vca.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 "pbd/convert.h"
20
21 #include "ardour/automation_control.h"
22 #include "ardour/gain_control.h"
23 #include "ardour/rc_configuration.h"
24 #include "ardour/route.h"
25 #include "ardour/session.h"
26 #include "ardour/vca.h"
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace PBD;
32 using std::string;
33
34 gint VCA::next_number = 1;
35 string VCA::xml_node_name (X_("VCA"));
36
37 string
38 VCA::default_name_template ()
39 {
40         return _("VCA %n");
41 }
42
43 int
44 VCA::next_vca_number ()
45 {
46         /* recall that atomic_int_add() returns the value before the add. We
47          * start at one, then next one will be two etc.
48          */
49         return g_atomic_int_add (&next_number, 1);
50 }
51
52 void
53 VCA::set_next_vca_number (uint32_t n)
54 {
55         g_atomic_int_set (&next_number, n);
56 }
57
58 uint32_t
59 VCA::get_next_vca_number ()
60 {
61         return g_atomic_int_get (&next_number);
62 }
63
64 VCA::VCA (Session& s,  uint32_t num, const string& name)
65         : Stripable (s, name)
66         , Automatable (s)
67         , _number (num)
68         , _gain_control (new GainControl (s, Evoral::Parameter (GainAutomation), boost::shared_ptr<AutomationList> ()))
69         , _solo_requested (false)
70         , _mute_requested (false)
71 {
72 }
73
74 int
75 VCA::init ()
76 {
77         _solo_control.reset (new VCASoloControllable (X_("solo"), shared_from_this()));
78         _mute_control.reset (new VCAMuteControllable (X_("mute"), shared_from_this()));
79
80         add_control (_gain_control);
81         add_control (_solo_control);
82         add_control (_mute_control);
83
84         return 0;
85 }
86
87 VCA::~VCA ()
88 {
89         DropReferences (); /* emit signal */
90 }
91
92 uint32_t
93 VCA::remote_control_id () const
94 {
95         return 9999999 + _number;
96 }
97
98 XMLNode&
99 VCA::get_state ()
100 {
101         XMLNode* node = new XMLNode (xml_node_name);
102         node->add_property (X_("name"), _name);
103         node->add_property (X_("number"), _number);
104         node->add_property (X_("soloed"), (_solo_requested ? X_("yes") : X_("no")));
105         node->add_property (X_("muted"), (_mute_requested ? X_("yes") : X_("no")));
106
107         node->add_child_nocopy (_gain_control->get_state());
108         node->add_child_nocopy (get_automation_xml_state());
109
110         return *node;
111 }
112
113 int
114 VCA::set_state (XMLNode const& node, int version)
115 {
116         XMLProperty const* prop;
117
118         if ((prop = node.property ("name")) != 0) {
119                 set_name (prop->value());
120         }
121
122         if ((prop = node.property ("number")) != 0) {
123                 _number = atoi (prop->value());
124         }
125
126         XMLNodeList const &children (node.children());
127         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
128                 if ((*i)->name() == Controllable::xml_node_name) {
129                         XMLProperty* prop = (*i)->property ("name");
130                         if (prop && prop->value() == X_("gaincontrol")) {
131                                 _gain_control->set_state (**i, version);
132                         }
133                 }
134         }
135
136         return 0;
137 }
138
139 void
140 VCA::add_solo_target (boost::shared_ptr<Route> r)
141 {
142         Glib::Threads::RWLock::WriterLock lm (solo_lock);
143         solo_targets.push_back (r);
144         r->DropReferences.connect_same_thread (solo_connections, boost::bind (&VCA::solo_target_going_away, this, boost::weak_ptr<Route> (r)));
145 }
146
147 void
148 VCA::remove_solo_target (boost::shared_ptr<Route> r)
149 {
150         Glib::Threads::RWLock::WriterLock lm (solo_lock);
151         solo_targets.remove (r);
152 }
153
154 void
155 VCA::solo_target_going_away (boost::weak_ptr<Route> wr)
156 {
157         boost::shared_ptr<Route> r (wr.lock());
158         if (!r) {
159                 return;
160         }
161         remove_solo_target (r);
162 }
163
164 void
165 VCA::set_solo (bool yn)
166 {
167         {
168                 Glib::Threads::RWLock::ReaderLock lm (solo_lock);
169
170                 if (yn == _solo_requested) {
171                         return;
172                 }
173
174                 if (solo_targets.empty()) {
175                         return;
176                 }
177
178                 boost::shared_ptr<RouteList> rl (new RouteList (solo_targets));
179
180                 if (Config->get_solo_control_is_listen_control()) {
181                         _session.set_listen (rl, yn, Session::rt_cleanup, Controllable::NoGroup);
182                 } else {
183                         _session.set_solo (rl, yn, Session::rt_cleanup, Controllable::NoGroup);
184                 }
185         }
186
187         _solo_requested = yn;
188 }
189
190 void
191 VCA::add_mute_target (boost::shared_ptr<Route> r)
192 {
193         Glib::Threads::RWLock::WriterLock lm (mute_lock);
194         mute_targets.push_back (r);
195         r->DropReferences.connect_same_thread (mute_connections, boost::bind (&VCA::mute_target_going_away, this, boost::weak_ptr<Route> (r)));
196 }
197
198 void
199 VCA::remove_mute_target (boost::shared_ptr<Route> r)
200 {
201         Glib::Threads::RWLock::WriterLock lm (mute_lock);
202         mute_targets.remove (r);
203 }
204
205 void
206 VCA::mute_target_going_away (boost::weak_ptr<Route> wr)
207 {
208         boost::shared_ptr<Route> r (wr.lock());
209         if (!r) {
210                 return;
211         }
212         remove_mute_target (r);
213 }
214
215 void
216 VCA::set_mute (bool yn)
217 {
218         {
219                 Glib::Threads::RWLock::ReaderLock lm (mute_lock);
220                 if (yn == _mute_requested) {
221                         return;
222                 }
223
224                 boost::shared_ptr<RouteList> rl (new RouteList (mute_targets));
225                 _session.set_mute (rl, yn, Session::rt_cleanup, Controllable::NoGroup);
226         }
227
228         _mute_requested = yn;
229 }
230
231 bool
232 VCA::soloed () const
233 {
234         return _solo_requested;
235 }
236
237 bool
238 VCA::muted () const
239 {
240         return _mute_requested;
241 }
242
243 VCA::VCASoloControllable::VCASoloControllable (string const & name, boost::shared_ptr<VCA> vca)
244         : AutomationControl (vca->session(), Evoral::Parameter (SoloAutomation), ParameterDescriptor (Evoral::Parameter (SoloAutomation)),
245                              boost::shared_ptr<AutomationList>(), name)
246         , _vca (vca)
247 {
248 }
249
250 void
251 VCA::VCASoloControllable::set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
252 {
253         if (writable()) {
254                 _set_value (val, gcd);
255         }
256 }
257
258 void
259 VCA::VCASoloControllable::_set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
260 {
261         boost::shared_ptr<VCA> vca = _vca.lock();
262         if (!vca) {
263                 return;
264         }
265
266         vca->set_solo (val >= 0.5);
267
268         AutomationControl::set_value (val, gcd);
269 }
270
271 void
272 VCA::VCASoloControllable::set_value_unchecked (double val)
273 {
274         /* used only by automation playback */
275         _set_value (val, Controllable::NoGroup);
276 }
277
278 double
279 VCA::VCASoloControllable::get_value() const
280 {
281         boost::shared_ptr<VCA> vca = _vca.lock();
282         if (!vca) {
283                 return 0.0;
284         }
285
286         return vca->soloed() ? 1.0 : 0.0;
287 }
288
289 /*----*/
290
291 VCA::VCAMuteControllable::VCAMuteControllable (string const & name, boost::shared_ptr<VCA> vca)
292         : AutomationControl (vca->session(), Evoral::Parameter (MuteAutomation), ParameterDescriptor (Evoral::Parameter (MuteAutomation)),
293                              boost::shared_ptr<AutomationList>(), name)
294         , _vca (vca)
295 {
296 }
297
298 void
299 VCA::VCAMuteControllable::set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
300 {
301         if (writable()) {
302                 _set_value (val, gcd);
303         }
304 }
305
306 void
307 VCA::VCAMuteControllable::_set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
308 {
309         boost::shared_ptr<VCA> vca = _vca.lock();
310
311         if (!vca) {
312                 return;
313         }
314
315         vca->set_mute (val >= 0.5);
316
317         AutomationControl::set_value (val, gcd);
318 }
319
320 void
321 VCA::VCAMuteControllable::set_value_unchecked (double val)
322 {
323         /* used only by automation playback */
324         _set_value (val, Controllable::NoGroup);
325 }
326
327 double
328 VCA::VCAMuteControllable::get_value() const
329 {
330         boost::shared_ptr<VCA> vca = _vca.lock();
331         if (!vca) {
332                 return 0.0;
333         }
334
335         return vca->muted() ? 1.0 : 0.0;
336 }