2 Copyright (C) 2013-2017 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/audio_merger.h"
27 #include "lib/audio_buffers.h"
28 #include <boost/test/unit_test.hpp>
29 #include <boost/bind.hpp>
30 #include <boost/function.hpp>
31 #include <boost/signals2.hpp>
37 using boost::shared_ptr;
39 using namespace dcpomatic;
41 static shared_ptr<const AudioBuffers> last_audio;
43 int const sampling_rate = 48000;
46 push (AudioMerger& merger, int from, int to, int at)
48 shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, to - from));
49 for (int i = 0; i < (to - from); ++i) {
50 buffers->data()[0][i] = from + i;
52 merger.push (buffers, DCPTime(at, sampling_rate));
55 /* Basic mixing, 2 overlapping pushes */
56 BOOST_AUTO_TEST_CASE (audio_merger_test1)
58 AudioMerger merger (sampling_rate);
60 push (merger, 0, 64, 0);
61 push (merger, 0, 64, 22);
63 list<pair<shared_ptr<AudioBuffers>, DCPTime> > tb = merger.pull (DCPTime::from_frames (22, sampling_rate));
64 BOOST_REQUIRE (tb.size() == 1);
65 BOOST_CHECK (tb.front().first != shared_ptr<const AudioBuffers> ());
66 BOOST_CHECK_EQUAL (tb.front().first->frames(), 22);
67 BOOST_CHECK_EQUAL (tb.front().second.get(), 0);
69 /* And they should be a staircase */
70 for (int i = 0; i < 22; ++i) {
71 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
74 tb = merger.pull (DCPTime::from_frames (22 + 64, sampling_rate));
75 BOOST_REQUIRE (tb.size() == 1);
76 BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
77 BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(22, sampling_rate).get());
79 /* Check the sample values */
80 for (int i = 0; i < 64; ++i) {
85 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], correct);
89 /* Push at non-zero time */
90 BOOST_AUTO_TEST_CASE (audio_merger_test2)
92 AudioMerger merger (sampling_rate);
94 push (merger, 0, 64, 9);
96 /* There's nothing from 0 to 9 */
97 list<pair<shared_ptr<AudioBuffers>, DCPTime> > tb = merger.pull (DCPTime::from_frames (9, sampling_rate));
98 BOOST_CHECK_EQUAL (tb.size(), 0);
100 /* Then there's our data at 9 */
101 tb = merger.pull (DCPTime::from_frames (9 + 64, sampling_rate));
103 BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
104 BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(9, sampling_rate).get());
106 /* Check the sample values */
107 for (int i = 0; i < 64; ++i) {
108 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
112 /* Push two non contiguous blocks */
113 BOOST_AUTO_TEST_CASE (audio_merger_test3)
115 AudioMerger merger (sampling_rate);
117 push (merger, 0, 64, 17);
118 push (merger, 0, 64, 114);
122 list<pair<shared_ptr<AudioBuffers>, DCPTime> > tb = merger.pull (DCPTime::from_frames (100, sampling_rate));
123 BOOST_REQUIRE (tb.size() == 1);
124 BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
125 BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(17, sampling_rate).get());
126 for (int i = 0; i < 64; ++i) {
127 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
130 tb = merger.pull (DCPTime::from_frames (200, sampling_rate));
131 BOOST_REQUIRE (tb.size() == 1);
132 BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
133 BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(114, sampling_rate).get());
134 for (int i = 0; i < 64; ++i) {
135 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);