b74a57dbce8badfeede821a8973b3ed995ddf121
[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/midi_buffer.h"
32 #include "ardour/session.h"
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 Amp::Amp (Session& s)
40         : Processor(s, "Amp")
41         , _apply_gain(true)
42         , _apply_gain_automation(false)
43         , _current_gain(1.0)
44 {
45         Evoral::Parameter p (GainAutomation);
46         /* gain range of -inf to +6dB, default 0dB */
47         p.set_range (0, 1.99526231f, 1, false);
48         boost::shared_ptr<AutomationList> gl (new AutomationList (p));
49         _gain_control = boost::shared_ptr<GainControl> (new GainControl (X_("gaincontrol"), s, this, p, gl));
50         add_control(_gain_control);
51 }
52
53 std::string
54 Amp::display_name() const
55 {
56         return _("Fader");
57 }
58
59 bool
60 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
61 {
62         out = in;
63         return true;
64 }
65
66 bool
67 Amp::configure_io (ChanCount in, ChanCount out)
68 {
69         if (out != in) { // always 1:1
70                 return false;
71         }
72
73         return Processor::configure_io (in, out);
74 }
75
76 void
77 Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
78 {
79         if (!_active && !_pending_active) {
80                 return;
81         }
82
83         if (_apply_gain) {
84
85                 if (_apply_gain_automation) {
86
87                         gain_t* gab = _session.gain_automation_buffer ();
88
89                         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
90                                 Sample* const sp = i->data();
91                                 for (pframes_t nx = 0; nx < nframes; ++nx) {
92                                         sp[nx] *= gab[nx];
93                                 }
94                         }
95
96                         _current_gain = gab[nframes-1];
97
98                 } else { /* manual (scalar) gain */
99
100                         gain_t const dg = _gain_control->user_double();
101
102                         if (_current_gain != dg) {
103
104                                 Amp::apply_gain (bufs, nframes, _current_gain, dg);
105                                 _current_gain = dg;
106
107                         } else if (_current_gain != 1.0f) {
108
109                                 /* gain has not changed, but its non-unity
110                                 */
111
112                                 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
113
114                                         MidiBuffer& mb (*i);
115
116                                         for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
117                                                 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
118                                                 if (ev.is_note_on()) {
119                                                         ev.scale_velocity (_current_gain);
120                                                 }
121                                         }
122                                 }
123
124                                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
125                                         apply_gain_to_buffer (i->data(), nframes, _current_gain);
126                                 }
127                         }
128                 }
129         }
130
131         _active = _pending_active;
132 }
133
134 void
135 Amp::apply_gain (BufferSet& bufs, framecnt_t nframes, gain_t initial, gain_t target)
136 {
137         /** Apply a (potentially) declicked gain to the buffers of @a bufs
138          */
139
140         if (nframes == 0 || bufs.count().n_total() == 0) {
141                 return;
142         }
143
144         // if we don't need to declick, defer to apply_simple_gain
145         if (initial == target) {
146                 apply_simple_gain (bufs, nframes, target);
147                 return;
148         }
149
150         const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
151         gain_t         delta;
152         double         fractional_shift = -1.0/declick;
153         double         fractional_pos;
154
155         if (target < initial) {
156                 /* fade out: remove more and more of delta from initial */
157                 delta = -(initial - target);
158         } else {
159                 /* fade in: add more and more of delta from initial */
160                 delta = target - initial;
161         }
162
163         /* MIDI Gain */
164
165         for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
166
167                 MidiBuffer& mb (*i);
168
169                 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
170                         Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
171
172                         if (ev.is_note_on()) {
173                                 gain_t scale = delta * (ev.time()/(double) nframes);
174                                 ev.scale_velocity (initial+scale);
175                         }
176                 }
177         }
178
179         /* Audio Gain */
180
181         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
182                 Sample* const buffer = i->data();
183
184                 fractional_pos = 1.0;
185
186                 for (pframes_t nx = 0; nx < declick; ++nx) {
187                         buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
188                         fractional_pos += fractional_shift;
189                 }
190
191                 /* now ensure the rest of the buffer has the target value applied, if necessary. */
192
193                 if (declick != nframes) {
194
195                         if (target == 0.0) {
196                                 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
197                         } else if (target != 1.0) {
198                                 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
199                         }
200                 }
201         }
202 }
203
204 void
205 Amp::declick (BufferSet& bufs, framecnt_t nframes, int dir)
206 {
207         /* Almost exactly like ::apply_gain() but skips MIDI buffers and has fixed initial+target
208            values.
209          */
210
211         if (nframes == 0 || bufs.count().n_total() == 0) {
212                 return;
213         }
214
215         const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
216         gain_t         delta, initial, target;
217         double         fractional_shift = -1.0/(declick-1);
218         double         fractional_pos;
219
220         if (dir < 0) {
221                 /* fade out: remove more and more of delta from initial */
222                 delta = -1.0;
223                 initial = 1.0;
224                 target = 0.0;
225         } else {
226                 /* fade in: add more and more of delta from initial */
227                 delta = 1.0;
228                 initial = 0.0;
229                 target = 1.0;
230         }
231
232         /* Audio Gain */
233
234         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
235                 Sample* const buffer = i->data();
236
237                 fractional_pos = 1.0;
238
239                 for (pframes_t nx = 0; nx < declick; ++nx) {
240                         buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
241                         fractional_pos += fractional_shift;
242                 }
243
244                 /* now ensure the rest of the buffer has the target value applied, if necessary. */
245
246                 if (declick != nframes) {
247
248                         if (target == 0.0) {
249                                 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
250                         } else if (target != 1.0) {
251                                 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
252                         }
253                 }
254         }
255 }
256
257 void
258 Amp::apply_gain (AudioBuffer& buf, framecnt_t nframes, gain_t initial, gain_t target)
259 {
260         /** Apply a (potentially) declicked gain to the contents of @a buf
261          */
262
263         if (nframes == 0) {
264                 return;
265         }
266
267         // if we don't need to declick, defer to apply_simple_gain
268         if (initial == target) {
269                 apply_simple_gain (buf, nframes, target);
270                 return;
271         }
272
273         const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
274         gain_t         delta;
275         double         fractional_shift = -1.0/declick;
276         double         fractional_pos;
277
278         if (target < initial) {
279                 /* fade out: remove more and more of delta from initial */
280                 delta = -(initial - target);
281         } else {
282                 /* fade in: add more and more of delta from initial */
283                 delta = target - initial;
284         }
285
286
287         Sample* const buffer = buf.data();
288
289         fractional_pos = 1.0;
290
291         for (pframes_t nx = 0; nx < declick; ++nx) {
292                 buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
293                 fractional_pos += fractional_shift;
294         }
295
296         /* now ensure the rest of the buffer has the target value applied, if necessary. */
297
298         if (declick != nframes) {
299
300                 if (target == 0.0) {
301                         memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
302                 } else if (target != 1.0) {
303                         apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
304                 }
305         }
306 }
307
308 void
309 Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target)
310 {
311         if (target == 0.0) {
312
313                 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
314                         MidiBuffer& mb (*i);
315
316                         for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
317                                 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
318                                 if (ev.is_note_on()) {
319                                         ev.set_velocity (0);
320                                 }
321                         }
322                 }
323
324                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
325                         memset (i->data(), 0, sizeof (Sample) * nframes);
326                 }
327
328         } else if (target != 1.0) {
329
330                 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
331                         MidiBuffer& mb (*i);
332
333                         for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
334                                 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
335                                 if (ev.is_note_on()) {
336                                         ev.scale_velocity (target);
337                                 }
338                         }
339                 }
340
341                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
342                         apply_gain_to_buffer (i->data(), nframes, target);
343                 }
344         }
345 }
346
347 void
348 Amp::apply_simple_gain (AudioBuffer& buf, framecnt_t nframes, gain_t target)
349 {
350         if (target == 0.0) {
351                 memset (buf.data(), 0, sizeof (Sample) * nframes);
352         } else if (target != 1.0) {
353                 apply_gain_to_buffer (buf.data(), nframes, target);
354         }
355 }
356
357 void
358 Amp::inc_gain (gain_t factor, void *src)
359 {
360         float desired_gain = _gain_control->user_double();
361
362         if (desired_gain == 0.0f) {
363                 set_gain (0.000001f + (0.000001f * factor), src);
364         } else {
365                 set_gain (desired_gain + (desired_gain * factor), src);
366         }
367 }
368
369 void
370 Amp::set_gain (gain_t val, void *src)
371 {
372         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
373         if (val > 1.99526231f) {
374                 val = 1.99526231f;
375         }
376
377         if (src != _gain_control.get()) {
378                 _gain_control->set_value (val);
379                 // bit twisty, this will come back and call us again
380                 // (this keeps control in sync with reality)
381                 return;
382         }
383
384         _gain_control->set_double(val, false);
385         _session.set_dirty();
386 }
387
388 XMLNode&
389 Amp::state (bool full_state)
390 {
391         XMLNode& node (Processor::state (full_state));
392         node.add_property("type", "amp");
393         node.add_child_nocopy (_gain_control->get_state());
394
395         return node;
396 }
397
398 int
399 Amp::set_state (const XMLNode& node, int version)
400 {
401         XMLNode* gain_node;
402
403         Processor::set_state (node, version);
404
405         if ((gain_node = node.child (Controllable::xml_node_name.c_str())) != 0) {
406                 _gain_control->set_state (*gain_node, version);
407         }
408
409         return 0;
410 }
411
412 void
413 Amp::GainControl::set_value (double val)
414 {
415         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
416         if (val > 1.99526231) {
417                 val = 1.99526231;
418         }
419
420         _amp->set_gain (val, this);
421
422         AutomationControl::set_value(val);
423 }
424
425 double
426 Amp::GainControl::internal_to_interface (double v) const
427 {
428         return gain_to_slider_position (v);
429 }
430
431 double
432 Amp::GainControl::interface_to_internal (double v) const
433 {
434         return slider_position_to_gain (v);
435 }
436
437 double
438 Amp::GainControl::internal_to_user (double v) const
439 {
440         return accurate_coefficient_to_dB (v);
441 }
442
443 void
444 Amp::setup_gain_automation (framepos_t start_frame, framepos_t end_frame, framecnt_t nframes)
445 {
446         Glib::Mutex::Lock am (control_lock(), Glib::TRY_LOCK);
447
448         if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
449                 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
450                         start_frame, end_frame, _session.gain_automation_buffer(), nframes);
451         } else {
452                 _apply_gain_automation = false;
453         }
454 }
455
456 bool
457 Amp::visible() const
458 {
459         return true;
460 }
461
462 std::string
463 Amp::value_as_string (boost::shared_ptr<AutomationControl> ac) const
464 {
465         if (ac == _gain_control) {
466                 char buffer[32];
467                 snprintf (buffer, sizeof (buffer), "%.2fdB", ac->internal_to_user (ac->get_value ()));
468                 return buffer;
469         }
470
471         return Automatable::value_as_string (ac);
472 }
473