fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / control_group.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 <vector>
20
21 #include "pbd/unwind.h"
22
23 #include "ardour/control_group.h"
24 #include "ardour/gain_control.h"
25
26 using namespace ARDOUR;
27 using namespace PBD;
28
29 ControlGroup::ControlGroup (Evoral::Parameter p)
30         : _parameter (p)
31         , _active (true)
32         , _mode (Mode (0))
33         , propagating (false)
34 {
35 }
36
37
38 ControlGroup::~ControlGroup ()
39 {
40         clear ();
41 }
42
43 void
44 ControlGroup::set_active (bool yn)
45 {
46         _active = yn;
47 }
48
49 void
50 ControlGroup::set_mode (Mode m)
51 {
52         _mode = m;
53 }
54
55 void
56 ControlGroup::clear ()
57 {
58         /* we're giving up on all members, so we don't care about their
59          * DropReferences signals anymore
60          */
61
62         member_connections.drop_connections ();
63
64         /* make a copy so that when the control calls ::remove_control(), we
65          * don't deadlock.
66          */
67
68         std::vector<boost::shared_ptr<AutomationControl> > controls;
69         {
70                 Glib::Threads::RWLock::WriterLock lm (controls_lock);
71                 for (ControlMap::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
72                         controls.push_back (i->second);
73                 }
74         }
75
76         _controls.clear ();
77
78         for (std::vector<boost::shared_ptr<AutomationControl> >::iterator c = controls.begin(); c != controls.end(); ++c) {
79                 (*c)->set_group (boost::shared_ptr<ControlGroup>());
80         }
81 }
82
83 ControlList
84 ControlGroup::controls () const
85 {
86         ControlList c;
87
88         if (_active) {
89                 Glib::Threads::RWLock::WriterLock lm (controls_lock);
90                 for (ControlMap::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
91                         c.push_back (i->second);
92                 }
93         }
94
95         return c;
96 }
97
98 void
99 ControlGroup::control_going_away (boost::weak_ptr<AutomationControl> wac)
100 {
101         boost::shared_ptr<AutomationControl> ac (wac.lock());
102         if (!ac) {
103                 return;
104         }
105
106         remove_control (ac);
107 }
108
109 int
110 ControlGroup::remove_control (boost::shared_ptr<AutomationControl> ac)
111 {
112         int erased;
113
114         {
115                 Glib::Threads::RWLock::WriterLock lm (controls_lock);
116                 erased = _controls.erase (ac->id());
117         }
118
119         if (erased) {
120                 ac->set_group (boost::shared_ptr<ControlGroup>());
121         }
122
123         /* return zero if erased, non-zero otherwise */
124         return !erased;
125 }
126
127 int
128 ControlGroup::add_control (boost::shared_ptr<AutomationControl> ac)
129 {
130         if (ac->parameter() != _parameter) {
131                 return -1;
132         }
133
134         std::pair<ControlMap::iterator,bool> res;
135
136         {
137                 Glib::Threads::RWLock::WriterLock lm (controls_lock);
138                 res = _controls.insert (std::make_pair (ac->id(), ac));
139         }
140
141         if (!res.second) {
142                 /* already in ControlMap */
143                 return -1;
144         }
145
146         /* Inserted */
147
148         ac->set_group (shared_from_this());
149
150         ac->DropReferences.connect_same_thread (member_connections, boost::bind (&ControlGroup::control_going_away, this, boost::weak_ptr<AutomationControl>(ac)));
151
152         return 0;
153 }
154
155 void
156 ControlGroup::set_group_value (boost::shared_ptr<AutomationControl> control, double val)
157 {
158         double old = control->get_value ();
159
160         /* set the primary control */
161
162         control->set_value (val, Controllable::ForGroup);
163
164         if (!_active) {
165                 return;
166         }
167
168         /* now propagate across the group */
169
170         Glib::Threads::RWLock::ReaderLock lm (controls_lock);
171
172         if (_mode & Relative) {
173
174                 const double factor = old / control->get_value ();
175
176                 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
177                         if (c->second != control) {
178                                 c->second->set_value (factor * c->second->get_value(), Controllable::ForGroup);
179                         }
180                 }
181
182         } else {
183
184                 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
185                         if (c->second != control) {
186                                 c->second->set_value (val, Controllable::ForGroup);
187                         }
188                 }
189         }
190 }
191
192 /*---- GAIN CONTROL GROUP -----------*/
193
194 GainControlGroup::GainControlGroup ()
195         : ControlGroup (GainAutomation)
196 {
197 }
198
199 gain_t
200 GainControlGroup::get_min_factor (gain_t factor)
201 {
202         /* CALLER MUST HOLD READER LOCK */
203
204         for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
205                 gain_t const g = c->second->get_value();
206
207                 if ((g + g * factor) >= 0.0f) {
208                         continue;
209                 }
210
211                 if (g <= 0.0000003f) {
212                         return 0.0f;
213                 }
214
215                 factor = 0.0000003f / g - 1.0f;
216         }
217
218         return factor;
219 }
220
221 gain_t
222 GainControlGroup::get_max_factor (gain_t factor)
223 {
224         /* CALLER MUST HOLD READER LOCK */
225
226         for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
227                 gain_t const g = c->second->get_value();
228
229                 // if the current factor woulnd't raise this route above maximum
230                 if ((g + g * factor) <= 1.99526231f) {
231                         continue;
232                 }
233
234                 // if route gain is already at peak, return 0.0f factor
235                 if (g >= 1.99526231f) {
236                         return 0.0f;
237                 }
238
239                 // factor is calculated so that it would raise current route to max
240                 factor = 1.99526231f / g - 1.0f;
241         }
242
243         return factor;
244 }
245
246 void
247 GainControlGroup::set_group_value (boost::shared_ptr<AutomationControl> control, double val)
248 {
249         if (!_active) {
250                 /* set the primary control */
251                 control->set_value (val, Controllable::ForGroup);
252                 return;
253         }
254
255         Glib::Threads::RWLock::ReaderLock lm (controls_lock);
256
257         if (_mode & Relative) {
258
259                 gain_t usable_gain = control->get_value();
260
261                 if (usable_gain < 0.000001f) {
262                         usable_gain = 0.000001f;
263                 }
264
265                 gain_t delta = val;
266                 if (delta < 0.000001f) {
267                         delta = 0.000001f;
268                 }
269
270                 delta -= usable_gain;
271
272                 if (delta == 0.0f) {
273                         return;
274                 }
275
276                 gain_t factor = delta / usable_gain;
277
278                 if (factor > 0.0f) {
279                         factor = get_max_factor (factor);
280                         if (factor == 0.0f) {
281                                 control->Changed (true, Controllable::ForGroup); /* EMIT SIGNAL */
282                                 return;
283                         }
284                 } else {
285                         factor = get_min_factor (factor);
286                         if (factor == 0.0f) {
287                                 control->Changed (true, Controllable::ForGroup); /* EMIT SIGNAL */
288                                 return;
289                         }
290                 }
291
292                 /* set the primary control */
293
294                 control->set_value (val, Controllable::ForGroup);
295
296                 /* now propagate across the group */
297
298                 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
299                         if (c->second == control) {
300                                 continue;
301                         }
302
303                         boost::shared_ptr<GainControl> gc = boost::dynamic_pointer_cast<GainControl> (c->second);
304
305                         if (gc) {
306                                 gc->inc_gain (factor);
307                         }
308                 }
309
310         } else {
311
312                 /* just set entire group */
313
314                 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
315                         c->second->set_value (val, Controllable::ForGroup);
316                 }
317         }
318 }