2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "audio_filter.h"
22 #include "audio_buffers.h"
26 using boost::shared_ptr;
28 /** @return array of floats which the caller must destroy with delete[] */
30 AudioFilter::sinc_blackman (float cutoff, bool invert) const
32 float* ir = new float[_M + 1];
34 /* Impulse response */
36 for (int i = 0; i <= _M; ++i) {
38 ir[i] = 2 * M_PI * cutoff;
41 ir[i] = sin (2 * M_PI * cutoff * (i - _M / 2)) / (i - _M / 2);
43 ir[i] *= (0.42 - 0.5 * cos (2 * M_PI * i / _M) + 0.08 * cos (4 * M_PI * i / _M));
50 for (int i = 0; i <= _M; ++i) {
54 for (int i = 0; i <= _M; ++i) {
58 /* Frequency inversion (swapping low-pass for high-pass, or whatever) */
61 for (int i = 0; i <= _M; ++i) {
70 AudioFilter::~AudioFilter ()
75 shared_ptr<AudioBuffers>
76 AudioFilter::run (shared_ptr<const AudioBuffers> in)
78 shared_ptr<AudioBuffers> out (new AudioBuffers (in->channels(), in->frames()));
81 _tail.reset (new AudioBuffers (in->channels(), _M + 1));
82 _tail->make_silent ();
85 int const channels = in->channels ();
86 int const frames = in->frames ();
88 for (int i = 0; i < channels; ++i) {
89 float* tail_p = _tail->data (i);
90 float* in_p = in->data (i);
91 float* out_p = out->data (i);
92 for (int j = 0; j < frames; ++j) {
94 for (int k = 0; k <= _M; ++k) {
96 s += tail_p[j - k + _M + 1] * _ir[k];
98 s += in_p[j - k] * _ir[k];
106 int const amount = min (in->frames(), _tail->frames());
107 if (amount < _tail->frames ()) {
108 _tail->move (amount, 0, _tail->frames() - amount);
110 _tail->copy_from (in.get(), amount, in->frames() - amount, _tail->frames () - amount);
116 AudioFilter::flush ()
121 LowPassAudioFilter::LowPassAudioFilter (float transition_bandwidth, float cutoff)
122 : AudioFilter (transition_bandwidth)
124 _ir = sinc_blackman (cutoff, false);
128 HighPassAudioFilter::HighPassAudioFilter (float transition_bandwidth, float cutoff)
129 : AudioFilter (transition_bandwidth)
131 _ir = sinc_blackman (cutoff, true);
134 BandPassAudioFilter::BandPassAudioFilter (float transition_bandwidth, float lower, float higher)
135 : AudioFilter (transition_bandwidth)
137 float* lpf = sinc_blackman (lower, false);
138 float* hpf = sinc_blackman (higher, true);
141 _ir = new float[_M + 1];
142 for (int i = 0; i <= _M; ++i) {
143 _ir[i] = lpf[i] + hpf[i];
149 /* We now have a band-stop, so invert for band-pass */
150 for (int i = 0; i <= _M; ++i) {