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