fix clicking when processors become active/inactive; reduce crazy 2.5sec delay for...
[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 std::string
51 Amp::display_name() const
52 {
53         return _("Fader");
54 }
55
56 bool
57 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
58 {
59         out = in;
60         return true;
61 }
62
63 bool
64 Amp::configure_io (ChanCount in, ChanCount out)
65 {
66         if (out != in) { // always 1:1
67                 return false;
68         }
69         
70         return Processor::configure_io (in, out);
71 }
72
73 void
74 Amp::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
75 {
76         gain_t mute_gain;
77
78         if (!_active && !_pending_active) {
79                 return;
80         }
81
82         if (_mute_master) {
83                 mute_gain = _mute_master->mute_gain_at (MuteMaster::PreFader);
84         } else {
85                 mute_gain = 1.0;
86         }
87
88         if (_apply_gain) {
89                 
90                 if (_apply_gain_automation) {
91                         
92                         gain_t* gab = _session.gain_automation_buffer ();
93
94                         if (mute_gain == 0.0) {
95                                 
96                                 /* absolute mute */
97
98                                 if (_current_gain == 0.0) {
99                                         
100                                         /* already silent */
101
102                                         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
103                                                 i->clear ();
104                                         }
105                                 } else {
106                                         
107                                         /* cut to silence */
108
109                                         Amp::apply_gain (bufs, nframes, _current_gain, 0.0);
110                                         _current_gain = 0.0;
111                                 }
112                                         
113
114                         } else if (mute_gain != 1.0) {
115
116                                 /* mute dimming */
117
118                                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
119                                         Sample* const sp = i->data();
120                                         for (nframes_t nx = 0; nx < nframes; ++nx) {
121                                                 sp[nx] *= gab[nx] * mute_gain;
122                                         }
123                                 }
124
125                                 _current_gain = gab[nframes-1] * mute_gain;
126
127                         } else {
128
129                                 /* no mute */
130
131                                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
132                                         Sample* const sp = i->data();
133                                         for (nframes_t nx = 0; nx < nframes; ++nx) {
134                                                 sp[nx] *= gab[nx];
135                                         }
136                                 }
137
138                                 _current_gain = gab[nframes-1];
139                         }
140                                 
141                         
142                 } else { /* manual (scalar) gain */
143
144                         gain_t dg = _gain_control->user_float() * mute_gain;
145                         
146                         if (_current_gain != dg) {
147                                 
148                                 Amp::apply_gain (bufs, nframes, _current_gain, dg);
149                                 _current_gain = dg;
150                                 
151                         } else if (_current_gain != 1.0f) {
152                                 
153                                 /* gain has not changed, but its non-unity
154                                 */
155
156                                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
157                                         apply_gain_to_buffer (i->data(), nframes, _current_gain);
158                                 }
159                         } 
160                 }
161         }
162
163         _active = _pending_active;
164 }
165
166 void
167 Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target)
168 {
169         /** Apply a (potentially) declicked gain to the audio buffers of @a bufs 
170          */
171         
172         if (nframes == 0 || bufs.count().n_audio() == 0) {
173                 return;
174         }
175
176         // if we don't need to declick, defer to apply_simple_gain
177         if (initial == target) {
178                 apply_simple_gain (bufs, nframes, target);
179                 return;
180         }
181
182         const nframes_t declick = std::min ((nframes_t)128, nframes);
183         gain_t         delta;
184         double         fractional_shift = -1.0/declick;
185         double         fractional_pos;
186         gain_t         polscale = 1.0f;
187
188         if (target < initial) {
189                 /* fade out: remove more and more of delta from initial */
190                 delta = -(initial - target);
191         } else {
192                 /* fade in: add more and more of delta from initial */
193                 delta = target - initial;
194         }
195
196         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
197                 Sample* const buffer = i->data();
198
199                 fractional_pos = 1.0;
200
201                 for (nframes_t nx = 0; nx < declick; ++nx) {
202                         buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
203                         fractional_pos += fractional_shift;
204                 }
205                 
206                 /* now ensure the rest of the buffer has the target value applied, if necessary. */
207                 
208                 if (declick != nframes) {
209
210                         if (target == 0.0) {
211                                 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
212                         } else if (target != 1.0) {
213                                 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
214                         }
215                 }
216         }
217 }
218
219 void
220 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
221 {
222         if (target == 0.0) {
223                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
224                         memset (i->data(), 0, sizeof (Sample) * nframes);
225                 }
226         } else if (target != 1.0) {
227                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
228                         apply_gain_to_buffer (i->data(), nframes, target);
229                 }
230         }
231 }
232
233 void
234 Amp::inc_gain (gain_t factor, void *src)
235 {
236         float desired_gain = _gain_control->user_float();
237         
238         if (desired_gain == 0.0f) {
239                 set_gain (0.000001f + (0.000001f * factor), src);
240         } else {
241                 set_gain (desired_gain + (desired_gain * factor), src);
242         }
243 }
244
245 void
246 Amp::set_gain (gain_t val, void *src)
247 {
248         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
249         if (val > 1.99526231f) {
250                 val = 1.99526231f;
251         }
252
253         //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
254
255         if (src != _gain_control.get()) {
256                 _gain_control->set_value (val);
257                 // bit twisty, this will come back and call us again
258                 // (this keeps control in sync with reality)
259                 return;
260         }
261
262         _gain_control->set_float(val, false);
263         _session.set_dirty();
264 }
265
266 XMLNode&
267 Amp::state (bool full_state)
268 {
269         XMLNode& node (Processor::state (full_state));
270         node.add_property("type", "amp");
271
272         char buf[32];
273         snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
274         node.add_property("gain", buf);
275
276         return node;
277 }
278
279 int
280 Amp::set_state (const XMLNode& node)
281 {
282         const XMLProperty* prop;
283
284         Processor::set_state (node);
285         prop = node.property ("gain");
286
287         if (prop) {
288                 gain_t val;
289
290                 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
291                         _gain_control->set_value (val);
292                 }
293         }
294
295         return 0;
296 }
297
298 void
299 Amp::GainControl::set_value (float val)
300 {
301         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
302         if (val > 1.99526231f)
303                 val = 1.99526231f;
304
305         _amp->set_gain (val, this);
306         
307         AutomationControl::set_value(val);
308 }
309
310 float
311 Amp::GainControl::get_value (void) const
312 {
313         return AutomationControl::get_value();
314 }
315
316 void
317 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
318 {
319         Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
320
321         if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
322                 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
323                         start_frame, end_frame, _session.gain_automation_buffer(), nframes);
324         } else {
325                 _apply_gain_automation = false;
326         }
327 }
328
329 bool
330 Amp::visible() const
331 {
332         return true;
333 }