Extract audio merging code from Player.
[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 static int last_time;
32
33 static int
34 pass_through (int x)
35 {
36         return x;
37 }
38
39 static void
40 process_audio (shared_ptr<const AudioBuffers> audio, int time)
41 {
42         last_audio = audio;
43         last_time = time;
44 }
45
46 static void
47 reset ()
48 {
49         last_audio.reset ();
50         last_time = 0;
51 }
52
53 BOOST_AUTO_TEST_CASE (audio_merger_test1)
54 {
55         AudioMerger<int, int> merger (1, bind (&pass_through, _1), boost::bind (&pass_through, _1));
56         merger.Audio.connect (bind (&process_audio, _1, _2));
57
58         reset ();
59         
60         /* Push 64 samples, 0 -> 63 at time 0 */
61         shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, 64));
62         for (int i = 0; i < 64; ++i) {
63                 buffers->data()[0][i] = i;
64         }
65         merger.push (buffers, 0);
66
67         /* That should not have caused an emission */
68         BOOST_CHECK_EQUAL (last_audio, shared_ptr<const AudioBuffers> ());
69         BOOST_CHECK_EQUAL (last_time, 0);
70
71         /* Push 64 samples, 0 -> 63 at time 22 */
72         merger.push (buffers, 22);
73
74         /* That should have caused an emission of 22 samples at 0 */
75         BOOST_CHECK (last_audio != shared_ptr<const AudioBuffers> ());
76         BOOST_CHECK_EQUAL (last_audio->frames(), 22);
77         BOOST_CHECK_EQUAL (last_time, 0);
78
79         /* And they should be a staircase */
80         for (int i = 0; i < 22; ++i) {
81                 BOOST_CHECK_EQUAL (last_audio->data()[0][i], i);
82         }
83
84         reset ();
85         merger.flush ();
86
87         /* That flush should give us 64 samples at 22 */
88         BOOST_CHECK_EQUAL (last_audio->frames(), 64);
89         BOOST_CHECK_EQUAL (last_time, 22);
90
91         /* Check the sample values */
92         for (int i = 0; i < 64; ++i) {
93                 int correct = i;
94                 if (i < (64 - 22)) {
95                         correct += i + 22;
96                 }
97                 BOOST_CHECK_EQUAL (last_audio->data()[0][i], correct);
98         }
99 }
100
101 BOOST_AUTO_TEST_CASE (audio_merger_test2)
102 {
103         AudioMerger<int, int> merger (1, bind (&pass_through, _1), boost::bind (&pass_through, _1));
104         merger.Audio.connect (bind (&process_audio, _1, _2));
105
106         reset ();
107         
108         /* Push 64 samples, 0 -> 63 at time 9 */
109         shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, 64));
110         for (int i = 0; i < 64; ++i) {
111                 buffers->data()[0][i] = i;
112         }
113         merger.push (buffers, 9);
114
115         /* That flush should give us 9 samples at 0 */
116         BOOST_CHECK_EQUAL (last_audio->frames(), 9);
117         BOOST_CHECK_EQUAL (last_time, 0);
118         
119         for (int i = 0; i < 9; ++i) {
120                 BOOST_CHECK_EQUAL (last_audio->data()[0][i], 0);
121         }
122         
123         reset ();
124         merger.flush ();
125
126         /* That flush should give us 64 samples at 9 */
127         BOOST_CHECK_EQUAL (last_audio->frames(), 64);
128         BOOST_CHECK_EQUAL (last_time, 9);
129         
130         /* Check the sample values */
131         for (int i = 0; i < 64; ++i) {
132                 BOOST_CHECK_EQUAL (last_audio->data()[0][i], i);
133         }
134 }