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