merge with master
[ardour.git] / gtk2_ardour / volume_controller.cc
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.
7
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.
12
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.
16
17     $Id: volume_controller.cc,v 1.4 2000/05/03 15:54:21 pbd Exp $
18 */
19
20 #include <algorithm>
21
22 #include <string.h>
23 #include <limits.h>
24
25 #include "pbd/controllable.h"
26 #include "pbd/stacktrace.h"
27
28 #include "gtkmm2ext/gui_thread.h"
29
30 #include "ardour/dB.h"
31 #include "ardour/rc_configuration.h"
32 #include "ardour/utils.h"
33
34 #include "volume_controller.h"
35
36 using namespace Gtk;
37
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)
47
48         : MotionFeedback (p, MotionFeedback::Rotary, c, def, step, page, "", with_numeric, subw, subh)
49         , _linear (linear)
50 {
51         set_print_func (VolumeController::_dB_printer, this);
52         value->set_width_chars (8);
53 }
54
55 void
56 VolumeController::_dB_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c, void* arg)
57 {
58         VolumeController* vc = reinterpret_cast<VolumeController*>(arg);
59         vc->dB_printer (buf, c);
60 }
61
62 void
63 VolumeController::dB_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c) 
64 {
65         if (c) {
66                 
67                 if (_linear) {
68
69                         double val = accurate_coefficient_to_dB (c->get_value());
70
71                         if (step_inc < 1.0) {
72                                 if (val >= 0.0) {
73                                         snprintf (buf, 32, "+%5.2f dB", val);
74                                 } else {
75                                         snprintf (buf, 32, "%5.2f dB", val);
76                                 }
77                         } else {
78                                 if (val >= 0.0) {
79                                         snprintf (buf, 32, "+%2ld dB", lrint (val));
80                                 } else {
81                                         snprintf (buf, 32, "%2ld dB", lrint (val));
82                                 }
83                         }
84
85                 } else {
86                         
87                         double dB = accurate_coefficient_to_dB (c->get_value());
88
89                         if (step_inc < 1.0) {
90                                 if (dB >= 0.0) {
91                                         snprintf (buf, 32, "+%5.2f dB", dB);
92                                 } else {
93                                         snprintf (buf, 32, "%5.2f dB", dB);
94                                 }
95                         } else {
96                                 if (dB >= 0.0) {
97                                         snprintf (buf, 32, "+%2ld dB", lrint (dB));
98                                 } else {
99                                         snprintf (buf, 32, "%2ld dB", lrint (dB));
100                                 }
101                         }
102                 }
103         } else {
104                 snprintf (buf, 32, "--");
105         }
106 }
107
108 double
109 VolumeController::to_control_value (double display_value)
110 {
111         double v;
112
113         /* display value is always clamped to 0.0 .. 1.0 */
114         display_value = std::max (0.0, std::min (1.0, display_value));
115
116         if (_linear) {
117                 v = _controllable->lower() + ((_controllable->upper() - _controllable->lower()) * display_value);
118         } else {
119                 v = slider_position_to_gain_with_max (display_value, ARDOUR::Config->get_max_gain());
120         }
121
122         return v;
123 }
124
125 double
126 VolumeController::to_display_value (double control_value)
127 {
128         double v;
129
130         if (_linear) {
131                 v = (control_value - _controllable->lower ()) / (_controllable->upper() - _controllable->lower());
132         } else {
133                 v = gain_to_slider_position_with_max (control_value, _controllable->upper());
134         }
135
136         return v;
137 }
138
139 double
140 VolumeController::adjust (double control_delta)
141 {
142         double v;
143
144         if (!_linear) {
145
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.
152                  */
153
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 */
157                 v += control_delta;
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;
169                 } else {
170                         v = floor (v * 10.0) / 10.0;
171                 }
172                 /* and return it */
173                 return dB_to_coefficient (v);
174         } else {
175                 double mult;
176
177                 if (control_delta < 0.0) {
178                         mult = -1.0;
179                 } else {
180                         mult = 1.0;
181                 }
182
183                 if (fabs (control_delta) < 0.05) {
184                         control_delta = mult * 0.05;
185                 } else  {
186                         control_delta = mult * 0.1;
187                 }
188
189                 v = _controllable->get_value();
190
191                 if (v == 0.0) {
192                         /* if we don't special case this, we can't escape from
193                            the -infinity dB black hole.
194                         */
195                         if (control_delta > 0.0) {
196                                 v = dB_to_coefficient (-100 + control_delta);
197                         }
198                 } else {
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);
203
204                         if (control_delta < 0 && v < dB_minus_200) {
205                                 v = 0.0;
206                         } else {
207
208                                 /* non-linear scaling as the dB level gets low 
209                                    so that we can hit -inf and get back out of
210                                    it appropriately.
211                                 */
212
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;
219                                 }
220
221                                 v = accurate_coefficient_to_dB (v);
222                                 v += control_delta;
223                                 v = dB_to_coefficient (v);
224                         }
225                 }
226
227                 return std::max (_controllable->lower(), std::min (_controllable->upper(), v));
228         }
229
230 }