New DCPTime/ContentTime types.
[dcpomatic.git] / test / audio_merger_test.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/test/unit_test.hpp>
21 #include <boost/bind.hpp>
22 #include <boost/function.hpp>
23 #include <boost/signals2.hpp>
24 #include "lib/audio_merger.h"
25 #include "lib/audio_buffers.h"
26
27 using boost::shared_ptr;
28 using boost::bind;
29
30 static shared_ptr<const AudioBuffers> last_audio;
31
32 BOOST_AUTO_TEST_CASE (audio_merger_test1)
33 {
34         int const frame_rate = 48000;
35         AudioMerger merger (1, frame_rate);
36
37         /* Push 64 samples, 0 -> 63 at time 0 */
38         shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, 64));
39         for (int i = 0; i < 64; ++i) {
40                 buffers->data()[0][i] = i;
41         }
42         merger.push (buffers, DCPTime ());
43
44         /* Push 64 samples, 0 -> 63 at time 22 */
45         merger.push (buffers, DCPTime::from_frames (22, frame_rate));
46
47         TimedAudioBuffers<DCPTime> tb = merger.pull (DCPTime::from_frames (22, frame_rate));
48         BOOST_CHECK (tb.audio != shared_ptr<const AudioBuffers> ());
49         BOOST_CHECK_EQUAL (tb.audio->frames(), 22);
50         BOOST_CHECK_EQUAL (tb.time, 0);
51
52         /* And they should be a staircase */
53         for (int i = 0; i < 22; ++i) {
54                 BOOST_CHECK_EQUAL (tb.audio->data()[0][i], i);
55         }
56
57         tb = merger.flush ();
58
59         /* That flush should give us 64 samples at 22 */
60         BOOST_CHECK_EQUAL (tb.audio->frames(), 64);
61         BOOST_CHECK_EQUAL (tb.time, DCPTime::from_frames (22, frame_rate));
62
63         /* Check the sample values */
64         for (int i = 0; i < 64; ++i) {
65                 int correct = i;
66                 if (i < (64 - 22)) {
67                         correct += i + 22;
68                 }
69                 BOOST_CHECK_EQUAL (tb.audio->data()[0][i], correct);
70         }
71 }
72
73 BOOST_AUTO_TEST_CASE (audio_merger_test2)
74 {
75         int const frame_rate = 48000;
76         AudioMerger merger (1, frame_rate);
77
78         /* Push 64 samples, 0 -> 63 at time 9 */
79         shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, 64));
80         for (int i = 0; i < 64; ++i) {
81                 buffers->data()[0][i] = i;
82         }
83         merger.push (buffers, DCPTime::from_frames (9, frame_rate));
84
85         TimedAudioBuffers<DCPTime> tb = merger.pull (DCPTime::from_frames (9, frame_rate));
86         BOOST_CHECK_EQUAL (tb.audio->frames(), 9);
87         BOOST_CHECK_EQUAL (tb.time, 0);
88         
89         for (int i = 0; i < 9; ++i) {
90                 BOOST_CHECK_EQUAL (tb.audio->data()[0][i], 0);
91         }
92         
93         tb = merger.flush ();
94
95         /* That flush should give us 64 samples at 9 */
96         BOOST_CHECK_EQUAL (tb.audio->frames(), 64);
97         BOOST_CHECK_EQUAL (tb.time, 9);
98         
99         /* Check the sample values */
100         for (int i = 0; i < 64; ++i) {
101                 BOOST_CHECK_EQUAL (tb.audio->data()[0][i], i);
102         }
103 }