d4fc550f16e7e5ae9aef244fb5b48bf03dd0dfc6
[dcpomatic.git] / test / audio_filter_test.cc
1 /*
2     Copyright (C) 2014-2021 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
22 /** @file  test/audio_filter_test.cc
23  *  @brief Test AudioFilter, LowPassAudioFilter, HighPassAudioFilter classes.
24  *  @ingroup selfcontained
25  */
26
27
28 #include <boost/test/unit_test.hpp>
29 #include "lib/audio_filter.h"
30 #include "lib/audio_buffers.h"
31
32
33 using std::make_shared;
34 using std::shared_ptr;
35
36
37 static void
38 audio_filter_impulse_test_one (AudioFilter& f, int block_size, int num_blocks)
39 {
40         int c = 0;
41
42         for (int i = 0; i < num_blocks; ++i) {
43
44                 auto in = make_shared<AudioBuffers>(1, block_size);
45                 for (int j = 0; j < block_size; ++j) {
46                         in->data()[0][j] = c + j;
47                 }
48
49                 auto out = f.run (in);
50
51                 for (int j = 0; j < out->frames(); ++j) {
52                         BOOST_CHECK_EQUAL (out->data()[0][j], c + j);
53                 }
54
55                 c += block_size;
56         }
57 }
58
59
60 /** Create a filter with an impulse as a kernel and check that it
61  *  passes data through unaltered.
62  */
63 BOOST_AUTO_TEST_CASE (audio_filter_impulse_kernel_test)
64 {
65         AudioFilter f (0.02);
66         delete[] f._ir;
67         f._ir = new float[f._M + 1];
68
69         f._ir[0] = 1;
70         for (int i = 1; i <= f._M; ++i) {
71                 f._ir[i] = 0;
72         }
73
74         audio_filter_impulse_test_one (f, 32, 1);
75         audio_filter_impulse_test_one (f, 256, 1);
76         audio_filter_impulse_test_one (f, 2048, 1);
77 }
78
79
80 /** Create filters and pass them impulses as input and check that
81  *  the filter kernels comes back.
82  */
83 BOOST_AUTO_TEST_CASE (audio_filter_impulse_input_test)
84 {
85         LowPassAudioFilter lpf (0.02, 0.3);
86
87         auto in = make_shared<AudioBuffers>(1, 1751);
88         in->make_silent ();
89         in->data(0)[0] = 1;
90
91         auto out = lpf.run (in);
92         for (int j = 0; j < out->frames(); ++j) {
93                 if (j <= lpf._M) {
94                         BOOST_CHECK_EQUAL (out->data(0)[j], lpf._ir[j]);
95                 } else {
96                         BOOST_CHECK_EQUAL (out->data(0)[j], 0);
97                 }
98         }
99
100         HighPassAudioFilter hpf (0.02, 0.3);
101
102         in = make_shared<AudioBuffers>(1, 9133);
103         in->make_silent ();
104         in->data(0)[0] = 1;
105
106         out = hpf.run (in);
107         for (int j = 0; j < out->frames(); ++j) {
108                 if (j <= hpf._M) {
109                         BOOST_CHECK_EQUAL (out->data(0)[j], hpf._ir[j]);
110                 } else {
111                         BOOST_CHECK_EQUAL (out->data(0)[j], 0);
112                 }
113         }
114 }