Remove muting behaviour from the Amp processor. Fix some small
[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_float();
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
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()/nframes);
171                                 std::cerr << "scale by " << scale << " for " << ev.time() << " of " << nframes << std::endl;
172                                 ev.scale_velocity (scale);
173                         }
174                 }
175         }
176
177         /* Audio Gain */
178
179         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
180                 Sample* const buffer = i->data();
181
182                 fractional_pos = 1.0;
183
184                 for (nframes_t nx = 0; nx < declick; ++nx) {
185                         buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
186                         fractional_pos += fractional_shift;
187                 }
188
189                 /* now ensure the rest of the buffer has the target value applied, if necessary. */
190
191                 if (declick != nframes) {
192
193                         if (target == 0.0) {
194                                 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
195                         } else if (target != 1.0) {
196                                 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
197                         }
198                 }
199         }
200 }
201
202 void
203 Amp::apply_gain (AudioBuffer& buf, nframes_t nframes, gain_t initial, gain_t target)
204 {
205         /** Apply a (potentially) declicked gain to the contents of @a buf
206          */
207
208         if (nframes == 0) {
209                 return;
210         }
211
212         // if we don't need to declick, defer to apply_simple_gain
213         if (initial == target) {
214                 apply_simple_gain (buf, nframes, target);
215                 return;
216         }
217
218         const nframes_t declick = std::min ((nframes_t)128, nframes);
219         gain_t         delta;
220         double         fractional_shift = -1.0/declick;
221         double         fractional_pos;
222
223         if (target < initial) {
224                 /* fade out: remove more and more of delta from initial */
225                 delta = -(initial - target);
226         } else {
227                 /* fade in: add more and more of delta from initial */
228                 delta = target - initial;
229         }
230
231
232         Sample* const buffer = buf.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 void
254 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
255 {
256         if (target == 0.0) {
257
258                 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
259                         MidiBuffer& mb (*i);
260
261                         for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
262                                 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
263                                 if (ev.is_note_on()) {
264                                         ev.set_velocity (0);
265                                 }
266                         }
267                 }
268
269                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
270                         memset (i->data(), 0, sizeof (Sample) * nframes);
271                 }
272
273         } else if (target != 1.0) {
274
275                 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
276                         MidiBuffer& mb (*i);
277
278                         for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
279                                 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
280                                 if (ev.is_note_on()) {
281                                         ev.scale_velocity (target);
282                                 }
283                         }
284                 }
285
286                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
287                         apply_gain_to_buffer (i->data(), nframes, target);
288                 }
289         }
290 }
291
292 void
293 Amp::apply_simple_gain (AudioBuffer& buf, nframes_t nframes, gain_t target)
294 {
295         if (target == 0.0) {
296                 memset (buf.data(), 0, sizeof (Sample) * nframes);
297         } else if (target != 1.0) {
298                 apply_gain_to_buffer (buf.data(), nframes, target);
299         }
300 }
301
302 void
303 Amp::inc_gain (gain_t factor, void *src)
304 {
305         float desired_gain = _gain_control->user_float();
306
307         if (desired_gain == 0.0f) {
308                 set_gain (0.000001f + (0.000001f * factor), src);
309         } else {
310                 set_gain (desired_gain + (desired_gain * factor), src);
311         }
312 }
313
314 void
315 Amp::set_gain (gain_t val, void *src)
316 {
317         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
318         if (val > 1.99526231f) {
319                 val = 1.99526231f;
320         }
321
322         //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
323
324         if (src != _gain_control.get()) {
325                 _gain_control->set_value (val);
326                 // bit twisty, this will come back and call us again
327                 // (this keeps control in sync with reality)
328                 return;
329         }
330
331         _gain_control->set_float(val, false);
332         _session.set_dirty();
333 }
334
335 XMLNode&
336 Amp::state (bool full_state)
337 {
338         XMLNode& node (Processor::state (full_state));
339         node.add_property("type", "amp");
340
341         char buf[32];
342         snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
343         node.add_property("gain", buf);
344
345         return node;
346 }
347
348 int
349 Amp::set_state (const XMLNode& node, int version)
350 {
351         const XMLProperty* prop;
352
353         Processor::set_state (node, version);
354         prop = node.property ("gain");
355
356         if (prop) {
357                 gain_t val;
358
359                 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
360                         _gain_control->set_value (val);
361                 }
362         }
363
364         return 0;
365 }
366
367 void
368 Amp::GainControl::set_value (float val)
369 {
370         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
371         if (val > 1.99526231f)
372                 val = 1.99526231f;
373
374         _amp->set_gain (val, this);
375
376         AutomationControl::set_value(val);
377 }
378
379 float
380 Amp::GainControl::get_value (void) const
381 {
382         return AutomationControl::get_value();
383 }
384
385 void
386 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
387 {
388         Glib::Mutex::Lock am (control_lock(), Glib::TRY_LOCK);
389
390         if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
391                 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
392                         start_frame, end_frame, _session.gain_automation_buffer(), nframes);
393         } else {
394                 _apply_gain_automation = false;
395         }
396 }
397
398 bool
399 Amp::visible() const
400 {
401         return true;
402 }