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