remove getter for Amp::_apply_automation_gain; reset member to false after use, and...
[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 <cstring>
20 #include <cmath>
21 #include <algorithm>
22
23 #include "evoral/Curve.hpp"
24
25 #include "ardour/amp.h"
26 #include "ardour/audio_buffer.h"
27 #include "ardour/buffer_set.h"
28 #include "ardour/gain_control.h"
29 #include "ardour/midi_buffer.h"
30 #include "ardour/rc_configuration.h"
31 #include "ardour/session.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace ARDOUR;
36 using namespace PBD;
37
38 #define GAIN_COEFF_DELTA (1e-5)
39
40 Amp::Amp (Session& s, const std::string& name, boost::shared_ptr<GainControl> gc, bool control_midi_also)
41         : Processor(s, "Amp")
42         , _apply_gain_automation(false)
43         , _current_gain(GAIN_COEFF_ZERO)
44         , _current_automation_sample (INT64_MAX)
45         , _gain_control (gc)
46         , _gain_automation_buffer(0)
47         , _midi_amp (control_midi_also)
48 {
49         set_display_name (name);
50         add_control (_gain_control);
51 }
52
53 bool
54 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out)
55 {
56         out = in;
57         return true;
58 }
59
60 bool
61 Amp::configure_io (ChanCount in, ChanCount out)
62 {
63         if (out != in) { // always 1:1
64                 return false;
65         }
66
67         return Processor::configure_io (in, out);
68 }
69
70 static void
71 scale_midi_velocity(Evoral::Event<MidiBuffer::TimeType>& ev, float factor)
72 {
73         factor = std::max(factor, 0.0f);
74         ev.set_velocity(std::min(127L, lrintf(ev.velocity() * factor)));
75 }
76
77 void
78 Amp::run (BufferSet& bufs, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, double /*speed*/, pframes_t nframes, bool)
79 {
80         if (!_active && !_pending_active) {
81                 return;
82         }
83
84         if (_apply_gain_automation) {
85
86                 gain_t* gab = _gain_automation_buffer;
87                 assert (gab);
88
89                 /* see note in PluginInsert::connect_and_run -- effectively emit Changed signal */
90                 _gain_control->set_value_unchecked (gab[0]);
91
92                 if (_midi_amp) {
93                         for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
94                                 MidiBuffer& mb (*i);
95                                 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
96                                         Evoral::Event<MidiBuffer::TimeType> ev = *m;
97                                         if (ev.is_note_on()) {
98                                                 assert(ev.time() >= 0 && ev.time() < nframes);
99                                                 scale_midi_velocity (ev, fabsf (gab[ev.time()]));
100                                         }
101                                 }
102                         }
103                 }
104
105                 const gain_t a = 156.825f / (gain_t)_session.nominal_sample_rate(); // 25 Hz LPF; see Amp::apply_gain for details
106                 gain_t lpf = _current_gain;
107
108                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
109                         Sample* const sp = i->data();
110                         lpf = _current_gain;
111                         for (pframes_t nx = 0; nx < nframes; ++nx) {
112                                 sp[nx] *= lpf;
113                                 lpf += a * (gab[nx] - lpf);
114                         }
115                 }
116
117                 if (fabsf (lpf) < GAIN_COEFF_SMALL) {
118                         _current_gain = GAIN_COEFF_ZERO;
119                 } else {
120                         _current_gain = lpf;
121                 }
122
123                 /* used it, don't do it again until setup_gain_automation() is
124                    called successfully.
125                 */
126                 _apply_gain_automation = false;
127
128         } else { /* manual (scalar) gain */
129
130                 gain_t const target_gain = _gain_control->get_value();
131
132                 if (fabsf (_current_gain - target_gain) >= GAIN_COEFF_DELTA) {
133
134                         _current_gain = Amp::apply_gain (bufs, _session.nominal_sample_rate(), nframes, _current_gain, target_gain, _midi_amp);
135
136                         /* see note in PluginInsert::connect_and_run ()
137                          * set_value_unchecked() won't emit a signal since the value is effectively unchanged
138                          */
139                         _gain_control->Changed (false, PBD::Controllable::NoGroup);
140
141                 } else if (target_gain != GAIN_COEFF_UNITY) {
142
143                         _current_gain = target_gain;
144
145                         if (_midi_amp) {
146                                 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
147                                 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
148
149                                         MidiBuffer& mb (*i);
150
151                                         for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
152                                                 Evoral::Event<MidiBuffer::TimeType> ev = *m;
153                                                 if (ev.is_note_on()) {
154                                                         scale_midi_velocity (ev, fabsf (_current_gain));
155                                                 }
156                                         }
157                                 }
158                         }
159
160                         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
161                                 apply_gain_to_buffer (i->data(), nframes, _current_gain);
162                         }
163                 } else {
164                         /* unity target gain */
165                         _current_gain = target_gain;
166                 }
167         }
168
169         _active = _pending_active;
170 }
171
172 gain_t
173 Amp::apply_gain (BufferSet& bufs, samplecnt_t sample_rate, samplecnt_t nframes, gain_t initial, gain_t target, bool midi_amp)
174 {
175         /** Apply a (potentially) declicked gain to the buffers of @a bufs */
176         gain_t rv = target;
177
178         if (nframes == 0 || bufs.count().n_total() == 0) {
179                 return initial;
180         }
181
182         // if we don't need to declick, defer to apply_simple_gain
183         if (initial == target) {
184                 apply_simple_gain (bufs, nframes, target);
185                 return target;
186         }
187
188         /* MIDI Gain */
189         if (midi_amp) {
190                 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
191                 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
192
193                         gain_t  delta;
194                         if (target < initial) {
195                                 /* fade out: remove more and more of delta from initial */
196                                 delta = -(initial - target);
197                         } else {
198                                 /* fade in: add more and more of delta from initial */
199                                 delta = target - initial;
200                         }
201
202                         MidiBuffer& mb (*i);
203
204                         for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
205                                 Evoral::Event<MidiBuffer::TimeType> ev = *m;
206
207                                 if (ev.is_note_on()) {
208                                         const gain_t scale = delta * (ev.time()/(double) nframes);
209                                         scale_midi_velocity (ev, fabsf (initial + scale));
210                                 }
211                         }
212                 }
213         }
214
215         /* Audio Gain */
216
217         /* Low pass filter coefficient: 1.0 - e^(-2.0 * π * f / 48000) f in Hz.
218          * for f << SR,  approx a ~= 6.2 * f / SR;
219          */
220         const gain_t a = 156.825f / (gain_t)sample_rate; // 25 Hz LPF
221
222         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
223                 Sample* const buffer = i->data();
224                 double lpf = initial;
225
226                 for (pframes_t nx = 0; nx < nframes; ++nx) {
227                         buffer[nx] *= lpf;
228                         lpf += a * (target - lpf);
229                 }
230                 if (i == bufs.audio_begin()) {
231                         rv = lpf;
232                 }
233         }
234         if (fabsf (rv - target) < GAIN_COEFF_DELTA) return target;
235         return rv;
236 }
237
238 void
239 Amp::declick (BufferSet& bufs, samplecnt_t nframes, int dir)
240 {
241         if (nframes == 0 || bufs.count().n_total() == 0) {
242                 return;
243         }
244
245         const samplecnt_t declick = std::min ((samplecnt_t) 512, nframes);
246         const double     fractional_shift = 1.0 / declick ;
247         gain_t           delta, initial;
248
249         if (dir < 0) {
250                 /* fade out: remove more and more of delta from initial */
251                 delta = -1.0;
252                 initial = GAIN_COEFF_UNITY;
253         } else {
254                 /* fade in: add more and more of delta from initial */
255                 delta = 1.0;
256                 initial = GAIN_COEFF_ZERO;
257         }
258
259         /* Audio Gain */
260         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
261                 Sample* const buffer = i->data();
262
263                 double fractional_pos = 0.0;
264
265                 for (pframes_t nx = 0; nx < declick; ++nx) {
266                         buffer[nx] *= initial + (delta * fractional_pos);
267                         fractional_pos += fractional_shift;
268                 }
269
270                 /* now ensure the rest of the buffer has the target value applied, if necessary. */
271                 if (declick != nframes) {
272                         if (dir < 0) {
273                                 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
274                         }
275                 }
276         }
277 }
278
279
280 gain_t
281 Amp::apply_gain (AudioBuffer& buf, samplecnt_t sample_rate, samplecnt_t nframes, gain_t initial, gain_t target)
282 {
283         /* Apply a (potentially) declicked gain to the contents of @a buf
284          * -- used by MonitorProcessor::run()
285          */
286
287         if (nframes == 0) {
288                 return initial;
289         }
290
291         // if we don't need to declick, defer to apply_simple_gain
292         if (initial == target) {
293                 apply_simple_gain (buf, nframes, target);
294                 return target;
295         }
296
297         Sample* const buffer = buf.data();
298         const gain_t a = 156.825f / (gain_t)sample_rate; // 25 Hz LPF, see [other] Amp::apply_gain() above for details
299
300         gain_t lpf = initial;
301         for (pframes_t nx = 0; nx < nframes; ++nx) {
302                 buffer[nx] *= lpf;
303                 lpf += a * (target - lpf);
304         }
305
306         if (fabsf (lpf - target) < GAIN_COEFF_DELTA) return target;
307         return lpf;
308 }
309
310 void
311 Amp::apply_simple_gain (BufferSet& bufs, samplecnt_t nframes, gain_t target, bool midi_amp)
312 {
313         if (fabsf (target) < GAIN_COEFF_SMALL) {
314
315                 if (midi_amp) {
316                         /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
317                         for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
318                                 MidiBuffer& mb (*i);
319
320                                 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
321                                         Evoral::Event<MidiBuffer::TimeType> ev = *m;
322                                         if (ev.is_note_on()) {
323                                                 ev.set_velocity (0);
324                                         }
325                                 }
326                         }
327                 }
328
329                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
330                         memset (i->data(), 0, sizeof (Sample) * nframes);
331                 }
332
333         } else if (target != GAIN_COEFF_UNITY) {
334
335                 if (midi_amp) {
336                         /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
337                         for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
338                                 MidiBuffer& mb (*i);
339
340                                 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
341                                         Evoral::Event<MidiBuffer::TimeType> ev = *m;
342                                         if (ev.is_note_on()) {
343                                                 scale_midi_velocity(ev, fabsf (target));
344                                         }
345                                 }
346                         }
347                 }
348
349                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
350                         apply_gain_to_buffer (i->data(), nframes, target);
351                 }
352         }
353 }
354
355 void
356 Amp::apply_simple_gain (AudioBuffer& buf, samplecnt_t nframes, gain_t target)
357 {
358         if (fabsf (target) < GAIN_COEFF_SMALL) {
359                 memset (buf.data(), 0, sizeof (Sample) * nframes);
360         } else if (target != GAIN_COEFF_UNITY) {
361                 apply_gain_to_buffer (buf.data(), nframes, target);
362         }
363 }
364
365 XMLNode&
366 Amp::state (bool full_state)
367 {
368         XMLNode& node (Processor::state (full_state));
369         node.set_property("type", _gain_control->parameter().type() == GainAutomation ? "amp" : "trim");
370         node.add_child_nocopy (_gain_control->get_state());
371
372         return node;
373 }
374
375 int
376 Amp::set_state (const XMLNode& node, int version)
377 {
378         XMLNode* gain_node;
379
380         Processor::set_state (node, version);
381
382         if ((gain_node = node.child (Controllable::xml_node_name.c_str ())) != 0) {
383                 _gain_control->set_state (*gain_node, version);
384         }
385
386         return 0;
387 }
388
389 /** Write gain automation for this cycle into the buffer previously passed in to
390  *  set_gain_automation_buffer (if we are in automation playback mode and the
391  *  transport is rolling).
392  */
393 void
394 Amp::setup_gain_automation (samplepos_t start_sample, samplepos_t end_sample, samplecnt_t nframes)
395 {
396         Glib::Threads::Mutex::Lock am (control_lock(), Glib::Threads::TRY_LOCK);
397
398         if (am.locked()
399             && (_session.transport_rolling() || _session.bounce_processing())
400             && _gain_control->automation_playback())
401         {
402                 assert (_gain_automation_buffer);
403
404                 _apply_gain_automation = _gain_control->get_masters_curve ( start_sample, end_sample, _gain_automation_buffer, nframes);
405
406                 if (start_sample != _current_automation_sample && _session.bounce_processing ()) {
407                         _current_gain = _gain_automation_buffer[0];
408                 }
409                 _current_automation_sample = end_sample;
410         } else {
411                 _apply_gain_automation = false;
412                 _current_automation_sample = INT64_MAX;
413         }
414 }
415
416 bool
417 Amp::visible() const
418 {
419         return true;
420 }
421
422 /** Sets up the buffer that setup_gain_automation and ::run will use for
423  *  gain automationc curves.  Must be called before setup_gain_automation,
424  *  and must be called with process lock held.
425  */
426 void
427 Amp::set_gain_automation_buffer (gain_t* g)
428 {
429         _gain_automation_buffer = g;
430 }