Merge master.
[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         f._ir.resize (f._M + 1);
59
60         f._ir[0] = 1;
61         for (int i = 1; i <= f._M; ++i) {
62                 f._ir[i] = 0;
63         }
64
65         audio_filter_impulse_test_one (f, 32, 1);
66         audio_filter_impulse_test_one (f, 256, 1);
67         audio_filter_impulse_test_one (f, 2048, 1);
68 }
69
70 /** Create filters and pass them impulses as input and check that
71  *  the filter kernels comes back.
72  */
73 BOOST_AUTO_TEST_CASE (audio_filter_impulse_input_test)
74 {
75         LowPassAudioFilter lpf (0.02, 0.3);
76
77         shared_ptr<AudioBuffers> in (new AudioBuffers (1, 1751));
78         in->make_silent ();
79         in->data(0)[0] = 1;
80         
81         shared_ptr<AudioBuffers> out = lpf.run (in);
82         for (int j = 0; j < out->frames(); ++j) {
83                 if (j <= lpf._M) {
84                         BOOST_CHECK_EQUAL (out->data(0)[j], lpf._ir[j]);
85                 } else {
86                         BOOST_CHECK_EQUAL (out->data(0)[j], 0);
87                 }
88         }
89
90         HighPassAudioFilter hpf (0.02, 0.3);
91
92         in.reset (new AudioBuffers (1, 9133));
93         in->make_silent ();
94         in->data(0)[0] = 1;
95         
96         out = hpf.run (in);
97         for (int j = 0; j < out->frames(); ++j) {
98                 if (j <= hpf._M) {
99                         BOOST_CHECK_EQUAL (out->data(0)[j], hpf._ir[j]);
100                 } else {
101                         BOOST_CHECK_EQUAL (out->data(0)[j], 0);
102                 }
103         }
104 }