2 Copyright (C) 2006 Paul Davis
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)
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
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.
24 #include "evoral/Curve.hpp"
26 #include "ardour/amp.h"
27 #include "ardour/audio_buffer.h"
28 #include "ardour/buffer_set.h"
29 #include "ardour/gain_control.h"
30 #include "ardour/midi_buffer.h"
31 #include "ardour/rc_configuration.h"
32 #include "ardour/session.h"
36 using namespace ARDOUR;
39 // used for low-pass filter denormal protection
40 #define GAIN_COEFF_TINY (1e-10) // -200dB
42 Amp::Amp (Session& s, const std::string& name, boost::shared_ptr<GainControl> gc, bool control_midi_also)
45 , _apply_gain_automation(false)
46 , _current_gain(GAIN_COEFF_ZERO)
47 , _current_automation_frame (INT64_MAX)
49 , _gain_automation_buffer(0)
50 , _midi_amp (control_midi_also)
52 set_display_name (name);
53 add_control (_gain_control);
57 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out)
64 Amp::configure_io (ChanCount in, ChanCount out)
66 if (out != in) { // always 1:1
70 return Processor::configure_io (in, out);
74 Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
76 if (!_active && !_pending_active) {
82 if (_apply_gain_automation) {
84 gain_t* gab = _gain_automation_buffer;
88 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
90 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
91 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
92 if (ev.is_note_on()) {
93 assert(ev.time() >= 0 && ev.time() < nframes);
94 ev.scale_velocity (fabsf (gab[ev.time()]));
101 const double a = 156.825 / _session.nominal_frame_rate(); // 25 Hz LPF; see Amp::apply_gain for details
102 double lpf = _current_gain;
104 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
105 Sample* const sp = i->data();
107 for (pframes_t nx = 0; nx < nframes; ++nx) {
109 lpf += a * (gab[nx] - lpf);
113 if (fabs (lpf) < GAIN_COEFF_TINY) {
114 _current_gain = GAIN_COEFF_ZERO;
119 } else { /* manual (scalar) gain */
121 gain_t const dg = _gain_control->user_double();
123 if (_current_gain != dg) {
125 _current_gain = Amp::apply_gain (bufs, _session.nominal_frame_rate(), nframes, _current_gain, dg, _midi_amp);
127 } else if (_current_gain != GAIN_COEFF_UNITY) {
129 /* gain has not changed, but its non-unity */
132 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
133 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
137 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
138 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
139 if (ev.is_note_on()) {
140 ev.scale_velocity (fabsf (_current_gain));
146 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
147 apply_gain_to_buffer (i->data(), nframes, _current_gain);
153 _active = _pending_active;
157 Amp::apply_gain (BufferSet& bufs, framecnt_t sample_rate, framecnt_t nframes, gain_t initial, gain_t target, bool midi_amp)
159 /** Apply a (potentially) declicked gain to the buffers of @a bufs */
162 if (nframes == 0 || bufs.count().n_total() == 0) {
166 // if we don't need to declick, defer to apply_simple_gain
167 if (initial == target) {
168 apply_simple_gain (bufs, nframes, target);
174 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
175 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
178 if (target < initial) {
179 /* fade out: remove more and more of delta from initial */
180 delta = -(initial - target);
182 /* fade in: add more and more of delta from initial */
183 delta = target - initial;
188 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
189 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
191 if (ev.is_note_on()) {
192 const gain_t scale = delta * (ev.time()/(double) nframes);
193 ev.scale_velocity (fabsf (initial+scale));
201 /* Low pass filter coefficient: 1.0 - e^(-2.0 * π * f / 48000) f in Hz.
202 * for f << SR, approx a ~= 6.2 * f / SR;
204 const double a = 156.825 / sample_rate; // 25 Hz LPF
206 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
207 Sample* const buffer = i->data();
208 double lpf = initial;
210 for (pframes_t nx = 0; nx < nframes; ++nx) {
212 lpf += a * (target - lpf);
214 if (i == bufs.audio_begin()) {
218 if (fabsf (rv - target) < GAIN_COEFF_TINY) return target;
219 if (fabsf (rv) < GAIN_COEFF_TINY) return GAIN_COEFF_ZERO;
224 Amp::declick (BufferSet& bufs, framecnt_t nframes, int dir)
226 if (nframes == 0 || bufs.count().n_total() == 0) {
230 const framecnt_t declick = std::min ((framecnt_t) 512, nframes);
231 const double fractional_shift = 1.0 / declick ;
232 gain_t delta, initial;
235 /* fade out: remove more and more of delta from initial */
237 initial = GAIN_COEFF_UNITY;
239 /* fade in: add more and more of delta from initial */
241 initial = GAIN_COEFF_ZERO;
245 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
246 Sample* const buffer = i->data();
248 double fractional_pos = 0.0;
250 for (pframes_t nx = 0; nx < declick; ++nx) {
251 buffer[nx] *= initial + (delta * fractional_pos);
252 fractional_pos += fractional_shift;
255 /* now ensure the rest of the buffer has the target value applied, if necessary. */
256 if (declick != nframes) {
258 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
266 Amp::apply_gain (AudioBuffer& buf, framecnt_t sample_rate, framecnt_t nframes, gain_t initial, gain_t target)
268 /* Apply a (potentially) declicked gain to the contents of @a buf
269 * -- used by MonitorProcessor::run()
276 // if we don't need to declick, defer to apply_simple_gain
277 if (initial == target) {
278 apply_simple_gain (buf, nframes, target);
282 Sample* const buffer = buf.data();
283 const double a = 156.825 / sample_rate; // 25 Hz LPF, see [other] Amp::apply_gain() above for details
285 double lpf = initial;
286 for (pframes_t nx = 0; nx < nframes; ++nx) {
288 lpf += a * (target - lpf);
291 if (fabs (lpf - target) < GAIN_COEFF_TINY) return target;
292 if (fabs (lpf) < GAIN_COEFF_TINY) return GAIN_COEFF_ZERO;
297 Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target, bool midi_amp)
299 if (fabsf (target) < GAIN_COEFF_SMALL) {
302 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
303 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
306 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
307 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
308 if (ev.is_note_on()) {
315 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
316 memset (i->data(), 0, sizeof (Sample) * nframes);
319 } else if (target != GAIN_COEFF_UNITY) {
322 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
323 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
326 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
327 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
328 if (ev.is_note_on()) {
329 ev.scale_velocity (fabsf (target));
335 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
336 apply_gain_to_buffer (i->data(), nframes, target);
342 Amp::apply_simple_gain (AudioBuffer& buf, framecnt_t nframes, gain_t target)
344 if (fabsf (target) < GAIN_COEFF_SMALL) {
345 memset (buf.data(), 0, sizeof (Sample) * nframes);
346 } else if (target != GAIN_COEFF_UNITY) {
347 apply_gain_to_buffer (buf.data(), nframes, target);
352 Amp::state (bool full_state)
354 XMLNode& node (Processor::state (full_state));
355 node.add_property("type", _gain_control->parameter().type() == GainAutomation ? "amp" : "trim");
356 node.add_child_nocopy (_gain_control->get_state());
362 Amp::set_state (const XMLNode& node, int version)
366 Processor::set_state (node, version);
368 if ((gain_node = node.child (Controllable::xml_node_name.c_str())) != 0) {
369 _gain_control->set_state (*gain_node, version);
375 /** Write gain automation for this cycle into the buffer previously passed in to
376 * set_gain_automation_buffer (if we are in automation playback mode and the
377 * transport is rolling).
380 Amp::setup_gain_automation (framepos_t start_frame, framepos_t end_frame, framecnt_t nframes)
382 Glib::Threads::Mutex::Lock am (control_lock(), Glib::Threads::TRY_LOCK);
385 && (_session.transport_rolling() || _session.bounce_processing())
386 && _gain_control->automation_playback())
388 assert (_gain_automation_buffer);
389 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
390 start_frame, end_frame, _gain_automation_buffer, nframes);
391 if (start_frame != _current_automation_frame) {
392 _current_gain = _gain_automation_buffer[0];
394 _current_automation_frame = end_frame;
396 _apply_gain_automation = false;
397 _current_automation_frame = INT64_MAX;
408 Amp::value_as_string (boost::shared_ptr<AutomationControl> ac) const
410 if (ac == _gain_control) {
412 snprintf (buffer, sizeof (buffer), _("%.2fdB"), ac->internal_to_user (ac->get_value ()));
416 return Automatable::value_as_string (ac);
419 /** Sets up the buffer that setup_gain_automation and ::run will use for
420 * gain automationc curves. Must be called before setup_gain_automation,
421 * and must be called with process lock held.
425 Amp::set_gain_automation_buffer (gain_t* g)
427 _gain_automation_buffer = g;