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/midi_buffer.h"
32 #include "ardour/mute_master.h"
33 #include "ardour/session.h"
37 using namespace ARDOUR;
39 Amp::Amp(Session& s, boost::shared_ptr<MuteMaster> mm)
42 , _apply_gain_automation(false)
46 boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(GainAutomation)));
47 _gain_control = boost::shared_ptr<GainControl>( new GainControl(X_("gaincontrol"), s, this, Evoral::Parameter(GainAutomation), gl ));
48 add_control(_gain_control);
52 Amp::display_name() const
58 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
65 Amp::configure_io (ChanCount in, ChanCount out)
67 if (out != in) { // always 1:1
71 return Processor::configure_io (in, out);
75 Amp::run (BufferSet& bufs, sframes_t /*start_frame*/, sframes_t /*end_frame*/, nframes_t nframes)
79 if (!_active && !_pending_active) {
84 mute_gain = _mute_master->mute_gain_at (MuteMaster::PreFader);
91 if (_apply_gain_automation) {
93 gain_t* gab = _session.gain_automation_buffer ();
95 if (mute_gain == 0.0) {
99 if (_current_gain == 0.0) {
103 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
110 Amp::apply_gain (bufs, nframes, _current_gain, 0.0);
115 } else if (mute_gain != 1.0) {
119 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
120 Sample* const sp = i->data();
121 for (nframes_t nx = 0; nx < nframes; ++nx) {
122 sp[nx] *= gab[nx] * mute_gain;
126 _current_gain = gab[nframes-1] * mute_gain;
132 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
133 Sample* const sp = i->data();
134 for (nframes_t nx = 0; nx < nframes; ++nx) {
139 _current_gain = gab[nframes-1];
143 } else { /* manual (scalar) gain */
145 gain_t dg = _gain_control->user_float() * mute_gain;
147 if (_current_gain != dg) {
149 Amp::apply_gain (bufs, nframes, _current_gain, dg);
152 } else if (_current_gain != 1.0f) {
154 /* gain has not changed, but its non-unity
157 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
158 apply_gain_to_buffer (i->data(), nframes, _current_gain);
164 _active = _pending_active;
168 Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target)
170 /** Apply a (potentially) declicked gain to the audio buffers of @a bufs
173 if (nframes == 0 || bufs.count().n_audio() == 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);
183 const nframes_t declick = std::min ((nframes_t)128, nframes);
185 double fractional_shift = -1.0/declick;
186 double fractional_pos;
187 gain_t polscale = 1.0f;
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 (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
203 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
204 Evoral::MIDIEvent<MidiBuffer::TimeType> ev (*m);
205 if (ev.buffer()[0] == MIDI_CMD_NOTE_ON) {
206 ev.buffer()[2] = (uint8_t) rint (ev.buffer()[2] * 1.0);
214 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
215 Sample* const buffer = i->data();
217 fractional_pos = 1.0;
219 for (nframes_t nx = 0; nx < declick; ++nx) {
220 buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
221 fractional_pos += fractional_shift;
224 /* now ensure the rest of the buffer has the target value applied, if necessary. */
226 if (declick != nframes) {
229 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
230 } else if (target != 1.0) {
231 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
238 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
241 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
242 memset (i->data(), 0, sizeof (Sample) * nframes);
244 } else if (target != 1.0) {
245 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
246 apply_gain_to_buffer (i->data(), nframes, target);
252 Amp::inc_gain (gain_t factor, void *src)
254 float desired_gain = _gain_control->user_float();
256 if (desired_gain == 0.0f) {
257 set_gain (0.000001f + (0.000001f * factor), src);
259 set_gain (desired_gain + (desired_gain * factor), src);
264 Amp::set_gain (gain_t val, void *src)
266 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
267 if (val > 1.99526231f) {
271 //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
273 if (src != _gain_control.get()) {
274 _gain_control->set_value (val);
275 // bit twisty, this will come back and call us again
276 // (this keeps control in sync with reality)
280 _gain_control->set_float(val, false);
281 _session.set_dirty();
285 Amp::state (bool full_state)
287 XMLNode& node (Processor::state (full_state));
288 node.add_property("type", "amp");
291 snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
292 node.add_property("gain", buf);
298 Amp::set_state (const XMLNode& node)
300 const XMLProperty* prop;
302 Processor::set_state (node);
303 prop = node.property ("gain");
308 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
309 _gain_control->set_value (val);
317 Amp::GainControl::set_value (float val)
319 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
320 if (val > 1.99526231f)
323 _amp->set_gain (val, this);
325 AutomationControl::set_value(val);
329 Amp::GainControl::get_value (void) const
331 return AutomationControl::get_value();
335 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
337 Glib::Mutex::Lock am (data().control_lock(), Glib::TRY_LOCK);
339 if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
340 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
341 start_frame, end_frame, _session.gain_automation_buffer(), nframes);
343 _apply_gain_automation = false;