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.
23 #include "evoral/Curve.hpp"
25 #include "ardour/amp.h"
26 #include "ardour/audio_buffer.h"
27 #include "ardour/buffer_set.h"
28 #include "ardour/gain_control.h"
29 #include "ardour/midi_buffer.h"
30 #include "ardour/rc_configuration.h"
31 #include "ardour/session.h"
35 using namespace ARDOUR;
38 // used for low-pass filter denormal protection
39 #define GAIN_COEFF_TINY (1e-10) // -200dB
41 Amp::Amp (Session& s, const std::string& name, boost::shared_ptr<GainControl> gc, bool control_midi_also)
44 , _apply_gain_automation(false)
45 , _current_gain(GAIN_COEFF_ZERO)
46 , _current_automation_frame (INT64_MAX)
48 , _gain_automation_buffer(0)
49 , _midi_amp (control_midi_also)
51 set_display_name (name);
52 add_control (_gain_control);
56 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out)
63 Amp::configure_io (ChanCount in, ChanCount out)
65 if (out != in) { // always 1:1
69 return Processor::configure_io (in, out);
73 scale_midi_velocity(Evoral::Event<MidiBuffer::TimeType>& ev, float factor)
75 factor = std::max(factor, 0.0f);
76 ev.set_velocity(std::min(127L, lrintf(ev.velocity() * factor)));
80 Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double /*speed*/, pframes_t nframes, bool)
82 if (!_active && !_pending_active) {
88 if (_apply_gain_automation) {
90 gain_t* gab = _gain_automation_buffer;
94 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
96 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
97 Evoral::Event<MidiBuffer::TimeType> ev = *m;
98 if (ev.is_note_on()) {
99 assert(ev.time() >= 0 && ev.time() < nframes);
100 scale_midi_velocity (ev, fabsf (gab[ev.time()]));
107 const double a = 156.825 / _session.nominal_frame_rate(); // 25 Hz LPF; see Amp::apply_gain for details
108 double lpf = _current_gain;
110 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
111 Sample* const sp = i->data();
113 for (pframes_t nx = 0; nx < nframes; ++nx) {
115 lpf += a * (gab[nx] - lpf);
119 if (fabs (lpf) < GAIN_COEFF_TINY) {
120 _current_gain = GAIN_COEFF_ZERO;
125 } else { /* manual (scalar) gain */
127 gain_t const dg = _gain_control->get_value();
129 if (_current_gain != dg) {
131 _current_gain = Amp::apply_gain (bufs, _session.nominal_frame_rate(), nframes, _current_gain, dg, _midi_amp);
133 } else if (_current_gain != GAIN_COEFF_UNITY) {
135 /* gain has not changed, but its non-unity */
138 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
139 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
143 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
144 Evoral::Event<MidiBuffer::TimeType> ev = *m;
145 if (ev.is_note_on()) {
146 scale_midi_velocity (ev, fabsf (_current_gain));
152 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
153 apply_gain_to_buffer (i->data(), nframes, _current_gain);
159 _active = _pending_active;
163 Amp::apply_gain (BufferSet& bufs, framecnt_t sample_rate, framecnt_t nframes, gain_t initial, gain_t target, bool midi_amp)
165 /** Apply a (potentially) declicked gain to the buffers of @a bufs */
168 if (nframes == 0 || bufs.count().n_total() == 0) {
172 // if we don't need to declick, defer to apply_simple_gain
173 if (initial == target) {
174 apply_simple_gain (bufs, nframes, target);
180 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
181 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
184 if (target < initial) {
185 /* fade out: remove more and more of delta from initial */
186 delta = -(initial - target);
188 /* fade in: add more and more of delta from initial */
189 delta = target - initial;
194 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
195 Evoral::Event<MidiBuffer::TimeType> ev = *m;
197 if (ev.is_note_on()) {
198 const gain_t scale = delta * (ev.time()/(double) nframes);
199 scale_midi_velocity (ev, fabsf (initial + scale));
207 /* Low pass filter coefficient: 1.0 - e^(-2.0 * π * f / 48000) f in Hz.
208 * for f << SR, approx a ~= 6.2 * f / SR;
210 const double a = 156.825 / sample_rate; // 25 Hz LPF
212 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
213 Sample* const buffer = i->data();
214 double lpf = initial;
216 for (pframes_t nx = 0; nx < nframes; ++nx) {
218 lpf += a * (target - lpf);
220 if (i == bufs.audio_begin()) {
224 if (fabsf (rv - target) < GAIN_COEFF_TINY) return target;
225 if (fabsf (rv) < GAIN_COEFF_TINY) return GAIN_COEFF_ZERO;
230 Amp::declick (BufferSet& bufs, framecnt_t nframes, int dir)
232 if (nframes == 0 || bufs.count().n_total() == 0) {
236 const framecnt_t declick = std::min ((framecnt_t) 512, nframes);
237 const double fractional_shift = 1.0 / declick ;
238 gain_t delta, initial;
241 /* fade out: remove more and more of delta from initial */
243 initial = GAIN_COEFF_UNITY;
245 /* fade in: add more and more of delta from initial */
247 initial = GAIN_COEFF_ZERO;
251 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
252 Sample* const buffer = i->data();
254 double fractional_pos = 0.0;
256 for (pframes_t nx = 0; nx < declick; ++nx) {
257 buffer[nx] *= initial + (delta * fractional_pos);
258 fractional_pos += fractional_shift;
261 /* now ensure the rest of the buffer has the target value applied, if necessary. */
262 if (declick != nframes) {
264 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
272 Amp::apply_gain (AudioBuffer& buf, framecnt_t sample_rate, framecnt_t nframes, gain_t initial, gain_t target)
274 /* Apply a (potentially) declicked gain to the contents of @a buf
275 * -- used by MonitorProcessor::run()
282 // if we don't need to declick, defer to apply_simple_gain
283 if (initial == target) {
284 apply_simple_gain (buf, nframes, target);
288 Sample* const buffer = buf.data();
289 const double a = 156.825 / sample_rate; // 25 Hz LPF, see [other] Amp::apply_gain() above for details
291 double lpf = initial;
292 for (pframes_t nx = 0; nx < nframes; ++nx) {
294 lpf += a * (target - lpf);
297 if (fabs (lpf - target) < GAIN_COEFF_TINY) return target;
298 if (fabs (lpf) < GAIN_COEFF_TINY) return GAIN_COEFF_ZERO;
303 Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target, bool midi_amp)
305 if (fabsf (target) < GAIN_COEFF_SMALL) {
308 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
309 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
312 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
313 Evoral::Event<MidiBuffer::TimeType> ev = *m;
314 if (ev.is_note_on()) {
321 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
322 memset (i->data(), 0, sizeof (Sample) * nframes);
325 } else if (target != GAIN_COEFF_UNITY) {
328 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
329 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
332 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
333 Evoral::Event<MidiBuffer::TimeType> ev = *m;
334 if (ev.is_note_on()) {
335 scale_midi_velocity(ev, fabsf (target));
341 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
342 apply_gain_to_buffer (i->data(), nframes, target);
348 Amp::apply_simple_gain (AudioBuffer& buf, framecnt_t nframes, gain_t target)
350 if (fabsf (target) < GAIN_COEFF_SMALL) {
351 memset (buf.data(), 0, sizeof (Sample) * nframes);
352 } else if (target != GAIN_COEFF_UNITY) {
353 apply_gain_to_buffer (buf.data(), nframes, target);
358 Amp::state (bool full_state)
360 XMLNode& node (Processor::state (full_state));
361 node.set_property("type", _gain_control->parameter().type() == GainAutomation ? "amp" : "trim");
362 node.add_child_nocopy (_gain_control->get_state());
368 Amp::set_state (const XMLNode& node, int version)
372 Processor::set_state (node, version);
374 if ((gain_node = node.child (Controllable::xml_node_name.c_str ())) != 0) {
375 _gain_control->set_state (*gain_node, version);
381 /** Write gain automation for this cycle into the buffer previously passed in to
382 * set_gain_automation_buffer (if we are in automation playback mode and the
383 * transport is rolling).
386 Amp::setup_gain_automation (framepos_t start_frame, framepos_t end_frame, framecnt_t nframes)
388 Glib::Threads::Mutex::Lock am (control_lock(), Glib::Threads::TRY_LOCK);
391 && (_session.transport_rolling() || _session.bounce_processing())
392 && _gain_control->automation_playback())
394 assert (_gain_automation_buffer);
396 _apply_gain_automation = _gain_control->get_masters_curve ( start_frame, end_frame, _gain_automation_buffer, nframes);
398 if (start_frame != _current_automation_frame && _session.bounce_processing ()) {
399 _current_gain = _gain_automation_buffer[0];
401 _current_automation_frame = end_frame;
403 _apply_gain_automation = false;
404 _current_automation_frame = INT64_MAX;
415 Amp::value_as_string (boost::shared_ptr<const AutomationControl> ac) const
417 if (ac == _gain_control) {
419 snprintf (buffer, sizeof (buffer), _("%.2fdB"), ac->internal_to_user (ac->get_value ()));
423 return Automatable::value_as_string (ac);
426 /** Sets up the buffer that setup_gain_automation and ::run will use for
427 * gain automationc curves. Must be called before setup_gain_automation,
428 * and must be called with process lock held.
432 Amp::set_gain_automation_buffer (gain_t* g)
434 _gain_automation_buffer = g;