2 Copyright (C) 2016-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "lib/audio_ring_buffers.h"
23 #include <boost/test/unit_test.hpp>
26 using std::make_shared;
27 using namespace dcpomatic;
33 /* XXX: these tests don't check the timestamping in AudioRingBuffers */
35 /** Basic tests fetching the same number of channels as went in */
36 BOOST_AUTO_TEST_CASE (audio_ring_buffers_test1)
40 /* Should start off empty */
41 BOOST_CHECK_EQUAL (rb.size(), 0);
43 /* Getting some data should give an underrun and write zeros */
44 float buffer[256 * 6];
45 buffer[240 * 6] = CANARY;
46 BOOST_CHECK (!rb.get(buffer, 6, 240));
47 for (int i = 0; i < 240 * 6; ++i) {
48 BOOST_REQUIRE_EQUAL (buffer[i], 0);
50 BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
52 /* clear() should give the same result */
54 BOOST_CHECK_EQUAL (rb.size(), 0);
55 buffer[240 * 6] = CANARY;
56 BOOST_CHECK (rb.get(buffer, 6, 240) == boost::optional<DCPTime>());
57 for (int i = 0; i < 240 * 6; ++i) {
58 BOOST_REQUIRE_EQUAL (buffer[i], 0);
60 BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
62 /* Put some data in */
63 auto data = make_shared<AudioBuffers>(6, 91);
65 for (int i = 0; i < 91; ++i) {
66 for (int j = 0; j < 6; ++j) {
67 data->data(j)[i] = value++;
70 rb.put (data, DCPTime(), 48000);
71 BOOST_CHECK_EQUAL (rb.size(), 91);
73 /* Get part of it out */
74 buffer[40 * 6] = CANARY;
75 BOOST_CHECK (*rb.get(buffer, 6, 40) == DCPTime());
77 for (int i = 0; i < 40 * 6; ++i) {
78 BOOST_REQUIRE_EQUAL (buffer[i], check++);
80 BOOST_CHECK_EQUAL (buffer[40 * 6], CANARY);
81 BOOST_CHECK_EQUAL (rb.size(), 51);
84 buffer[51 * 6] = CANARY;
85 BOOST_CHECK (*rb.get(buffer, 6, 51) == DCPTime::from_frames(40, 48000));
86 for (int i = 0; i < 51 * 6; ++i) {
87 BOOST_REQUIRE_EQUAL (buffer[i], check++);
89 BOOST_CHECK_EQUAL (buffer[51 * 6], CANARY);
90 BOOST_CHECK_EQUAL (rb.size(), 0);
92 /* Now there should be an underrun */
93 buffer[240 * 6] = CANARY;
94 BOOST_CHECK (!rb.get(buffer, 6, 240));
95 BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
98 /** Similar tests but fetching more channels than were put in */
99 BOOST_AUTO_TEST_CASE (audio_ring_buffers_test2)
103 /* Put some data in */
104 auto data = make_shared<AudioBuffers>(2, 91);
106 for (int i = 0; i < 91; ++i) {
107 for (int j = 0; j < 2; ++j) {
108 data->data(j)[i] = value++;
111 rb.put (data, DCPTime(), 48000);
112 BOOST_CHECK_EQUAL (rb.size(), 91);
114 /* Get part of it out */
115 float buffer[256 * 6];
116 buffer[40 * 6] = CANARY;
117 BOOST_CHECK (*rb.get(buffer, 6, 40) == DCPTime());
119 for (int i = 0; i < 40; ++i) {
120 for (int j = 0; j < 2; ++j) {
121 BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], check++);
123 for (int j = 2; j < 6; ++j) {
124 BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], 0);
127 BOOST_CHECK_EQUAL (buffer[40 * 6], CANARY);
128 BOOST_CHECK_EQUAL (rb.size(), 51);
131 buffer[51 * 6] = CANARY;
132 BOOST_CHECK (*rb.get(buffer, 6, 51) == DCPTime::from_frames(40, 48000));
133 for (int i = 0; i < 51; ++i) {
134 for (int j = 0; j < 2; ++j) {
135 BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], check++);
137 for (int j = 2; j < 6; ++j) {
138 BOOST_REQUIRE_EQUAL (buffer[i * 6 + j], 0);
141 BOOST_CHECK_EQUAL (buffer[51 * 6], CANARY);
142 BOOST_CHECK_EQUAL (rb.size(), 0);
144 /* Now there should be an underrun */
145 buffer[240 * 6] = CANARY;
146 BOOST_CHECK (!rb.get(buffer, 6, 240));
147 BOOST_CHECK_EQUAL (buffer[240 * 6], CANARY);
150 /** Similar tests but fetching fewer channels than were put in */
151 BOOST_AUTO_TEST_CASE (audio_ring_buffers_test3)
155 /* Put some data in */
156 auto data = make_shared<AudioBuffers>(6, 91);
158 for (int i = 0; i < 91; ++i) {
159 for (int j = 0; j < 6; ++j) {
160 data->data(j)[i] = value++;
163 rb.put (data, DCPTime(), 48000);
164 BOOST_CHECK_EQUAL (rb.size(), 91);
166 /* Get part of it out */
167 float buffer[256 * 6];
168 buffer[40 * 2] = CANARY;
169 BOOST_CHECK (*rb.get(buffer, 2, 40) == DCPTime());
171 for (int i = 0; i < 40; ++i) {
172 for (int j = 0; j < 2; ++j) {
173 BOOST_REQUIRE_EQUAL (buffer[i * 2 + j], check++);
177 BOOST_CHECK_EQUAL (buffer[40 * 2], CANARY);
178 BOOST_CHECK_EQUAL (rb.size(), 51);
181 buffer[51 * 2] = CANARY;
182 BOOST_CHECK (*rb.get(buffer, 2, 51) == DCPTime::from_frames(40, 48000));
183 for (int i = 0; i < 51; ++i) {
184 for (int j = 0; j < 2; ++j) {
185 BOOST_REQUIRE_EQUAL (buffer[i * 2 + j], check++);
189 BOOST_CHECK_EQUAL (buffer[51 * 2], CANARY);
190 BOOST_CHECK_EQUAL (rb.size(), 0);
192 /* Now there should be an underrun */
193 buffer[240 * 2] = CANARY;
194 BOOST_CHECK (!rb.get(buffer, 2, 240));
195 BOOST_CHECK_EQUAL (buffer[240 * 2], CANARY);