78f8e689596272ae8551233a9a945f08d27b7232
[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 <cmath>
25 #include <iostream>
26
27 using std::cerr;
28 using std::cout;
29 using boost::shared_ptr;
30
31 #define CHECK_SAMPLE(c,f,r) \
32         if (fabs(out->data(c)[f] - (r)) > 0.1) {                        \
33                 cerr << "Sample " << f << " at line " << __LINE__ << " is " << out->data(c)[f] << " not " << r << "; difference is " << fabs(out->data(c)[f] - (r)) << "\n"; \
34                 BOOST_REQUIRE (fabs(out->data(c)[f] - (r)) <= 0.1);     \
35         }
36
37 /** Block size greater than delay */
38 BOOST_AUTO_TEST_CASE (audio_processor_delay_test1)
39 {
40         AudioDelay delay (64);
41
42         int const C = 2;
43
44         shared_ptr<AudioBuffers> in (new AudioBuffers (C, 256));
45         for (int i = 0; i < C; ++i) {
46                 for (int j = 0; j < 256; ++j) {
47                         in->data(i)[j] = j;
48                 }
49         }
50
51         shared_ptr<AudioBuffers> out = delay.run (in);
52         BOOST_REQUIRE_EQUAL (out->frames(), in->frames());
53
54         /* Silence at the start */
55         for (int i = 0; i < C; ++i) {
56                 for (int j = 0; j < 64; ++j) {
57                         CHECK_SAMPLE (i, j, 0);
58                 }
59         }
60
61         /* Then the delayed data */
62         for (int i = 0; i < C; ++i) {
63                 for (int j = 64; j < 256; ++j) {
64                         CHECK_SAMPLE (i, j, j - 64);
65                 }
66         }
67
68         /* Feed some more in */
69         for (int i = 0; i < C; ++i) {
70                 for (int j = 0; j < 256; ++j) {
71                         in->data(i)[j] = j + 256;
72                 }
73         }
74         out = delay.run (in);
75
76         /* Check again */
77         for (int i = 0; i < C; ++i) {
78                 for (int j = 256; j < 512; ++j) {
79                         CHECK_SAMPLE (i, j - 256, j - 64);
80                 }
81         }
82 }
83
84 /** Block size less than delay */
85 BOOST_AUTO_TEST_CASE (audio_processor_delay_test2)
86 {
87         AudioDelay delay (256);
88
89         int const C = 2;
90
91         /* Feeding 4 blocks of 64 should give silence each time */
92
93         for (int i = 0; i < 4; ++i) {
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 }