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