2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file test/audio_buffers_test.cc
21 * @brief Test AudioBuffers in various ways.
25 #include <boost/test/unit_test.hpp>
26 #include "lib/audio_buffers.h"
30 static float tolerance = 1e-3;
35 return float (rand ()) / RAND_MAX;
39 random_fill (AudioBuffers& buffers)
41 for (int i = 0; i < buffers.frames(); ++i) {
42 for (int j = 0; j < buffers.channels(); ++j) {
43 buffers.data(j)[i] = random_float ();
49 random_check (AudioBuffers& buffers, int from, int frames)
51 for (int i = from; i < (from + frames); ++i) {
52 for (int j = 0; j < buffers.channels(); ++j) {
53 BOOST_CHECK_CLOSE (buffers.data(j)[i], random_float (), tolerance);
59 BOOST_AUTO_TEST_CASE (audio_buffers_setup_test)
61 AudioBuffers buffers (4, 9155);
63 BOOST_CHECK (buffers.data ());
64 for (int i = 0; i < 4; ++i) {
65 BOOST_CHECK (buffers.data (i));
68 BOOST_CHECK_EQUAL (buffers.channels(), 4);
69 BOOST_CHECK_EQUAL (buffers.frames(), 9155);
72 /** Extending some buffers */
73 BOOST_AUTO_TEST_CASE (audio_buffers_extend_test)
75 AudioBuffers buffers (3, 150);
77 random_fill (buffers);
80 buffers.ensure_size (299);
83 random_check (buffers, 0, 150);
85 /* New space should be silent */
86 for (int i = 150; i < 299; ++i) {
87 for (int c = 0; c < 3; ++c) {
88 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
94 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_test)
96 AudioBuffers buffers (9, 9933);
98 random_fill (buffers);
100 buffers.make_silent ();
102 for (int i = 0; i < 9933; ++i) {
103 for (int c = 0; c < 9; ++c) {
104 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
109 /** make_silent (int c) */
110 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_channel_test)
112 AudioBuffers buffers (9, 9933);
114 random_fill (buffers);
116 buffers.make_silent (4);
119 for (int i = 0; i < 9933; ++i) {
120 for (int c = 0; c < 9; ++c) {
123 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
125 BOOST_CHECK_CLOSE (buffers.data(c)[i], random_float (), tolerance);
131 /** make_silent (int from, int frames) */
132 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_part_test)
134 AudioBuffers buffers (9, 9933);
136 random_fill (buffers);
138 buffers.make_silent (145, 833);
141 for (int i = 0; i < 145; ++i) {
142 for (int c = 0; c < 9; ++c) {
143 BOOST_CHECK_EQUAL (buffers.data(c)[i], random_float ());
147 for (int i = 145; i < (145 + 833); ++i) {
148 for (int c = 0; c < 9; ++c) {
150 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
154 for (int i = (145 + 833); i < 9933; ++i) {
155 for (int c = 0; c < 9; ++c) {
156 BOOST_CHECK_EQUAL (buffers.data(c)[i], random_float ());
162 BOOST_AUTO_TEST_CASE (audio_buffers_apply_gain)
164 AudioBuffers buffers (2, 417315);
166 random_fill (buffers);
168 buffers.apply_gain (5.4);
171 for (int i = 0; i < 417315; ++i) {
172 for (int c = 0; c < 2; ++c) {
173 BOOST_CHECK_CLOSE (buffers.data(c)[i], random_float() * pow (10, 5.4 / 20), tolerance);
179 BOOST_AUTO_TEST_CASE (audio_buffers_copy_from)
181 AudioBuffers a (5, 63711);
182 AudioBuffers b (5, 12345);
190 a.copy_from (&b, 517, 233, 194);
192 /* Re-seed a's generator and check the numbers that came from it */
194 /* First part; not copied-over */
196 random_check (a, 0, 194);
198 /* Second part; copied-over (just burn generator a's numbers) */
199 for (int i = 0; i < (517 * 5); ++i) {
203 /* Third part; not copied-over */
204 random_check (a, 194 + 517, a.frames() - 194 - 517);
206 /* Re-seed b's generator and check the numbers that came from it */
209 /* First part; burn */
210 for (int i = 0; i < 194 * 5; ++i) {
214 /* Second part; copied */
215 random_check (b, 194, 517);
219 BOOST_AUTO_TEST_CASE (audio_buffers_move)
221 AudioBuffers buffers (7, 65536);
224 random_fill (buffers);
226 int const from = 888;
228 int const frames = 444;
230 buffers.move (from, to, frames);
232 /* Re-seed and check the un-moved parts */
235 random_check (buffers, 0, to);
238 for (int i = 0; i < (from - to + frames) * 7; ++i) {
242 random_check (buffers, from + frames, 65536 - frames - from);
244 /* Re-seed and check the moved part */
248 for (int i = 0; i < from * 7; ++i) {
252 random_check (buffers, to, frames);
255 /** accumulate_channel */
256 BOOST_AUTO_TEST_CASE (audio_buffers_accumulate_channel)
258 AudioBuffers a (3, 256);
262 AudioBuffers b (3, 256);
265 a.accumulate_channel (&b, 2, 1, 1.2);
268 for (int i = 0; i < 256; ++i) {
269 for (int c = 0; c < 3; ++c) {
270 float const A = random_float ();
272 BOOST_CHECK_CLOSE (a.data(c)[i], A + b.data(2)[i] * 1.2, tolerance);
274 BOOST_CHECK_CLOSE (a.data(c)[i], A, tolerance);
280 /** accumulate_frames */
281 BOOST_AUTO_TEST_CASE (audio_buffers_accumulate_frames)
283 AudioBuffers a (3, 256);
287 AudioBuffers b (3, 256);
290 a.accumulate_frames (&b, 91, 44, 129);
293 for (int i = 0; i < 256; ++i) {
294 for (int c = 0; c < 3; ++c) {
295 float const A = random_float ();
296 if (i < 44 || i >= (44 + 129)) {
297 BOOST_CHECK_CLOSE (a.data(c)[i], A, tolerance);
299 BOOST_CHECK_CLOSE (a.data(c)[i], A + b.data(c)[i + 91 - 44], tolerance);