Fill test disk partitions with random noise to expose more bugs.
[dcpomatic.git] / test / audio_merger_test.cc
1 /*
2     Copyright (C) 2013-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_merger_test.cc
23  *  @brief Test AudioMerger class.
24  *  @ingroup selfcontained
25  */
26
27
28 #include "lib/cross.h"
29 #include "lib/audio_merger.h"
30 #include "lib/audio_buffers.h"
31 #include "lib/dcpomatic_time.h"
32 #include "test.h"
33 #include <dcp/file.h>
34 #include <dcp/raw_convert.h>
35 #include <boost/test/unit_test.hpp>
36 #include <boost/bind/bind.hpp>
37 #include <boost/signals2.hpp>
38 #include <iostream>
39
40
41 using std::pair;
42 using std::make_shared;
43 using std::list;
44 using std::cout;
45 using std::string;
46 using std::shared_ptr;
47 using boost::bind;
48 using namespace dcpomatic;
49
50
51 static shared_ptr<const AudioBuffers> last_audio;
52
53
54 int const sampling_rate = 48000;
55
56
57 static void
58 push (AudioMerger& merger, int from, int to, int at)
59 {
60         auto buffers = make_shared<AudioBuffers>(1, to - from);
61         for (int i = 0; i < (to - from); ++i) {
62                 buffers->data()[0][i] = from + i;
63         }
64         merger.push (buffers, DCPTime(at, sampling_rate));
65 }
66
67
68 /* Basic mixing, 2 overlapping pushes */
69 BOOST_AUTO_TEST_CASE (audio_merger_test1)
70 {
71         AudioMerger merger (sampling_rate);
72
73         push (merger, 0, 64, 0);
74         push (merger, 0, 64, 22);
75
76         auto tb = merger.pull (DCPTime::from_frames (22, sampling_rate));
77         BOOST_REQUIRE (tb.size() == 1U);
78         BOOST_CHECK (tb.front().first != shared_ptr<const AudioBuffers> ());
79         BOOST_CHECK_EQUAL (tb.front().first->frames(), 22);
80         BOOST_CHECK_EQUAL (tb.front().second.get(), 0);
81
82         /* And they should be a staircase */
83         for (int i = 0; i < 22; ++i) {
84                 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
85         }
86
87         tb = merger.pull (DCPTime::from_frames (22 + 64, sampling_rate));
88         BOOST_REQUIRE (tb.size() == 1U);
89         BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
90         BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(22, sampling_rate).get());
91
92         /* Check the sample values */
93         for (int i = 0; i < 64; ++i) {
94                 int correct = i;
95                 if (i < (64 - 22)) {
96                         correct += i + 22;
97                 }
98                 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], correct);
99         }
100 }
101
102
103 /* Push at non-zero time */
104 BOOST_AUTO_TEST_CASE (audio_merger_test2)
105 {
106         AudioMerger merger (sampling_rate);
107
108         push (merger, 0, 64, 9);
109
110         /* There's nothing from 0 to 9 */
111         auto tb = merger.pull (DCPTime::from_frames (9, sampling_rate));
112         BOOST_CHECK_EQUAL (tb.size(), 0U);
113
114         /* Then there's our data at 9 */
115         tb = merger.pull (DCPTime::from_frames (9 + 64, sampling_rate));
116
117         BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
118         BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(9, sampling_rate).get());
119
120         /* Check the sample values */
121         for (int i = 0; i < 64; ++i) {
122                 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
123         }
124 }
125
126
127 /* Push two non contiguous blocks */
128 BOOST_AUTO_TEST_CASE (audio_merger_test3)
129 {
130         AudioMerger merger (sampling_rate);
131
132         push (merger, 0, 64, 17);
133         push (merger, 0, 64, 114);
134
135         /* Get them back */
136
137         auto tb = merger.pull (DCPTime::from_frames (100, sampling_rate));
138         BOOST_REQUIRE (tb.size() == 1U);
139         BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
140         BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(17, sampling_rate).get());
141         for (int i = 0; i < 64; ++i) {
142                 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
143         }
144
145         tb = merger.pull (DCPTime::from_frames (200, sampling_rate));
146         BOOST_REQUIRE (tb.size() == 1U);
147         BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
148         BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(114, sampling_rate).get());
149         for (int i = 0; i < 64; ++i) {
150                 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
151         }
152 }
153
154
155 /* Reply a sequence of calls to AudioMerger that resulted in a crash */
156 BOOST_AUTO_TEST_CASE (audio_merger_test4)
157 {
158         dcp::File f("test/data/audio_merger_bug1.log", "r");
159         BOOST_REQUIRE (f);
160         list<string> tokens;
161         char buf[64];
162         while (fscanf(f.get(), "%63s", buf) == 1) {
163                 tokens.push_back (buf);
164         }
165
166         shared_ptr<AudioMerger> merger;
167         auto i = tokens.begin ();
168         while (i != tokens.end()) {
169                 BOOST_CHECK (*i++ == "I/AM");
170                 string const cmd = *i++;
171                 if (cmd == "frame_rate") {
172                         BOOST_REQUIRE (i != tokens.end());
173                         merger.reset (new AudioMerger(dcp::raw_convert<int>(*i++)));
174                 } else if (cmd == "clear") {
175                         merger->clear ();
176                 } else if (cmd == "push") {
177                         BOOST_REQUIRE (i != tokens.end());
178                         DCPTime time(dcp::raw_convert<DCPTime::Type>(*i++));
179                         BOOST_REQUIRE (i != tokens.end());
180                         int const frames = dcp::raw_convert<int>(*i++);
181                         auto buffers = make_shared<AudioBuffers>(1, frames);
182                         BOOST_REQUIRE (merger);
183                         merger->push (buffers, time);
184                 } else if (cmd == "pull") {
185                         BOOST_REQUIRE (i != tokens.end());
186                         DCPTime time(dcp::raw_convert<DCPTime::Type>(*i++));
187                         merger->pull (time);
188                 }
189         }
190 }
191