summaryrefslogtreecommitdiff
path: root/test/audio_merger_test.cc
blob: 3a677bf820d034323761e322dc6e741481bbe5b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
    Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


/** @file  test/audio_merger_test.cc
 *  @brief Test AudioMerger class.
 *  @ingroup selfcontained
 */


#include "lib/cross.h"
#include "lib/audio_merger.h"
#include "lib/audio_buffers.h"
#include "lib/dcpomatic_time.h"
#include "test.h"
#include <dcp/raw_convert.h>
#include <boost/test/unit_test.hpp>
#include <boost/bind/bind.hpp>
#include <boost/signals2.hpp>
#include <iostream>


using std::pair;
using std::make_shared;
using std::list;
using std::cout;
using std::string;
using std::shared_ptr;
using boost::bind;
using namespace dcpomatic;


static shared_ptr<const AudioBuffers> last_audio;


int const sampling_rate = 48000;


static void
push (AudioMerger& merger, int from, int to, int at)
{
	auto buffers = make_shared<AudioBuffers>(1, to - from);
	for (int i = 0; i < (to - from); ++i) {
		buffers->data()[0][i] = from + i;
	}
	merger.push (buffers, DCPTime(at, sampling_rate));
}


/* Basic mixing, 2 overlapping pushes */
BOOST_AUTO_TEST_CASE (audio_merger_test1)
{
	AudioMerger merger (sampling_rate);

	push (merger, 0, 64, 0);
	push (merger, 0, 64, 22);

	auto tb = merger.pull (DCPTime::from_frames (22, sampling_rate));
	BOOST_REQUIRE (tb.size() == 1U);
	BOOST_CHECK (tb.front().first != shared_ptr<const AudioBuffers> ());
	BOOST_CHECK_EQUAL (tb.front().first->frames(), 22);
	BOOST_CHECK_EQUAL (tb.front().second.get(), 0);

	/* And they should be a staircase */
	for (int i = 0; i < 22; ++i) {
		BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
	}

	tb = merger.pull (DCPTime::from_frames (22 + 64, sampling_rate));
	BOOST_REQUIRE (tb.size() == 1U);
	BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
	BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(22, sampling_rate).get());

	/* Check the sample values */
	for (int i = 0; i < 64; ++i) {
		int correct = i;
		if (i < (64 - 22)) {
			correct += i + 22;
		}
		BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], correct);
	}
}


/* Push at non-zero time */
BOOST_AUTO_TEST_CASE (audio_merger_test2)
{
	AudioMerger merger (sampling_rate);

	push (merger, 0, 64, 9);

	/* There's nothing from 0 to 9 */
	auto tb = merger.pull (DCPTime::from_frames (9, sampling_rate));
	BOOST_CHECK_EQUAL (tb.size(), 0U);

	/* Then there's our data at 9 */
	tb = merger.pull (DCPTime::from_frames (9 + 64, sampling_rate));

	BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
	BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(9, sampling_rate).get());

	/* Check the sample values */
	for (int i = 0; i < 64; ++i) {
		BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
	}
}


/* Push two non contiguous blocks */
BOOST_AUTO_TEST_CASE (audio_merger_test3)
{
	AudioMerger merger (sampling_rate);

	push (merger, 0, 64, 17);
	push (merger, 0, 64, 114);

	/* Get them back */

	auto tb = merger.pull (DCPTime::from_frames (100, sampling_rate));
	BOOST_REQUIRE (tb.size() == 1U);
	BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
	BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(17, sampling_rate).get());
	for (int i = 0; i < 64; ++i) {
		BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
	}

	tb = merger.pull (DCPTime::from_frames (200, sampling_rate));
	BOOST_REQUIRE (tb.size() == 1U);
	BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
	BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(114, sampling_rate).get());
	for (int i = 0; i < 64; ++i) {
		BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
	}
}


/* Reply a sequence of calls to AudioMerger that resulted in a crash */
BOOST_AUTO_TEST_CASE (audio_merger_test4)
{
	auto f = fopen_boost("test/data/audio_merger_bug1.log", "r");
	BOOST_REQUIRE (f);
	list<string> tokens;
	char buf[64];
	while (fscanf(f, "%63s", buf) == 1) {
		tokens.push_back (buf);
	}

	shared_ptr<AudioMerger> merger;
	auto i = tokens.begin ();
	while (i != tokens.end()) {
		BOOST_CHECK (*i++ == "I/AM");
		string const cmd = *i++;
		if (cmd == "frame_rate") {
			BOOST_REQUIRE (i != tokens.end());
			merger.reset (new AudioMerger(dcp::raw_convert<int>(*i++)));
		} else if (cmd == "clear") {
			merger->clear ();
		} else if (cmd == "push") {
			BOOST_REQUIRE (i != tokens.end());
			DCPTime time(dcp::raw_convert<DCPTime::Type>(*i++));
			BOOST_REQUIRE (i != tokens.end());
			int const frames = dcp::raw_convert<int>(*i++);
			auto buffers = make_shared<AudioBuffers>(1, frames);
			BOOST_REQUIRE (merger);
			merger->push (buffers, time);
		} else if (cmd == "pull") {
			BOOST_REQUIRE (i != tokens.end());
			DCPTime time(dcp::raw_convert<DCPTime::Type>(*i++));
			merger->pull (time);
		}
	}
}