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