1 /* test_streams - Simple test pattern generator
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2023 Xiph.Org Foundation
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "share/compat.h"
28 #if defined _MSC_VER || defined __MINGW32__
29 #include <time.h>
30 #else
31 #include <sys/time.h>
32 #endif
33 #include "FLAC/assert.h"
34 #include "FLAC/ordinals.h"
35 #include "share/compat.h"
36
37 #if !defined _MSC_VER && !defined __MINGW32__
38 #define GET_RANDOM_BYTE (((unsigned)random()) & 0xff)
39 #else
40 #define GET_RANDOM_BYTE (((unsigned)rand()) & 0xff)
41 #endif
42
43 static FLAC__bool is_big_endian_host;
44
45
write_little_endian_unsigned(FILE * f,FLAC__uint32 x,size_t bytes)46 static FLAC__bool write_little_endian_unsigned(FILE *f, FLAC__uint32 x, size_t bytes)
47 {
48 while(bytes) {
49 if(fputc(x, f) == EOF)
50 return false;
51 x >>= 8;
52 bytes--;
53 }
54 return true;
55 }
56
write_little_endian_signed(FILE * f,FLAC__int32 x,size_t bytes)57 static FLAC__bool write_little_endian_signed(FILE *f, FLAC__int32 x, size_t bytes)
58 {
59 return write_little_endian_unsigned(f, (FLAC__uint32) x, bytes);
60 }
61
write_little_endian_uint16(FILE * f,FLAC__uint16 x)62 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
63 {
64 return
65 fputc(x, f) != EOF &&
66 fputc(x >> 8, f) != EOF
67 ;
68 }
69
write_little_endian_int16(FILE * f,FLAC__int16 x)70 static FLAC__bool write_little_endian_int16(FILE *f, FLAC__int16 x)
71 {
72 return write_little_endian_uint16(f, (FLAC__uint16)x);
73 }
74
write_little_endian_uint24(FILE * f,FLAC__uint32 x)75 static FLAC__bool write_little_endian_uint24(FILE *f, FLAC__uint32 x)
76 {
77 return
78 fputc(x, f) != EOF &&
79 fputc(x >> 8, f) != EOF &&
80 fputc(x >> 16, f) != EOF
81 ;
82 }
83
write_little_endian_int24(FILE * f,FLAC__int32 x)84 static FLAC__bool write_little_endian_int24(FILE *f, FLAC__int32 x)
85 {
86 return write_little_endian_uint24(f, (FLAC__uint32)x);
87 }
88
write_little_endian_uint32(FILE * f,FLAC__uint32 x)89 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
90 {
91 return
92 fputc(x, f) != EOF &&
93 fputc(x >> 8, f) != EOF &&
94 fputc(x >> 16, f) != EOF &&
95 fputc(x >> 24, f) != EOF
96 ;
97 }
98
write_little_endian_int32(FILE * f,FLAC__int32 x)99 static FLAC__bool write_little_endian_int32(FILE *f, FLAC__int32 x)
100 {
101 return write_little_endian_uint32(f, (FLAC__uint32)x);
102 }
103
104 #if defined(_MSC_VER)
105 // silence 4 MSVC warnings 'conversion from 'FLAC__uint64' to 'int', possible loss of data'
106 #pragma warning ( disable : 4244 )
107 #endif
write_little_endian_uint64(FILE * f,FLAC__uint64 x)108 static FLAC__bool write_little_endian_uint64(FILE *f, FLAC__uint64 x)
109 {
110 return
111 fputc(x, f) != EOF &&
112 fputc(x >> 8, f) != EOF &&
113 fputc(x >> 16, f) != EOF &&
114 fputc(x >> 24, f) != EOF &&
115 fputc(x >> 32, f) != EOF &&
116 fputc(x >> 40, f) != EOF &&
117 fputc(x >> 48, f) != EOF &&
118 fputc(x >> 56, f) != EOF
119 ;
120 }
121 #if defined(_MSC_VER)
122 #pragma warning ( default : 4244 )
123 #endif
124
write_big_endian(FILE * f,FLAC__int32 x,size_t bytes)125 static FLAC__bool write_big_endian(FILE *f, FLAC__int32 x, size_t bytes)
126 {
127 if(bytes < 4)
128 x <<= 8*(4-bytes);
129 while(bytes) {
130 if(fputc(x>>24, f) == EOF)
131 return false;
132 x <<= 8;
133 bytes--;
134 }
135 return true;
136 }
137
write_big_endian_uint16(FILE * f,FLAC__uint16 x)138 static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 x)
139 {
140 return
141 fputc(x >> 8, f) != EOF &&
142 fputc(x, f) != EOF
143 ;
144 }
145
146 #if 0
147 /* @@@ not used (yet) */
148 static FLAC__bool write_big_endian_int16(FILE *f, FLAC__int16 x)
149 {
150 return write_big_endian_uint16(f, (FLAC__uint16)x);
151 }
152 #endif
153
154 #if 0
155 /* @@@ not used (yet) */
156 static FLAC__bool write_big_endian_uint24(FILE *f, FLAC__uint32 x)
157 {
158 return
159 fputc(x >> 16, f) != EOF &&
160 fputc(x >> 8, f) != EOF &&
161 fputc(x, f) != EOF
162 ;
163 }
164 #endif
165
166 #if 0
167 /* @@@ not used (yet) */
168 static FLAC__bool write_big_endian_int24(FILE *f, FLAC__int32 x)
169 {
170 return write_big_endian_uint24(f, (FLAC__uint32)x);
171 }
172 #endif
173
write_big_endian_uint32(FILE * f,FLAC__uint32 x)174 static FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 x)
175 {
176 return
177 fputc(x >> 24, f) != EOF &&
178 fputc(x >> 16, f) != EOF &&
179 fputc(x >> 8, f) != EOF &&
180 fputc(x, f) != EOF
181 ;
182 }
183
184 #if 0
185 /* @@@ not used (yet) */
186 static FLAC__bool write_big_endian_int32(FILE *f, FLAC__int32 x)
187 {
188 return write_big_endian_uint32(f, (FLAC__uint32)x);
189 }
190 #endif
191
write_sane_extended(FILE * f,unsigned val)192 static FLAC__bool write_sane_extended(FILE *f, unsigned val)
193 /* Write to 'f' a SANE extended representation of 'val'. Return false if
194 * the write succeeds; return true otherwise.
195 *
196 * SANE extended is an 80-bit IEEE-754 representation with sign bit, 15 bits
197 * of exponent, and 64 bits of significand (mantissa). Unlike most IEEE-754
198 * representations, it does not imply a 1 above the MSB of the significand.
199 *
200 * Preconditions:
201 * val!=0U
202 */
203 {
204 unsigned int shift, exponent;
205
206 FLAC__ASSERT(val!=0U); /* handling 0 would require a special case */
207
208 for(shift= 0U; (val>>(31-shift))==0U; ++shift)
209 ;
210 val<<= shift;
211 exponent= 63U-(shift+32U); /* add 32 for unused second word */
212
213 if(!write_big_endian_uint16(f, (FLAC__uint16)(exponent+0x3FFF)))
214 return false;
215 if(!write_big_endian_uint32(f, val))
216 return false;
217 if(!write_big_endian_uint32(f, 0)) /* unused second word */
218 return false;
219
220 return true;
221 }
222
223 /* a mono one-sample 16bps stream */
generate_01(void)224 static FLAC__bool generate_01(void)
225 {
226 FILE *f;
227 FLAC__int16 x = -32768;
228
229 if(0 == (f = fopen("test01.raw", "wb")))
230 return false;
231
232 if(!write_little_endian_int16(f, x))
233 goto foo;
234
235 fclose(f);
236 return true;
237 foo:
238 fclose(f);
239 return false;
240 }
241
242 /* a stereo one-sample 16bps stream */
generate_02(void)243 static FLAC__bool generate_02(void)
244 {
245 FILE *f;
246 FLAC__int16 xl = -32768, xr = 32767;
247
248 if(0 == (f = fopen("test02.raw", "wb")))
249 return false;
250
251 if(!write_little_endian_int16(f, xl))
252 goto foo;
253 if(!write_little_endian_int16(f, xr))
254 goto foo;
255
256 fclose(f);
257 return true;
258 foo:
259 fclose(f);
260 return false;
261 }
262
263 /* a mono five-sample 16bps stream */
generate_03(void)264 static FLAC__bool generate_03(void)
265 {
266 FILE *f;
267 FLAC__int16 x[] = { -25, 0, 25, 50, 100 };
268 unsigned i;
269
270 if(0 == (f = fopen("test03.raw", "wb")))
271 return false;
272
273 for(i = 0; i < 5; i++)
274 if(!write_little_endian_int16(f, x[i]))
275 goto foo;
276
277 fclose(f);
278 return true;
279 foo:
280 fclose(f);
281 return false;
282 }
283
284 /* a stereo five-sample 16bps stream */
generate_04(void)285 static FLAC__bool generate_04(void)
286 {
287 FILE *f;
288 FLAC__int16 x[] = { -25, 500, 0, 400, 25, 300, 50, 200, 100, 100 };
289 unsigned i;
290
291 if(0 == (f = fopen("test04.raw", "wb")))
292 return false;
293
294 for(i = 0; i < 10; i++)
295 if(!write_little_endian_int16(f, x[i]))
296 goto foo;
297
298 fclose(f);
299 return true;
300 foo:
301 fclose(f);
302 return false;
303 }
304
305 /* a mono full-scale deflection 8bps stream */
generate_fsd8(const char * fn,const int pattern[],unsigned reps)306 static FLAC__bool generate_fsd8(const char *fn, const int pattern[], unsigned reps)
307 {
308 FILE *f;
309 unsigned rep, p;
310
311 FLAC__ASSERT(pattern != 0);
312
313 if(0 == (f = fopen(fn, "wb")))
314 return false;
315
316 for(rep = 0; rep < reps; rep++) {
317 for(p = 0; pattern[p]; p++) {
318 signed char x = pattern[p] > 0? 127 : -128;
319 if(fwrite(&x, sizeof(x), 1, f) < 1)
320 goto foo;
321 }
322 }
323
324 fclose(f);
325 return true;
326 foo:
327 fclose(f);
328 return false;
329 }
330
331 /* a mono full-scale deflection 16bps stream */
generate_fsd16(const char * fn,const int pattern[],unsigned reps)332 static FLAC__bool generate_fsd16(const char *fn, const int pattern[], unsigned reps)
333 {
334 FILE *f;
335 unsigned rep, p;
336
337 FLAC__ASSERT(pattern != 0);
338
339 if(0 == (f = fopen(fn, "wb")))
340 return false;
341
342 for(rep = 0; rep < reps; rep++) {
343 for(p = 0; pattern[p]; p++) {
344 FLAC__int16 x = pattern[p] > 0? 32767 : -32768;
345 if(!write_little_endian_int16(f, x))
346 goto foo;
347 }
348 }
349
350 fclose(f);
351 return true;
352 foo:
353 fclose(f);
354 return false;
355 }
356
357 /* a stereo wasted-bits-per-sample 16bps stream */
generate_wbps16(const char * fn,unsigned samples)358 static FLAC__bool generate_wbps16(const char *fn, unsigned samples)
359 {
360 FILE *f;
361 unsigned sample;
362
363 if(0 == (f = fopen(fn, "wb")))
364 return false;
365
366 for(sample = 0; sample < samples; sample++) {
367 FLAC__int16 l = (sample % 2000) << 2;
368 FLAC__int16 r = (sample % 1000) << 3;
369 if(!write_little_endian_int16(f, l))
370 goto foo;
371 if(!write_little_endian_int16(f, r))
372 goto foo;
373 }
374
375 fclose(f);
376 return true;
377 foo:
378 fclose(f);
379 return false;
380 }
381
382 /* a mono full-scale deflection 24bps stream */
generate_fsd24(const char * fn,const int pattern[],unsigned reps)383 static FLAC__bool generate_fsd24(const char *fn, const int pattern[], unsigned reps)
384 {
385 FILE *f;
386 unsigned rep, p;
387
388 FLAC__ASSERT(pattern != 0);
389
390 if(0 == (f = fopen(fn, "wb")))
391 return false;
392
393 for(rep = 0; rep < reps; rep++) {
394 for(p = 0; pattern[p]; p++) {
395 FLAC__int32 x = pattern[p] > 0? 8388607 : -8388608;
396 if(!write_little_endian_int24(f, x))
397 goto foo;
398 }
399 }
400
401 fclose(f);
402 return true;
403 foo:
404 fclose(f);
405 return false;
406 }
407
408 /* a mono full-scale deflection 32bps stream */
generate_fsd32(const char * fn,const int pattern[],unsigned reps)409 static FLAC__bool generate_fsd32(const char *fn, const int pattern[], unsigned reps)
410 {
411 FILE *f;
412 unsigned rep, p;
413
414 FLAC__ASSERT(pattern != 0);
415
416 if(0 == (f = fopen(fn, "wb")))
417 return false;
418
419 for(rep = 0; rep < reps; rep++) {
420 for(p = 0; pattern[p]; p++) {
421 FLAC__int32 x = pattern[p] > 0? 2147483647 : -2147483648;
422 if(!write_little_endian_int32(f, x))
423 goto foo;
424 }
425 }
426
427 fclose(f);
428 return true;
429 foo:
430 fclose(f);
431 return false;
432 }
433
434 /* a mono sine-wave 8bps stream */
generate_sine8_1(const char * fn,const double sample_rate,const unsigned samples,const double f1,const double a1,const double f2,const double a2)435 static FLAC__bool generate_sine8_1(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2)
436 {
437 const FLAC__int8 full_scale = 127;
438 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
439 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
440 FILE *f;
441 double theta1, theta2;
442 unsigned i;
443
444 if(0 == (f = fopen(fn, "wb")))
445 return false;
446
447 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
448 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
449 FLAC__int8 v = (FLAC__int8)(val + 0.5);
450 if(fwrite(&v, sizeof(v), 1, f) < 1)
451 goto foo;
452 }
453
454 fclose(f);
455 return true;
456 foo:
457 fclose(f);
458 return false;
459 }
460
461 /* a stereo sine-wave 8bps stream */
generate_sine8_2(const char * fn,const double sample_rate,const unsigned samples,const double f1,const double a1,const double f2,const double a2,double fmult)462 static FLAC__bool generate_sine8_2(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2, double fmult)
463 {
464 const FLAC__int8 full_scale = 127;
465 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
466 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
467 FILE *f;
468 double theta1, theta2;
469 unsigned i;
470
471 if(0 == (f = fopen(fn, "wb")))
472 return false;
473
474 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
475 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
476 FLAC__int8 v = (FLAC__int8)(val + 0.5);
477 if(fwrite(&v, sizeof(v), 1, f) < 1)
478 goto foo;
479 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
480 v = (FLAC__int8)(val + 0.5);
481 if(fwrite(&v, sizeof(v), 1, f) < 1)
482 goto foo;
483 }
484
485 fclose(f);
486 return true;
487 foo:
488 fclose(f);
489 return false;
490 }
491
492 /* a mono sine-wave 16bps stream */
generate_sine16_1(const char * fn,const double sample_rate,const unsigned samples,const double f1,const double a1,const double f2,const double a2)493 static FLAC__bool generate_sine16_1(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2)
494 {
495 const FLAC__int16 full_scale = 32767;
496 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
497 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
498 FILE *f;
499 double theta1, theta2;
500 unsigned i;
501
502 if(0 == (f = fopen(fn, "wb")))
503 return false;
504
505 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
506 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
507 FLAC__int16 v = (FLAC__int16)(val + 0.5);
508 if(!write_little_endian_int16(f, v))
509 goto foo;
510 }
511
512 fclose(f);
513 return true;
514 foo:
515 fclose(f);
516 return false;
517 }
518
519 /* a stereo sine-wave 16bps stream */
generate_sine16_2(const char * fn,const double sample_rate,const unsigned samples,const double f1,const double a1,const double f2,const double a2,double fmult)520 static FLAC__bool generate_sine16_2(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2, double fmult)
521 {
522 const FLAC__int16 full_scale = 32767;
523 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
524 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
525 FILE *f;
526 double theta1, theta2;
527 unsigned i;
528
529 if(0 == (f = fopen(fn, "wb")))
530 return false;
531
532 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
533 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
534 FLAC__int16 v = (FLAC__int16)(val + 0.5);
535 if(!write_little_endian_int16(f, v))
536 goto foo;
537 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
538 v = (FLAC__int16)(val + 0.5);
539 if(!write_little_endian_int16(f, v))
540 goto foo;
541 }
542
543 fclose(f);
544 return true;
545 foo:
546 fclose(f);
547 return false;
548 }
549
550 /* a mono sine-wave 24bps stream */
generate_sine24_1(const char * fn,const double sample_rate,const unsigned samples,const double f1,const double a1,const double f2,const double a2)551 static FLAC__bool generate_sine24_1(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2)
552 {
553 const FLAC__int32 full_scale = 0x7fffff;
554 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
555 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
556 FILE *f;
557 double theta1, theta2;
558 unsigned i;
559
560 if(0 == (f = fopen(fn, "wb")))
561 return false;
562
563 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
564 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
565 FLAC__int32 v = (FLAC__int32)(val + 0.5);
566 if(!write_little_endian_int24(f, v))
567 goto foo;
568 }
569
570 fclose(f);
571 return true;
572 foo:
573 fclose(f);
574 return false;
575 }
576
577 /* a stereo sine-wave 24bps stream */
generate_sine24_2(const char * fn,const double sample_rate,const unsigned samples,const double f1,const double a1,const double f2,const double a2,double fmult)578 static FLAC__bool generate_sine24_2(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2, double fmult)
579 {
580 const FLAC__int32 full_scale = 0x7fffff;
581 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
582 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
583 FILE *f;
584 double theta1, theta2;
585 unsigned i;
586
587 if(0 == (f = fopen(fn, "wb")))
588 return false;
589
590 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
591 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
592 FLAC__int32 v = (FLAC__int32)(val + 0.5);
593 if(!write_little_endian_int24(f, v))
594 goto foo;
595 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
596 v = (FLAC__int32)(val + 0.5);
597 if(!write_little_endian_int24(f, v))
598 goto foo;
599 }
600
601 fclose(f);
602 return true;
603 foo:
604 fclose(f);
605 return false;
606 }
607
608 /* a mono sine-wave 32bps stream */
generate_sine32_1(const char * fn,const double sample_rate,const unsigned samples,const double f1,const double a1,const double f2,const double a2)609 static FLAC__bool generate_sine32_1(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2)
610 {
611 const FLAC__int32 full_scale = 0x7fffffff;
612 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
613 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
614 FILE *f;
615 double theta1, theta2;
616 unsigned i;
617
618 if(0 == (f = fopen(fn, "wb")))
619 return false;
620
621 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
622 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
623 FLAC__int32 v = (FLAC__int32)(val + 0.5);
624 if(!write_little_endian_int32(f, v))
625 goto foo;
626 }
627
628 fclose(f);
629 return true;
630 foo:
631 fclose(f);
632 return false;
633 }
634
635 /* a stereo sine-wave 32bps stream */
generate_sine32_2(const char * fn,const double sample_rate,const unsigned samples,const double f1,const double a1,const double f2,const double a2,double fmult)636 static FLAC__bool generate_sine32_2(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2, double fmult)
637 {
638 const FLAC__int32 full_scale = 0x7fffffff;
639 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
640 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
641 FILE *f;
642 double theta1, theta2;
643 unsigned i;
644
645 if(0 == (f = fopen(fn, "wb")))
646 return false;
647
648 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
649 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
650 FLAC__int32 v = (FLAC__int32)(val + 0.5);
651 if(!write_little_endian_int32(f, v))
652 goto foo;
653 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
654 v = (FLAC__int32)(val + 0.5);
655 if(!write_little_endian_int32(f, v))
656 goto foo;
657 }
658
659 fclose(f);
660 return true;
661 foo:
662 fclose(f);
663 return false;
664 }
665
generate_noise(const char * fn,unsigned bytes)666 static FLAC__bool generate_noise(const char *fn, unsigned bytes)
667 {
668 FILE *f;
669 unsigned b;
670
671 if(0 == (f = fopen(fn, "wb")))
672 return false;
673
674 for(b = 0; b < bytes; b++) {
675 #if !defined _MSC_VER && !defined __MINGW32__
676 FLAC__byte x = (FLAC__byte)(((unsigned)random()) & 0xff);
677 #else
678 FLAC__byte x = (FLAC__byte)(((unsigned)rand()) & 0xff);
679 #endif
680 if(fwrite(&x, sizeof(x), 1, f) < 1)
681 goto foo;
682 }
683
684 fclose(f);
685 return true;
686 foo:
687 fclose(f);
688 return false;
689 }
690
generate_signed_raw(const char * filename,unsigned channels,unsigned bytes_per_sample,unsigned samples)691 static FLAC__bool generate_signed_raw(const char *filename, unsigned channels, unsigned bytes_per_sample, unsigned samples)
692 {
693 const FLAC__int32 full_scale = (1 << (bytes_per_sample*8-1)) - 1;
694 const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
695 const double delta1 = 2.0 * M_PI / ( 44100.0 / f1);
696 const double delta2 = 2.0 * M_PI / ( 44100.0 / f2);
697 double theta1, theta2;
698 FILE *f;
699 unsigned i, j;
700
701 if(0 == (f = fopen(filename, "wb")))
702 return false;
703
704 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
705 for(j = 0; j < channels; j++) {
706 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
707 FLAC__int32 v = (FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8);
708 if(!write_little_endian_signed(f, v, bytes_per_sample))
709 goto foo;
710 }
711 }
712
713 fclose(f);
714 return true;
715 foo:
716 fclose(f);
717 return false;
718 }
719
generate_unsigned_raw(const char * filename,unsigned channels,unsigned bytes_per_sample,unsigned samples)720 static FLAC__bool generate_unsigned_raw(const char *filename, unsigned channels, unsigned bytes_per_sample, unsigned samples)
721 {
722 const FLAC__int32 full_scale = (1 << (bytes_per_sample*8-1)) - 1;
723 const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
724 const double delta1 = 2.0 * M_PI / ( 44100.0 / f1);
725 const double delta2 = 2.0 * M_PI / ( 44100.0 / f2);
726 const double half_scale = 0.5 * full_scale;
727 double theta1, theta2;
728 FILE *f;
729 unsigned i, j;
730
731 if(0 == (f = fopen(filename, "wb")))
732 return false;
733
734 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
735 for(j = 0; j < channels; j++) {
736 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
737 FLAC__int32 v = (FLAC__int32)(half_scale + val + 0.5) + ((GET_RANDOM_BYTE>>4)-8);
738 if(!write_little_endian_unsigned(f, v, bytes_per_sample))
739 goto foo;
740 }
741 }
742
743 fclose(f);
744 return true;
745 foo:
746 fclose(f);
747 return false;
748 }
749
generate_aiff(const char * filename,unsigned sample_rate,unsigned channels,unsigned bps,unsigned samples)750 static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples)
751 {
752 const unsigned bytes_per_sample = (bps+7)/8;
753 const unsigned true_size = channels * bytes_per_sample * samples;
754 const unsigned padded_size = (true_size + 1) & (~1u);
755 const unsigned shift = (bps%8)? 8 - (bps%8) : 0;
756 const FLAC__int32 full_scale = (1 << (bps-1)) - 1;
757 const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
758 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
759 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
760 double theta1, theta2;
761 FILE *f;
762 unsigned i, j;
763
764 if(0 == (f = fopen(filename, "wb")))
765 return false;
766 if(fwrite("FORM", 1, 4, f) < 4)
767 goto foo;
768 if(!write_big_endian_uint32(f, padded_size + 46))
769 goto foo;
770 if(fwrite("AIFFCOMM\000\000\000\022", 1, 12, f) < 12)
771 goto foo;
772 if(!write_big_endian_uint16(f, (FLAC__uint16)channels))
773 goto foo;
774 if(!write_big_endian_uint32(f, samples))
775 goto foo;
776 if(!write_big_endian_uint16(f, (FLAC__uint16)bps))
777 goto foo;
778 if(!write_sane_extended(f, sample_rate))
779 goto foo;
780 if(fwrite("SSND", 1, 4, f) < 4)
781 goto foo;
782 if(!write_big_endian_uint32(f, true_size + 8))
783 goto foo;
784 if(fwrite("\000\000\000\000\000\000\000\000", 1, 8, f) < 8)
785 goto foo;
786
787 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
788 for(j = 0; j < channels; j++) {
789 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
790 FLAC__int32 v = ((FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8)) << shift;
791 if(!write_big_endian(f, v, bytes_per_sample))
792 goto foo;
793 }
794 }
795 for(i = true_size; i < padded_size; i++)
796 if(fputc(0, f) == EOF)
797 goto foo;
798
799 fclose(f);
800 return true;
801 foo:
802 fclose(f);
803 return false;
804 }
805
806 /* flavor is: 0:WAVE, 1:RF64, 2:WAVE64 */
generate_wav(const char * filename,unsigned sample_rate,unsigned channels,unsigned bps,unsigned samples,FLAC__bool strict,int flavor)807 static FLAC__bool generate_wav(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples, FLAC__bool strict, int flavor)
808 {
809 const FLAC__bool waveformatextensible = strict && (channels > 2 || (bps != 8 && bps != 16));
810
811 const unsigned bytes_per_sample = (bps+7)/8;
812 const unsigned shift = (bps%8)? 8 - (bps%8) : 0;
813 /* this rig is not going over 4G so we're ok with 32-bit sizes here */
814 const FLAC__uint32 true_size = channels * bytes_per_sample * samples;
815 const FLAC__uint32 padded_size = flavor<2? (true_size + 1) & (~1u) : (true_size + 7) & (~7u);
816 const FLAC__int32 full_scale = (1 << (bps-1)) - 1;
817 const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
818 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
819 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
820 double theta1, theta2;
821 FILE *f;
822 unsigned i, j;
823
824 if(0 == (f = fopen(filename, "wb")))
825 return false;
826 /* RIFFxxxxWAVE or equivalent: */
827 switch(flavor) {
828 case 0:
829 if(fwrite("RIFF", 1, 4, f) < 4)
830 goto foo;
831 /* +4 for WAVE */
832 /* +8+{40,16} for fmt chunk */
833 /* +8 for data chunk header */
834 if(!write_little_endian_uint32(f, 4 + 8+(waveformatextensible?40:16) + 8 + padded_size))
835 goto foo;
836 if(fwrite("WAVE", 1, 4, f) < 4)
837 goto foo;
838 break;
839 case 1:
840 if(fwrite("RF64", 1, 4, f) < 4)
841 goto foo;
842 if(!write_little_endian_uint32(f, 0xffffffff))
843 goto foo;
844 if(fwrite("WAVE", 1, 4, f) < 4)
845 goto foo;
846 break;
847 case 2:
848 /* RIFF GUID 66666972-912E-11CF-A5D6-28DB04C10000 */
849 if(fwrite("\x72\x69\x66\x66\x2E\x91\xCF\x11\xA5\xD6\x28\xDB\x04\xC1\x00\x00", 1, 16, f) < 16)
850 goto foo;
851 /* +(16+8) for RIFF GUID + size */
852 /* +16 for WAVE GUID */
853 /* +16+8+{40,16} for fmt chunk */
854 /* +16+8 for data chunk header */
855 if(!write_little_endian_uint64(f, (16+8) + 16 + 16+8+(waveformatextensible?40:16) + (16+8) + padded_size))
856 goto foo;
857 /* WAVE GUID 65766177-ACF3-11D3-8CD1-00C04F8EDB8A */
858 if(fwrite("\x77\x61\x76\x65\xF3\xAC\xD3\x11\x8C\xD1\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) < 16)
859 goto foo;
860 break;
861 default:
862 goto foo;
863 }
864 if(flavor == 1) { /* rf64 */
865 if(fwrite("ds64", 1, 4, f) < 4)
866 goto foo;
867 if(!write_little_endian_uint32(f, 28)) /* ds64 chunk size */
868 goto foo;
869 if(!write_little_endian_uint64(f, 36 + padded_size + (waveformatextensible?60:36)))
870 goto foo;
871 if(!write_little_endian_uint64(f, true_size))
872 goto foo;
873 if(!write_little_endian_uint64(f, samples))
874 goto foo;
875 if(!write_little_endian_uint32(f, 0)) /* table size */
876 goto foo;
877 }
878 /* fmt chunk */
879 if(flavor < 2) {
880 if(fwrite("fmt ", 1, 4, f) < 4)
881 goto foo;
882 /* chunk size */
883 if(!write_little_endian_uint32(f, waveformatextensible?40:16))
884 goto foo;
885 }
886 else { /* wave64 */
887 /* fmt GUID 20746D66-ACF3-11D3-8CD1-00C04F8EDB8A */
888 if(fwrite("\x66\x6D\x74\x20\xF3\xAC\xD3\x11\x8C\xD1\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) < 16)
889 goto foo;
890 /* chunk size (+16+8 for GUID and size fields) */
891 if(!write_little_endian_uint64(f, 16+8+(waveformatextensible?40:16)))
892 goto foo;
893 }
894 if(!write_little_endian_uint16(f, (FLAC__uint16)(waveformatextensible?65534:1)))
895 goto foo;
896 if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
897 goto foo;
898 if(!write_little_endian_uint32(f, sample_rate))
899 goto foo;
900 if(!write_little_endian_uint32(f, sample_rate * channels * bytes_per_sample))
901 goto foo;
902 if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * bytes_per_sample))) /* block align */
903 goto foo;
904 if(!write_little_endian_uint16(f, (FLAC__uint16)(bps+shift)))
905 goto foo;
906 if(waveformatextensible) {
907 if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
908 goto foo;
909 if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
910 goto foo;
911 if(!write_little_endian_uint32(f, 0)) /* channelMask */
912 goto foo;
913 /* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
914 if(fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
915 goto foo;
916 }
917 /* data chunk */
918 if(flavor < 2) {
919 if(fwrite("data", 1, 4, f) < 4)
920 goto foo;
921 if(!write_little_endian_uint32(f, flavor==1? 0xffffffff : true_size))
922 goto foo;
923 }
924 else { /* wave64 */
925 /* data GUID 61746164-ACF3-11D3-8CD1-00C04F8EDB8A */
926 if(fwrite("\x64\x61\x74\x61\xF3\xAC\xD3\x11\x8C\xD1\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) != 16)
927 goto foo;
928 /* +16+8 for GUID and size fields */
929 if(!write_little_endian_uint64(f, 16+8 + true_size))
930 goto foo;
931 }
932
933 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
934 for(j = 0; j < channels; j++) {
935 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
936 FLAC__int32 v = ((FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8)) << shift;
937 if(!write_little_endian_signed(f, v, bytes_per_sample))
938 goto foo;
939 }
940 }
941 for(i = true_size; i < padded_size; i++)
942 if(fputc(0, f) == EOF)
943 goto foo;
944
945 fclose(f);
946 return true;
947 foo:
948 fclose(f);
949 return false;
950 }
951
generate_wackywavs(void)952 static FLAC__bool generate_wackywavs(void)
953 {
954 FILE *f;
955 FLAC__byte wav[] = {
956 'R', 'I', 'F', 'F', 76, 0, 0, 0,
957 'W', 'A', 'V', 'E', 'j', 'u', 'n', 'k',
958 4, 0, 0, 0 , 'b', 'l', 'a', 'h',
959 'p', 'a', 'd', ' ', 4, 0, 0, 0,
960 'B', 'L', 'A', 'H', 'f', 'm', 't', ' ',
961 16, 0, 0, 0, 1, 0, 1, 0,
962 0x44,0xAC, 0, 0,0x88,0x58,0x01, 0,
963 2, 0, 16, 0, 'd', 'a', 't', 'a',
964 16, 0, 0, 0, 0, 0, 1, 0,
965 4, 0, 9, 0, 16, 0, 25, 0,
966 36, 0, 49, 0, 'p', 'a', 'd', ' ',
967 4, 0, 0, 0, 'b', 'l', 'a', 'h'
968 };
969
970 if(0 == (f = fopen("wacky1.wav", "wb")))
971 return false;
972 if(fwrite(wav, 1, 84, f) < 84)
973 goto foo;
974 fclose(f);
975
976 wav[4] += 12;
977 if(0 == (f = fopen("wacky2.wav", "wb")))
978 return false;
979 if(fwrite(wav, 1, 96, f) < 96)
980 goto foo;
981 fclose(f);
982
983 return true;
984 foo:
985 fclose(f);
986 return false;
987 }
988
write_simple_wavex_header(FILE * f,unsigned samplerate,unsigned channels,unsigned bytespersample,unsigned frames)989 static FLAC__bool write_simple_wavex_header (FILE * f, unsigned samplerate, unsigned channels, unsigned bytespersample, unsigned frames)
990 {
991 unsigned datalen = channels * bytespersample * frames ;
992
993 if (fwrite("RIFF", 1, 4, f) != 4)
994 return false;
995 if (!write_little_endian_uint32(f, 60 + datalen))
996 return false;
997
998 if (fwrite("WAVEfmt ", 8, 1, f) != 1)
999 return false;
1000 if (!write_little_endian_uint32(f, 40))
1001 return false;
1002
1003 if(!write_little_endian_uint16(f, 65534)) /* WAVEFORMATEXTENSIBLE tag */
1004 return false;
1005 if(!write_little_endian_uint16(f, channels))
1006 return false;
1007 if(!write_little_endian_uint32(f, samplerate))
1008 return false;
1009 if(!write_little_endian_uint32(f, samplerate * channels * bytespersample))
1010 return false;
1011 if(!write_little_endian_uint16(f, channels * bytespersample)) /* block align */
1012 return false;
1013 if(!write_little_endian_uint16(f, bytespersample * 8))
1014 return false;
1015
1016 if(!write_little_endian_uint16(f, 22)) /* cbSize */
1017 return false;
1018 if(!write_little_endian_uint16(f, bytespersample * 8)) /* validBitsPerSample */
1019 return false;
1020 if(!write_little_endian_uint32(f, 0)) /* channelMask */
1021 return false;
1022 /* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
1023 if(fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
1024 return false;
1025
1026 if (fwrite("data", 1, 4, f) != 4)
1027 return false;
1028 if (!write_little_endian_uint32(f, datalen))
1029 return false;
1030
1031 return true;
1032 }
1033
generate_noisy_sine(void)1034 static FLAC__bool generate_noisy_sine(void)
1035 {
1036 FILE *f;
1037 int64_t randstate = 0x1243456;
1038 double sample, last_val = 0.0;
1039 int k;
1040 int seconds = 300;
1041
1042 if(0 == (f = fopen("noisy-sine.wav", "wb")))
1043 return false;
1044
1045 if(!write_simple_wavex_header (f, 44100, 1, 2, 44100*seconds))
1046 goto foo;
1047
1048 for (k = 0 ; k < seconds * 44100 ; k++) {
1049 /* Obvioulsy not a crypto quality RNG. */
1050 randstate = 11117 * randstate + 211231;
1051 randstate = 11117 * randstate + 211231;
1052 randstate = 11117 * randstate + 211231;
1053
1054 sample = ((int32_t) randstate) / (0x7fffffff * 1.000001);
1055 sample = 0.2 * sample - 0.9 * last_val;
1056
1057 last_val = sample;
1058
1059 sample += sin (2.0 * k * M_PI * 1.0 / 32.0);
1060 sample *= 0.4;
1061 #if !defined _MSC_VER
1062 write_little_endian_int16(f, lrintf(sample * 32700.0));
1063 #else
1064 write_little_endian_int16(f, (FLAC__int16)(sample * 32700.0));
1065 #endif
1066 };
1067
1068 fclose(f);
1069
1070 return true;
1071 foo:
1072 fclose(f);
1073 return false;
1074 }
1075
generate_wackywav64s(void)1076 static FLAC__bool generate_wackywav64s(void)
1077 {
1078 FILE *f;
1079 FLAC__byte wav[] = {
1080 0x72,0x69,0x66,0x66,0x2E,0x91,0xCF,0x11, /* RIFF GUID */
1081 0xA5,0xD6,0x28,0xDB,0x04,0xC1,0x00,0x00,
1082 152, 0, 0, 0, 0, 0, 0, 0,
1083 0x77,0x61,0x76,0x65,0xF3,0xAC,0xD3,0x11, /* WAVE GUID */
1084 0x8C,0xD1,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
1085 0x6A,0x75,0x6E,0x6B,0xF3,0xAC,0xD3,0x11, /* junk GUID */
1086 0x8C,0xD1,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
1087 32, 0, 0, 0 , 0, 0, 0, 0,
1088 'b', 'l', 'a', 'h', 'b', 'l', 'a', 'h',
1089 0x66,0x6D,0x74,0x20,0xF3,0xAC,0xD3,0x11, /* fmt GUID */
1090 0x8C,0xD1,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
1091 40, 0, 0, 0 , 0, 0, 0, 0,
1092 1, 0, 1, 0,0x44,0xAC, 0, 0,
1093 0x88,0x58,0x01, 0, 2, 0, 16, 0,
1094 0x64,0x61,0x74,0x61,0xF3,0xAC,0xD3,0x11, /* data GUID */
1095 0x8C,0xD1,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
1096 40, 0, 0, 0 , 0, 0, 0, 0,
1097 0, 0, 1, 0, 4, 0, 9, 0,
1098 16, 0, 25, 0, 36, 0, 49, 0,
1099 0x6A,0x75,0x6E,0x6B,0xF3,0xAC,0xD3,0x11, /* junk GUID */
1100 0x8C,0xD1,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
1101 32, 0, 0, 0 , 0, 0, 0, 0,
1102 'b', 'l', 'a', 'h', 'b', 'l', 'a', 'h'
1103 };
1104
1105 if(0 == (f = fopen("wacky1.w64", "wb")))
1106 return false;
1107 if(fwrite(wav, 1, wav[16], f) < wav[16])
1108 goto foo;
1109 fclose(f);
1110
1111 wav[16] += 32;
1112 if(0 == (f = fopen("wacky2.w64", "wb")))
1113 return false;
1114 if(fwrite(wav, 1, wav[16], f) < wav[16])
1115 goto foo;
1116 fclose(f);
1117
1118 return true;
1119 foo:
1120 fclose(f);
1121 return false;
1122 }
1123
generate_wackyrf64s(void)1124 static FLAC__bool generate_wackyrf64s(void)
1125 {
1126 FILE *f;
1127 FLAC__byte wav[] = {
1128 'R', 'F', '6', '4', 255, 255, 255, 255,
1129 'W', 'A', 'V', 'E', 'd', 's', '6', '4',
1130 28, 0, 0, 0, 112, 0, 0, 0,
1131 0, 0, 0, 0, 16, 0, 0, 0,
1132 0, 0, 0, 0, 8, 0, 0, 0,
1133 0, 0, 0, 0, 0, 0, 0, 0,
1134 'j', 'u', 'n', 'k',
1135 4, 0, 0, 0, 'b', 'l', 'a', 'h',
1136 'p', 'a', 'd', ' ', 4, 0, 0, 0,
1137 'B', 'L', 'A', 'H', 'f', 'm', 't', ' ',
1138 16, 0, 0, 0, 1, 0, 1, 0,
1139 0x44,0xAC, 0, 0,0x88,0x58,0x01, 0,
1140 2, 0, 16, 0, 'd', 'a', 't', 'a',
1141 255, 255, 255, 255, 0, 0, 1, 0,
1142 4, 0, 9, 0, 16, 0, 25, 0,
1143 36, 0, 49, 0, 'p', 'a', 'd', ' ',
1144 4, 0, 0, 0, 'b', 'l', 'a', 'h'
1145 };
1146
1147 if(0 == (f = fopen("wacky1.rf64", "wb")))
1148 return false;
1149 if(fwrite(wav, 1, 120, f) < 120)
1150 goto foo;
1151 fclose(f);
1152
1153 wav[20] += 12;
1154 if(0 == (f = fopen("wacky2.rf64", "wb")))
1155 return false;
1156 if(fwrite(wav, 1, 132, f) < 132)
1157 goto foo;
1158 fclose(f);
1159
1160 return true;
1161 foo:
1162 fclose(f);
1163 return false;
1164 }
1165
generate_replaygain_tone(unsigned samplerate)1166 static FLAC__bool generate_replaygain_tone (unsigned samplerate)
1167 {
1168 FILE *f;
1169 char fname [256] ;
1170 double tone, sample, samplerange;
1171 int k;
1172
1173 flac_snprintf(fname, sizeof(fname), "rpg-tone-%u.wav", samplerate);
1174
1175 if(0 == (f = fopen(fname, "wb")))
1176 return false;
1177
1178 if(!write_simple_wavex_header (f, samplerate, 1, 3, 220500))
1179 goto foo;
1180
1181
1182 samplerange = 0x7fffff; /* Largest sample value allowed for a 24 bit PCM file. */
1183 tone = 1000.0; /* 1 kHz */
1184
1185 for (k = 0 ; k < 5 * 44100 ; k++) {
1186 sample = sin(2 * M_PI * tone * k / samplerate);
1187 sample *= samplerange;
1188 if (!write_little_endian_uint24(f, (FLAC__int32) sample))
1189 goto foo;
1190 };
1191
1192 fclose(f);
1193
1194 return true;
1195 foo:
1196 fclose(f);
1197 return false;
1198 }
1199
main(int argc,char * argv[])1200 int main(int argc, char *argv[])
1201 {
1202 FLAC__uint32 test = 1;
1203 unsigned channels;
1204
1205 int pattern01[] = { 1, -1, 0 };
1206 int pattern02[] = { 1, 1, -1, 0 };
1207 int pattern03[] = { 1, -1, -1, 0 };
1208 int pattern04[] = { 1, -1, 1, -1, 0 };
1209 int pattern05[] = { 1, -1, -1, 1, 0 };
1210 int pattern06[] = { 1, -1, 1, 1, -1, 0 };
1211 int pattern07[] = { 1, -1, -1, 1, -1, 0 };
1212
1213 (void)argc;
1214 (void)argv;
1215 is_big_endian_host = (*((FLAC__byte*)(&test)))? false : true;
1216
1217 #if !defined _MSC_VER && !defined __MINGW32__
1218 {
1219 struct timeval tv;
1220
1221 if(gettimeofday(&tv, 0) < 0) {
1222 fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
1223 tv.tv_usec = 4321;
1224 }
1225 srandom(tv.tv_usec);
1226 }
1227 #else
1228 srand((unsigned)time(0));
1229 #endif
1230
1231 if(!generate_01()) return 1;
1232 if(!generate_02()) return 1;
1233 if(!generate_03()) return 1;
1234 if(!generate_04()) return 1;
1235
1236 if(!generate_fsd8("fsd8-01.raw", pattern01, 100)) return 1;
1237 if(!generate_fsd8("fsd8-02.raw", pattern02, 100)) return 1;
1238 if(!generate_fsd8("fsd8-03.raw", pattern03, 100)) return 1;
1239 if(!generate_fsd8("fsd8-04.raw", pattern04, 100)) return 1;
1240 if(!generate_fsd8("fsd8-05.raw", pattern05, 100)) return 1;
1241 if(!generate_fsd8("fsd8-06.raw", pattern06, 100)) return 1;
1242 if(!generate_fsd8("fsd8-07.raw", pattern07, 100)) return 1;
1243
1244 if(!generate_fsd16("fsd16-01.raw", pattern01, 100)) return 1;
1245 if(!generate_fsd16("fsd16-02.raw", pattern02, 100)) return 1;
1246 if(!generate_fsd16("fsd16-03.raw", pattern03, 100)) return 1;
1247 if(!generate_fsd16("fsd16-04.raw", pattern04, 100)) return 1;
1248 if(!generate_fsd16("fsd16-05.raw", pattern05, 100)) return 1;
1249 if(!generate_fsd16("fsd16-06.raw", pattern06, 100)) return 1;
1250 if(!generate_fsd16("fsd16-07.raw", pattern07, 100)) return 1;
1251
1252 if(!generate_fsd24("fsd24-01.raw", pattern01, 100)) return 1;
1253 if(!generate_fsd24("fsd24-02.raw", pattern02, 100)) return 1;
1254 if(!generate_fsd24("fsd24-03.raw", pattern03, 100)) return 1;
1255 if(!generate_fsd24("fsd24-04.raw", pattern04, 100)) return 1;
1256 if(!generate_fsd24("fsd24-05.raw", pattern05, 100)) return 1;
1257 if(!generate_fsd24("fsd24-06.raw", pattern06, 100)) return 1;
1258 if(!generate_fsd24("fsd24-07.raw", pattern07, 100)) return 1;
1259
1260 if(!generate_fsd32("fsd32-01.raw", pattern01, 100)) return 1;
1261 if(!generate_fsd32("fsd32-02.raw", pattern02, 100)) return 1;
1262 if(!generate_fsd32("fsd32-03.raw", pattern03, 100)) return 1;
1263 if(!generate_fsd32("fsd32-04.raw", pattern04, 100)) return 1;
1264 if(!generate_fsd32("fsd32-05.raw", pattern05, 100)) return 1;
1265 if(!generate_fsd32("fsd32-06.raw", pattern06, 100)) return 1;
1266 if(!generate_fsd32("fsd32-07.raw", pattern07, 100)) return 1;
1267
1268 if(!generate_wbps16("wbps16-01.raw", 1000)) return 1;
1269
1270 if(!generate_sine8_1("sine8-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
1271 if(!generate_sine8_1("sine8-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
1272 if(!generate_sine8_1("sine8-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
1273 if(!generate_sine8_1("sine8-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
1274 if(!generate_sine8_1("sine8-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
1275
1276 if(!generate_sine8_2("sine8-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
1277 if(!generate_sine8_2("sine8-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
1278 if(!generate_sine8_2("sine8-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
1279 if(!generate_sine8_2("sine8-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
1280 if(!generate_sine8_2("sine8-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
1281 if(!generate_sine8_2("sine8-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
1282 if(!generate_sine8_2("sine8-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
1283 if(!generate_sine8_2("sine8-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
1284 if(!generate_sine8_2("sine8-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
1285 if(!generate_sine8_2("sine8-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
1286
1287 if(!generate_sine16_1("sine16-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
1288 if(!generate_sine16_1("sine16-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
1289 if(!generate_sine16_1("sine16-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
1290 if(!generate_sine16_1("sine16-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
1291 if(!generate_sine16_1("sine16-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
1292
1293 if(!generate_sine16_2("sine16-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
1294 if(!generate_sine16_2("sine16-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
1295 if(!generate_sine16_2("sine16-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
1296 if(!generate_sine16_2("sine16-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
1297 if(!generate_sine16_2("sine16-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
1298 if(!generate_sine16_2("sine16-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
1299 if(!generate_sine16_2("sine16-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
1300 if(!generate_sine16_2("sine16-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
1301 if(!generate_sine16_2("sine16-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
1302 if(!generate_sine16_2("sine16-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
1303
1304 if(!generate_sine24_1("sine24-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
1305 if(!generate_sine24_1("sine24-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
1306 if(!generate_sine24_1("sine24-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
1307 if(!generate_sine24_1("sine24-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
1308 if(!generate_sine24_1("sine24-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
1309
1310 if(!generate_sine24_2("sine24-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
1311 if(!generate_sine24_2("sine24-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
1312 if(!generate_sine24_2("sine24-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
1313 if(!generate_sine24_2("sine24-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
1314 if(!generate_sine24_2("sine24-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
1315 if(!generate_sine24_2("sine24-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
1316 if(!generate_sine24_2("sine24-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
1317 if(!generate_sine24_2("sine24-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
1318 if(!generate_sine24_2("sine24-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
1319 if(!generate_sine24_2("sine24-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
1320
1321 if(!generate_sine32_1("sine32-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
1322 if(!generate_sine32_1("sine32-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
1323 if(!generate_sine32_1("sine32-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
1324 if(!generate_sine32_1("sine32-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
1325 if(!generate_sine32_1("sine32-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
1326
1327 if(!generate_sine32_2("sine32-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
1328 if(!generate_sine32_2("sine32-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
1329 if(!generate_sine32_2("sine32-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
1330 if(!generate_sine32_2("sine32-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
1331 if(!generate_sine32_2("sine32-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
1332 if(!generate_sine32_2("sine32-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
1333 if(!generate_sine32_2("sine32-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
1334 if(!generate_sine32_2("sine32-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
1335 if(!generate_sine32_2("sine32-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
1336 if(!generate_sine32_2("sine32-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
1337
1338 if(!generate_replaygain_tone(8000)) return 1;
1339 if(!generate_replaygain_tone(11025)) return 1;
1340 if(!generate_replaygain_tone(12000)) return 1;
1341 if(!generate_replaygain_tone(16000)) return 1;
1342 if(!generate_replaygain_tone(18900)) return 1;
1343 if(!generate_replaygain_tone(22050)) return 1;
1344 if(!generate_replaygain_tone(24000)) return 1;
1345 if(!generate_replaygain_tone(28000)) return 1;
1346 if(!generate_replaygain_tone(32000)) return 1;
1347 if(!generate_replaygain_tone(36000)) return 1;
1348 if(!generate_replaygain_tone(37800)) return 1;
1349 if(!generate_replaygain_tone(44100)) return 1;
1350 if(!generate_replaygain_tone(48000)) return 1;
1351 if(!generate_replaygain_tone(96000)) return 1;
1352 if(!generate_replaygain_tone(192000)) return 1;
1353
1354 /* WATCHOUT: the size of noise.raw is hardcoded into test/test_flac.sh */
1355 if(!generate_noise("noise.raw", 65536 * 8 * 3)) return 1;
1356 if(!generate_noise("noise8m32.raw", 32)) return 1;
1357 if(!generate_wackywavs()) return 1;
1358 if(!generate_wackywav64s()) return 1;
1359 if(!generate_wackyrf64s()) return 1;
1360 if(!generate_noisy_sine()) return 1;
1361 for(channels = 1; channels <= 8; channels *= 2) {
1362 unsigned bits_per_sample;
1363 for(bits_per_sample = 8; bits_per_sample <= 24; bits_per_sample += 4) {
1364 static const unsigned nsamples[] = { 1, 111, 4777 } ;
1365 unsigned samples;
1366 for(samples = 0; samples < sizeof(nsamples)/sizeof(nsamples[0]); samples++) {
1367 char fn[64];
1368
1369 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.aiff", channels, bits_per_sample, nsamples[samples]);
1370 if(!generate_aiff(fn, 44100, channels, bits_per_sample, nsamples[samples]))
1371 return 1;
1372
1373 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.wav", channels, bits_per_sample, nsamples[samples]);
1374 if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/0))
1375 return 1;
1376
1377 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.rf64", channels, bits_per_sample, nsamples[samples]);
1378 if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/1))
1379 return 1;
1380
1381 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.w64", channels, bits_per_sample, nsamples[samples]);
1382 if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/2))
1383 return 1;
1384
1385 if(bits_per_sample % 8 == 0) {
1386 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-signed-%u.raw", channels, bits_per_sample, nsamples[samples]);
1387 if(!generate_signed_raw(fn, channels, bits_per_sample/8, nsamples[samples]))
1388 return 1;
1389 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-unsigned-%u.raw", channels, bits_per_sample, nsamples[samples]);
1390 if(!generate_unsigned_raw(fn, channels, bits_per_sample/8, nsamples[samples]))
1391 return 1;
1392 }
1393 }
1394 }
1395 }
1396
1397 return 0;
1398 }
1399