2 * Copyright (C) 2006-2016 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2013-2018 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2014-2015 Ben Loftis <ben@harrisonconsoles.com>
7 * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "evoral/Curve.hpp"
30 #include "ardour/amp.h"
31 #include "ardour/audio_buffer.h"
32 #include "ardour/buffer_set.h"
33 #include "ardour/gain_control.h"
34 #include "ardour/midi_buffer.h"
35 #include "ardour/rc_configuration.h"
36 #include "ardour/session.h"
40 using namespace ARDOUR;
43 #define GAIN_COEFF_DELTA (1e-5)
45 Amp::Amp (Session& s, const std::string& name, boost::shared_ptr<GainControl> gc, bool control_midi_also)
47 , _apply_gain_automation(false)
48 , _current_gain(GAIN_COEFF_ZERO)
49 , _current_automation_sample (INT64_MAX)
51 , _gain_automation_buffer(0)
52 , _midi_amp (control_midi_also)
54 set_display_name (name);
55 add_control (_gain_control);
59 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out)
66 Amp::configure_io (ChanCount in, ChanCount out)
68 if (out != in) { // always 1:1
72 return Processor::configure_io (in, out);
76 scale_midi_velocity(Evoral::Event<MidiBuffer::TimeType>& ev, float factor)
78 factor = std::max(factor, 0.0f);
79 ev.set_velocity(std::min(127L, lrintf(ev.velocity() * factor)));
83 Amp::run (BufferSet& bufs, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, double /*speed*/, pframes_t nframes, bool)
85 if (!_active && !_pending_active) {
86 /* disregard potentially prepared gain-automation. */
87 _apply_gain_automation = false;
91 if (_apply_gain_automation) {
93 gain_t* gab = _gain_automation_buffer;
96 /* see note in PluginInsert::connect_and_run -- effectively emit Changed signal */
97 _gain_control->set_value_unchecked (gab[0]);
100 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
102 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
103 Evoral::Event<MidiBuffer::TimeType> ev = *m;
104 if (ev.is_note_on()) {
105 assert(ev.time() >= 0 && ev.time() < nframes);
106 scale_midi_velocity (ev, fabsf (gab[ev.time()]));
112 const gain_t a = 156.825f / (gain_t)_session.nominal_sample_rate(); // 25 Hz LPF; see Amp::apply_gain for details
113 gain_t lpf = _current_gain;
115 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
116 Sample* const sp = i->data();
118 for (pframes_t nx = 0; nx < nframes; ++nx) {
120 lpf += a * (gab[nx] - lpf);
124 if (fabsf (lpf) < GAIN_COEFF_SMALL) {
125 _current_gain = GAIN_COEFF_ZERO;
130 /* used it, don't do it again until setup_gain_automation() is
131 * called successfully.
133 _apply_gain_automation = false;
135 } else { /* manual (scalar) gain */
137 gain_t const target_gain = _gain_control->get_value();
139 if (fabsf (_current_gain - target_gain) >= GAIN_COEFF_DELTA) {
141 _current_gain = Amp::apply_gain (bufs, _session.nominal_sample_rate(), nframes, _current_gain, target_gain, _midi_amp);
143 /* see note in PluginInsert::connect_and_run ()
144 * set_value_unchecked() won't emit a signal since the value is effectively unchanged
146 _gain_control->Changed (false, PBD::Controllable::NoGroup);
148 } else if (target_gain != GAIN_COEFF_UNITY) {
150 _current_gain = target_gain;
153 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
154 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
158 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
159 Evoral::Event<MidiBuffer::TimeType> ev = *m;
160 if (ev.is_note_on()) {
161 scale_midi_velocity (ev, fabsf (_current_gain));
167 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
168 apply_gain_to_buffer (i->data(), nframes, _current_gain);
171 /* unity target gain */
172 _current_gain = target_gain;
176 _active = _pending_active;
180 Amp::apply_gain (BufferSet& bufs, samplecnt_t sample_rate, samplecnt_t nframes, gain_t initial, gain_t target, bool midi_amp)
182 /** Apply a (potentially) declicked gain to the buffers of @a bufs */
185 if (nframes == 0 || bufs.count().n_total() == 0) {
189 // if we don't need to declick, defer to apply_simple_gain
190 if (initial == target) {
191 apply_simple_gain (bufs, nframes, target);
197 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
198 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
201 if (target < initial) {
202 /* fade out: remove more and more of delta from initial */
203 delta = -(initial - target);
205 /* fade in: add more and more of delta from initial */
206 delta = target - initial;
211 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
212 Evoral::Event<MidiBuffer::TimeType> ev = *m;
214 if (ev.is_note_on()) {
215 const gain_t scale = delta * (ev.time()/(double) nframes);
216 scale_midi_velocity (ev, fabsf (initial + scale));
224 /* Low pass filter coefficient: 1.0 - e^(-2.0 * π * f / 48000) f in Hz.
225 * for f << SR, approx a ~= 6.2 * f / SR;
227 const gain_t a = 156.825f / (gain_t)sample_rate; // 25 Hz LPF
229 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
230 Sample* const buffer = i->data();
231 double lpf = initial;
233 for (pframes_t nx = 0; nx < nframes; ++nx) {
235 lpf += a * (target - lpf);
237 if (i == bufs.audio_begin()) {
241 if (fabsf (rv - target) < GAIN_COEFF_DELTA) return target;
246 Amp::apply_gain (AudioBuffer& buf, samplecnt_t sample_rate, samplecnt_t nframes, gain_t initial, gain_t target, sampleoffset_t offset)
248 /* Apply a (potentially) declicked gain to the contents of @a buf
249 * -- used by MonitorProcessor::run()
256 // if we don't need to declick, defer to apply_simple_gain
257 if (initial == target) {
258 apply_simple_gain (buf, nframes, target, offset);
262 Sample* const buffer = buf.data (offset);
263 const gain_t a = 156.825f / (gain_t)sample_rate; // 25 Hz LPF, see [other] Amp::apply_gain() above for details
265 gain_t lpf = initial;
266 for (pframes_t nx = 0; nx < nframes; ++nx) {
268 lpf += a * (target - lpf);
271 if (fabsf (lpf - target) < GAIN_COEFF_DELTA) return target;
276 Amp::apply_simple_gain (BufferSet& bufs, samplecnt_t nframes, gain_t target, bool midi_amp)
278 if (fabsf (target) < GAIN_COEFF_SMALL) {
281 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
282 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
285 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
286 Evoral::Event<MidiBuffer::TimeType> ev = *m;
287 if (ev.is_note_on()) {
294 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
295 memset (i->data(), 0, sizeof (Sample) * nframes);
298 } else if (target != GAIN_COEFF_UNITY) {
301 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
302 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
305 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
306 Evoral::Event<MidiBuffer::TimeType> ev = *m;
307 if (ev.is_note_on()) {
308 scale_midi_velocity(ev, fabsf (target));
314 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
315 apply_gain_to_buffer (i->data(), nframes, target);
321 Amp::apply_simple_gain (AudioBuffer& buf, samplecnt_t nframes, gain_t target, sampleoffset_t offset)
323 if (fabsf (target) < GAIN_COEFF_SMALL) {
324 memset (buf.data (offset), 0, sizeof (Sample) * nframes);
325 } else if (target != GAIN_COEFF_UNITY) {
326 apply_gain_to_buffer (buf.data(offset), nframes, target);
333 XMLNode& node (Processor::state ());
334 node.set_property("type", _gain_control->parameter().type() == GainAutomation ? "amp" : "trim");
335 node.add_child_nocopy (_gain_control->get_state());
341 Amp::set_state (const XMLNode& node, int version)
345 Processor::set_state (node, version);
347 if ((gain_node = node.child (Controllable::xml_node_name.c_str ())) != 0) {
348 _gain_control->set_state (*gain_node, version);
354 /** Write gain automation for this cycle into the buffer previously passed in to
355 * set_gain_automation_buffer (if we are in automation playback mode and the
356 * transport is rolling).
358 * After calling this, the gain-automation buffer is valid for the next run.
359 * so make sure to call ::run() which invalidates the buffer again.
362 Amp::setup_gain_automation (samplepos_t start_sample, samplepos_t end_sample, samplecnt_t nframes)
364 Glib::Threads::Mutex::Lock am (control_lock(), Glib::Threads::TRY_LOCK);
367 && (_session.transport_rolling() || _session.bounce_processing())
368 && _gain_control->automation_playback())
370 assert (_gain_automation_buffer);
372 _apply_gain_automation = _gain_control->get_masters_curve ( start_sample, end_sample, _gain_automation_buffer, nframes);
374 if (start_sample != _current_automation_sample && _session.bounce_processing ()) {
375 _current_gain = _gain_automation_buffer[0];
377 _current_automation_sample = end_sample;
379 _apply_gain_automation = false;
380 _current_automation_sample = INT64_MAX;
390 /** Sets up the buffer that setup_gain_automation and ::run will use for
391 * gain automationc curves. Must be called before setup_gain_automation,
392 * and must be called with process lock held.
395 Amp::set_gain_automation_buffer (gain_t* g)
397 _gain_automation_buffer = g;