make i18n build work ; add mackie dir back to build ; token work on amp for MIDI...
[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::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
158                                         apply_gain_to_buffer (i->data(), nframes, _current_gain);
159                                 }
160                         } 
161                 }
162         }
163
164         _active = _pending_active;
165 }
166
167 void
168 Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target)
169 {
170         /** Apply a (potentially) declicked gain to the audio buffers of @a bufs 
171          */
172         
173         if (nframes == 0 || bufs.count().n_audio() == 0) {
174                 return;
175         }
176
177         // if we don't need to declick, defer to apply_simple_gain
178         if (initial == target) {
179                 apply_simple_gain (bufs, nframes, target);
180                 return;
181         }
182
183         const nframes_t declick = std::min ((nframes_t)128, nframes);
184         gain_t         delta;
185         double         fractional_shift = -1.0/declick;
186         double         fractional_pos;
187         gain_t         polscale = 1.0f;
188
189         if (target < initial) {
190                 /* fade out: remove more and more of delta from initial */
191                 delta = -(initial - target);
192         } else {
193                 /* fade in: add more and more of delta from initial */
194                 delta = target - initial;
195         }
196
197         /* MIDI Gain */
198
199         for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
200 #if 0
201                 MidiBuffer& mb (*i);
202
203                 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
204                         Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*m);
205                         if (ev.buffer()[0] == MIDI_CMD_NOTE_ON) {
206                                 ev.buffer()[2] = (uint8_t) rint (ev.buffer()[2] * 1.0);
207                         }
208                 }
209 #endif
210         }
211
212         /* Audio Gain */
213
214         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
215                 Sample* const buffer = i->data();
216                 
217                 fractional_pos = 1.0;
218
219                 for (nframes_t nx = 0; nx < declick; ++nx) {
220                         buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
221                         fractional_pos += fractional_shift;
222                 }
223                 
224                 /* now ensure the rest of the buffer has the target value applied, if necessary. */
225                 
226                 if (declick != nframes) {
227
228                         if (target == 0.0) {
229                                 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
230                         } else if (target != 1.0) {
231                                 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
232                         }
233                 }
234         }
235 }
236
237 void
238 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
239 {
240         if (target == 0.0) {
241                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
242                         memset (i->data(), 0, sizeof (Sample) * nframes);
243                 }
244         } else if (target != 1.0) {
245                 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
246                         apply_gain_to_buffer (i->data(), nframes, target);
247                 }
248         }
249 }
250
251 void
252 Amp::inc_gain (gain_t factor, void *src)
253 {
254         float desired_gain = _gain_control->user_float();
255         
256         if (desired_gain == 0.0f) {
257                 set_gain (0.000001f + (0.000001f * factor), src);
258         } else {
259                 set_gain (desired_gain + (desired_gain * factor), src);
260         }
261 }
262
263 void
264 Amp::set_gain (gain_t val, void *src)
265 {
266         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
267         if (val > 1.99526231f) {
268                 val = 1.99526231f;
269         }
270
271         //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
272
273         if (src != _gain_control.get()) {
274                 _gain_control->set_value (val);
275                 // bit twisty, this will come back and call us again
276                 // (this keeps control in sync with reality)
277                 return;
278         }
279
280         _gain_control->set_float(val, false);
281         _session.set_dirty();
282 }
283
284 XMLNode&
285 Amp::state (bool full_state)
286 {
287         XMLNode& node (Processor::state (full_state));
288         node.add_property("type", "amp");
289
290         char buf[32];
291         snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
292         node.add_property("gain", buf);
293
294         return node;
295 }
296
297 int
298 Amp::set_state (const XMLNode& node)
299 {
300         const XMLProperty* prop;
301
302         Processor::set_state (node);
303         prop = node.property ("gain");
304
305         if (prop) {
306                 gain_t val;
307
308                 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
309                         _gain_control->set_value (val);
310                 }
311         }
312
313         return 0;
314 }
315
316 void
317 Amp::GainControl::set_value (float val)
318 {
319         // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
320         if (val > 1.99526231f)
321                 val = 1.99526231f;
322
323         _amp->set_gain (val, this);
324         
325         AutomationControl::set_value(val);
326 }
327
328 float
329 Amp::GainControl::get_value (void) const
330 {
331         return AutomationControl::get_value();
332 }
333
334 void
335 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
336 {
337         Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
338
339         if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
340                 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
341                         start_frame, end_frame, _session.gain_automation_buffer(), nframes);
342         } else {
343                 _apply_gain_automation = false;
344         }
345 }
346
347 bool
348 Amp::visible() const
349 {
350         return true;
351 }