2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "audio_filter.h"
21 #include "audio_buffers.h"
25 using boost::shared_ptr;
27 /** @return array of floats which the caller must destroy with delete[] */
29 AudioFilter::sinc_blackman (float cutoff, bool invert) const
31 float* ir = new float[_M + 1];
33 /* Impulse response */
35 for (int i = 0; i <= _M; ++i) {
37 ir[i] = 2 * M_PI * cutoff;
40 ir[i] = sin (2 * M_PI * cutoff * (i - _M / 2)) / (i - _M / 2);
42 ir[i] *= (0.42 - 0.5 * cos (2 * M_PI * i / _M) + 0.08 * cos (4 * M_PI * i / _M));
49 for (int i = 0; i <= _M; ++i) {
53 for (int i = 0; i <= _M; ++i) {
57 /* Frequency inversion (swapping low-pass for high-pass, or whatever) */
60 for (int i = 0; i <= _M; ++i) {
69 AudioFilter::~AudioFilter ()
74 shared_ptr<AudioBuffers>
75 AudioFilter::run (shared_ptr<const AudioBuffers> in)
77 shared_ptr<AudioBuffers> out (new AudioBuffers (in->channels(), in->frames()));
80 _tail.reset (new AudioBuffers (in->channels(), _M + 1));
81 _tail->make_silent ();
84 int const channels = in->channels ();
85 int const frames = in->frames ();
87 for (int i = 0; i < channels; ++i) {
88 float* tail_p = _tail->data (i);
89 float* in_p = in->data (i);
90 float* out_p = out->data (i);
91 for (int j = 0; j < frames; ++j) {
93 for (int k = 0; k <= _M; ++k) {
95 s += tail_p[j - k + _M + 1] * _ir[k];
97 s += in_p[j - k] * _ir[k];
105 int const amount = min (in->frames(), _tail->frames());
106 if (amount < _tail->frames ()) {
107 _tail->move (amount, 0, _tail->frames() - amount);
109 _tail->copy_from (in.get(), amount, in->frames() - amount, _tail->frames () - amount);
115 AudioFilter::flush ()
120 LowPassAudioFilter::LowPassAudioFilter (float transition_bandwidth, float cutoff)
121 : AudioFilter (transition_bandwidth)
123 _ir = sinc_blackman (cutoff, false);
127 HighPassAudioFilter::HighPassAudioFilter (float transition_bandwidth, float cutoff)
128 : AudioFilter (transition_bandwidth)
130 _ir = sinc_blackman (cutoff, true);
133 BandPassAudioFilter::BandPassAudioFilter (float transition_bandwidth, float lower, float higher)
134 : AudioFilter (transition_bandwidth)
136 float* lpf = sinc_blackman (lower, false);
137 float* hpf = sinc_blackman (higher, true);
140 _ir = new float[_M + 1];
141 for (int i = 0; i <= _M; ++i) {
142 _ir[i] = lpf[i] + hpf[i];
148 /* We now have a band-stop, so invert for band-pass */
149 for (int i = 0; i <= _M; ++i) {