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::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
58 Amp::configure_io (ChanCount in, ChanCount out)
60 if (out != in) { // always 1:1
64 return Processor::configure_io (in, out);
68 Amp::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
73 mute_gain = _mute_master->mute_gain_at (MuteMaster::PreFader);
80 if (_apply_gain_automation) {
82 gain_t* gab = _session.gain_automation_buffer ();
84 if (mute_gain == 0.0) {
88 if (_current_gain == 0.0) {
92 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
99 Amp::apply_gain (bufs, nframes, _current_gain, 0.0);
104 } else if (mute_gain != 1.0) {
108 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
109 Sample* const sp = i->data();
110 for (nframes_t nx = 0; nx < nframes; ++nx) {
111 sp[nx] *= gab[nx] * mute_gain;
115 _current_gain = gab[nframes-1] * mute_gain;
121 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
122 Sample* const sp = i->data();
123 for (nframes_t nx = 0; nx < nframes; ++nx) {
128 _current_gain = gab[nframes-1];
132 } else { /* manual (scalar) gain */
134 gain_t dg = _gain_control->user_float() * mute_gain;
136 if (_current_gain != dg) {
138 Amp::apply_gain (bufs, nframes, _current_gain, dg);
141 } else if (_current_gain != 1.0f) {
143 /* gain has not changed, but its non-unity
146 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
147 apply_gain_to_buffer (i->data(), nframes, _current_gain);
155 Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target)
157 /** Apply a (potentially) declicked gain to the audio buffers of @a bufs
160 if (nframes == 0 || bufs.count().n_audio() == 0) {
164 // if we don't need to declick, defer to apply_simple_gain
165 if (initial == target) {
166 apply_simple_gain (bufs, nframes, target);
170 const nframes_t declick = std::min ((nframes_t)128, nframes);
172 double fractional_shift = -1.0/declick;
173 double fractional_pos;
174 gain_t polscale = 1.0f;
176 if (target < initial) {
177 /* fade out: remove more and more of delta from initial */
178 delta = -(initial - target);
180 /* fade in: add more and more of delta from initial */
181 delta = target - initial;
184 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
185 Sample* const buffer = i->data();
187 fractional_pos = 1.0;
189 for (nframes_t nx = 0; nx < declick; ++nx) {
190 buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
191 fractional_pos += fractional_shift;
194 /* now ensure the rest of the buffer has the target value applied, if necessary. */
196 if (declick != nframes) {
199 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
200 } else if (target != 1.0) {
201 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
208 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
211 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
212 memset (i->data(), 0, sizeof (Sample) * nframes);
214 } else if (target != 1.0) {
215 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
216 apply_gain_to_buffer (i->data(), nframes, target);
222 Amp::inc_gain (gain_t factor, void *src)
224 float desired_gain = _gain_control->user_float();
225 if (desired_gain == 0.0f) {
226 set_gain (0.000001f + (0.000001f * factor), src);
228 set_gain (desired_gain + (desired_gain * factor), src);
233 Amp::set_gain (gain_t val, void *src)
235 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
236 if (val > 1.99526231f) {
240 //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
242 if (src != _gain_control.get()) {
243 _gain_control->set_value(val);
244 // bit twisty, this will come back and call us again
245 // (this keeps control in sync with reality)
250 // Glib::Mutex::Lock dm (declick_lock);
251 _gain_control->set_float(val, false);
254 if (_session.transport_stopped()) {
259 if (_session.transport_stopped() && src != 0 && src != this && _gain_control->automation_write()) {
260 _gain_control->list()->add (_session.transport_frame(), val);
265 _session.set_dirty();
269 Amp::state (bool full_state)
271 XMLNode& node (Processor::state (full_state));
272 node.add_property("type", "amp");
277 Amp::GainControl::set_value (float val)
279 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
280 if (val > 1.99526231f)
283 _amp->set_gain (val, this);
285 AutomationControl::set_value(val);
289 Amp::GainControl::get_value (void) const
291 return AutomationControl::get_value();
295 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
297 Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
299 if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
300 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
301 start_frame, end_frame, _session.gain_automation_buffer(), nframes);
303 _apply_gain_automation = false;