add VCAStatusChange message to GainControl
[ardour.git] / libs / ardour / gain_control.cc
1 /*
2     Copyright (C) 2006-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 #include "pbd/strsplit.h"
21
22 #include "ardour/dB.h"
23 #include "ardour/gain_control.h"
24 #include "ardour/session.h"
25 #include "ardour/vca.h"
26 #include "ardour/vca_manager.h"
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace std;
32
33 GainControl::GainControl (Session& session, const Evoral::Parameter &param, boost::shared_ptr<AutomationList> al)
34         : AutomationControl (session, param, ParameterDescriptor(param),
35                              al ? al : boost::shared_ptr<AutomationList> (new AutomationList (param)),
36                              param.type() == GainAutomation ? X_("gaincontrol") : X_("trimcontrol")) {
37
38         alist()->reset_default (1.0);
39
40         lower_db = accurate_coefficient_to_dB (_desc.lower);
41         range_db = accurate_coefficient_to_dB (_desc.upper) - lower_db;
42 }
43
44 void
45 GainControl::set_value (double val, PBD::Controllable::GroupControlDisposition group_override)
46 {
47         if (writable()) {
48                 _set_value (val, group_override);
49         }
50 }
51
52 void
53 GainControl::set_value_unchecked (double val)
54 {
55         /* used only automation playback */
56         _set_value (val, Controllable::NoGroup);
57 }
58
59 void
60 GainControl::_set_value (double val, Controllable::GroupControlDisposition group_override)
61 {
62         AutomationControl::set_value (std::max (std::min (val, (double)_desc.upper), (double)_desc.lower), group_override);
63         _session.set_dirty ();
64 }
65
66 double
67 GainControl::internal_to_interface (double v) const
68 {
69         if (_desc.type == GainAutomation) {
70                 return gain_to_slider_position (v);
71         } else {
72                 return (accurate_coefficient_to_dB (v) - lower_db) / range_db;
73         }
74 }
75
76 double
77 GainControl::interface_to_internal (double v) const
78 {
79         if (_desc.type == GainAutomation) {
80                 return slider_position_to_gain (v);
81         } else {
82                 return dB_to_coefficient (lower_db + v * range_db);
83         }
84 }
85
86 double
87 GainControl::internal_to_user (double v) const
88 {
89         return accurate_coefficient_to_dB (v);
90 }
91
92 double
93 GainControl::user_to_internal (double u) const
94 {
95         return dB_to_coefficient (u);
96 }
97
98 std::string
99 GainControl::get_user_string () const
100 {
101         char theBuf[32]; sprintf( theBuf, _("%3.1f dB"), accurate_coefficient_to_dB (get_value()));
102         return std::string(theBuf);
103 }
104
105 gain_t
106 GainControl::get_master_gain () const
107 {
108         Glib::Threads::Mutex::Lock sm (master_lock, Glib::Threads::TRY_LOCK);
109
110         if (sm.locked()) {
111                 return get_master_gain_locked ();
112         }
113
114         return 1.0;
115 }
116
117 gain_t
118 GainControl::get_master_gain_locked () const
119 {
120         /* Master lock MUST be held */
121
122         gain_t g = 1.0;
123
124         for (Masters::const_iterator m = _masters.begin(); m != _masters.end(); ++m) {
125                 g *= (*m)->get_value ();
126         }
127
128         return g;
129 }
130
131 void
132 GainControl::add_master (boost::shared_ptr<VCA> vca)
133 {
134         gain_t old_master_val;
135         gain_t new_master_val;
136         std::pair<set<boost::shared_ptr<GainControl> >::iterator,bool> res;
137
138         {
139                 Glib::Threads::Mutex::Lock lm (master_lock);
140                 old_master_val = get_master_gain_locked ();
141                 res = _masters.insert (vca->control());
142                 _masters_numbers.insert (vca->number());
143                 new_master_val = get_master_gain_locked ();
144
145                 /* note that we bind @param m as a weak_ptr<GainControl>, thus
146                    avoiding holding a reference to the control in the binding
147                    itself.
148                 */
149
150                 vca->DropReferences.connect_same_thread (masters_connections, boost::bind (&GainControl::master_going_away, this, vca));
151
152         }
153
154         if (old_master_val != new_master_val) {
155                 Changed(); /* EMIT SIGNAL */
156         }
157
158         if (res.second) {
159                 VCAStatusChange (); /* EMIT SIGNAL */
160         }
161 }
162
163 void
164 GainControl::master_going_away (boost::weak_ptr<VCA> wv)
165 {
166         boost::shared_ptr<VCA> v = wv.lock();
167         if (v) {
168                 remove_master (v);
169         }
170 }
171
172 void
173 GainControl::remove_master (boost::shared_ptr<VCA> vca)
174 {
175         gain_t old_master_val;
176         gain_t new_master_val;
177         set<boost::shared_ptr<GainControl> >::size_type erased = 0;
178
179         {
180                 Glib::Threads::Mutex::Lock lm (master_lock);
181                 old_master_val = get_master_gain_locked ();
182                 erased = _masters.erase (vca->control());
183                 _masters_numbers.erase (vca->number());
184                 new_master_val = get_master_gain_locked ();
185         }
186
187         if (old_master_val != new_master_val) {
188                 Changed(); /* EMIT SIGNAL */
189         }
190
191         if (erased) {
192                 VCAStatusChange (); /* EMIT SIGNAL */
193         }
194 }
195
196 void
197 GainControl::clear_masters ()
198 {
199         gain_t old_master_val;
200         gain_t new_master_val;
201         bool had_masters = false;
202
203         {
204                 Glib::Threads::Mutex::Lock lm (master_lock);
205                 old_master_val = get_master_gain_locked ();
206                 if (!_masters.empty()) {
207                         had_masters = true;
208                 }
209                 _masters.clear ();
210                 _masters_numbers.clear ();
211                 new_master_val = get_master_gain_locked ();
212         }
213
214         if (old_master_val != new_master_val) {
215                 Changed(); /* EMIT SIGNAL */
216         }
217
218         if (had_masters) {
219                 VCAStatusChange (); /* EMIT SIGNAL */
220         }
221 }
222
223 bool
224 GainControl::slaved_to (boost::shared_ptr<VCA> vca) const
225 {
226         Glib::Threads::Mutex::Lock lm (master_lock);
227         return find (_masters.begin(), _masters.end(), vca->control()) != _masters.end();
228 }
229
230 XMLNode&
231 GainControl::get_state ()
232 {
233         XMLNode& node (AutomationControl::get_state());
234
235         /* store VCA master IDs */
236
237         string str;
238
239         {
240                 Glib::Threads::Mutex::Lock lm (master_lock);
241                 for (set<uint32_t>::const_iterator m = _masters_numbers.begin(); m != _masters_numbers.end(); ++m) {
242                         if (!str.empty()) {
243                                 str += ',';
244                         }
245                         str += PBD::to_string (*m, std::dec);
246                 }
247         }
248
249         if (!str.empty()) {
250                 node.add_property (X_("masters"), str);
251         }
252
253         return node;
254 }
255
256 int
257 GainControl::set_state (XMLNode const& node, int version)
258 {
259         AutomationControl::set_state (node, version);
260
261         XMLProperty const* prop = node.property (X_("masters"));
262
263         /* XXXProblem here if we allow VCA's to be slaved to other VCA's .. we
264          * have to load all VCAs first, then call ::set_state() so that
265          * vca_by_number() will succeed.
266          */
267
268         if (prop) {
269                 vector<string> masters;
270                 split (prop->value(), masters, ',');
271
272                 for (vector<string>::const_iterator m = masters.begin(); m != masters.end(); ++m) {
273                         boost::shared_ptr<VCA> vca = _session.vca_manager().vca_by_number (PBD::atoi (*m));
274                         if (vca) {
275                                 add_master (vca);
276                         }
277                 }
278         }
279
280         return 0;
281 }