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