VCA solo and mute controls need to call AutomationControl::set_value() to emit a...
[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 = 0;
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 */
47         return g_atomic_int_add (&next_number, 1) + 1;
48 }
49
50 VCA::VCA (Session& s,  uint32_t num, const string& name)
51         : Stripable (s, name)
52         , Automatable (s)
53         , _number (num)
54         , _gain_control (new GainControl (s, Evoral::Parameter (GainAutomation), boost::shared_ptr<AutomationList> ()))
55         , _solo_requested (false)
56         , _mute_requested (false)
57 {
58 }
59
60 int
61 VCA::init ()
62 {
63         _solo_control.reset (new VCASoloControllable (X_("solo"), shared_from_this()));
64         _mute_control.reset (new VCAMuteControllable (X_("mute"), shared_from_this()));
65
66         add_control (_gain_control);
67         add_control (_solo_control);
68         add_control (_mute_control);
69
70         return 0;
71 }
72
73 VCA::~VCA ()
74 {
75         DropReferences (); /* emit signal */
76 }
77
78 uint32_t
79 VCA::remote_control_id () const
80 {
81         return 9999999 + _number;
82 }
83
84 XMLNode&
85 VCA::get_state ()
86 {
87         XMLNode* node = new XMLNode (xml_node_name);
88         node->add_property (X_("name"), _name);
89         node->add_property (X_("number"), _number);
90         node->add_property (X_("soloed"), (_solo_requested ? X_("yes") : X_("no")));
91         node->add_property (X_("muted"), (_mute_requested ? X_("yes") : X_("no")));
92
93         node->add_child_nocopy (_gain_control->get_state());
94         node->add_child_nocopy (get_automation_xml_state());
95
96         return *node;
97 }
98
99 int
100 VCA::set_state (XMLNode const& node, int version)
101 {
102         XMLProperty const* prop;
103
104         if ((prop = node.property ("name")) != 0) {
105                 set_name (prop->value());
106         }
107
108         if ((prop = node.property ("number")) != 0) {
109                 _number = atoi (prop->value());
110         }
111
112         XMLNodeList const &children (node.children());
113         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
114                 if ((*i)->name() == Controllable::xml_node_name) {
115                         XMLProperty* prop = (*i)->property ("name");
116                         if (prop && prop->value() == X_("gaincontrol")) {
117                                 _gain_control->set_state (**i, version);
118                         }
119                 }
120         }
121
122         return 0;
123 }
124
125 void
126 VCA::add_solo_mute_target (boost::shared_ptr<Route> r)
127 {
128         Glib::Threads::RWLock::WriterLock lm (solo_mute_lock);
129         solo_mute_targets.push_back (r);
130         r->DropReferences.connect_same_thread (solo_mute_connections, boost::bind (&VCA::solo_mute_target_going_away, this, boost::weak_ptr<Route> (r)));
131 }
132
133 void
134 VCA::remove_solo_mute_target (boost::shared_ptr<Route> r)
135 {
136         Glib::Threads::RWLock::WriterLock lm (solo_mute_lock);
137         solo_mute_targets.remove (r);
138 }
139
140 void
141 VCA::solo_mute_target_going_away (boost::weak_ptr<Route> wr)
142 {
143         boost::shared_ptr<Route> r (wr.lock());
144         if (!r) {
145                 return;
146         }
147
148         Glib::Threads::RWLock::WriterLock lm (solo_mute_lock);
149         solo_mute_targets.remove (r);
150 }
151
152 void
153 VCA::set_solo (bool yn)
154 {
155         {
156                 Glib::Threads::RWLock::ReaderLock lm (solo_mute_lock);
157
158                 if (yn == _solo_requested) {
159                         return;
160                 }
161
162                 if (solo_mute_targets.empty()) {
163                         return;
164                 }
165
166                 boost::shared_ptr<RouteList> rl (new RouteList (solo_mute_targets));
167
168                 if (Config->get_solo_control_is_listen_control()) {
169                         _session.set_listen (rl, yn, Session::rt_cleanup, Controllable::NoGroup);
170                 } else {
171                         _session.set_solo (rl, yn, Session::rt_cleanup, Controllable::NoGroup);
172                 }
173         }
174
175         _solo_requested = yn;
176 }
177
178 void
179 VCA::set_mute (bool yn)
180 {
181         {
182                 Glib::Threads::RWLock::ReaderLock lm (solo_mute_lock);
183                 if (yn == _mute_requested) {
184                         return;
185                 }
186
187                 boost::shared_ptr<RouteList> rl (new RouteList (solo_mute_targets));
188                 _session.set_mute (rl, yn, Session::rt_cleanup, Controllable::NoGroup);
189         }
190
191         _mute_requested = yn;
192 }
193
194 bool
195 VCA::soloed () const
196 {
197         return _solo_requested;
198 }
199
200 bool
201 VCA::muted () const
202 {
203         return _mute_requested;
204 }
205
206 VCA::VCASoloControllable::VCASoloControllable (string const & name, boost::shared_ptr<VCA> vca)
207         : AutomationControl (vca->session(), Evoral::Parameter (SoloAutomation), ParameterDescriptor (Evoral::Parameter (SoloAutomation)),
208                              boost::shared_ptr<AutomationList>(), name)
209         , _vca (vca)
210 {
211 }
212
213 void
214 VCA::VCASoloControllable::set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
215 {
216         if (writable()) {
217                 _set_value (val, gcd);
218         }
219 }
220
221 void
222 VCA::VCASoloControllable::_set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
223 {
224         boost::shared_ptr<VCA> vca = _vca.lock();
225         if (!vca) {
226                 return;
227         }
228
229         vca->set_solo (val >= 0.5);
230
231         AutomationControl::set_value (val, gcd);
232 }
233
234 void
235 VCA::VCASoloControllable::set_value_unchecked (double val)
236 {
237         /* used only by automation playback */
238         _set_value (val, Controllable::NoGroup);
239 }
240
241 double
242 VCA::VCASoloControllable::get_value() const
243 {
244         boost::shared_ptr<VCA> vca = _vca.lock();
245         if (!vca) {
246                 return 0.0;
247         }
248
249         return vca->soloed() ? 1.0 : 0.0;
250 }
251
252 /*----*/
253
254 VCA::VCAMuteControllable::VCAMuteControllable (string const & name, boost::shared_ptr<VCA> vca)
255         : AutomationControl (vca->session(), Evoral::Parameter (MuteAutomation), ParameterDescriptor (Evoral::Parameter (MuteAutomation)),
256                              boost::shared_ptr<AutomationList>(), name)
257         , _vca (vca)
258 {
259 }
260
261 void
262 VCA::VCAMuteControllable::set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
263 {
264         if (writable()) {
265                 _set_value (val, gcd);
266         }
267 }
268
269 void
270 VCA::VCAMuteControllable::_set_value (double val, PBD::Controllable::GroupControlDisposition gcd)
271 {
272         boost::shared_ptr<VCA> vca = _vca.lock();
273
274         if (!vca) {
275                 return;
276         }
277
278         vca->set_mute (val >= 0.5);
279
280         AutomationControl::set_value (val, gcd);
281 }
282
283 void
284 VCA::VCAMuteControllable::set_value_unchecked (double val)
285 {
286         /* used only by automation playback */
287         _set_value (val, Controllable::NoGroup);
288 }
289
290 double
291 VCA::VCAMuteControllable::get_value() const
292 {
293         boost::shared_ptr<VCA> vca = _vca.lock();
294         if (!vca) {
295                 return 0.0;
296         }
297
298         return vca->muted() ? 1.0 : 0.0;
299 }