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