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