5af0e2d397cf13d0d86d81dd17f82bbb23e7a7cb
[ardour.git] / libs / ardour / gain_control.cc
1 /*
2     Copyright (C) 2006-2016 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "ardour/dB.h"
20 #include "ardour/gain_control.h"
21 #include "ardour/session.h"
22
23 #include "i18n.h"
24
25 using namespace ARDOUR;
26 using namespace std;
27
28 GainControl::GainControl (Session& session, const Evoral::Parameter &param, boost::shared_ptr<AutomationList> al)
29         : AutomationControl (session, param, ParameterDescriptor(param),
30                              al ? al : boost::shared_ptr<AutomationList> (new AutomationList (param)),
31                              param.type() == GainAutomation ? X_("gaincontrol") : X_("trimcontrol")) {
32
33         alist()->reset_default (1.0);
34
35         lower_db = accurate_coefficient_to_dB (_desc.lower);
36         range_db = accurate_coefficient_to_dB (_desc.upper) - lower_db;
37 }
38
39 double
40 GainControl::get_value() const
41 {
42         if (!_master) {
43                 return AutomationControl::get_value();
44         }
45         return AutomationControl::get_value() * _master->get_value();
46 }
47
48 void
49 GainControl::set_value (double val, PBD::Controllable::GroupControlDisposition group_override)
50 {
51         if (writable()) {
52                 _set_value (val, group_override);
53         }
54 }
55
56 void
57 GainControl::set_value_unchecked (double val)
58 {
59         /* used only automation playback */
60         _set_value (val, Controllable::NoGroup);
61 }
62
63 void
64 GainControl::_set_value (double val, Controllable::GroupControlDisposition group_override)
65 {
66         AutomationControl::set_value (std::max (std::min (val, (double)_desc.upper), (double)_desc.lower), group_override);
67         _session.set_dirty ();
68 }
69
70 double
71 GainControl::internal_to_interface (double v) const
72 {
73         if (_desc.type == GainAutomation) {
74                 return gain_to_slider_position (v);
75         } else {
76                 return (accurate_coefficient_to_dB (v) - lower_db) / range_db;
77         }
78 }
79
80 double
81 GainControl::interface_to_internal (double v) const
82 {
83         if (_desc.type == GainAutomation) {
84                 return slider_position_to_gain (v);
85         } else {
86                 return dB_to_coefficient (lower_db + v * range_db);
87         }
88 }
89
90 double
91 GainControl::internal_to_user (double v) const
92 {
93         return accurate_coefficient_to_dB (v);
94 }
95
96 double
97 GainControl::user_to_internal (double u) const
98 {
99         return dB_to_coefficient (u);
100 }
101
102 std::string
103 GainControl::get_user_string () const
104 {
105         char theBuf[32]; sprintf( theBuf, _("%3.1f dB"), accurate_coefficient_to_dB (get_value()));
106         return std::string(theBuf);
107 }
108
109 void
110 GainControl::set_master (boost::shared_ptr<GainControl> m)
111 {
112         double old_master_val;
113
114         if (_master) {
115                 old_master_val = _master->get_value();
116         } else {
117                 old_master_val = 1.0;
118         }
119
120         _master = m;
121
122         double new_master_val;
123
124         if (_master) {
125                 new_master_val = _master->get_value();
126         } else {
127                 new_master_val = 1.0;
128         }
129
130         if (old_master_val != new_master_val) {
131                 Changed(); /* EMIT SIGNAL */
132         }
133 }
134