2 Copyright (C) 2013-2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file test/audio_merger_test.cc
22 * @brief Test AudioMerger class.
23 * @ingroup selfcontained
26 #include "lib/cross.h"
27 #include "lib/audio_merger.h"
28 #include "lib/audio_buffers.h"
29 #include "lib/dcpomatic_time.h"
31 #include <dcp/raw_convert.h>
32 #include <boost/test/unit_test.hpp>
33 #include <boost/bind.hpp>
34 #include <boost/function.hpp>
35 #include <boost/signals2.hpp>
42 using boost::shared_ptr;
45 static shared_ptr<const AudioBuffers> last_audio;
47 int const sampling_rate = 48000;
50 push (AudioMerger& merger, int from, int to, int at)
52 shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, to - from));
53 for (int i = 0; i < (to - from); ++i) {
54 buffers->data()[0][i] = from + i;
56 merger.push (buffers, DCPTime(at, sampling_rate));
59 /* Basic mixing, 2 overlapping pushes */
60 BOOST_AUTO_TEST_CASE (audio_merger_test1)
62 AudioMerger merger (sampling_rate);
64 push (merger, 0, 64, 0);
65 push (merger, 0, 64, 22);
67 list<pair<shared_ptr<AudioBuffers>, DCPTime> > tb = merger.pull (DCPTime::from_frames (22, sampling_rate));
68 BOOST_REQUIRE (tb.size() == 1);
69 BOOST_CHECK (tb.front().first != shared_ptr<const AudioBuffers> ());
70 BOOST_CHECK_EQUAL (tb.front().first->frames(), 22);
71 BOOST_CHECK_EQUAL (tb.front().second.get(), 0);
73 /* And they should be a staircase */
74 for (int i = 0; i < 22; ++i) {
75 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
78 tb = merger.pull (DCPTime::from_frames (22 + 64, sampling_rate));
79 BOOST_REQUIRE (tb.size() == 1);
80 BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
81 BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(22, sampling_rate).get());
83 /* Check the sample values */
84 for (int i = 0; i < 64; ++i) {
89 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], correct);
93 /* Push at non-zero time */
94 BOOST_AUTO_TEST_CASE (audio_merger_test2)
96 AudioMerger merger (sampling_rate);
98 push (merger, 0, 64, 9);
100 /* There's nothing from 0 to 9 */
101 list<pair<shared_ptr<AudioBuffers>, DCPTime> > tb = merger.pull (DCPTime::from_frames (9, sampling_rate));
102 BOOST_CHECK_EQUAL (tb.size(), 0);
104 /* Then there's our data at 9 */
105 tb = merger.pull (DCPTime::from_frames (9 + 64, sampling_rate));
107 BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
108 BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(9, sampling_rate).get());
110 /* Check the sample values */
111 for (int i = 0; i < 64; ++i) {
112 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
116 /* Push two non contiguous blocks */
117 BOOST_AUTO_TEST_CASE (audio_merger_test3)
119 AudioMerger merger (sampling_rate);
121 push (merger, 0, 64, 17);
122 push (merger, 0, 64, 114);
126 list<pair<shared_ptr<AudioBuffers>, DCPTime> > tb = merger.pull (DCPTime::from_frames (100, sampling_rate));
127 BOOST_REQUIRE (tb.size() == 1);
128 BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
129 BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(17, sampling_rate).get());
130 for (int i = 0; i < 64; ++i) {
131 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
134 tb = merger.pull (DCPTime::from_frames (200, sampling_rate));
135 BOOST_REQUIRE (tb.size() == 1);
136 BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
137 BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(114, sampling_rate).get());
138 for (int i = 0; i < 64; ++i) {
139 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
143 /* Reply a sequence of calls to AudioMerger that resulted in a crash */
144 BOOST_AUTO_TEST_CASE (audio_merger_test4)
146 FILE* f = fopen_boost("test/data/audio_merger_bug1.log", "r");
150 while (fscanf(f, "%63s", buf) == 1) {
151 tokens.push_back (buf);
154 shared_ptr<AudioMerger> merger;
155 list<string>::const_iterator i = tokens.begin ();
156 while (i != tokens.end()) {
157 BOOST_CHECK (*i++ == "I/AM");
158 string const cmd = *i++;
159 if (cmd == "frame_rate") {
160 BOOST_REQUIRE (i != tokens.end());
161 merger.reset (new AudioMerger(dcp::raw_convert<int>(*i++)));
162 } else if (cmd == "clear") {
164 } else if (cmd == "push") {
165 BOOST_REQUIRE (i != tokens.end());
166 DCPTime time(dcp::raw_convert<DCPTime::Type>(*i++));
167 BOOST_REQUIRE (i != tokens.end());
168 int const frames = dcp::raw_convert<int>(*i++);
169 shared_ptr<AudioBuffers> buffers(new AudioBuffers(1, frames));
170 BOOST_REQUIRE (merger);
171 merger->push (buffers, time);
172 } else if (cmd == "pull") {
173 BOOST_REQUIRE (i != tokens.end());
174 DCPTime time(dcp::raw_convert<DCPTime::Type>(*i++));