Re-add audio merger test.
[dcpomatic.git] / test / audio_merger_test.cc
1 /*
2     Copyright (C) 2013-2017 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 #include "lib/audio_merger.h"
22 #include "lib/audio_buffers.h"
23 #include <boost/test/unit_test.hpp>
24 #include <boost/bind.hpp>
25 #include <boost/function.hpp>
26 #include <boost/signals2.hpp>
27
28 using std::pair;
29 using boost::shared_ptr;
30 using boost::bind;
31
32 static shared_ptr<const AudioBuffers> last_audio;
33
34 int const sampling_rate = 48000;
35
36 BOOST_AUTO_TEST_CASE (audio_merger_test1)
37 {
38         AudioMerger merger (1, sampling_rate);
39
40         /* Push 64 samples, 0 -> 63 at time 0 */
41         shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, 64));
42         for (int i = 0; i < 64; ++i) {
43                 buffers->data()[0][i] = i;
44         }
45         merger.push (buffers, DCPTime());
46
47         /* Push 64 samples, 0 -> 63 at time 22 */
48         merger.push (buffers, DCPTime::from_frames (22, sampling_rate));
49
50         pair<shared_ptr<AudioBuffers>, DCPTime> tb = merger.pull (DCPTime::from_frames (22, sampling_rate));
51         BOOST_CHECK (tb.first != shared_ptr<const AudioBuffers> ());
52         BOOST_CHECK_EQUAL (tb.first->frames(), 22);
53         BOOST_CHECK_EQUAL (tb.second.get(), 0);
54
55         /* And they should be a staircase */
56         for (int i = 0; i < 22; ++i) {
57                 BOOST_CHECK_EQUAL (tb.first->data()[0][i], i);
58         }
59
60         tb = merger.pull (DCPTime::from_frames (22 + 64, sampling_rate));
61
62         BOOST_CHECK_EQUAL (tb.first->frames(), 64);
63         BOOST_CHECK_EQUAL (tb.second.get(), DCPTime::from_frames(22, sampling_rate).get());
64
65         /* Check the sample values */
66         for (int i = 0; i < 64; ++i) {
67                 int correct = i;
68                 if (i < (64 - 22)) {
69                         correct += i + 22;
70                 }
71                 BOOST_CHECK_EQUAL (tb.first->data()[0][i], correct);
72         }
73 }
74
75 BOOST_AUTO_TEST_CASE (audio_merger_test2)
76 {
77         AudioMerger merger (1, sampling_rate);
78
79         /* Push 64 samples, 0 -> 63 at time 9 */
80         shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, 64));
81         for (int i = 0; i < 64; ++i) {
82                 buffers->data()[0][i] = i;
83         }
84         merger.push (buffers, DCPTime::from_frames (9, sampling_rate));
85
86         pair<shared_ptr<AudioBuffers>, DCPTime> tb = merger.pull (DCPTime::from_frames (9, sampling_rate));
87         BOOST_CHECK_EQUAL (tb.first->frames(), 9);
88         BOOST_CHECK_EQUAL (tb.second.get(), 0);
89
90         for (int i = 0; i < 9; ++i) {
91                 BOOST_CHECK_EQUAL (tb.first->data()[0][i], 0);
92         }
93
94         tb = merger.pull (DCPTime::from_frames (9 + 64, sampling_rate));
95
96         BOOST_CHECK_EQUAL (tb.first->frames(), 64);
97         BOOST_CHECK_EQUAL (tb.second.get(), DCPTime::from_frames(9, sampling_rate).get());
98
99         /* Check the sample values */
100         for (int i = 0; i < 64; ++i) {
101                 BOOST_CHECK_EQUAL (tb.first->data()[0][i], i);
102         }
103 }