2 Copyright (C) 2016 Paul Davis
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)
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
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.
21 #include "pbd/unwind.h"
23 #include "ardour/control_group.h"
24 #include "ardour/gain_control.h"
26 using namespace ARDOUR;
29 ControlGroup::ControlGroup (Evoral::Parameter p)
38 ControlGroup::~ControlGroup ()
44 ControlGroup::set_active (bool yn)
47 std::cerr << " CG for " << enum_2_string ((AutomationType) _parameter.type()) << " now active ? " << _active << std::endl;
51 ControlGroup::clear ()
53 /* we're giving up on all members, so we don't care about their
54 * DropReferences signals anymore
57 member_connections.drop_connections ();
59 /* make a copy so that when the control calls ::remove_control(), we
63 std::vector<boost::shared_ptr<AutomationControl> > controls;
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);
73 for (std::vector<boost::shared_ptr<AutomationControl> >::iterator c = controls.begin(); c != controls.end(); ++c) {
74 (*c)->set_group (boost::shared_ptr<ControlGroup>());
79 ControlGroup::controls () const
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);
94 ControlGroup::control_going_away (boost::weak_ptr<AutomationControl> wac)
96 boost::shared_ptr<AutomationControl> ac (wac.lock());
105 ControlGroup::remove_control (boost::shared_ptr<AutomationControl> ac)
110 Glib::Threads::RWLock::WriterLock lm (controls_lock);
111 erased = _controls.erase (ac->id());
115 ac->set_group (boost::shared_ptr<ControlGroup>());
118 /* return zero if erased, non-zero otherwise */
123 ControlGroup::add_control (boost::shared_ptr<AutomationControl> ac)
125 if (ac->parameter() != _parameter) {
129 std::pair<ControlMap::iterator,bool> res;
132 Glib::Threads::RWLock::WriterLock lm (controls_lock);
133 res = _controls.insert (std::make_pair (ac->id(), ac));
137 /* already in ControlMap */
143 ac->set_group (shared_from_this());
145 ac->DropReferences.connect_same_thread (member_connections, boost::bind (&ControlGroup::control_going_away, this, boost::weak_ptr<AutomationControl>(ac)));
151 ControlGroup::set_group_value (boost::shared_ptr<AutomationControl> control, double val)
153 double old = control->get_value ();
155 /* set the primary control */
157 control->set_value (val, Controllable::ForGroup);
159 /* now propagate across the group */
161 Glib::Threads::RWLock::ReaderLock lm (controls_lock);
163 if (_mode & Relative) {
165 const double factor = old / control->get_value ();
167 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
168 if (c->second != control) {
169 c->second->set_value (factor * c->second->get_value(), Controllable::ForGroup);
175 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
176 if (c->second != control) {
177 c->second->set_value (val, Controllable::ForGroup);
183 /*---- GAIN CONTROL GROUP -----------*/
186 GainControlGroup::get_min_factor (gain_t factor)
188 /* CALLER MUST HOLD READER LOCK */
190 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
191 gain_t const g = c->second->get_value();
193 if ((g + g * factor) >= 0.0f) {
197 if (g <= 0.0000003f) {
201 factor = 0.0000003f / g - 1.0f;
208 GainControlGroup::get_max_factor (gain_t factor)
210 /* CALLER MUST HOLD READER LOCK */
212 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
213 gain_t const g = c->second->get_value();
215 // if the current factor woulnd't raise this route above maximum
216 if ((g + g * factor) <= 1.99526231f) {
220 // if route gain is already at peak, return 0.0f factor
221 if (g >= 1.99526231f) {
225 // factor is calculated so that it would raise current route to max
226 factor = 1.99526231f / g - 1.0f;
233 GainControlGroup::set_group_value (boost::shared_ptr<AutomationControl> control, double val)
235 /* set the primary control */
237 control->set_value (val, Controllable::ForGroup);
239 /* now propagate across the group */
241 Glib::Threads::RWLock::ReaderLock lm (controls_lock);
243 if (_mode & Relative) {
245 gain_t usable_gain = control->get_value();
247 if (usable_gain < 0.000001f) {
248 usable_gain = 0.000001f;
252 if (delta < 0.000001f) {
256 delta -= usable_gain;
261 gain_t factor = delta / usable_gain;
264 factor = get_max_factor (factor);
265 if (factor == 0.0f) {
266 control->Changed (true, Controllable::ForGroup); /* EMIT SIGNAL */
270 factor = get_min_factor (factor);
271 if (factor == 0.0f) {
272 control->Changed (true, Controllable::ForGroup); /* EMIT SIGNAL */
277 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
278 if (c->second == control) {
282 boost::shared_ptr<GainControl> gc = boost::dynamic_pointer_cast<GainControl> (c->second);
285 gc->inc_gain (factor);
291 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
292 if (c->second != control) {
293 c->second->set_value (val, Controllable::ForGroup);