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