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/midi_buffer.h"
30 #include "ardour/session.h"
34 using namespace ARDOUR;
38 /* gain range of -inf to +6dB, default 0dB */
39 const float Amp::max_gain_coefficient = 1.99526231f;
44 , _apply_gain_automation(false)
46 , _gain_automation_buffer(0)
48 Evoral::Parameter p (GainAutomation);
49 boost::shared_ptr<AutomationList> gl (new AutomationList (p));
50 _gain_control = boost::shared_ptr<GainControl> (new GainControl (X_("gaincontrol"), s, this, p, gl));
51 _gain_control->set_flags (Controllable::GainLike);
53 add_control(_gain_control);
57 Amp::display_name() const
63 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out)
70 Amp::configure_io (ChanCount in, ChanCount out)
72 if (out != in) { // always 1:1
76 return Processor::configure_io (in, out);
80 Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
82 if (!_active && !_pending_active) {
88 if (_apply_gain_automation) {
90 gain_t* gab = _gain_automation_buffer;
93 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
94 Sample* const sp = i->data();
95 for (pframes_t nx = 0; nx < nframes; ++nx) {
100 _current_gain = gab[nframes-1];
102 } else { /* manual (scalar) gain */
104 gain_t const dg = _gain_control->user_double();
106 if (_current_gain != dg) {
108 Amp::apply_gain (bufs, nframes, _current_gain, dg);
111 } else if (_current_gain != 1.0f) {
113 /* gain has not changed, but its non-unity
116 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
120 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
121 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
122 if (ev.is_note_on()) {
123 ev.scale_velocity (_current_gain);
128 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
129 apply_gain_to_buffer (i->data(), nframes, _current_gain);
135 _active = _pending_active;
139 Amp::apply_gain (BufferSet& bufs, framecnt_t nframes, gain_t initial, gain_t target)
141 /** Apply a (potentially) declicked gain to the buffers of @a bufs
144 if (nframes == 0 || bufs.count().n_total() == 0) {
148 // if we don't need to declick, defer to apply_simple_gain
149 if (initial == target) {
150 apply_simple_gain (bufs, nframes, target);
154 const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
156 double fractional_shift = -1.0/declick;
157 double fractional_pos;
159 if (target < initial) {
160 /* fade out: remove more and more of delta from initial */
161 delta = -(initial - target);
163 /* fade in: add more and more of delta from initial */
164 delta = target - initial;
169 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
173 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
174 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
176 if (ev.is_note_on()) {
177 const gain_t scale = delta * (ev.time()/(double) nframes);
178 ev.scale_velocity (initial+scale);
185 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
186 Sample* const buffer = i->data();
188 fractional_pos = 1.0;
190 for (pframes_t nx = 0; nx < declick; ++nx) {
191 buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
192 fractional_pos += fractional_shift;
195 /* now ensure the rest of the buffer has the target value applied, if necessary. */
197 if (declick != nframes) {
200 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
201 } else if (target != 1.0) {
202 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
209 Amp::declick (BufferSet& bufs, framecnt_t nframes, int dir)
211 /* Almost exactly like ::apply_gain() but skips MIDI buffers and has fixed initial+target
215 if (nframes == 0 || bufs.count().n_total() == 0) {
219 const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
220 gain_t delta, initial, target;
221 double fractional_shift = -1.0/(declick-1);
222 double fractional_pos;
225 /* fade out: remove more and more of delta from initial */
230 /* fade in: add more and more of delta from initial */
238 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
239 Sample* const buffer = i->data();
241 fractional_pos = 1.0;
243 for (pframes_t nx = 0; nx < declick; ++nx) {
244 buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
245 fractional_pos += fractional_shift;
248 /* now ensure the rest of the buffer has the target value applied, if necessary. */
250 if (declick != nframes) {
253 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
254 } else if (target != 1.0) {
255 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
262 Amp::apply_gain (AudioBuffer& buf, framecnt_t nframes, gain_t initial, gain_t target)
264 /** Apply a (potentially) declicked gain to the contents of @a buf
271 // if we don't need to declick, defer to apply_simple_gain
272 if (initial == target) {
273 apply_simple_gain (buf, nframes, target);
277 const framecnt_t declick = std::min ((framecnt_t) 128, nframes);
279 double fractional_shift = -1.0/declick;
280 double fractional_pos;
282 if (target < initial) {
283 /* fade out: remove more and more of delta from initial */
284 delta = -(initial - target);
286 /* fade in: add more and more of delta from initial */
287 delta = target - initial;
291 Sample* const buffer = buf.data();
293 fractional_pos = 1.0;
295 for (pframes_t nx = 0; nx < declick; ++nx) {
296 buffer[nx] *= (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
297 fractional_pos += fractional_shift;
300 /* now ensure the rest of the buffer has the target value applied, if necessary. */
302 if (declick != nframes) {
305 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
306 } else if (target != 1.0) {
307 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
313 Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target)
317 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
320 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
321 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
322 if (ev.is_note_on()) {
328 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
329 memset (i->data(), 0, sizeof (Sample) * nframes);
332 } else if (target != 1.0) {
334 for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
337 for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
338 Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
339 if (ev.is_note_on()) {
340 ev.scale_velocity (target);
345 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
346 apply_gain_to_buffer (i->data(), nframes, target);
352 Amp::apply_simple_gain (AudioBuffer& buf, framecnt_t nframes, gain_t target)
355 memset (buf.data(), 0, sizeof (Sample) * nframes);
356 } else if (target != 1.0) {
357 apply_gain_to_buffer (buf.data(), nframes, target);
362 Amp::inc_gain (gain_t factor, void *src)
364 float desired_gain = _gain_control->user_double();
366 if (desired_gain == 0.0f) {
367 set_gain (0.000001f + (0.000001f * factor), src);
369 set_gain (desired_gain + (desired_gain * factor), src);
374 Amp::set_gain (gain_t val, void *)
376 _gain_control->set_value (val);
380 Amp::state (bool full_state)
382 XMLNode& node (Processor::state (full_state));
383 node.add_property("type", "amp");
384 node.add_child_nocopy (_gain_control->get_state());
390 Amp::set_state (const XMLNode& node, int version)
394 Processor::set_state (node, version);
396 if ((gain_node = node.child (Controllable::xml_node_name.c_str())) != 0) {
397 _gain_control->set_state (*gain_node, version);
404 Amp::GainControl::set_value (double val)
406 AutomationControl::set_value (min (val, (double) max_gain_coefficient));
407 _amp->session().set_dirty ();
411 Amp::GainControl::internal_to_interface (double v) const
413 return gain_to_slider_position (v);
417 Amp::GainControl::interface_to_internal (double v) const
419 return slider_position_to_gain (v);
423 Amp::GainControl::internal_to_user (double v) const
425 return accurate_coefficient_to_dB (v);
429 Amp::GainControl::user_to_internal (double u) const
431 return dB_to_coefficient (u);
435 Amp::GainControl::get_user_string () const
437 char theBuf[32]; sprintf( theBuf, "%3.1f dB", accurate_coefficient_to_dB (get_value()));
438 return std::string(theBuf);
441 /** Write gain automation for this cycle into the buffer previously passed in to
442 * set_gain_automation_buffer (if we are in automation playback mode and the
443 * transport is rolling).
446 Amp::setup_gain_automation (framepos_t start_frame, framepos_t end_frame, framecnt_t nframes)
448 Glib::Threads::Mutex::Lock am (control_lock(), Glib::Threads::TRY_LOCK);
451 && (_session.transport_rolling() || _session.bounce_processing())
452 && _gain_control->automation_playback())
454 assert (_gain_automation_buffer);
455 _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
456 start_frame, end_frame, _gain_automation_buffer, nframes);
458 _apply_gain_automation = false;
469 Amp::value_as_string (boost::shared_ptr<AutomationControl> ac) const
471 if (ac == _gain_control) {
473 snprintf (buffer, sizeof (buffer), "%.2fdB", ac->internal_to_user (ac->get_value ()));
477 return Automatable::value_as_string (ac);
480 /** Sets up the buffer that setup_gain_automation and ::run will use for
481 * gain automationc curves. Must be called before setup_gain_automation,
482 * and must be called with process lock held.
486 Amp::set_gain_automation_buffer (gain_t* g)
488 _gain_automation_buffer = g;