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