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