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.
22 #include "ardour/amp.h"
23 #include "ardour/audio_buffer.h"
24 #include "ardour/buffer_set.h"
25 #include "ardour/configuration.h"
26 #include "ardour/io.h"
27 #include "ardour/session.h"
31 Amp::Amp(Session& s, IO& io)
36 , _apply_gain_automation(false)
43 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
50 Amp::configure_io (ChanCount in, ChanCount out)
52 if (out != in) { // always 1:1
56 return Processor::configure_io (in, out);
60 Amp::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes)
62 gain_t* gab = _session.gain_automation_buffer();
64 if (_mute && !bufs.is_silent()) {
65 Amp::apply_gain (bufs, nframes, _current_mute_gain, _desired_mute_gain, false);
66 if (_desired_mute_gain == 0.0f) {
73 if (_apply_gain_automation) {
75 if (_io.phase_invert()) {
76 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
77 Sample* const sp = i->data();
78 for (nframes_t nx = 0; nx < nframes; ++nx) {
83 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
84 Sample* const sp = i->data();
85 for (nframes_t nx = 0; nx < nframes; ++nx) {
91 } else { /* manual (scalar) gain */
93 if (_current_gain != _desired_gain) {
95 Amp::apply_gain (bufs, nframes, _current_gain, _desired_gain, _io.phase_invert());
96 _current_gain = _desired_gain;
98 } else if (_current_gain != 0.0f && (_io.phase_invert() || _current_gain != 1.0f)) {
100 /* no need to interpolate current gain value,
101 but its non-unity, so apply it. if the gain
102 is zero, do nothing because we'll ship silence
108 if (_io.phase_invert()) {
109 this_gain = -_current_gain;
111 this_gain = _current_gain;
114 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
115 Sample* const sp = i->data();
116 apply_gain_to_buffer(sp, nframes, this_gain);
119 } else if (_current_gain == 0.0f) {
120 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
128 /** Apply a declicked gain to the audio buffers of @a bufs */
130 Amp::apply_gain (BufferSet& bufs, nframes_t nframes,
131 gain_t initial, gain_t target, bool invert_polarity)
137 if (bufs.count().n_audio() == 0) {
141 // if we don't need to declick, defer to apply_simple_gain
142 if (initial == target) {
144 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
145 memset (i->data(), 0, sizeof (Sample) * nframes);
147 } else if (target != 1.0) {
148 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
149 apply_gain_to_buffer (i->data(), nframes, target);
155 const nframes_t declick = std::min ((nframes_t)128, nframes);
157 double fractional_shift = -1.0/declick;
158 double fractional_pos;
159 gain_t polscale = invert_polarity ? -1.0f : 1.0f;
161 if (target < initial) {
162 /* fade out: remove more and more of delta from initial */
163 delta = -(initial - target);
165 /* fade in: add more and more of delta from initial */
166 delta = target - initial;
169 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
170 Sample* const buffer = i->data();
172 fractional_pos = 1.0;
174 for (nframes_t nx = 0; nx < declick; ++nx) {
175 buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos))));
176 fractional_pos += fractional_shift;
179 /* now ensure the rest of the buffer has the target value applied, if necessary. */
181 if (declick != nframes) {
183 if (invert_polarity) {
188 memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick));
189 } else if (target != 1.0) {
190 apply_gain_to_buffer (&buffer[declick], nframes - declick, target);
197 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
202 Amp::state (bool full_state)
210 XMLNode* node = new XMLNode(state_node_name);
211 node->add_property("type", "amp");
215 } // namespace ARDOUR