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)
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);
165 Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target)
167 /** Apply a (potentially) declicked gain to the audio buffers of @a bufs
170 if (nframes == 0 || bufs.count().n_audio() == 0) {
174 // if we don't need to declick, defer to apply_simple_gain
175 if (initial == target) {
176 apply_simple_gain (bufs, nframes, target);
180 const nframes_t declick = std::min ((nframes_t)128, nframes);
182 double fractional_shift = -1.0/declick;
183 double fractional_pos;
184 gain_t polscale = 1.0f;
186 if (target < initial) {
187 /* fade out: remove more and more of delta from initial */
188 delta = -(initial - target);
190 /* fade in: add more and more of delta from initial */
191 delta = target - initial;
194 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
195 Sample* const buffer = i->data();
197 fractional_pos = 1.0;
199 for (nframes_t nx = 0; nx < declick; ++nx) {
200 buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
201 fractional_pos += fractional_shift;
204 /* now ensure the rest of the buffer has the target value applied, if necessary. */
206 if (declick != nframes) {
209 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
210 } else if (target != 1.0) {
211 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
218 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
221 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
222 memset (i->data(), 0, sizeof (Sample) * nframes);
224 } else if (target != 1.0) {
225 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
226 apply_gain_to_buffer (i->data(), nframes, target);
232 Amp::inc_gain (gain_t factor, void *src)
234 float desired_gain = _gain_control->user_float();
236 if (desired_gain == 0.0f) {
237 set_gain (0.000001f + (0.000001f * factor), src);
239 set_gain (desired_gain + (desired_gain * factor), src);
244 Amp::set_gain (gain_t val, void *src)
246 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
247 if (val > 1.99526231f) {
251 //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
253 if (src != _gain_control.get()) {
254 _gain_control->set_value (val);
255 // bit twisty, this will come back and call us again
256 // (this keeps control in sync with reality)
260 _gain_control->set_float(val, false);
261 _session.set_dirty();
265 Amp::state (bool full_state)
267 XMLNode& node (Processor::state (full_state));
268 node.add_property("type", "amp");
271 snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
272 node.add_property("gain", buf);
278 Amp::set_state (const XMLNode& node)
280 const XMLProperty* prop;
282 Processor::set_state (node);
283 prop = node.property ("gain");
288 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
289 _gain_control->set_value (val);
297 Amp::GainControl::set_value (float val)
299 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
300 if (val > 1.99526231f)
303 _amp->set_gain (val, this);
305 AutomationControl::set_value(val);
309 Amp::GainControl::get_value (void) const
311 return AutomationControl::get_value();
315 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
317 Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
319 if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
320 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
321 start_frame, end_frame, _session.gain_automation_buffer(), nframes);
323 _apply_gain_automation = false;