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/midi_buffer.h"
30 #include "ardour/rc_configuration.h"
31 #include "ardour/session.h"
35 using namespace ARDOUR;
42 , _apply_gain_automation(false)
43 , _current_gain(GAIN_COEFF_UNITY)
44 , _current_automation_frame (INT64_MAX)
45 , _gain_automation_buffer(0)
47 Evoral::Parameter p (GainAutomation);
48 boost::shared_ptr<AutomationList> gl (new AutomationList (p));
49 _gain_control = boost::shared_ptr<GainControl> (new GainControl (X_("gaincontrol"), s, this, p, gl));
50 _gain_control->set_flags (Controllable::GainLike);
52 add_control(_gain_control);
56 Amp::display_name() const
62 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out)
69 Amp::configure_io (ChanCount in, ChanCount out)
71 if (out != in) { // always 1:1
75 return Processor::configure_io (in, out);
79 Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
81 if (!_active && !_pending_active) {
87 if (_apply_gain_automation) {
89 gain_t* gab = _gain_automation_buffer;
92 const float a = 62.78 / _session.nominal_frame_rate(); // 10 Hz LPF
93 float lpf = _current_gain;
95 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
96 Sample* const sp = i->data();
98 for (pframes_t nx = 0; nx < nframes; ++nx) {
100 lpf += a * (gab[nx] - lpf);
110 } else { /* manual (scalar) gain */
112 gain_t const dg = _gain_control->user_double();
114 if (_current_gain != dg) {
116 _current_gain = Amp::apply_gain (bufs, _session.nominal_frame_rate(), nframes, _current_gain, dg);
118 } else if (_current_gain != GAIN_COEFF_UNITY) {
120 /* gain has not changed, but its non-unity
123 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
127 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
128 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
129 if (ev.is_note_on()) {
130 ev.scale_velocity (_current_gain);
135 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
136 apply_gain_to_buffer (i->data(), nframes, _current_gain);
142 _active = _pending_active;
146 Amp::apply_gain (BufferSet& bufs, framecnt_t sample_rate, framecnt_t nframes, gain_t initial, gain_t target)
148 /** Apply a (potentially) declicked gain to the buffers of @a bufs */
151 if (nframes == 0 || bufs.count().n_total() == 0) {
155 // if we don't need to declick, defer to apply_simple_gain
156 if (initial == target) {
157 apply_simple_gain (bufs, nframes, target);
162 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
165 if (target < initial) {
166 /* fade out: remove more and more of delta from initial */
167 delta = -(initial - target);
169 /* fade in: add more and more of delta from initial */
170 delta = target - initial;
175 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
176 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
178 if (ev.is_note_on()) {
179 const gain_t scale = delta * (ev.time()/(double) nframes);
180 ev.scale_velocity (initial+scale);
187 /* Low pass filter coefficient: 1.0 - e^(-2.0 * π * f / 48000) f in Hz.
188 * for f << SR, approx a ~= 6.2 * f / SR;
190 const float a = 62.78 / sample_rate; // 10 Hz LPF
192 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
193 Sample* const buffer = i->data();
196 for (pframes_t nx = 0; nx < nframes; ++nx) {
198 lpf += a * (target - lpf);
200 if (i == bufs.audio_begin()) {
204 // 1e-10 ~ 200dB, prevent denormals.
205 if (rv < 1e-10) return 0;
206 if (fabsf(rv - 1.0) < 1e-10) return 1.0;
211 Amp::declick (BufferSet& bufs, framecnt_t nframes, int dir)
213 if (nframes == 0 || bufs.count().n_total() == 0) {
217 const framecnt_t declick = std::min ((framecnt_t) 512, nframes);
218 const double fractional_shift = 1.0 / declick ;
219 gain_t delta, initial;
222 /* fade out: remove more and more of delta from initial */
224 initial = GAIN_COEFF_UNITY;
226 /* fade in: add more and more of delta from initial */
228 initial = GAIN_COEFF_ZERO;
232 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
233 Sample* const buffer = i->data();
235 double fractional_pos = 0.0;
237 for (pframes_t nx = 0; nx < declick; ++nx) {
238 buffer[nx] *= initial + (delta * fractional_pos);
239 fractional_pos += fractional_shift;
242 /* now ensure the rest of the buffer has the target value applied, if necessary. */
243 if (declick != nframes) {
245 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
253 Amp::apply_gain (AudioBuffer& buf, framecnt_t sample_rate, framecnt_t nframes, gain_t initial, gain_t target)
255 /* Apply a (potentially) declicked gain to the contents of @a buf
256 * -- used by MonitorProcessor::run()
263 // if we don't need to declick, defer to apply_simple_gain
264 if (initial == target) {
265 apply_simple_gain (buf, nframes, target);
269 Sample* const buffer = buf.data();
270 const float a = 62.78 / sample_rate; // 10 Hz LPF, see [other] Amp::apply_gain() above,
273 for (pframes_t nx = 0; nx < nframes; ++nx) {
275 lpf += a * (target - lpf);
278 if (lpf < 1e-10) return 0; // TODO use GAIN_COEFF_TINY or _DENORMAL
279 if (fabsf(lpf - GAIN_COEFF_UNITY) < 1e-10) return GAIN_COEFF_UNITY;
284 Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target)
286 if (target < GAIN_COEFF_SMALL) {
288 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
291 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
292 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
293 if (ev.is_note_on()) {
299 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
300 memset (i->data(), 0, sizeof (Sample) * nframes);
303 } else if (target != GAIN_COEFF_UNITY) {
305 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
308 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
309 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
310 if (ev.is_note_on()) {
311 ev.scale_velocity (target);
316 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
317 apply_gain_to_buffer (i->data(), nframes, target);
323 Amp::apply_simple_gain (AudioBuffer& buf, framecnt_t nframes, gain_t target)
325 if (target < GAIN_COEFF_SMALL) {
326 memset (buf.data(), 0, sizeof (Sample) * nframes);
327 } else if (target != GAIN_COEFF_UNITY) {
328 apply_gain_to_buffer (buf.data(), nframes, target);
333 Amp::inc_gain (gain_t factor, void *src)
335 float desired_gain = _gain_control->user_double();
337 if (desired_gain < GAIN_COEFF_SMALL) {
338 set_gain (0.000001f + (0.000001f * factor), src);
340 set_gain (desired_gain + (desired_gain * factor), src);
345 Amp::set_gain (gain_t val, void *)
347 _gain_control->set_value (val);
351 Amp::state (bool full_state)
353 XMLNode& node (Processor::state (full_state));
354 node.add_property("type", "amp");
355 node.add_child_nocopy (_gain_control->get_state());
361 Amp::set_state (const XMLNode& node, int version)
365 Processor::set_state (node, version);
367 if ((gain_node = node.child (Controllable::xml_node_name.c_str())) != 0) {
368 _gain_control->set_state (*gain_node, version);
375 Amp::GainControl::set_value (double val)
377 AutomationControl::set_value (min (val, (double) Config->get_max_gain()));
378 _amp->session().set_dirty ();
382 Amp::GainControl::internal_to_interface (double v) const
384 return gain_to_slider_position (v);
388 Amp::GainControl::interface_to_internal (double v) const
390 return slider_position_to_gain (v);
394 Amp::GainControl::internal_to_user (double v) const
396 return accurate_coefficient_to_dB (v);
400 Amp::GainControl::user_to_internal (double u) const
402 return dB_to_coefficient (u);
406 Amp::GainControl::get_user_string () const
408 char theBuf[32]; sprintf( theBuf, _("%3.1f dB"), accurate_coefficient_to_dB (get_value()));
409 return std::string(theBuf);
412 /** Write gain automation for this cycle into the buffer previously passed in to
413 * set_gain_automation_buffer (if we are in automation playback mode and the
414 * transport is rolling).
417 Amp::setup_gain_automation (framepos_t start_frame, framepos_t end_frame, framecnt_t nframes)
419 Glib::Threads::Mutex::Lock am (control_lock(), Glib::Threads::TRY_LOCK);
422 && (_session.transport_rolling() || _session.bounce_processing())
423 && _gain_control->automation_playback())
425 assert (_gain_automation_buffer);
426 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
427 start_frame, end_frame, _gain_automation_buffer, nframes);
428 if (start_frame != _current_automation_frame) {
429 _current_gain = _gain_automation_buffer[0];
431 _current_automation_frame = end_frame;
433 _apply_gain_automation = false;
434 _current_automation_frame = INT64_MAX;
445 Amp::value_as_string (boost::shared_ptr<AutomationControl> ac) const
447 if (ac == _gain_control) {
449 snprintf (buffer, sizeof (buffer), _("%.2fdB"), ac->internal_to_user (ac->get_value ()));
453 return Automatable::value_as_string (ac);
456 /** Sets up the buffer that setup_gain_automation and ::run will use for
457 * gain automationc curves. Must be called before setup_gain_automation,
458 * and must be called with process lock held.
462 Amp::set_gain_automation_buffer (gain_t* g)
464 _gain_automation_buffer = g;