change VolumeController::adjust() to do discrete dB increments+decrements
[ardour2.git] / gtk2_ardour / volume_controller.cc
blob7d6a3362deafaecd12a1cd3761a5266ea9e571d3
1 /*
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 $
20 #include <algorithm>
22 #include <string.h>
23 #include <limits.h>
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"
36 using namespace Gtk;
38 VolumeController::VolumeController (Glib::RefPtr<Gdk::Pixbuf> p,
39 boost::shared_ptr<PBD::Controllable> c,
40 double def,
41 double step,
42 double page,
43 bool with_numeric,
44 int subw,
45 int subh,
46 bool linear)
48 : MotionFeedback (p, MotionFeedback::Rotary, c, def, step, page, "", with_numeric, subw, subh)
49 , _linear (linear)
51 set_print_func (VolumeController::_dB_printer, this);
53 if (step < 1.0) {
54 value->set_width_chars (6 + abs ((int) ceil (log10 (step))));
55 } else {
56 value->set_width_chars (5); // -NNdB
61 void
62 VolumeController::_dB_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c, void* arg)
64 VolumeController* vc = reinterpret_cast<VolumeController*>(arg);
65 vc->dB_printer (buf, c);
68 void
69 VolumeController::dB_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c)
71 if (c) {
73 if (_linear) {
75 double val = accurate_coefficient_to_dB (c->get_value());
77 if (step_inc < 1.0) {
78 if (val >= 0.0) {
79 snprintf (buf, 32, "+%5.2f dB", val);
80 } else {
81 snprintf (buf, 32, "%5.2f dB", val);
83 } else {
84 if (val >= 0.0) {
85 snprintf (buf, 32, "+%2ld dB", lrint (val));
86 } else {
87 snprintf (buf, 32, "%2ld dB", lrint (val));
91 } else {
93 double dB = accurate_coefficient_to_dB (c->get_value());
95 if (step_inc < 1.0) {
96 if (dB >= 0.0) {
97 snprintf (buf, 32, "+%5.2f dB", dB);
98 } else {
99 snprintf (buf, 32, "%5.2f dB", dB);
101 } else {
102 if (dB >= 0.0) {
103 snprintf (buf, 32, "+%2ld dB", lrint (dB));
104 } else {
105 snprintf (buf, 32, "%2ld dB", lrint (dB));
109 } else {
110 snprintf (buf, sizeof (buf), "--");
114 double
115 VolumeController::to_control_value (double display_value)
117 double v;
119 /* display value is always clamped to 0.0 .. 1.0 */
120 display_value = std::max (0.0, std::min (1.0, display_value));
122 if (_linear) {
123 v = _controllable->lower() + ((_controllable->upper() - _controllable->lower()) * display_value);
124 } else {
125 v = slider_position_to_gain_with_max (display_value, ARDOUR::Config->get_max_gain());
128 return v;
131 double
132 VolumeController::to_display_value (double control_value)
134 double v;
136 if (_linear) {
137 v = (control_value - _controllable->lower ()) / (_controllable->upper() - _controllable->lower());
138 } else {
139 v = gain_to_slider_position_with_max (control_value, ARDOUR::Config->get_max_gain());
142 return v;
145 double
146 VolumeController::adjust (double control_delta)
148 double v = _controllable->get_value ();
149 double abs_delta = fabs (control_delta);
151 /* convert to linear/fractional slider position domain */
152 v = gain_to_slider_position_with_max (v, ARDOUR::Config->get_max_gain());
153 /* adjust in this domain */
154 v += control_delta;
155 /* convert back to gain coefficient domain */
156 v = slider_position_to_gain_with_max (v, ARDOUR::Config->get_max_gain());
158 /* now round to some precision in the dB domain */
159 v = accurate_coefficient_to_dB (v);
161 if (abs_delta <= 0.01) {
162 v -= fmod (v, 0.05);
163 } else {
164 v -= fmod (v, 0.1);
167 /* and return it */
168 return dB_to_coefficient (v);