diff options
Diffstat (limited to 'libs/ardour/amp.cc')
-rw-r--r-- | libs/ardour/amp.cc | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/libs/ardour/amp.cc b/libs/ardour/amp.cc new file mode 100644 index 0000000000..cfa20afe3b --- /dev/null +++ b/libs/ardour/amp.cc @@ -0,0 +1,98 @@ +/* + Copyright (C) 2006 Paul Davis + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include <ardour/amp.h> + +#include <algorithm> +#include <cmath> +#include <ardour/buffer_set.h> +#include <ardour/buffer.h> + +namespace ARDOUR { + + +/** Apply a declicked gain to the audio buffers of @a bufs */ +void +Amp::run (BufferSet& bufs, jack_nframes_t nframes, gain_t initial, gain_t target, bool invert_polarity) +{ + if (bufs.count().get(DataType::AUDIO) == 0) + return; + + assert(bufs.buffer_capacity(DataType::AUDIO) >= nframes); + + // if we don't need to declick, defer to apply_simple_gain + if (initial == target) { + apply_simple_gain(bufs, nframes, invert_polarity ? target : -target); + } + + const jack_nframes_t declick = std::min ((jack_nframes_t)128, nframes); + gain_t delta; + double fractional_shift = -1.0/declick; + double fractional_pos; + gain_t polscale = invert_polarity ? -1.0f : 1.0f; + + if (nframes == 0) + return; + + if (target < initial) { + /* fade out: remove more and more of delta from initial */ + delta = -(initial - target); + } else { + /* fade in: add more and more of delta from initial */ + delta = target - initial; + } + + for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) { + Sample* const buffer = i->data(nframes); + + fractional_pos = 1.0; + + for (jack_nframes_t nx = 0; nx < declick; ++nx) { + buffer[nx] *= polscale * (initial + (delta * (0.5 + 0.5 * cos (M_PI * fractional_pos)))); + fractional_pos += fractional_shift; + } + + /* now ensure the rest of the buffer has the target value applied, if necessary. */ + + if (declick != nframes) { + + if (invert_polarity) { + target = -target; + } + + if (target == 0.0) { + memset (&buffer[declick], 0, sizeof (Sample) * (nframes - declick)); + } else if (target != 1.0) { + for (jack_nframes_t nx = declick; nx < nframes; ++nx) { + buffer[nx] *= target; + } + } + } + } +} + +void +Amp::apply_simple_gain (BufferSet& bufs, jack_nframes_t nframes, gain_t target) +{ + for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) { + i->apply_gain(target, nframes); + } +} + + +} // namespace ARDOUR |