make RouteGroup gain control work again ; fix what solo button label shows under...
[ardour.git] / libs / ardour / amp.cc
1 /*
2     Copyright (C) 2006 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 <iostream>
20 #include <cstring>
21 #include <cmath>
22 #include <algorithm>
23
24 #include "evoral/Curve.hpp"
25
26 #include "ardour/amp.h"
27 #include "ardour/audio_buffer.h"
28 #include "ardour/buffer_set.h"
29 #include "ardour/configuration.h"
30 #include "ardour/io.h"
31 #include "ardour/mute_master.h"
32 #include "ardour/session.h"
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37
38 Amp::Amp(Session& s, boost::shared_ptr<MuteMaster> mm)
39         : Processor(s, "Amp")
40         , _apply_gain(true)
41         , _apply_gain_automation(false)
42         , _current_gain(1.0)
43         , _mute_master (mm)
44 {
45         boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(GainAutomation)));
46         _gain_control = boost::shared_ptr<GainControl>( new GainControl(X_("gaincontrol"), s, this, Evoral::Parameter(GainAutomation), gl ));
47         add_control(_gain_control);
48 }
49
50 bool
51 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
52 {
53         out = in;
54         return true;
55 }
56
57 bool
58 Amp::configure_io (ChanCount in, ChanCount out)
59 {
60         if (out != in) { // always 1:1
61                 return false;
62         }
63         
64         return Processor::configure_io (in, out);
65 }
66
67 void
68 Amp::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
69 {
70         gain_t mute_gain;
71
72         if (_mute_master) {
73                 mute_gain = _mute_master->mute_gain_at (MuteMaster::PreFader);
74         } else {
75                 mute_gain = 1.0;
76         }
77
78         if (_apply_gain) {
79                 
80                 if (_apply_gain_automation) {
81                         
82                         gain_t* gab = _session.gain_automation_buffer ();
83
84                         if (mute_gain == 0.0) {
85                                 
86                                 /* absolute mute */
87
88                                 if (_current_gain == 0.0) {
89                                         
90                                         /* already silent */
91
92                                         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
93                                                 i->clear ();
94                                         }
95                                 } else {
96                                         
97                                         /* cut to silence */
98
99                                         Amp::apply_gain (bufs, nframes, _current_gain, 0.0);
100                                         _current_gain = 0.0;
101                                 }
102                                         
103
104                         } else if (mute_gain != 1.0) {
105
106                                 /* mute dimming */
107
108                                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
109                                         Sample* const sp = i->data();
110                                         for (nframes_t nx = 0; nx < nframes; ++nx) {
111                                                 sp[nx] *= gab[nx] * mute_gain;
112                                         }
113                                 }
114
115                                 _current_gain = gab[nframes-1] * mute_gain;
116
117                         } else {
118
119                                 /* no mute */
120
121                                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
122                                         Sample* const sp = i->data();
123                                         for (nframes_t nx = 0; nx < nframes; ++nx) {
124                                                 sp[nx] *= gab[nx];
125                                         }
126                                 }
127
128                                 _current_gain = gab[nframes-1];
129                         }
130                                 
131                         
132                 } else { /* manual (scalar) gain */
133
134                         gain_t dg = _gain_control->user_float() * mute_gain;
135                         
136                         if (_current_gain != dg) {
137                                 
138                                 Amp::apply_gain (bufs, nframes, _current_gain, dg);
139                                 _current_gain = dg;
140                                 
141                         } else if (_current_gain != 1.0f) {
142                                 
143                                 /* gain has not changed, but its non-unity
144                                 */
145
146                                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
147                                         apply_gain_to_buffer (i->data(), nframes, _current_gain);
148                                 }
149                         } 
150                 }
151         }
152 }
153
154 void
155 Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target)
156 {
157         /** Apply a (potentially) declicked gain to the audio buffers of @a bufs 
158          */
159         
160         if (nframes == 0 || bufs.count().n_audio() == 0) {
161                 return;
162         }
163
164         // if we don't need to declick, defer to apply_simple_gain
165         if (initial == target) {
166                 apply_simple_gain (bufs, nframes, target);
167                 return;
168         }
169
170         const nframes_t declick = std::min ((nframes_t)128, nframes);
171         gain_t         delta;
172         double         fractional_shift = -1.0/declick;
173         double         fractional_pos;
174         gain_t         polscale = 1.0f;
175
176         if (target < initial) {
177                 /* fade out: remove more and more of delta from initial */
178                 delta = -(initial - target);
179         } else {
180                 /* fade in: add more and more of delta from initial */
181                 delta = target - initial;
182         }
183
184         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
185                 Sample* const buffer = i->data();
186
187                 fractional_pos = 1.0;
188
189                 for (nframes_t nx = 0; nx < declick; ++nx) {
190                         buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
191                         fractional_pos += fractional_shift;
192                 }
193                 
194                 /* now ensure the rest of the buffer has the target value applied, if necessary. */
195                 
196                 if (declick != nframes) {
197
198                         if (target == 0.0) {
199                                 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
200                         } else if (target != 1.0) {
201                                 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
202                         }
203                 }
204         }
205 }
206
207 void
208 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
209 {
210         if (target == 0.0) {
211                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
212                         memset (i->data(), 0, sizeof (Sample) * nframes);
213                 }
214         } else if (target != 1.0) {
215                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
216                         apply_gain_to_buffer (i->data(), nframes, target);
217                 }
218         }
219 }
220
221 void
222 Amp::inc_gain (gain_t factor, void *src)
223 {
224         float desired_gain = _gain_control->user_float();
225         if (desired_gain == 0.0f) {
226                 set_gain (0.000001f + (0.000001f * factor), src);
227         } else {
228                 set_gain (desired_gain + (desired_gain * factor), src);
229         }
230 }
231
232 void
233 Amp::set_gain (gain_t val, void *src)
234 {
235         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
236         if (val > 1.99526231f) {
237                 val = 1.99526231f;
238         }
239
240         //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
241
242         if (src != _gain_control.get()) {
243                 _gain_control->set_value (val);
244                 // bit twisty, this will come back and call us again
245                 // (this keeps control in sync with reality)
246                 return;
247         }
248
249         _gain_control->set_float(val, false);
250         _session.set_dirty();
251 }
252
253 XMLNode&
254 Amp::state (bool full_state)
255 {
256         XMLNode& node (Processor::state (full_state));
257         node.add_property("type", "amp");
258         return node;
259 }
260
261 void
262 Amp::GainControl::set_value (float val)
263 {
264         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
265         if (val > 1.99526231f)
266                 val = 1.99526231f;
267
268         _amp->set_gain (val, this);
269         
270         AutomationControl::set_value(val);
271 }
272
273 float
274 Amp::GainControl::get_value (void) const
275 {
276         return AutomationControl::get_value();
277 }
278
279 void
280 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
281 {
282         Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
283
284         if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
285                 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
286                         start_frame, end_frame, _session.gain_automation_buffer(), nframes);
287         } else {
288                 _apply_gain_automation = false;
289         }
290 }