add new files to source tree
[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         std::cerr << " CG for " << enum_2_string ((AutomationType) _parameter.type()) << " now active ? " << _active << std::endl;
48 }
49
50 void
51 ControlGroup::clear ()
52 {
53         /* we're giving up on all members, so we don't care about their
54          * DropReferences signals anymore
55          */
56
57         member_connections.drop_connections ();
58
59         /* make a copy so that when the control calls ::remove_control(), we
60          * don't deadlock.
61          */
62
63         std::vector<boost::shared_ptr<AutomationControl> > controls;
64         {
65                 Glib::Threads::RWLock::WriterLock lm (controls_lock);
66                 for (ControlMap::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
67                         controls.push_back (i->second);
68                 }
69         }
70
71         _controls.clear ();
72
73         for (std::vector<boost::shared_ptr<AutomationControl> >::iterator c = controls.begin(); c != controls.end(); ++c) {
74                 (*c)->set_group (boost::shared_ptr<ControlGroup>());
75         }
76 }
77
78 ControlList
79 ControlGroup::controls () const
80 {
81         ControlList c;
82
83         if (_active) {
84                 Glib::Threads::RWLock::WriterLock lm (controls_lock);
85                 for (ControlMap::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
86                         c.push_back (i->second);
87                 }
88         }
89
90         return c;
91 }
92
93 void
94 ControlGroup::control_going_away (boost::weak_ptr<AutomationControl> wac)
95 {
96         boost::shared_ptr<AutomationControl> ac (wac.lock());
97         if (!ac) {
98                 return;
99         }
100
101         remove_control (ac);
102 }
103
104 int
105 ControlGroup::remove_control (boost::shared_ptr<AutomationControl> ac)
106 {
107         Glib::Threads::RWLock::WriterLock lm (controls_lock);
108         /* return zero if erased, non-zero otherwise */
109         return !(_controls.erase (ac->id()) > 0);
110 }
111
112 int
113 ControlGroup::add_control (boost::shared_ptr<AutomationControl> ac)
114 {
115         if (ac->parameter() != _parameter) {
116                 return -1;
117         }
118
119         std::pair<ControlMap::iterator,bool> res;
120
121         {
122                 Glib::Threads::RWLock::WriterLock lm (controls_lock);
123                 res = _controls.insert (std::make_pair (ac->id(), ac));
124         }
125
126         if (!res.second) {
127                 /* already in ControlMap */
128                 return -1;
129         }
130
131         /* Inserted */
132
133         ac->set_group (shared_from_this());
134
135         ac->DropReferences.connect_same_thread (member_connections, boost::bind (&ControlGroup::control_going_away, this, boost::weak_ptr<AutomationControl>(ac)));
136
137         return 0;
138 }
139
140 void
141 ControlGroup::set_group_value (boost::shared_ptr<AutomationControl> control, double val)
142 {
143         double old = control->get_value ();
144
145         /* set the primary control */
146
147         control->set_value (val, Controllable::ForGroup);
148
149         /* now propagate across the group */
150
151         Glib::Threads::RWLock::ReaderLock lm (controls_lock);
152
153         if (_mode & Relative) {
154
155                 const double factor = old / control->get_value ();
156
157                 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
158                         if (c->second != control) {
159                                 c->second->set_value (factor * c->second->get_value(), Controllable::ForGroup);
160                         }
161                 }
162
163         } else {
164
165                 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
166                         if (c->second != control) {
167                                 c->second->set_value (val, Controllable::ForGroup);
168                         }
169                 }
170         }
171 }
172
173 /*---- GAIN CONTROL GROUP -----------*/
174
175 gain_t
176 GainControlGroup::get_min_factor (gain_t factor)
177 {
178         /* CALLER MUST HOLD READER LOCK */
179
180         for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
181                 gain_t const g = c->second->get_value();
182
183                 if ((g + g * factor) >= 0.0f) {
184                         continue;
185                 }
186
187                 if (g <= 0.0000003f) {
188                         return 0.0f;
189                 }
190
191                 factor = 0.0000003f / g - 1.0f;
192         }
193
194         return factor;
195 }
196
197 gain_t
198 GainControlGroup::get_max_factor (gain_t factor)
199 {
200         /* CALLER MUST HOLD READER LOCK */
201
202         for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
203                 gain_t const g = c->second->get_value();
204
205                 // if the current factor woulnd't raise this route above maximum
206                 if ((g + g * factor) <= 1.99526231f) {
207                         continue;
208                 }
209
210                 // if route gain is already at peak, return 0.0f factor
211                 if (g >= 1.99526231f) {
212                         return 0.0f;
213                 }
214
215                 // factor is calculated so that it would raise current route to max
216                 factor = 1.99526231f / g - 1.0f;
217         }
218
219         return factor;
220 }
221
222 void
223 GainControlGroup::set_group_value (boost::shared_ptr<AutomationControl> control, double val)
224 {
225         /* set the primary control */
226
227         control->set_value (val, Controllable::ForGroup);
228
229         /* now propagate across the group */
230
231         Glib::Threads::RWLock::ReaderLock lm (controls_lock);
232
233         if (_mode & Relative) {
234
235                 gain_t usable_gain = control->get_value();
236
237                 if (usable_gain < 0.000001f) {
238                         usable_gain = 0.000001f;
239                 }
240
241                 gain_t delta = val;
242                 if (delta < 0.000001f) {
243                         delta = 0.000001f;
244                 }
245
246                 delta -= usable_gain;
247
248                 if (delta == 0.0f)
249                         return;
250
251                 gain_t factor = delta / usable_gain;
252
253                 if (factor > 0.0f) {
254                         factor = get_max_factor (factor);
255                         if (factor == 0.0f) {
256                                 control->Changed (true, Controllable::ForGroup); /* EMIT SIGNAL */
257                                 return;
258                         }
259                 } else {
260                         factor = get_min_factor (factor);
261                         if (factor == 0.0f) {
262                                 control->Changed (true, Controllable::ForGroup); /* EMIT SIGNAL */
263                                 return;
264                         }
265                 }
266
267                 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
268                         if (c->second == control) {
269                                 continue;
270                         }
271
272                         boost::shared_ptr<GainControl> gc = boost::dynamic_pointer_cast<GainControl> (c->second);
273
274                         if (gc) {
275                                 gc->inc_gain (factor);
276                         }
277                 }
278
279         } else {
280
281                 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
282                         if (c->second != control) {
283                                 c->second->set_value (val, Controllable::ForGroup);
284                         }
285                 }
286         }
287 }