2 Copyright (C) 2014 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/>.
21 /** @file test/audio_buffers_test.cc
22 * @brief Test AudioBuffers class.
23 * @ingroup selfcontained
27 #include <boost/test/unit_test.hpp>
28 #include "lib/audio_buffers.h"
32 static float tolerance = 1e-3;
37 return float (rand ()) / RAND_MAX;
41 random_fill (AudioBuffers& buffers)
43 for (int i = 0; i < buffers.frames(); ++i) {
44 for (int j = 0; j < buffers.channels(); ++j) {
45 buffers.data(j)[i] = random_float ();
51 random_check (AudioBuffers& buffers, int from, int frames)
53 for (int i = from; i < (from + frames); ++i) {
54 for (int j = 0; j < buffers.channels(); ++j) {
55 BOOST_CHECK_CLOSE (buffers.data(j)[i], random_float (), tolerance);
61 BOOST_AUTO_TEST_CASE (audio_buffers_setup_test)
63 AudioBuffers buffers (4, 9155);
65 BOOST_CHECK (buffers.data ());
66 for (int i = 0; i < 4; ++i) {
67 BOOST_CHECK (buffers.data (i));
70 BOOST_CHECK_EQUAL (buffers.channels(), 4);
71 BOOST_CHECK_EQUAL (buffers.frames(), 9155);
74 /** Extending some buffers */
75 BOOST_AUTO_TEST_CASE (audio_buffers_extend_test)
77 AudioBuffers buffers (3, 150);
79 random_fill (buffers);
82 buffers.set_frames (299);
85 random_check (buffers, 0, 150);
87 /* New space should be silent */
88 for (int i = 150; i < 299; ++i) {
89 for (int c = 0; c < 3; ++c) {
90 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
96 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_test)
98 AudioBuffers buffers (9, 9933);
100 random_fill (buffers);
102 buffers.make_silent ();
104 for (int i = 0; i < 9933; ++i) {
105 for (int c = 0; c < 9; ++c) {
106 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
111 /** make_silent (int c) */
112 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_channel_test)
114 AudioBuffers buffers (9, 9933);
116 random_fill (buffers);
118 buffers.make_silent (4);
121 for (int i = 0; i < 9933; ++i) {
122 for (int c = 0; c < 9; ++c) {
125 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
127 BOOST_CHECK_CLOSE (buffers.data(c)[i], random_float (), tolerance);
133 /** make_silent (int from, int frames) */
134 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_part_test)
136 AudioBuffers buffers (9, 9933);
138 random_fill (buffers);
140 buffers.make_silent (145, 833);
143 for (int i = 0; i < 145; ++i) {
144 for (int c = 0; c < 9; ++c) {
145 BOOST_CHECK_EQUAL (buffers.data(c)[i], random_float ());
149 for (int i = 145; i < (145 + 833); ++i) {
150 for (int c = 0; c < 9; ++c) {
152 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
156 for (int i = (145 + 833); i < 9933; ++i) {
157 for (int c = 0; c < 9; ++c) {
158 BOOST_CHECK_EQUAL (buffers.data(c)[i], random_float ());
164 BOOST_AUTO_TEST_CASE (audio_buffers_apply_gain)
166 AudioBuffers buffers (2, 417315);
168 random_fill (buffers);
170 buffers.apply_gain (5.4);
173 for (int i = 0; i < 417315; ++i) {
174 for (int c = 0; c < 2; ++c) {
175 BOOST_CHECK_CLOSE (buffers.data(c)[i], random_float() * pow (10, 5.4 / 20), tolerance);
181 BOOST_AUTO_TEST_CASE (audio_buffers_copy_from)
183 AudioBuffers a (5, 63711);
184 AudioBuffers b (5, 12345);
192 a.copy_from (&b, 517, 233, 194);
194 /* Re-seed a's generator and check the numbers that came from it */
196 /* First part; not copied-over */
198 random_check (a, 0, 194);
200 /* Second part; copied-over (just burn generator a's numbers) */
201 for (int i = 0; i < (517 * 5); ++i) {
205 /* Third part; not copied-over */
206 random_check (a, 194 + 517, a.frames() - 194 - 517);
208 /* Re-seed b's generator and check the numbers that came from it */
211 /* First part; burn */
212 for (int i = 0; i < 194 * 5; ++i) {
216 /* Second part; copied */
217 random_check (b, 194, 517);
221 BOOST_AUTO_TEST_CASE (audio_buffers_move)
223 AudioBuffers buffers (7, 65536);
226 random_fill (buffers);
228 int const from = 888;
230 int const frames = 444;
232 buffers.move (frames, from, to);
234 /* Re-seed and check the un-moved parts */
237 random_check (buffers, 0, to);
240 for (int i = 0; i < (from - to + frames) * 7; ++i) {
244 random_check (buffers, from + frames, 65536 - frames - from);
246 /* Re-seed and check the moved part */
250 for (int i = 0; i < from * 7; ++i) {
254 random_check (buffers, to, frames);
257 /** accumulate_channel */
258 BOOST_AUTO_TEST_CASE (audio_buffers_accumulate_channel)
260 AudioBuffers a (3, 256);
264 AudioBuffers b (3, 256);
267 a.accumulate_channel (&b, 2, 1, 1.2);
270 for (int i = 0; i < 256; ++i) {
271 for (int c = 0; c < 3; ++c) {
272 float const A = random_float ();
274 BOOST_CHECK_CLOSE (a.data(c)[i], A + b.data(2)[i] * 1.2, tolerance);
276 BOOST_CHECK_CLOSE (a.data(c)[i], A, tolerance);
282 /** accumulate_frames */
283 BOOST_AUTO_TEST_CASE (audio_buffers_accumulate_frames)
285 AudioBuffers a (3, 256);
289 AudioBuffers b (3, 256);
292 a.accumulate_frames (&b, 129, 91, 44);
295 for (int i = 0; i < 256; ++i) {
296 for (int c = 0; c < 3; ++c) {
297 float const A = random_float ();
298 if (i < 44 || i >= (44 + 129)) {
299 BOOST_CHECK_CLOSE (a.data(c)[i], A, tolerance);
301 BOOST_CHECK_CLOSE (a.data(c)[i], A + b.data(c)[i + 91 - 44], tolerance);
308 BOOST_AUTO_TEST_CASE (audio_buffers_data)
310 AudioBuffers a (94, 512);
312 for (int i = 0; i < 94; ++i) {
313 BOOST_CHECK_EQUAL (a.data()[i], a.data(i));
318 for (int i = 0; i < 94; ++i) {
319 BOOST_CHECK_EQUAL (a.data()[i], a.data(i));
324 BOOST_AUTO_TEST_CASE (audio_buffers_trim_start)
326 AudioBuffers a (13, 999);
335 /* Burn the first 101 numbers in the sequence */
336 for (int i = 0; i < 101 * 13; ++i) {
340 for (int i = 0; i < (999 - 101); ++i) {
341 for (int j = 0; j < 13; ++j) {
342 BOOST_CHECK_CLOSE (a.data(j)[i], random_float(), tolerance);