how about that ... a monitor/main section .. GUI is still unfinished .. several 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/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, bool)
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 buffers of @a bufs
183          */
184
185         if (nframes == 0 || bufs.count().n_total() == 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
200         if (target < initial) {
201                 /* fade out: remove more and more of delta from initial */
202                 delta = -(initial - target);
203         } else {
204                 /* fade in: add more and more of delta from initial */
205                 delta = target - initial;
206         }
207
208         /* MIDI Gain */
209
210         for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
211
212
213                 MidiBuffer& mb (*i);
214
215                 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
216                         Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
217
218                         if (ev.is_note_on()) {
219                                 gain_t scale = delta * (ev.time()/nframes);
220                                 std::cerr << "scale by " << scale << " for " << ev.time() << " of " << nframes << std::endl;
221                                 ev.scale_velocity (scale);
222                         }
223                 }
224         }
225
226         /* Audio Gain */
227
228         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
229                 Sample* const buffer = i->data();
230
231                 fractional_pos = 1.0;
232
233                 for (nframes_t nx = 0; nx < declick; ++nx) {
234                         buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
235                         fractional_pos += fractional_shift;
236                 }
237
238                 /* now ensure the rest of the buffer has the target value applied, if necessary. */
239
240                 if (declick != nframes) {
241
242                         if (target == 0.0) {
243                                 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
244                         } else if (target != 1.0) {
245                                 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
246                         }
247                 }
248         }
249 }
250
251 void
252 Amp::apply_gain (AudioBuffer& buf, nframes_t nframes, gain_t initial, gain_t target)
253 {
254         /** Apply a (potentially) declicked gain to the contents of @a buf
255          */
256
257         if (nframes == 0) {
258                 return;
259         }
260
261         // if we don't need to declick, defer to apply_simple_gain
262         if (initial == target) {
263                 apply_simple_gain (buf, nframes, target);
264                 return;
265         }
266
267         const nframes_t declick = std::min ((nframes_t)128, nframes);
268         gain_t         delta;
269         double         fractional_shift = -1.0/declick;
270         double         fractional_pos;
271
272         if (target < initial) {
273                 /* fade out: remove more and more of delta from initial */
274                 delta = -(initial - target);
275         } else {
276                 /* fade in: add more and more of delta from initial */
277                 delta = target - initial;
278         }
279
280
281         Sample* const buffer = buf.data();
282         
283         fractional_pos = 1.0;
284         
285         for (nframes_t nx = 0; nx < declick; ++nx) {
286                 buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
287                 fractional_pos += fractional_shift;
288         }
289         
290         /* now ensure the rest of the buffer has the target value applied, if necessary. */
291         
292         if (declick != nframes) {
293                 
294                 if (target == 0.0) {
295                         memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
296                 } else if (target != 1.0) {
297                         apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
298                 }
299         }
300 }
301
302 void
303 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
304 {
305         if (target == 0.0) {
306
307                 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
308                         MidiBuffer& mb (*i);
309
310                         for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
311                                 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
312                                 if (ev.is_note_on()) {
313                                         ev.set_velocity (0);
314                                 }
315                         }
316                 }
317
318                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
319                         memset (i->data(), 0, sizeof (Sample) * nframes);
320                 }
321
322         } else if (target != 1.0) {
323
324                 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
325                         MidiBuffer& mb (*i);
326
327                         for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
328                                 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
329                                 if (ev.is_note_on()) {
330                                         ev.scale_velocity (target);
331                                 }
332                         }
333                 }
334
335                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
336                         apply_gain_to_buffer (i->data(), nframes, target);
337                 }
338         }
339 }
340
341 void
342 Amp::apply_simple_gain (AudioBuffer& buf, nframes_t nframes, gain_t target)
343 {
344         if (target == 0.0) {
345                 memset (buf.data(), 0, sizeof (Sample) * nframes);
346         } else if (target != 1.0) {
347                 apply_gain_to_buffer (buf.data(), nframes, target);
348         }
349 }
350
351 void
352 Amp::inc_gain (gain_t factor, void *src)
353 {
354         float desired_gain = _gain_control->user_float();
355
356         if (desired_gain == 0.0f) {
357                 set_gain (0.000001f + (0.000001f * factor), src);
358         } else {
359                 set_gain (desired_gain + (desired_gain * factor), src);
360         }
361 }
362
363 void
364 Amp::set_gain (gain_t val, void *src)
365 {
366         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
367         if (val > 1.99526231f) {
368                 val = 1.99526231f;
369         }
370
371         //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
372
373         if (src != _gain_control.get()) {
374                 _gain_control->set_value (val);
375                 // bit twisty, this will come back and call us again
376                 // (this keeps control in sync with reality)
377                 return;
378         }
379
380         _gain_control->set_float(val, false);
381         _session.set_dirty();
382 }
383
384 XMLNode&
385 Amp::state (bool full_state)
386 {
387         XMLNode& node (Processor::state (full_state));
388         node.add_property("type", "amp");
389
390         char buf[32];
391         snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
392         node.add_property("gain", buf);
393
394         return node;
395 }
396
397 int
398 Amp::set_state (const XMLNode& node, int version)
399 {
400         const XMLProperty* prop;
401
402         Processor::set_state (node, version);
403         prop = node.property ("gain");
404
405         if (prop) {
406                 gain_t val;
407
408                 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
409                         _gain_control->set_value (val);
410                 }
411         }
412
413         return 0;
414 }
415
416 void
417 Amp::GainControl::set_value (float val)
418 {
419         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
420         if (val > 1.99526231f)
421                 val = 1.99526231f;
422
423         _amp->set_gain (val, this);
424
425         AutomationControl::set_value(val);
426 }
427
428 float
429 Amp::GainControl::get_value (void) const
430 {
431         return AutomationControl::get_value();
432 }
433
434 void
435 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
436 {
437         Glib::Mutex::Lock am (control_lock(), Glib::TRY_LOCK);
438
439         if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
440                 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
441                         start_frame, end_frame, _session.gain_automation_buffer(), nframes);
442         } else {
443                 _apply_gain_automation = false;
444         }
445 }
446
447 bool
448 Amp::visible() const
449 {
450         return true;
451 }