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)
107 Glib::Threads::RWLock::WriterLock lm (controls_lock);
108 /* return zero if erased, non-zero otherwise */
109 return !(_controls.erase (ac->id()) > 0);
113 ControlGroup::add_control (boost::shared_ptr<AutomationControl> ac)
115 if (ac->parameter() != _parameter) {
119 std::pair<ControlMap::iterator,bool> res;
122 Glib::Threads::RWLock::WriterLock lm (controls_lock);
123 res = _controls.insert (std::make_pair (ac->id(), ac));
127 /* already in ControlMap */
133 ac->set_group (shared_from_this());
135 ac->DropReferences.connect_same_thread (member_connections, boost::bind (&ControlGroup::control_going_away, this, boost::weak_ptr<AutomationControl>(ac)));
141 ControlGroup::set_group_value (boost::shared_ptr<AutomationControl> control, double val)
143 double old = control->get_value ();
145 /* set the primary control */
147 control->set_value (val, Controllable::ForGroup);
149 /* now propagate across the group */
151 Glib::Threads::RWLock::ReaderLock lm (controls_lock);
153 if (_mode & Relative) {
155 const double factor = old / control->get_value ();
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);
165 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
166 if (c->second != control) {
167 c->second->set_value (val, Controllable::ForGroup);
173 /*---- GAIN CONTROL GROUP -----------*/
176 GainControlGroup::get_min_factor (gain_t factor)
178 /* CALLER MUST HOLD READER LOCK */
180 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
181 gain_t const g = c->second->get_value();
183 if ((g + g * factor) >= 0.0f) {
187 if (g <= 0.0000003f) {
191 factor = 0.0000003f / g - 1.0f;
198 GainControlGroup::get_max_factor (gain_t factor)
200 /* CALLER MUST HOLD READER LOCK */
202 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
203 gain_t const g = c->second->get_value();
205 // if the current factor woulnd't raise this route above maximum
206 if ((g + g * factor) <= 1.99526231f) {
210 // if route gain is already at peak, return 0.0f factor
211 if (g >= 1.99526231f) {
215 // factor is calculated so that it would raise current route to max
216 factor = 1.99526231f / g - 1.0f;
223 GainControlGroup::set_group_value (boost::shared_ptr<AutomationControl> control, double val)
225 /* set the primary control */
227 control->set_value (val, Controllable::ForGroup);
229 /* now propagate across the group */
231 Glib::Threads::RWLock::ReaderLock lm (controls_lock);
233 if (_mode & Relative) {
235 gain_t usable_gain = control->get_value();
237 if (usable_gain < 0.000001f) {
238 usable_gain = 0.000001f;
242 if (delta < 0.000001f) {
246 delta -= usable_gain;
251 gain_t factor = delta / usable_gain;
254 factor = get_max_factor (factor);
255 if (factor == 0.0f) {
256 control->Changed (true, Controllable::ForGroup); /* EMIT SIGNAL */
260 factor = get_min_factor (factor);
261 if (factor == 0.0f) {
262 control->Changed (true, Controllable::ForGroup); /* EMIT SIGNAL */
267 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
268 if (c->second == control) {
272 boost::shared_ptr<GainControl> gc = boost::dynamic_pointer_cast<GainControl> (c->second);
275 gc->inc_gain (factor);
281 for (ControlMap::iterator c = _controls.begin(); c != _controls.end(); ++c) {
282 if (c->second != control) {
283 c->second->set_value (val, Controllable::ForGroup);