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