ardour
volume_controller.cc
Go to the documentation of this file.
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"
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,
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 {
52  value->set_width_chars (8);
53 }
54 
55 void
57 {
58  VolumeController* vc = reinterpret_cast<VolumeController*>(arg);
59  vc->dB_printer (buf, c);
60 }
61 
62 void
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
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 = ARDOUR::slider_position_to_gain_with_max (display_value, ARDOUR::Config->get_max_gain());
120  }
121 
122  return v;
123 }
124 
125 double
127 {
128  double v;
129 
130  if (_linear) {
131  v = (control_value - _controllable->lower ()) / (_controllable->upper() - _controllable->lower());
132  } else {
134  }
135 
136  return v;
137 }
138 
139 double
140 VolumeController::adjust (double control_delta)
141 {
142  double v;
143 
144  if (!_linear) {
145  /* we map back into the linear/fractional slider position,
146  * because this kind of control goes all the way down
147  * to -inf dB, and we want this occur in a reasonable way in
148  * terms of user interaction. if we leave the adjustment in the
149  * gain coefficient domain (or dB domain), the lower end of the
150  * control range (getting close to -inf dB) takes forever.
151  */
152 #if 0
153  /* convert to linear/fractional slider position domain */
155  /* increment in this domain */
156  v += control_delta;
157  /* clamp to appropriate range for linear/fractional slider domain */
158  v = std::max (0.0, std::min (1.0, v));
159  /* convert back to gain coefficient domain */
161  /* clamp in controller domain */
162  v = std::max (_controllable->lower(), std::min (_controllable->upper(), v));
163  /* convert to dB domain */
165  /* round up/down to nearest 0.1dB */
166  if (control_delta > 0.0) {
167  v = ceil (v * 10.0) / 10.0;
168  } else {
169  v = floor (v * 10.0) / 10.0;
170  }
171  /* and return it */
172  return dB_to_coefficient (v);
173 #else
174  /* ^^ Above algorithm is not symmetric. Scroll up to steps, scoll down two steps, -> different gain.
175  *
176  * see ./libs/gtkmm2ext/gtkmm2ext/motionfeedback.h and gtk2_ardour/monitor_section.cc:
177  * min-delta (corr) = MIN(0.01 * page inc, 1 * size_inc) // (gain_control uses size_inc=0.01, page_inc=0.1)
178  * range corr: 0..2 -> -inf..+6dB
179  * step sizes [0.01, 0.10, 0.20] * page_inc, [1,2,10,100] * step_inc. [1,2,10,100] * page_inc
180  *
181  * 0.001, 0.01, 0.02, 0.1, .2, 1, 10
182  * -> 1k steps between -inf..0dB
183  * -> 1k steps between 0..+dB
184  *
185  * IOW:
186  * the range is from *0 (-inf dB) to *2.0 ( +6dB)
187  * the knob is configured to to go in steps of 0.001 - that's 2000 steps between 0 and 2.
188  * or 1000 steps between 0 and 1.
189  *
190  * we cannot round to .01dB steps because
191  * There are only 600 possible values between +0db and +6dB when going in steps of .01dB
192  * 1000/600 = 1.66666...
193  *
194  ******
195  * idea: make the 'controllable use a fixed range of dB.
196  * do a 1:1 mapping between values. :et's stick with the range of 0..2 in 0.001 steps
197  *
198  * "-80" becomes 0 and "+6" becomes 2000. (NB +6dB is actually 1995, but we clamp that to the top)
199  *
200  * This approach is better (more consistet) but not good. At least the dial does not annoy me as much
201  * anymore as it did before.
202  *
203  * const double stretchfactor = rint((_controllable->upper() - _controllable->lower()) / 0.001); // 2000;
204  * const double logfactor = stretchfactor / ((20.0 * log10( _controllable->upper())) + 80.0); // = 23.250244732
205  */
206  v = _controllable->get_value ();
207  /* assume everything below -60dB is silent (.001 ^= -60dB)
208  * but map range -80db..+6dB to a scale of 0..2000
209  * 80db was motivated because 2000/((20.0 * log(1)) + 80.0) is an integer value. "0dB" is included on the scale.
210  * but this leaves a dead area at the bottom of the meter..
211  */
212  double arange = (v >= 0.001) ? ( ((20.0 * log10(v)) + 80.0) * 23.250244732 ) : ( 0 );
213  /* add the delta */
214  v = rint(arange) + rint(control_delta * 1000.0); // (min steps is 1.0/0.001 == 1000.0)
215  /* catch bottom -80..-60 db in one step */
216  if (v < 466) v = (control_delta > 0) ? 0.001 : 0;
217  /* reverse operation (pow(10, .05 * ((v / 23.250244732) - 80.0)))
218  * can be simplified to :*/
219  else v = pow(10, (v * 0.00215051499) - 4.0);
220  /* clamp value in coefficient domain */
221  v = std::max (_controllable->lower(), std::min (_controllable->upper(), v));
222  return v;
223 #endif
224  } else {
225  double mult;
226 
227  if (control_delta < 0.0) {
228  mult = -1.0;
229  } else {
230  mult = 1.0;
231  }
232 
233  if (fabs (control_delta) < 0.05) {
234  control_delta = mult * 0.05;
235  } else {
236  control_delta = mult * 0.1;
237  }
238 
239  v = _controllable->get_value();
240 
241  if (v == 0.0) {
242  /* if we don't special case this, we can't escape from
243  the -infinity dB black hole.
244  */
245  if (control_delta > 0.0) {
246  v = dB_to_coefficient (-100 + control_delta);
247  }
248  } else {
249  static const double dB_minus_200 = dB_to_coefficient (-200.0);
250  static const double dB_minus_100 = dB_to_coefficient (-100.0);
251  static const double dB_minus_50 = dB_to_coefficient (-50.0);
252  static const double dB_minus_20 = dB_to_coefficient (-20.0);
253 
254  if (control_delta < 0 && v < dB_minus_200) {
255  v = 0.0;
256  } else {
257 
258  /* non-linear scaling as the dB level gets low
259  so that we can hit -inf and get back out of
260  it appropriately.
261  */
262 
263  if (v < dB_minus_100) {
264  control_delta *= 1000.0;
265  } else if (v < dB_minus_50) {
266  control_delta *= 100.0;
267  } else if (v < dB_minus_20) {
268  control_delta *= 10.0;
269  }
270 
272  v += control_delta;
273  v = dB_to_coefficient (v);
274  }
275  }
276 
277  return std::max (_controllable->lower(), std::min (_controllable->upper(), v));
278  }
279 
280 }
void set_print_func(void(*pf)(char buf[32], const boost::shared_ptr< PBD::Controllable > &, void *), void *arg)
Definition: ardour_ui.h:130
LIBARDOUR_API double slider_position_to_gain_with_max(double g, double max_gain=2.0)
Definition: utils.cc:762
double to_display_value(double)
virtual double upper() const
Definition: controllable.h:108
double adjust(double nominal_delta)
static float accurate_coefficient_to_dB(float coeff)
Definition: dB.h:38
LIBARDOUR_API RCConfiguration * Config
Definition: globals.cc:119
virtual double lower() const
Definition: controllable.h:107
virtual double get_value(void) const =0
LIBARDOUR_API double gain_to_slider_position_with_max(double g, double max_gain=2.0)
Definition: utils.cc:756
void dB_printer(char buf[32], const boost::shared_ptr< PBD::Controllable > &adj)
static void _dB_printer(char buf[32], const boost::shared_ptr< PBD::Controllable > &adj, void *arg)
VolumeController(Glib::RefPtr< Gdk::Pixbuf >, boost::shared_ptr< PBD::Controllable >, double def, double step, double page, bool with_numeric=true, int image_width=40, int image_height=40, bool linear=true)
static float dB_to_coefficient(float dB)
Definition: dB.h:30
boost::shared_ptr< PBD::Controllable > _controllable
double to_control_value(double)