2 Copyright (C) 1998-2007 Paul Davis
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 $Id: volume_controller.cc,v 1.4 2000/05/03 15:54:21 pbd Exp $
25 #include "pbd/controllable.h"
26 #include "pbd/stacktrace.h"
28 #include "gtkmm2ext/gui_thread.h"
30 #include "ardour/dB.h"
31 #include "ardour/rc_configuration.h"
32 #include "ardour/utils.h"
34 #include "volume_controller.h"
38 VolumeController::VolumeController (Glib::RefPtr<Gdk::Pixbuf> p,
39 boost::shared_ptr<PBD::Controllable> c,
48 : MotionFeedback (p, MotionFeedback::Rotary, c, def, step, page, "", with_numeric, subw, subh)
51 set_print_func (VolumeController::_dB_printer, this);
52 value->set_width_chars (8);
56 VolumeController::_dB_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c, void* arg)
58 VolumeController* vc = reinterpret_cast<VolumeController*>(arg);
59 vc->dB_printer (buf, c);
63 VolumeController::dB_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c)
69 double val = accurate_coefficient_to_dB (c->get_value());
73 snprintf (buf, 32, "+%5.2f dB", val);
75 snprintf (buf, 32, "%5.2f dB", val);
79 snprintf (buf, 32, "+%2ld dB", lrint (val));
81 snprintf (buf, 32, "%2ld dB", lrint (val));
87 double dB = accurate_coefficient_to_dB (c->get_value());
91 snprintf (buf, 32, "+%5.2f dB", dB);
93 snprintf (buf, 32, "%5.2f dB", dB);
97 snprintf (buf, 32, "+%2ld dB", lrint (dB));
99 snprintf (buf, 32, "%2ld dB", lrint (dB));
104 snprintf (buf, 32, "--");
109 VolumeController::to_control_value (double display_value)
113 /* display value is always clamped to 0.0 .. 1.0 */
114 display_value = std::max (0.0, std::min (1.0, display_value));
117 v = _controllable->lower() + ((_controllable->upper() - _controllable->lower()) * display_value);
119 v = slider_position_to_gain_with_max (display_value, ARDOUR::Config->get_max_gain());
126 VolumeController::to_display_value (double control_value)
131 v = (control_value - _controllable->lower ()) / (_controllable->upper() - _controllable->lower());
133 v = gain_to_slider_position_with_max (control_value, _controllable->upper());
140 VolumeController::adjust (double control_delta)
146 /* we map back into the linear/fractional slider position,
147 * because this kind of control goes all the way down
148 * to -inf dB, and we want this occur in a reasonable way in
149 * terms of user interaction. if we leave the adjustment in the
150 * gain coefficient domain (or dB domain), the lower end of the
151 * control range (getting close to -inf dB) takes forever.
154 /* convert to linear/fractional slider position domain */
155 v = gain_to_slider_position_with_max (_controllable->get_value (), _controllable->upper());
156 /* increment in this domain */
158 /* clamp to appropriate range for linear/fractional slider domain */
159 v = std::max (0.0, std::min (1.0, v));
160 /* convert back to gain coefficient domain */
161 v = slider_position_to_gain_with_max (v, _controllable->upper());
162 /* clamp in controller domain */
163 v = std::max (_controllable->lower(), std::min (_controllable->upper(), v));
164 /* convert to dB domain */
165 v = accurate_coefficient_to_dB (v);
166 /* round up/down to nearest 0.1dB */
167 if (control_delta > 0.0) {
168 v = ceil (v * 10.0) / 10.0;
170 v = floor (v * 10.0) / 10.0;
173 return dB_to_coefficient (v);
177 if (control_delta < 0.0) {
183 if (fabs (control_delta) < 0.05) {
184 control_delta = mult * 0.05;
186 control_delta = mult * 0.1;
189 v = _controllable->get_value();
192 /* if we don't special case this, we can't escape from
193 the -infinity dB black hole.
195 if (control_delta > 0.0) {
196 v = dB_to_coefficient (-100 + control_delta);
199 static const double dB_minus_200 = dB_to_coefficient (-200.0);
200 static const double dB_minus_100 = dB_to_coefficient (-100.0);
201 static const double dB_minus_50 = dB_to_coefficient (-50.0);
202 static const double dB_minus_20 = dB_to_coefficient (-20.0);
204 if (control_delta < 0 && v < dB_minus_200) {
208 /* non-linear scaling as the dB level gets low
209 so that we can hit -inf and get back out of
213 if (v < dB_minus_100) {
214 control_delta *= 1000.0;
215 } else if (v < dB_minus_50) {
216 control_delta *= 100.0;
217 } else if (v < dB_minus_20) {
218 control_delta *= 10.0;
221 v = accurate_coefficient_to_dB (v);
223 v = dB_to_coefficient (v);
227 return std::max (_controllable->lower(), std::min (_controllable->upper(), v));