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