Missing include.
[dcpomatic.git] / test / audio_processor_delay_test.cc
1 /*
2     Copyright (C) 2015 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 #include "lib/audio_delay.h"
21 #include "lib/audio_buffers.h"
22 #include <boost/test/unit_test.hpp>
23 #include <cmath>
24 #include <iostream>
25
26 using std::cerr;
27 using std::cout;
28 using boost::shared_ptr;
29
30 #define CHECK_SAMPLE(c,f,r) \
31         if (fabs(out->data(c)[f] - (r)) > 0.1) {                        \
32                 cerr << "Sample " << f << " at line " << __LINE__ << " is " << out->data(c)[f] << " not " << r << "; difference is " << fabs(out->data(c)[f] - (r)) << "\n"; \
33                 BOOST_REQUIRE (fabs(out->data(c)[f] - (r)) <= 0.1);     \
34         }
35
36 /** Block size greater than delay */
37 BOOST_AUTO_TEST_CASE (audio_processor_delay_test1)
38 {
39         AudioDelay delay (64);
40
41         int const C = 2;
42
43         shared_ptr<AudioBuffers> in (new AudioBuffers (C, 256));
44         for (int i = 0; i < C; ++i) {
45                 for (int j = 0; j < 256; ++j) {
46                         in->data(i)[j] = j;
47                 }
48         }
49
50         shared_ptr<AudioBuffers> out = delay.run (in);
51         BOOST_REQUIRE_EQUAL (out->frames(), in->frames());
52
53         /* Silence at the start */
54         for (int i = 0; i < C; ++i) {
55                 for (int j = 0; j < 64; ++j) {
56                         CHECK_SAMPLE (i, j, 0);
57                 }
58         }
59
60         /* Then the delayed data */
61         for (int i = 0; i < C; ++i) {
62                 for (int j = 64; j < 256; ++j) {
63                         CHECK_SAMPLE (i, j, j - 64);
64                 }
65         }
66
67         /* Feed some more in */
68         for (int i = 0; i < C; ++i) {
69                 for (int j = 0; j < 256; ++j) {
70                         in->data(i)[j] = j + 256;
71                 }
72         }
73         out = delay.run (in);
74
75         /* Check again */
76         for (int i = 0; i < C; ++i) {
77                 for (int j = 256; j < 512; ++j) {
78                         CHECK_SAMPLE (i, j - 256, j - 64);
79                 }
80         }
81 }
82
83 /** Block size less than delay */
84 BOOST_AUTO_TEST_CASE (audio_processor_delay_test2)
85 {
86         AudioDelay delay (256);
87
88         int const C = 2;
89
90         /* Feeding 4 blocks of 64 should give silence each time */
91
92         for (int i = 0; i < 4; ++i) {
93                 cout << "i=" << i << "\n";
94                 shared_ptr<AudioBuffers> in (new AudioBuffers (C, 64));
95                 for (int j = 0; j < C; ++j) {
96                         for (int k = 0; k < 64; ++k) {
97                                 in->data(j)[k] = k + i * 64;
98                         }
99                 }
100
101                 shared_ptr<AudioBuffers> out = delay.run (in);
102                 BOOST_REQUIRE_EQUAL (out->frames(), in->frames());
103
104                 /* Check for silence */
105                 for (int j = 0; j < C; ++j) {
106                         for (int k = 0; k < 64; ++k) {
107                                 CHECK_SAMPLE (j, k, 0);
108                         }
109                 }
110         }
111
112         /* Now feed 4 blocks of silence and we should see the data */
113         for (int i = 0; i < 4; ++i) {
114                 /* Feed some silence */
115                 shared_ptr<AudioBuffers> in (new AudioBuffers (C, 64));
116                 in->make_silent ();
117                 shared_ptr<AudioBuffers> out = delay.run (in);
118
119                 /* Should now see the delayed data */
120                 for (int j = 0; j < C; ++j) {
121                         for (int k = 0; k < 64; ++k) {
122                                 CHECK_SAMPLE (j, k, k + i * 64);
123                         }
124                 }
125         }
126 }