Missed update to private test repo version.
[dcpomatic.git] / src / lib / audio_filter.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "audio_filter.h"
22 #include "audio_buffers.h"
23 #include "util.h"
24 #include <cmath>
25
26 using std::min;
27 using boost::shared_ptr;
28
29 /** @return array of floats which the caller must destroy with delete[] */
30 float *
31 AudioFilter::sinc_blackman (float cutoff, bool invert) const
32 {
33         float* ir = new float[_M + 1];
34
35         /* Impulse response */
36
37         for (int i = 0; i <= _M; ++i) {
38                 if (i == (_M / 2)) {
39                         ir[i] = 2 * M_PI * cutoff;
40                 } else {
41                         /* sinc */
42                         ir[i] = sin (2 * M_PI * cutoff * (i - _M / 2)) / (i - _M / 2);
43                         /* Blackman window */
44                         ir[i] *= (0.42 - 0.5 * cos (2 * M_PI * i / _M) + 0.08 * cos (4 * M_PI * i / _M));
45                 }
46         }
47
48         /* Normalise */
49
50         float sum = 0;
51         for (int i = 0; i <= _M; ++i) {
52                 sum += ir[i];
53         }
54
55         for (int i = 0; i <= _M; ++i) {
56                 ir[i] /= sum;
57         }
58
59         /* Frequency inversion (swapping low-pass for high-pass, or whatever) */
60
61         if (invert) {
62                 for (int i = 0; i <= _M; ++i) {
63                         ir[i] = -ir[i];
64                 }
65                 ir[_M / 2] += 1;
66         }
67
68         return ir;
69 }
70
71 AudioFilter::~AudioFilter ()
72 {
73         delete[] _ir;
74 }
75
76 shared_ptr<AudioBuffers>
77 AudioFilter::run (shared_ptr<const AudioBuffers> in)
78 {
79         shared_ptr<AudioBuffers> out (new AudioBuffers (in->channels(), in->frames()));
80
81         if (!_tail) {
82                 _tail.reset (new AudioBuffers (in->channels(), _M + 1));
83                 _tail->make_silent ();
84         }
85
86         int const channels = in->channels ();
87         int const frames = in->frames ();
88
89         for (int i = 0; i < channels; ++i) {
90                 float* tail_p = _tail->data (i);
91                 float* in_p = in->data (i);
92                 float* out_p = out->data (i);
93                 for (int j = 0; j < frames; ++j) {
94                         float s = 0;
95                         for (int k = 0; k <= _M; ++k) {
96                                 if ((j - k) < 0) {
97                                         s += tail_p[j - k + _M + 1] * _ir[k];
98                                 } else {
99                                         s += in_p[j - k] * _ir[k];
100                                 }
101                         }
102
103                         out_p[j] = s;
104                 }
105         }
106
107         int const amount = min (in->frames(), _tail->frames());
108         if (amount < _tail->frames ()) {
109                 _tail->move (_tail->frames() - amount, amount, 0);
110         }
111         _tail->copy_from (in.get(), amount, in->frames() - amount, _tail->frames () - amount);
112
113         return out;
114 }
115
116 void
117 AudioFilter::flush ()
118 {
119         _tail.reset ();
120 }
121
122 LowPassAudioFilter::LowPassAudioFilter (float transition_bandwidth, float cutoff)
123         : AudioFilter (transition_bandwidth)
124 {
125         _ir = sinc_blackman (cutoff, false);
126 }
127
128
129 HighPassAudioFilter::HighPassAudioFilter (float transition_bandwidth, float cutoff)
130         : AudioFilter (transition_bandwidth)
131 {
132         _ir = sinc_blackman (cutoff, true);
133 }
134
135 BandPassAudioFilter::BandPassAudioFilter (float transition_bandwidth, float lower, float higher)
136         : AudioFilter (transition_bandwidth)
137 {
138         float* lpf = sinc_blackman (lower, false);
139         float* hpf = sinc_blackman (higher, true);
140
141         delete[] _ir;
142         _ir = new float[_M + 1];
143         for (int i = 0; i <= _M; ++i) {
144                 _ir[i] = lpf[i] + hpf[i];
145         }
146
147         delete[] lpf;
148         delete[] hpf;
149
150         /* We now have a band-stop, so invert for band-pass */
151         for (int i = 0; i <= _M; ++i) {
152                 _ir[i] = -_ir[i];
153         }
154
155         _ir[_M / 2] += 1;
156 }