enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 <cmath>
20
21 #include "pbd/convert.h"
22 #include "pbd/strsplit.h"
23
24 #include "ardour/dB.h"
25 #include "ardour/gain_control.h"
26 #include "ardour/session.h"
27 #include "ardour/vca.h"
28 #include "ardour/vca_manager.h"
29
30 #include "pbd/i18n.h"
31
32 using namespace ARDOUR;
33 using namespace std;
34
35 GainControl::GainControl (Session& session, const Evoral::Parameter &param, boost::shared_ptr<AutomationList> al)
36         : SlavableAutomationControl (session, param, ParameterDescriptor(param),
37                                      al ? al : boost::shared_ptr<AutomationList> (new AutomationList (param)),
38                                      param.type() == GainAutomation ? X_("gaincontrol") : X_("trimcontrol")) {
39
40         alist()->reset_default (1.0);
41
42         lower_db = accurate_coefficient_to_dB (_desc.lower);
43         range_db = accurate_coefficient_to_dB (_desc.upper) - lower_db;
44 }
45
46 double
47 GainControl::internal_to_interface (double v) const
48 {
49         if (_desc.type == GainAutomation) {
50                 return gain_to_slider_position (v);
51         } else {
52                 return (accurate_coefficient_to_dB (v) - lower_db) / range_db;
53         }
54 }
55
56 double
57 GainControl::interface_to_internal (double v) const
58 {
59         if (_desc.type == GainAutomation) {
60                 return slider_position_to_gain (v);
61         } else {
62                 return dB_to_coefficient (lower_db + v * range_db);
63         }
64 }
65
66 double
67 GainControl::internal_to_user (double v) const
68 {
69         return accurate_coefficient_to_dB (v);
70 }
71
72 double
73 GainControl::user_to_internal (double u) const
74 {
75         return dB_to_coefficient (u);
76 }
77
78 std::string
79 GainControl::get_user_string () const
80 {
81         char theBuf[32]; sprintf( theBuf, _("%3.1f dB"), accurate_coefficient_to_dB (get_value()));
82         return std::string(theBuf);
83 }
84
85 void
86 GainControl::inc_gain (gain_t factor)
87 {
88         /* To be used ONLY when doing group-relative gain adjustment, from
89          * ControlGroup::set_group_values().
90          */
91
92         const float desired_gain = user_double();
93
94         if (fabsf (desired_gain) < GAIN_COEFF_SMALL) {
95                 // really?! what's the idea here?
96                 actually_set_value (0.000001f + (0.000001f * factor), Controllable::ForGroup);
97         } else {
98                 actually_set_value (desired_gain + (desired_gain * factor), Controllable::ForGroup);
99         }
100 }
101
102 void
103 GainControl::recompute_masters_ratios (double val)
104 {
105         /* Master WRITE lock must be held */
106
107         /* V' is the new gain value for this
108
109            Mv(n) is the return value of ::get_value() for the n-th master
110            Mr(n) is the return value of ::ratio() for the n-th master record
111
112            the slave should return V' on the next call to ::get_value().
113
114            but the value is determined by the masters, so we know:
115
116            V' = (Mv(1) * Mr(1)) * (Mv(2) * Mr(2)) * ... * (Mv(n) * Mr(n))
117
118            hence:
119
120            Mr(1) * Mr(2) * ... * (Mr(n) = V' / (Mv(1) * Mv(2) * ... * Mv(n))
121
122            if we make all ratios equal (i.e. each master contributes the same
123            fraction of its own gain level to make the final slave gain), then we
124            have:
125
126            pow (Mr(n), n) = V' / (Mv(1) * Mv(2) * ... * Mv(n))
127
128            which gives
129
130            Mr(n) = pow ((V' / (Mv(1) * Mv(2) * ... * Mv(n))), 1/n)
131
132            Mr(n) is the new ratio number for the slaves
133         */
134
135
136         const double nmasters = _masters.size();
137         double masters_total_gain_coefficient = 1.0;
138
139         for (Masters::iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
140                 masters_total_gain_coefficient *= mr->second.master()->get_value();
141         }
142
143         const double new_universal_ratio = pow ((val / masters_total_gain_coefficient), (1.0/nmasters));
144
145         for (Masters::iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
146                 mr->second.reset_ratio (new_universal_ratio);
147         }
148 }
149
150 XMLNode&
151 GainControl::get_state ()
152 {
153         XMLNode& node (AutomationControl::get_state());
154
155 #if 0
156         /* store VCA master IDs */
157
158         string str;
159
160         {
161                 Glib::Threads::RWLock::ReaderLock lm (master_lock);
162                 for (Masters::const_iterator mr = _masters.begin(); mr != _masters.end(); ++mr) {
163                         if (!str.empty()) {
164                                 str += ',';
165                         }
166                         str += PBD::to_string (mr->first, std::dec);
167                 }
168         }
169
170         if (!str.empty()) {
171                 node.add_property (X_("masters"), str);
172         }
173 #endif
174
175         return node;
176 }
177
178 int
179 GainControl::set_state (XMLNode const& node, int version)
180 {
181         return AutomationControl::set_state (node, version);
182 }
183