9aa88068a95052b419fdbf50798775e5c1b725b2
[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) {
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
164 void
165 Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target)
166 {
167         /** Apply a (potentially) declicked gain to the audio buffers of @a bufs 
168          */
169         
170         if (nframes == 0 || bufs.count().n_audio() == 0) {
171                 return;
172         }
173
174         // if we don't need to declick, defer to apply_simple_gain
175         if (initial == target) {
176                 apply_simple_gain (bufs, nframes, target);
177                 return;
178         }
179
180         const nframes_t declick = std::min ((nframes_t)128, nframes);
181         gain_t         delta;
182         double         fractional_shift = -1.0/declick;
183         double         fractional_pos;
184         gain_t         polscale = 1.0f;
185
186         if (target < initial) {
187                 /* fade out: remove more and more of delta from initial */
188                 delta = -(initial - target);
189         } else {
190                 /* fade in: add more and more of delta from initial */
191                 delta = target - initial;
192         }
193
194         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
195                 Sample* const buffer = i->data();
196
197                 fractional_pos = 1.0;
198
199                 for (nframes_t nx = 0; nx < declick; ++nx) {
200                         buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
201                         fractional_pos += fractional_shift;
202                 }
203                 
204                 /* now ensure the rest of the buffer has the target value applied, if necessary. */
205                 
206                 if (declick != nframes) {
207
208                         if (target == 0.0) {
209                                 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
210                         } else if (target != 1.0) {
211                                 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
212                         }
213                 }
214         }
215 }
216
217 void
218 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
219 {
220         if (target == 0.0) {
221                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
222                         memset (i->data(), 0, sizeof (Sample) * nframes);
223                 }
224         } else if (target != 1.0) {
225                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
226                         apply_gain_to_buffer (i->data(), nframes, target);
227                 }
228         }
229 }
230
231 void
232 Amp::inc_gain (gain_t factor, void *src)
233 {
234         float desired_gain = _gain_control->user_float();
235         
236         if (desired_gain == 0.0f) {
237                 set_gain (0.000001f + (0.000001f * factor), src);
238         } else {
239                 set_gain (desired_gain + (desired_gain * factor), src);
240         }
241 }
242
243 void
244 Amp::set_gain (gain_t val, void *src)
245 {
246         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
247         if (val > 1.99526231f) {
248                 val = 1.99526231f;
249         }
250
251         //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
252
253         if (src != _gain_control.get()) {
254                 _gain_control->set_value (val);
255                 // bit twisty, this will come back and call us again
256                 // (this keeps control in sync with reality)
257                 return;
258         }
259
260         _gain_control->set_float(val, false);
261         _session.set_dirty();
262 }
263
264 XMLNode&
265 Amp::state (bool full_state)
266 {
267         XMLNode& node (Processor::state (full_state));
268         node.add_property("type", "amp");
269
270         char buf[32];
271         snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
272         node.add_property("gain", buf);
273
274         return node;
275 }
276
277 int
278 Amp::set_state (const XMLNode& node)
279 {
280         const XMLProperty* prop;
281
282         Processor::set_state (node);
283         prop = node.property ("gain");
284
285         if (prop) {
286                 gain_t val;
287
288                 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
289                         _gain_control->set_value (val);
290                 }
291         }
292
293         return 0;
294 }
295
296 void
297 Amp::GainControl::set_value (float val)
298 {
299         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
300         if (val > 1.99526231f)
301                 val = 1.99526231f;
302
303         _amp->set_gain (val, this);
304         
305         AutomationControl::set_value(val);
306 }
307
308 float
309 Amp::GainControl::get_value (void) const
310 {
311         return AutomationControl::get_value();
312 }
313
314 void
315 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
316 {
317         Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
318
319         if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
320                 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
321                         start_frame, end_frame, _session.gain_automation_buffer(), nframes);
322         } else {
323                 _apply_gain_automation = false;
324         }
325 }
326
327 bool
328 Amp::visible() const
329 {
330         return true;
331 }