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)
50 ControlGroup::clear ()
52 /* we're giving up on all members, so we don't care about their
53 * DropReferences signals anymore
56 member_connections.drop_connections ();
58 /* make a copy so that when the control calls ::remove_control(), we
62 std::vector<boost::shared_ptr<AutomationControl> > controls;
64 Glib::Threads::RWLock::WriterLock lm (controls_lock);
65 for (ControlMap::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
66 controls.push_back (i->second);
72 for (std::vector<boost::shared_ptr<AutomationControl> >::iterator c = controls.begin(); c != controls.end(); ++c) {
73 (*c)->set_group (boost::shared_ptr<ControlGroup>());
78 ControlGroup::controls () const
83 Glib::Threads::RWLock::WriterLock lm (controls_lock);
84 for (ControlMap::const_iterator i = _controls.begin(); i != _controls.end(); ++i) {
85 c.push_back (i->second);
93 ControlGroup::control_going_away (boost::weak_ptr<AutomationControl> wac)
95 boost::shared_ptr<AutomationControl> ac (wac.lock());
104 ControlGroup::remove_control (boost::shared_ptr<AutomationControl> ac)
109 Glib::Threads::RWLock::WriterLock lm (controls_lock);
110 erased = _controls.erase (ac->id());
114 ac->set_group (boost::shared_ptr<ControlGroup>());
117 /* return zero if erased, non-zero otherwise */
122 ControlGroup::add_control (boost::shared_ptr<AutomationControl> ac)
124 if (ac->parameter() != _parameter) {
128 std::pair<ControlMap::iterator,bool> res;
131 Glib::Threads::RWLock::WriterLock lm (controls_lock);
132 res = _controls.insert (std::make_pair (ac->id(), ac));
136 /* already in ControlMap */
142 ac->set_group (shared_from_this());
144 ac->DropReferences.connect_same_thread (member_connections, boost::bind (&ControlGroup::control_going_away, this, boost::weak_ptr<AutomationControl>(ac)));
150 ControlGroup::set_group_value (boost::shared_ptr<AutomationControl> control, double val)
152 double old = control->get_value ();
154 /* set the primary control */
156 control->set_value (val, Controllable::ForGroup);
158 /* now propagate across the group */
160 Glib::Threads::RWLock::ReaderLock lm (controls_lock);
162 if (_mode & Relative) {
164 const double factor = old / control->get_value ();
166 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
167 if (c->second != control) {
168 c->second->set_value (factor * c->second->get_value(), Controllable::ForGroup);
174 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
175 if (c->second != control) {
176 c->second->set_value (val, Controllable::ForGroup);
182 /*---- GAIN CONTROL GROUP -----------*/
185 GainControlGroup::get_min_factor (gain_t factor)
187 /* CALLER MUST HOLD READER LOCK */
189 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
190 gain_t const g = c->second->get_value();
192 if ((g + g * factor) >= 0.0f) {
196 if (g <= 0.0000003f) {
200 factor = 0.0000003f / g - 1.0f;
207 GainControlGroup::get_max_factor (gain_t factor)
209 /* CALLER MUST HOLD READER LOCK */
211 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
212 gain_t const g = c->second->get_value();
214 // if the current factor woulnd't raise this route above maximum
215 if ((g + g * factor) <= 1.99526231f) {
219 // if route gain is already at peak, return 0.0f factor
220 if (g >= 1.99526231f) {
224 // factor is calculated so that it would raise current route to max
225 factor = 1.99526231f / g - 1.0f;
232 GainControlGroup::set_group_value (boost::shared_ptr<AutomationControl> control, double val)
234 /* set the primary control */
236 control->set_value (val, Controllable::ForGroup);
238 /* now propagate across the group */
240 Glib::Threads::RWLock::ReaderLock lm (controls_lock);
242 if (_mode & Relative) {
244 gain_t usable_gain = control->get_value();
246 if (usable_gain < 0.000001f) {
247 usable_gain = 0.000001f;
251 if (delta < 0.000001f) {
255 delta -= usable_gain;
260 gain_t factor = delta / usable_gain;
263 factor = get_max_factor (factor);
264 if (factor == 0.0f) {
265 control->Changed (true, Controllable::ForGroup); /* EMIT SIGNAL */
269 factor = get_min_factor (factor);
270 if (factor == 0.0f) {
271 control->Changed (true, Controllable::ForGroup); /* EMIT SIGNAL */
276 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
277 if (c->second == control) {
281 boost::shared_ptr<GainControl> gc = boost::dynamic_pointer_cast<GainControl> (c->second);
284 gc->inc_gain (factor);
290 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
291 if (c->second != control) {
292 c->second->set_value (val, Controllable::ForGroup);