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.
19 #include <ardour/amp.h>
23 #include <ardour/buffer_set.h>
24 #include <ardour/audio_buffer.h>
29 /** Apply a declicked gain to the audio buffers of @a bufs */
31 Amp::run_in_place (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t target, bool invert_polarity)
36 if (bufs.count().n_audio() == 0)
39 // assert(bufs.buffer_capacity(DataType::AUDIO) >= nframes);
41 // if we don't need to declick, defer to apply_simple_gain
42 if (initial == target) {
44 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
45 memset (i->data(), 0, sizeof (Sample) * nframes);
47 } else if (target != 1.0) {
48 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
49 apply_gain_to_buffer (i->data(), nframes, target);
55 const nframes_t declick = std::min ((nframes_t)128, nframes);
57 double fractional_shift = -1.0/declick;
58 double fractional_pos;
59 gain_t polscale = invert_polarity ? -1.0f : 1.0f;
61 if (target < initial) {
62 /* fade out: remove more and more of delta from initial */
63 delta = -(initial - target);
65 /* fade in: add more and more of delta from initial */
66 delta = target - initial;
69 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
70 Sample* const buffer = i->data();
74 for (nframes_t nx = 0; nx < declick; ++nx) {
75 buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
76 fractional_pos += fractional_shift;
79 /* now ensure the rest of the buffer has the target value applied, if necessary. */
81 if (declick != nframes) {
83 if (invert_polarity) {
88 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
89 } else if (target != 1.0) {
90 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
97 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
102 } // namespace ARDOUR