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 #define GAIN_COEFF_DELTA (1e-5)
40 Amp::Amp (Session& s, const std::string& name, boost::shared_ptr<GainControl> gc, bool control_midi_also)
42 , _apply_gain_automation(false)
43 , _current_gain(GAIN_COEFF_ZERO)
44 , _current_automation_frame (INT64_MAX)
46 , _gain_automation_buffer(0)
47 , _midi_amp (control_midi_also)
49 set_display_name (name);
50 add_control (_gain_control);
54 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out)
61 Amp::configure_io (ChanCount in, ChanCount out)
63 if (out != in) { // always 1:1
67 return Processor::configure_io (in, out);
71 scale_midi_velocity(Evoral::Event<MidiBuffer::TimeType>& ev, float factor)
73 factor = std::max(factor, 0.0f);
74 ev.set_velocity(std::min(127L, lrintf(ev.velocity() * factor)));
78 Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double /*speed*/, pframes_t nframes, bool)
80 if (!_active && !_pending_active) {
84 if (_apply_gain_automation) {
86 gain_t* gab = _gain_automation_buffer;
89 /* see note in PluginInsert::connect_and_run -- effectively emit Changed signal */
90 _gain_control->set_value_unchecked (gab[0]);
93 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
95 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
96 Evoral::Event<MidiBuffer::TimeType> ev = *m;
97 if (ev.is_note_on()) {
98 assert(ev.time() >= 0 && ev.time() < nframes);
99 scale_midi_velocity (ev, fabsf (gab[ev.time()]));
105 const gain_t a = 156.825f / (gain_t)_session.nominal_frame_rate(); // 25 Hz LPF; see Amp::apply_gain for details
106 gain_t lpf = _current_gain;
108 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
109 Sample* const sp = i->data();
111 for (pframes_t nx = 0; nx < nframes; ++nx) {
113 lpf += a * (gab[nx] - lpf);
117 if (fabsf (lpf) < GAIN_COEFF_SMALL) {
118 _current_gain = GAIN_COEFF_ZERO;
123 } else { /* manual (scalar) gain */
125 gain_t const target_gain = _gain_control->get_value();
127 if (fabsf (_current_gain - target_gain) >= GAIN_COEFF_DELTA) {
129 _current_gain = Amp::apply_gain (bufs, _session.nominal_frame_rate(), nframes, _current_gain, target_gain, _midi_amp);
131 /* see note in PluginInsert::connect_and_run ()
132 * set_value_unchecked() won't emit a signal since the value is effectively unchanged
134 _gain_control->Changed (false, PBD::Controllable::NoGroup);
136 } else if (target_gain != GAIN_COEFF_UNITY) {
138 _current_gain = target_gain;
141 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
142 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
146 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
147 Evoral::Event<MidiBuffer::TimeType> ev = *m;
148 if (ev.is_note_on()) {
149 scale_midi_velocity (ev, fabsf (_current_gain));
155 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
156 apply_gain_to_buffer (i->data(), nframes, _current_gain);
159 /* unity target gain */
160 _current_gain = target_gain;
164 _active = _pending_active;
168 Amp::apply_gain (BufferSet& bufs, framecnt_t sample_rate, framecnt_t nframes, gain_t initial, gain_t target, bool midi_amp)
170 /** Apply a (potentially) declicked gain to the buffers of @a bufs */
173 if (nframes == 0 || bufs.count().n_total() == 0) {
177 // if we don't need to declick, defer to apply_simple_gain
178 if (initial == target) {
179 apply_simple_gain (bufs, nframes, target);
185 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
186 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
189 if (target < initial) {
190 /* fade out: remove more and more of delta from initial */
191 delta = -(initial - target);
193 /* fade in: add more and more of delta from initial */
194 delta = target - initial;
199 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
200 Evoral::Event<MidiBuffer::TimeType> ev = *m;
202 if (ev.is_note_on()) {
203 const gain_t scale = delta * (ev.time()/(double) nframes);
204 scale_midi_velocity (ev, fabsf (initial + scale));
212 /* Low pass filter coefficient: 1.0 - e^(-2.0 * π * f / 48000) f in Hz.
213 * for f << SR, approx a ~= 6.2 * f / SR;
215 const gain_t a = 156.825f / (gain_t)sample_rate; // 25 Hz LPF
217 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
218 Sample* const buffer = i->data();
219 double lpf = initial;
221 for (pframes_t nx = 0; nx < nframes; ++nx) {
223 lpf += a * (target - lpf);
225 if (i == bufs.audio_begin()) {
229 if (fabsf (rv - target) < GAIN_COEFF_DELTA) return target;
234 Amp::declick (BufferSet& bufs, framecnt_t nframes, int dir)
236 if (nframes == 0 || bufs.count().n_total() == 0) {
240 const framecnt_t declick = std::min ((framecnt_t) 512, nframes);
241 const double fractional_shift = 1.0 / declick ;
242 gain_t delta, initial;
245 /* fade out: remove more and more of delta from initial */
247 initial = GAIN_COEFF_UNITY;
249 /* fade in: add more and more of delta from initial */
251 initial = GAIN_COEFF_ZERO;
255 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
256 Sample* const buffer = i->data();
258 double fractional_pos = 0.0;
260 for (pframes_t nx = 0; nx < declick; ++nx) {
261 buffer[nx] *= initial + (delta * fractional_pos);
262 fractional_pos += fractional_shift;
265 /* now ensure the rest of the buffer has the target value applied, if necessary. */
266 if (declick != nframes) {
268 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
276 Amp::apply_gain (AudioBuffer& buf, framecnt_t sample_rate, framecnt_t nframes, gain_t initial, gain_t target)
278 /* Apply a (potentially) declicked gain to the contents of @a buf
279 * -- used by MonitorProcessor::run()
286 // if we don't need to declick, defer to apply_simple_gain
287 if (initial == target) {
288 apply_simple_gain (buf, nframes, target);
292 Sample* const buffer = buf.data();
293 const gain_t a = 156.825f / (gain_t)sample_rate; // 25 Hz LPF, see [other] Amp::apply_gain() above for details
295 gain_t lpf = initial;
296 for (pframes_t nx = 0; nx < nframes; ++nx) {
298 lpf += a * (target - lpf);
301 if (fabsf (lpf - target) < GAIN_COEFF_DELTA) return target;
306 Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target, bool midi_amp)
308 if (fabsf (target) < GAIN_COEFF_SMALL) {
311 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
312 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
315 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
316 Evoral::Event<MidiBuffer::TimeType> ev = *m;
317 if (ev.is_note_on()) {
324 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
325 memset (i->data(), 0, sizeof (Sample) * nframes);
328 } else if (target != GAIN_COEFF_UNITY) {
331 /* don't Trim midi velocity -- only relevant for Midi on Audio tracks */
332 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
335 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
336 Evoral::Event<MidiBuffer::TimeType> ev = *m;
337 if (ev.is_note_on()) {
338 scale_midi_velocity(ev, fabsf (target));
344 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
345 apply_gain_to_buffer (i->data(), nframes, target);
351 Amp::apply_simple_gain (AudioBuffer& buf, framecnt_t nframes, gain_t target)
353 if (fabsf (target) < GAIN_COEFF_SMALL) {
354 memset (buf.data(), 0, sizeof (Sample) * nframes);
355 } else if (target != GAIN_COEFF_UNITY) {
356 apply_gain_to_buffer (buf.data(), nframes, target);
361 Amp::state (bool full_state)
363 XMLNode& node (Processor::state (full_state));
364 node.set_property("type", _gain_control->parameter().type() == GainAutomation ? "amp" : "trim");
365 node.add_child_nocopy (_gain_control->get_state());
371 Amp::set_state (const XMLNode& node, int version)
375 Processor::set_state (node, version);
377 if ((gain_node = node.child (Controllable::xml_node_name.c_str ())) != 0) {
378 _gain_control->set_state (*gain_node, version);
384 /** Write gain automation for this cycle into the buffer previously passed in to
385 * set_gain_automation_buffer (if we are in automation playback mode and the
386 * transport is rolling).
389 Amp::setup_gain_automation (framepos_t start_frame, framepos_t end_frame, framecnt_t nframes)
391 Glib::Threads::Mutex::Lock am (control_lock(), Glib::Threads::TRY_LOCK);
394 && (_session.transport_rolling() || _session.bounce_processing())
395 && _gain_control->automation_playback())
397 assert (_gain_automation_buffer);
399 _apply_gain_automation = _gain_control->get_masters_curve ( start_frame, end_frame, _gain_automation_buffer, nframes);
401 if (start_frame != _current_automation_frame && _session.bounce_processing ()) {
402 _current_gain = _gain_automation_buffer[0];
404 _current_automation_frame = end_frame;
406 _apply_gain_automation = false;
407 _current_automation_frame = INT64_MAX;
417 /** Sets up the buffer that setup_gain_automation and ::run will use for
418 * gain automationc curves. Must be called before setup_gain_automation,
419 * and must be called with process lock held.
422 Amp::set_gain_automation_buffer (gain_t* g)
424 _gain_automation_buffer = g;