Fix or remove several broken pixel formats in Image::fade and add
[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 /** @file  test/audio_merger_test.cc
22  *  @brief Test AudioMerger class.
23  *  @ingroup selfcontained
24  */
25
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>
32 #include <iostream>
33
34 using std::pair;
35 using std::list;
36 using std::cout;
37 using boost::shared_ptr;
38 using boost::bind;
39
40 static shared_ptr<const AudioBuffers> last_audio;
41
42 int const sampling_rate = 48000;
43
44 static void
45 push (AudioMerger& merger, int from, int to, int at)
46 {
47         shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, to - from));
48         for (int i = 0; i < (to - from); ++i) {
49                 buffers->data()[0][i] = from + i;
50         }
51         merger.push (buffers, DCPTime(at, sampling_rate));
52 }
53
54 /* Basic mixing, 2 overlapping pushes */
55 BOOST_AUTO_TEST_CASE (audio_merger_test1)
56 {
57         AudioMerger merger (sampling_rate);
58
59         push (merger, 0, 64, 0);
60         push (merger, 0, 64, 22);
61
62         list<pair<shared_ptr<AudioBuffers>, DCPTime> > tb = merger.pull (DCPTime::from_frames (22, sampling_rate));
63         BOOST_REQUIRE (tb.size() == 1);
64         BOOST_CHECK (tb.front().first != shared_ptr<const AudioBuffers> ());
65         BOOST_CHECK_EQUAL (tb.front().first->frames(), 22);
66         BOOST_CHECK_EQUAL (tb.front().second.get(), 0);
67
68         /* And they should be a staircase */
69         for (int i = 0; i < 22; ++i) {
70                 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
71         }
72
73         tb = merger.pull (DCPTime::from_frames (22 + 64, sampling_rate));
74         BOOST_REQUIRE (tb.size() == 1);
75         BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
76         BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(22, sampling_rate).get());
77
78         /* Check the sample values */
79         for (int i = 0; i < 64; ++i) {
80                 int correct = i;
81                 if (i < (64 - 22)) {
82                         correct += i + 22;
83                 }
84                 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], correct);
85         }
86 }
87
88 /* Push at non-zero time */
89 BOOST_AUTO_TEST_CASE (audio_merger_test2)
90 {
91         AudioMerger merger (sampling_rate);
92
93         push (merger, 0, 64, 9);
94
95         /* There's nothing from 0 to 9 */
96         list<pair<shared_ptr<AudioBuffers>, DCPTime> > tb = merger.pull (DCPTime::from_frames (9, sampling_rate));
97         BOOST_CHECK_EQUAL (tb.size(), 0);
98
99         /* Then there's our data at 9 */
100         tb = merger.pull (DCPTime::from_frames (9 + 64, sampling_rate));
101
102         BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
103         BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(9, sampling_rate).get());
104
105         /* Check the sample values */
106         for (int i = 0; i < 64; ++i) {
107                 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
108         }
109 }
110
111 /* Push two non contiguous blocks */
112 BOOST_AUTO_TEST_CASE (audio_merger_test3)
113 {
114         AudioMerger merger (sampling_rate);
115
116         push (merger, 0, 64, 17);
117         push (merger, 0, 64, 114);
118
119         /* Get them back */
120
121         list<pair<shared_ptr<AudioBuffers>, DCPTime> > tb = merger.pull (DCPTime::from_frames (100, sampling_rate));
122         BOOST_REQUIRE (tb.size() == 1);
123         BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
124         BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(17, sampling_rate).get());
125         for (int i = 0; i < 64; ++i) {
126                 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
127         }
128
129         tb = merger.pull (DCPTime::from_frames (200, sampling_rate));
130         BOOST_REQUIRE (tb.size() == 1);
131         BOOST_CHECK_EQUAL (tb.front().first->frames(), 64);
132         BOOST_CHECK_EQUAL (tb.front().second.get(), DCPTime::from_frames(114, sampling_rate).get());
133         for (int i = 0; i < 64; ++i) {
134                 BOOST_CHECK_EQUAL (tb.front().first->data()[0][i], i);
135         }
136 }