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