summaryrefslogtreecommitdiff
path: root/gtk2_ardour/volume_controller.cc
blob: 365379933dc29143d6fb8c6f416a661cfbb55e44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
    Copyright (C) 1998-2007 Paul Davis
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    $Id: volume_controller.cc,v 1.4 2000/05/03 15:54:21 pbd Exp $
*/

#include <algorithm>

#include <string.h>
#include <limits.h>

#include "pbd/controllable.h"
#include "pbd/stacktrace.h"

#include "gtkmm2ext/gui_thread.h"

#include "ardour/dB.h"
#include "ardour/rc_configuration.h"
#include "ardour/utils.h"

#include "volume_controller.h"

using namespace Gtk;

VolumeController::VolumeController (Glib::RefPtr<Gdk::Pixbuf> p,
				    boost::shared_ptr<PBD::Controllable> c,
				    double def,
				    double step,
				    double page,
				    bool with_numeric,
                                    int subw, 
				    int subh,
				    bool linear)

	: MotionFeedback (p, MotionFeedback::Rotary, c, def, step, page, "", with_numeric, subw, subh)
	, _linear (linear)
{
	set_print_func (VolumeController::_dB_printer, this);

	if (step < 1.0) {
		value->set_width_chars (6 + abs ((int) ceil (log10 (step))));
	} else {
		value->set_width_chars (5); // -NNdB
	}

}

void
VolumeController::_dB_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c, void* arg)
{
	VolumeController* vc = reinterpret_cast<VolumeController*>(arg);
	vc->dB_printer (buf, c);
}

void
VolumeController::dB_printer (char buf[32], const boost::shared_ptr<PBD::Controllable>& c) 
{
	if (c) {
		
		if (_linear) {

			double val = accurate_coefficient_to_dB (c->get_value());

			if (step_inc < 1.0) {
				if (val >= 0.0) {
					snprintf (buf, 32, "+%5.2f dB", val);
				} else {
					snprintf (buf, 32, "%5.2f dB", val);
				}
			} else {
				if (val >= 0.0) {
					snprintf (buf, 32, "+%2ld dB", lrint (val));
				} else {
					snprintf (buf, 32, "%2ld dB", lrint (val));
				}
			}

		} else {
			
			double dB = accurate_coefficient_to_dB (c->get_value());

			if (step_inc < 1.0) {
				if (dB >= 0.0) {
					snprintf (buf, 32, "+%5.2f dB", dB);
				} else {
					snprintf (buf, 32, "%5.2f dB", dB);
				}
			} else {
				if (dB >= 0.0) {
					snprintf (buf, 32, "+%2ld dB", lrint (dB));
				} else {
					snprintf (buf, 32, "%2ld dB", lrint (dB));
				}
			}
		}
	} else {
		snprintf (buf, sizeof (buf), "--");
	}
}

double
VolumeController::to_control_value (double display_value)
{
	double v;

	/* display value is always clamped to 0.0 .. 1.0 */
	display_value = std::max (0.0, std::min (1.0, display_value));

	if (_linear) {
		v = _controllable->lower() + ((_controllable->upper() - _controllable->lower()) * display_value);
	} else {
		v = slider_position_to_gain_with_max (display_value, ARDOUR::Config->get_max_gain());
	}

	return v;
}

double
VolumeController::to_display_value (double control_value)
{
	double v;

//	if (_linear) {
		v = (control_value - _controllable->lower ()) / (_controllable->upper() - _controllable->lower());
//	} else {
//		v = gain_to_slider_position_with_max (control_value, ARDOUR::Config->get_max_gain());
//	}

	return v;
}

double
VolumeController::adjust (double control_delta)
{
	return std::max (_controllable->lower(), std::min (_controllable->upper(), _controllable->get_value() + control_delta));
}