Optimise audio filters; tweak order of the LPFs in the upmixers.
[dcpomatic.git] / test / audio_filter_test.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  test/audio_filter_test.cc
21  *  @brief Basic tests of audio filters.
22  */
23
24 #include <boost/test/unit_test.hpp>
25 #include "lib/audio_filter.h"
26 #include "lib/audio_buffers.h"
27
28 using boost::shared_ptr;
29
30 static void
31 audio_filter_impulse_test_one (AudioFilter& f, int block_size, int num_blocks)
32 {
33         int c = 0;
34
35         for (int i = 0; i < num_blocks; ++i) {
36
37                 shared_ptr<AudioBuffers> in (new AudioBuffers (1, block_size));
38                 for (int j = 0; j < block_size; ++j) {
39                         in->data()[0][j] = c + j;
40                 }
41
42                 shared_ptr<AudioBuffers> out = f.run (in);
43
44                 for (int j = 0; j < out->frames(); ++j) {
45                         BOOST_CHECK_EQUAL (out->data()[0][j], c + j);
46                 }
47
48                 c += block_size;
49         }
50 }
51
52 /** Create a filter with an impulse as a kernel and check that it
53  *  passes data through unaltered.
54  */
55 BOOST_AUTO_TEST_CASE (audio_filter_impulse_kernel_test)
56 {
57         AudioFilter f (0.02);
58         delete[] f._ir;
59         f._ir = new float[f._M + 1];
60
61         f._ir[0] = 1;
62         for (int i = 1; i <= f._M; ++i) {
63                 f._ir[i] = 0;
64         }
65
66         audio_filter_impulse_test_one (f, 32, 1);
67         audio_filter_impulse_test_one (f, 256, 1);
68         audio_filter_impulse_test_one (f, 2048, 1);
69 }
70
71 /** Create filters and pass them impulses as input and check that
72  *  the filter kernels comes back.
73  */
74 BOOST_AUTO_TEST_CASE (audio_filter_impulse_input_test)
75 {
76         LowPassAudioFilter lpf (0.02, 0.3);
77
78         shared_ptr<AudioBuffers> in (new AudioBuffers (1, 1751));
79         in->make_silent ();
80         in->data(0)[0] = 1;
81
82         shared_ptr<AudioBuffers> out = lpf.run (in);
83         for (int j = 0; j < out->frames(); ++j) {
84                 if (j <= lpf._M) {
85                         BOOST_CHECK_EQUAL (out->data(0)[j], lpf._ir[j]);
86                 } else {
87                         BOOST_CHECK_EQUAL (out->data(0)[j], 0);
88                 }
89         }
90
91         HighPassAudioFilter hpf (0.02, 0.3);
92
93         in.reset (new AudioBuffers (1, 9133));
94         in->make_silent ();
95         in->data(0)[0] = 1;
96
97         out = hpf.run (in);
98         for (int j = 0; j < out->frames(); ++j) {
99                 if (j <= hpf._M) {
100                         BOOST_CHECK_EQUAL (out->data(0)[j], hpf._ir[j]);
101                 } else {
102                         BOOST_CHECK_EQUAL (out->data(0)[j], 0);
103                 }
104         }
105 }