test/data updates.
[dcpomatic.git] / test / audio_buffers_test.cc
1 /*
2     Copyright (C) 2014 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 /** @file  test/audio_buffers_test.cc
21  *  @brief Test AudioBuffers in various ways.
22  */
23
24 #include <cmath>
25 #include <boost/test/unit_test.hpp>
26 #include "lib/audio_buffers.h"
27
28 using std::pow;
29
30 static float tolerance = 1e-3;
31
32 static float
33 random_float ()
34 {
35         return float (rand ()) / RAND_MAX;
36 }
37
38 static void
39 random_fill (AudioBuffers& buffers)
40 {
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 ();
44                 }
45         }
46 }
47
48 static void
49 random_check (AudioBuffers& buffers, int from, int frames)
50 {
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);
54                 }
55         }
56 }
57
58 /** Basic setup */
59 BOOST_AUTO_TEST_CASE (audio_buffers_setup_test)
60 {
61         AudioBuffers buffers (4, 9155);
62
63         BOOST_CHECK (buffers.data ());
64         for (int i = 0; i < 4; ++i) {
65                 BOOST_CHECK (buffers.data (i));
66         }
67
68         BOOST_CHECK_EQUAL (buffers.channels(), 4);
69         BOOST_CHECK_EQUAL (buffers.frames(), 9155);
70 }
71
72 /** Extending some buffers */
73 BOOST_AUTO_TEST_CASE (audio_buffers_extend_test)
74 {
75         AudioBuffers buffers (3, 150);
76         srand (1);
77         random_fill (buffers);
78
79         /* Extend */
80         buffers.ensure_size (299);
81
82         srand (1);
83         random_check (buffers, 0, 150);
84
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);
89                 }
90         }
91 }
92
93 /** make_silent() */
94 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_test)
95 {
96         AudioBuffers buffers (9, 9933);
97         srand (2);
98         random_fill (buffers);
99
100         buffers.make_silent ();
101         
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);
105                 }
106         }
107 }
108
109 /** make_silent (int c) */
110 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_channel_test)
111 {
112         AudioBuffers buffers (9, 9933);
113         srand (3);
114         random_fill (buffers);
115
116         buffers.make_silent (4);
117
118         srand (3);
119         for (int i = 0; i < 9933; ++i) {
120                 for (int c = 0; c < 9; ++c) {
121                         if (c == 4) {
122                                 random_float ();
123                                 BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
124                         } else {
125                                 BOOST_CHECK_CLOSE (buffers.data(c)[i], random_float (), tolerance);
126                         }
127                 }
128         }
129 }
130
131 /** make_silent (int from, int frames) */
132 BOOST_AUTO_TEST_CASE (audio_buffers_make_silent_part_test)
133 {
134         AudioBuffers buffers (9, 9933);
135         srand (4);
136         random_fill (buffers);
137
138         buffers.make_silent (145, 833);
139
140         srand (4);
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 ());
144                 }
145         }
146
147         for (int i = 145; i < (145 + 833); ++i) {
148                 for (int c = 0; c < 9; ++c) {
149                         random_float ();
150                         BOOST_CHECK_EQUAL (buffers.data(c)[i], 0);
151                 }
152         }
153
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 ());
157                 }
158         }
159 }
160
161 /* apply_gain */
162 BOOST_AUTO_TEST_CASE (audio_buffers_apply_gain)
163 {
164         AudioBuffers buffers (2, 417315);
165         srand (9);
166         random_fill (buffers);
167
168         buffers.apply_gain (5.4);
169
170         srand (9);
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);
174                 }
175         }
176 }
177
178 /* copy_from */
179 BOOST_AUTO_TEST_CASE (audio_buffers_copy_from)
180 {
181         AudioBuffers a (5, 63711);
182         AudioBuffers b (5, 12345);
183
184         srand (42);
185         random_fill (a);
186
187         srand (99);
188         random_fill (b);
189
190         a.copy_from (&b, 517, 233, 194);
191
192         /* Re-seed a's generator and check the numbers that came from it */
193
194         /* First part; not copied-over */
195         srand (42);
196         random_check (a, 0, 194);
197
198         /* Second part; copied-over (just burn generator a's numbers) */
199         for (int i = 0; i < (517 * 5); ++i) {
200                 random_float ();
201         }
202
203         /* Third part; not copied-over */
204         random_check (a, 194 + 517, a.frames() - 194 - 517);
205
206         /* Re-seed b's generator and check the numbers that came from it */
207         srand (99);
208
209         /* First part; burn */
210         for (int i = 0; i < 194 * 5; ++i) {
211                 random_float ();
212         }
213
214         /* Second part; copied */
215         random_check (b, 194, 517);
216 }
217
218 /* move */
219 BOOST_AUTO_TEST_CASE (audio_buffers_move)
220 {
221         AudioBuffers buffers (7, 65536);
222
223         srand (84);
224         random_fill (buffers);
225
226         int const from = 888;
227         int const to = 666;
228         int const frames = 444;
229
230         buffers.move (from, to, frames);
231
232         /* Re-seed and check the un-moved parts */
233         srand (84);
234
235         random_check (buffers, 0, to);
236
237         /* Burn a few */
238         for (int i = 0; i < (from - to + frames) * 7; ++i) {
239                 random_float ();
240         }
241
242         random_check (buffers, from + frames, 65536 - frames - from);
243
244         /* Re-seed and check the moved part */
245         srand (84);
246
247         /* Burn a few */
248         for (int i = 0; i < from * 7; ++i) {
249                 random_float ();
250         }
251         
252         random_check (buffers, to, frames);
253 }
254
255 /** accumulate_channel */
256 BOOST_AUTO_TEST_CASE (audio_buffers_accumulate_channel)
257 {
258         AudioBuffers a (3, 256);
259         srand (38);
260         random_fill (a);
261         
262         AudioBuffers b (3, 256);
263         random_fill (b);
264
265         a.accumulate_channel (&b, 2, 1, 1.2);
266
267         srand (38);
268         for (int i = 0; i < 256; ++i) {
269                 for (int c = 0; c < 3; ++c) {
270                         float const A = random_float ();
271                         if (c == 1) {
272                                 BOOST_CHECK_CLOSE (a.data(c)[i], A + b.data(2)[i] * 1.2, tolerance);
273                         } else {
274                                 BOOST_CHECK_CLOSE (a.data(c)[i], A, tolerance);
275                         }
276                 }
277         }
278 }
279
280 /** accumulate_frames */
281 BOOST_AUTO_TEST_CASE (audio_buffers_accumulate_frames)
282 {
283         AudioBuffers a (3, 256);
284         srand (38);
285         random_fill (a);
286         
287         AudioBuffers b (3, 256);
288         random_fill (b);
289
290         a.accumulate_frames (&b, 91, 44, 129);
291
292         srand (38);
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);
298                         } else {
299                                 BOOST_CHECK_CLOSE (a.data(c)[i], A + b.data(c)[i + 91 - 44], tolerance);
300                         }
301                 }
302         }
303 }