7d6a3362deafaecd12a1cd3761a5266ea9e571d3
[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
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
57         }
58
59 }
60
61 void
62 VolumeController::_dB_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c, void* arg)
63 {
64         VolumeController* vc = reinterpret_cast<VolumeController*>(arg);
65         vc->dB_printer (buf, c);
66 }
67
68 void
69 VolumeController::dB_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c) 
70 {
71         if (c) {
72                 
73                 if (_linear) {
74
75                         double val = accurate_coefficient_to_dB (c->get_value());
76
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);
82                                 }
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));
88                                 }
89                         }
90
91                 } else {
92                         
93                         double dB = accurate_coefficient_to_dB (c->get_value());
94
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);
100                                 }
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));
106                                 }
107                         }
108                 }
109         } else {
110                 snprintf (buf, sizeof (buf), "--");
111         }
112 }
113
114 double
115 VolumeController::to_control_value (double display_value)
116 {
117         double v;
118
119         /* display value is always clamped to 0.0 .. 1.0 */
120         display_value = std::max (0.0, std::min (1.0, display_value));
121
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());
126         }
127
128         return v;
129 }
130
131 double
132 VolumeController::to_display_value (double control_value)
133 {
134         double v;
135
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());
140         }
141
142         return v;
143 }
144
145 double
146 VolumeController::adjust (double control_delta)
147 {
148         double v = _controllable->get_value ();
149         double abs_delta = fabs (control_delta);
150
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());
157
158         /* now round to some precision in the dB domain */
159         v = accurate_coefficient_to_dB (v);
160
161         if (abs_delta <= 0.01) {
162                 v -= fmod (v, 0.05);
163         } else {
164                 v -= fmod (v, 0.1);
165         } 
166
167         /* and return it */
168         return dB_to_coefficient (v);
169 }