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