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/configuration.h"
30 #include "ardour/io.h"
31 #include "ardour/mute_master.h"
32 #include "ardour/session.h"
36 using namespace ARDOUR;
38 Amp::Amp(Session& s, boost::shared_ptr<MuteMaster> mm)
41 , _apply_gain_automation(false)
45 boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(GainAutomation)));
46 _gain_control = boost::shared_ptr<GainControl>( new GainControl(X_("gaincontrol"), s, this, Evoral::Parameter(GainAutomation), gl ));
47 add_control(_gain_control);
51 Amp::display_name() const
57 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
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, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
78 if (!_active && !_pending_active) {
83 mute_gain = _mute_master->mute_gain_at (MuteMaster::PreFader);
90 if (_apply_gain_automation) {
92 gain_t* gab = _session.gain_automation_buffer ();
94 if (mute_gain == 0.0) {
98 if (_current_gain == 0.0) {
102 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
109 Amp::apply_gain (bufs, nframes, _current_gain, 0.0);
114 } else if (mute_gain != 1.0) {
118 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
119 Sample* const sp = i->data();
120 for (nframes_t nx = 0; nx < nframes; ++nx) {
121 sp[nx] *= gab[nx] * mute_gain;
125 _current_gain = gab[nframes-1] * mute_gain;
131 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
132 Sample* const sp = i->data();
133 for (nframes_t nx = 0; nx < nframes; ++nx) {
138 _current_gain = gab[nframes-1];
142 } else { /* manual (scalar) gain */
144 gain_t dg = _gain_control->user_float() * mute_gain;
146 if (_current_gain != dg) {
148 Amp::apply_gain (bufs, nframes, _current_gain, dg);
151 } else if (_current_gain != 1.0f) {
153 /* gain has not changed, but its non-unity
156 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
157 apply_gain_to_buffer (i->data(), nframes, _current_gain);
163 _active = _pending_active;
167 Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target)
169 /** Apply a (potentially) declicked gain to the audio buffers of @a bufs
172 if (nframes == 0 || bufs.count().n_audio() == 0) {
176 // if we don't need to declick, defer to apply_simple_gain
177 if (initial == target) {
178 apply_simple_gain (bufs, nframes, target);
182 const nframes_t declick = std::min ((nframes_t)128, nframes);
184 double fractional_shift = -1.0/declick;
185 double fractional_pos;
186 gain_t polscale = 1.0f;
188 if (target < initial) {
189 /* fade out: remove more and more of delta from initial */
190 delta = -(initial - target);
192 /* fade in: add more and more of delta from initial */
193 delta = target - initial;
196 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
197 Sample* const buffer = i->data();
199 fractional_pos = 1.0;
201 for (nframes_t nx = 0; nx < declick; ++nx) {
202 buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
203 fractional_pos += fractional_shift;
206 /* now ensure the rest of the buffer has the target value applied, if necessary. */
208 if (declick != nframes) {
211 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
212 } else if (target != 1.0) {
213 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
220 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
223 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
224 memset (i->data(), 0, sizeof (Sample) * nframes);
226 } else if (target != 1.0) {
227 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
228 apply_gain_to_buffer (i->data(), nframes, target);
234 Amp::inc_gain (gain_t factor, void *src)
236 float desired_gain = _gain_control->user_float();
238 if (desired_gain == 0.0f) {
239 set_gain (0.000001f + (0.000001f * factor), src);
241 set_gain (desired_gain + (desired_gain * factor), src);
246 Amp::set_gain (gain_t val, void *src)
248 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
249 if (val > 1.99526231f) {
253 //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
255 if (src != _gain_control.get()) {
256 _gain_control->set_value (val);
257 // bit twisty, this will come back and call us again
258 // (this keeps control in sync with reality)
262 _gain_control->set_float(val, false);
263 _session.set_dirty();
267 Amp::state (bool full_state)
269 XMLNode& node (Processor::state (full_state));
270 node.add_property("type", "amp");
273 snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
274 node.add_property("gain", buf);
280 Amp::set_state (const XMLNode& node)
282 const XMLProperty* prop;
284 Processor::set_state (node);
285 prop = node.property ("gain");
290 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
291 _gain_control->set_value (val);
299 Amp::GainControl::set_value (float val)
301 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
302 if (val > 1.99526231f)
305 _amp->set_gain (val, this);
307 AutomationControl::set_value(val);
311 Amp::GainControl::get_value (void) const
313 return AutomationControl::get_value();
317 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
319 Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
321 if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
322 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
323 start_frame, end_frame, _session.gain_automation_buffer(), nframes);
325 _apply_gain_automation = false;