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 in various ways.
26 #include <boost/test/unit_test.hpp>
27 #include "lib/audio_buffers.h"
31 static float tolerance = 1e-3;
36 return float (rand ()) / RAND_MAX;
40 random_fill (AudioBuffers& buffers)
42 for (int i = 0; i < buffers.frames(); ++i) {
43 for (int j = 0; j < buffers.channels(); ++j) {
44 buffers.data(j)[i] = random_float ();
50 random_check (AudioBuffers& buffers, int from, int frames)
52 for (int i = from; i < (from + frames); ++i) {
53 for (int j = 0; j < buffers.channels(); ++j) {
54 BOOST_CHECK_CLOSE (buffers.data(j)[i], random_float (), tolerance);
60 BOOST_AUTO_TEST_CASE (audio_buffers_setup_test)
62 AudioBuffers buffers (4, 9155);
64 BOOST_CHECK (buffers.data ());
65 for (int i = 0; i < 4; ++i) {
66 BOOST_CHECK (buffers.data (i));
69 BOOST_CHECK_EQUAL (buffers.channels(), 4);
70 BOOST_CHECK_EQUAL (buffers.frames(), 9155);
73 /** Extending some buffers */
74 BOOST_AUTO_TEST_CASE (audio_buffers_extend_test)
76 AudioBuffers buffers (3, 150);
78 random_fill (buffers);
81 buffers.ensure_size (299);
84 random_check (buffers, 0, 150);
86 /* New space should be silent */
87 for (int i = 150; i < 299; ++i) {
88 for (int c = 0; c < 3; ++c) {
89 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
95 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_test)
97 AudioBuffers buffers (9, 9933);
99 random_fill (buffers);
101 buffers.make_silent ();
103 for (int i = 0; i < 9933; ++i) {
104 for (int c = 0; c < 9; ++c) {
105 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
110 /** make_silent (int c) */
111 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_channel_test)
113 AudioBuffers buffers (9, 9933);
115 random_fill (buffers);
117 buffers.make_silent (4);
120 for (int i = 0; i < 9933; ++i) {
121 for (int c = 0; c < 9; ++c) {
124 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
126 BOOST_CHECK_CLOSE (buffers.data(c)[i], random_float (), tolerance);
132 /** make_silent (int from, int frames) */
133 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_part_test)
135 AudioBuffers buffers (9, 9933);
137 random_fill (buffers);
139 buffers.make_silent (145, 833);
142 for (int i = 0; i < 145; ++i) {
143 for (int c = 0; c < 9; ++c) {
144 BOOST_CHECK_EQUAL (buffers.data(c)[i], random_float ());
148 for (int i = 145; i < (145 + 833); ++i) {
149 for (int c = 0; c < 9; ++c) {
151 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
155 for (int i = (145 + 833); i < 9933; ++i) {
156 for (int c = 0; c < 9; ++c) {
157 BOOST_CHECK_EQUAL (buffers.data(c)[i], random_float ());
163 BOOST_AUTO_TEST_CASE (audio_buffers_apply_gain)
165 AudioBuffers buffers (2, 417315);
167 random_fill (buffers);
169 buffers.apply_gain (5.4);
172 for (int i = 0; i < 417315; ++i) {
173 for (int c = 0; c < 2; ++c) {
174 BOOST_CHECK_CLOSE (buffers.data(c)[i], random_float() * pow (10, 5.4 / 20), tolerance);
180 BOOST_AUTO_TEST_CASE (audio_buffers_copy_from)
182 AudioBuffers a (5, 63711);
183 AudioBuffers b (5, 12345);
191 a.copy_from (&b, 517, 233, 194);
193 /* Re-seed a's generator and check the numbers that came from it */
195 /* First part; not copied-over */
197 random_check (a, 0, 194);
199 /* Second part; copied-over (just burn generator a's numbers) */
200 for (int i = 0; i < (517 * 5); ++i) {
204 /* Third part; not copied-over */
205 random_check (a, 194 + 517, a.frames() - 194 - 517);
207 /* Re-seed b's generator and check the numbers that came from it */
210 /* First part; burn */
211 for (int i = 0; i < 194 * 5; ++i) {
215 /* Second part; copied */
216 random_check (b, 194, 517);
220 BOOST_AUTO_TEST_CASE (audio_buffers_move)
222 AudioBuffers buffers (7, 65536);
225 random_fill (buffers);
227 int const from = 888;
229 int const frames = 444;
231 buffers.move (from, to, frames);
233 /* Re-seed and check the un-moved parts */
236 random_check (buffers, 0, to);
239 for (int i = 0; i < (from - to + frames) * 7; ++i) {
243 random_check (buffers, from + frames, 65536 - frames - from);
245 /* Re-seed and check the moved part */
249 for (int i = 0; i < from * 7; ++i) {
253 random_check (buffers, to, frames);
256 /** accumulate_channel */
257 BOOST_AUTO_TEST_CASE (audio_buffers_accumulate_channel)
259 AudioBuffers a (3, 256);
263 AudioBuffers b (3, 256);
266 a.accumulate_channel (&b, 2, 1, 1.2);
269 for (int i = 0; i < 256; ++i) {
270 for (int c = 0; c < 3; ++c) {
271 float const A = random_float ();
273 BOOST_CHECK_CLOSE (a.data(c)[i], A + b.data(2)[i] * 1.2, tolerance);
275 BOOST_CHECK_CLOSE (a.data(c)[i], A, tolerance);
281 /** accumulate_frames */
282 BOOST_AUTO_TEST_CASE (audio_buffers_accumulate_frames)
284 AudioBuffers a (3, 256);
288 AudioBuffers b (3, 256);
291 a.accumulate_frames (&b, 91, 44, 129);
294 for (int i = 0; i < 256; ++i) {
295 for (int c = 0; c < 3; ++c) {
296 float const A = random_float ();
297 if (i < 44 || i >= (44 + 129)) {
298 BOOST_CHECK_CLOSE (a.data(c)[i], A, tolerance);
300 BOOST_CHECK_CLOSE (a.data(c)[i], A + b.data(c)[i + 91 - 44], tolerance);