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, bool)
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::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
161 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
162 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
163 if (ev.is_note_on()) {
164 ev.scale_velocity (_current_gain);
169 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
170 apply_gain_to_buffer (i->data(), nframes, _current_gain);
176 _active = _pending_active;
180 Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target)
182 /** Apply a (potentially) declicked gain to the audio buffers of @a bufs
185 if (nframes == 0 || bufs.count().n_audio() == 0) {
189 // if we don't need to declick, defer to apply_simple_gain
190 if (initial == target) {
191 apply_simple_gain (bufs, nframes, target);
195 const nframes_t declick = std::min ((nframes_t)128, nframes);
197 double fractional_shift = -1.0/declick;
198 double fractional_pos;
199 gain_t polscale = 1.0f;
201 if (target < initial) {
202 /* fade out: remove more and more of delta from initial */
203 delta = -(initial - target);
205 /* fade in: add more and more of delta from initial */
206 delta = target - initial;
211 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
216 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
217 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
219 if (ev.is_note_on()) {
220 gain_t scale = delta * (ev.time()/nframes);
221 std::cerr << "scale by " << scale << " for " << ev.time() << " of " << nframes << std::endl;
222 ev.scale_velocity (scale);
229 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
230 Sample* const buffer = i->data();
232 fractional_pos = 1.0;
234 for (nframes_t nx = 0; nx < declick; ++nx) {
235 buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
236 fractional_pos += fractional_shift;
239 /* now ensure the rest of the buffer has the target value applied, if necessary. */
241 if (declick != nframes) {
244 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
245 } else if (target != 1.0) {
246 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
253 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
257 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
260 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
261 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
262 if (ev.is_note_on()) {
268 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
269 memset (i->data(), 0, sizeof (Sample) * nframes);
272 } else if (target != 1.0) {
274 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
277 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
278 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
279 if (ev.is_note_on()) {
280 ev.scale_velocity (target);
285 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
286 apply_gain_to_buffer (i->data(), nframes, target);
292 Amp::inc_gain (gain_t factor, void *src)
294 float desired_gain = _gain_control->user_float();
296 if (desired_gain == 0.0f) {
297 set_gain (0.000001f + (0.000001f * factor), src);
299 set_gain (desired_gain + (desired_gain * factor), src);
304 Amp::set_gain (gain_t val, void *src)
306 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
307 if (val > 1.99526231f) {
311 //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
313 if (src != _gain_control.get()) {
314 _gain_control->set_value (val);
315 // bit twisty, this will come back and call us again
316 // (this keeps control in sync with reality)
320 _gain_control->set_float(val, false);
321 _session.set_dirty();
325 Amp::state (bool full_state)
327 XMLNode& node (Processor::state (full_state));
328 node.add_property("type", "amp");
331 snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
332 node.add_property("gain", buf);
338 Amp::set_state (const XMLNode& node, int version)
340 const XMLProperty* prop;
342 Processor::set_state (node, version);
343 prop = node.property ("gain");
348 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
349 _gain_control->set_value (val);
357 Amp::GainControl::set_value (float val)
359 // max gain at about +6dB (10.0 ^ ( 6 dB * 0.05))
360 if (val > 1.99526231f)
363 _amp->set_gain (val, this);
365 AutomationControl::set_value(val);
369 Amp::GainControl::get_value (void) const
371 return AutomationControl::get_value();
375 Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
377 Glib::Mutex::Lock am (control_lock(), Glib::TRY_LOCK);
379 if (am.locked() && _session.transport_rolling() && _gain_control->automation_playback()) {
380 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
381 start_frame, end_frame, _session.gain_automation_buffer(), nframes);
383 _apply_gain_automation = false;