convert C prototypes for functions with no args from () to (void)
[platform/upstream/flac.git] / src / test_streams / main.c
1 /* test_streams - Simple test pattern generator
2  * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <math.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #if defined _MSC_VER || defined __MINGW32__
27 #include <time.h>
28 #else
29 #include <sys/time.h>
30 #endif
31 #include "FLAC/assert.h"
32 #include "FLAC/ordinals.h"
33
34 #ifndef M_PI
35 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
36 #define M_PI 3.14159265358979323846
37 #endif
38
39 #if defined _WIN32 || defined __EMX__
40         static const char *mode = "wb";
41 #else
42         static const char *mode = "w";
43 #endif
44
45 #if !defined _MSC_VER && !defined __MINGW32__
46 #define GET_RANDOM_BYTE (((unsigned)random()) & 0xff)
47 #else
48 #define GET_RANDOM_BYTE (((unsigned)rand()) & 0xff)
49 #endif
50
51 static FLAC__bool is_big_endian_host;
52
53
54 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
55 {
56         return
57                 fputc(x, f) != EOF &&
58                 fputc(x >> 8, f) != EOF
59         ;
60 }
61
62 static FLAC__bool write_little_endian_int16(FILE *f, FLAC__int16 x)
63 {
64         return write_little_endian_uint16(f, (FLAC__uint16)x);
65 }
66
67 static FLAC__bool write_little_endian_uint24(FILE *f, FLAC__uint32 x)
68 {
69         return
70                 fputc(x, f) != EOF &&
71                 fputc(x >> 8, f) != EOF &&
72                 fputc(x >> 16, f) != EOF
73         ;
74 }
75
76 static FLAC__bool write_little_endian_int24(FILE *f, FLAC__int32 x)
77 {
78         return write_little_endian_uint24(f, (FLAC__uint32)x);
79 }
80
81 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
82 {
83         return
84                 fputc(x, f) != EOF &&
85                 fputc(x >> 8, f) != EOF &&
86                 fputc(x >> 16, f) != EOF &&
87                 fputc(x >> 24, f) != EOF
88         ;
89 }
90
91 #if 0
92 /* @@@ not used (yet) */
93 static FLAC__bool write_little_endian_int32(FILE *f, FLAC__int32 x)
94 {
95         return write_little_endian_uint32(f, (FLAC__uint32)x);
96 }
97 #endif
98
99 static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 x)
100 {
101         return
102                 fputc(x >> 8, f) != EOF &&
103                 fputc(x, f) != EOF
104         ;
105 }
106
107 #if 0
108 /* @@@ not used (yet) */
109 static FLAC__bool write_big_endian_int16(FILE *f, FLAC__int16 x)
110 {
111         return write_big_endian_uint16(f, (FLAC__uint16)x);
112 }
113 #endif
114
115 #if 0
116 /* @@@ not used (yet) */
117 static FLAC__bool write_big_endian_uint24(FILE *f, FLAC__uint32 x)
118 {
119         return
120                 fputc(x >> 16, f) != EOF &&
121                 fputc(x >> 8, f) != EOF &&
122                 fputc(x, f) != EOF
123         ;
124 }
125 #endif
126
127 #if 0
128 /* @@@ not used (yet) */
129 static FLAC__bool write_big_endian_int24(FILE *f, FLAC__int32 x)
130 {
131         return write_big_endian_uint24(f, (FLAC__uint32)x);
132 }
133 #endif
134
135 static FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 x)
136 {
137         return
138                 fputc(x >> 24, f) != EOF &&
139                 fputc(x >> 16, f) != EOF &&
140                 fputc(x >> 8, f) != EOF &&
141                 fputc(x, f) != EOF
142         ;
143 }
144
145 #if 0
146 /* @@@ not used (yet) */
147 static FLAC__bool write_big_endian_int32(FILE *f, FLAC__int32 x)
148 {
149         return write_big_endian_uint32(f, (FLAC__uint32)x);
150 }
151 #endif
152
153 static FLAC__bool write_sane_extended(FILE *f, unsigned val)
154         /* Write to 'f' a SANE extended representation of 'val'.  Return false if
155         * the write succeeds; return true otherwise.
156         *
157         * SANE extended is an 80-bit IEEE-754 representation with sign bit, 15 bits
158         * of exponent, and 64 bits of significand (mantissa).  Unlike most IEEE-754
159         * representations, it does not imply a 1 above the MSB of the significand.
160         *
161         * Preconditions:
162         *  val!=0U
163         */
164 {
165         unsigned int shift, exponent;
166
167         FLAC__ASSERT(val!=0U); /* handling 0 would require a special case */
168
169         for(shift= 0U; (val>>(31-shift))==0U; ++shift)
170                 ;
171         val<<= shift;
172         exponent= 63U-(shift+32U); /* add 32 for unused second word */
173
174         if(!write_big_endian_uint16(f, (FLAC__uint16)(exponent+0x3FFF)))
175                 return false;
176         if(!write_big_endian_uint32(f, val))
177                 return false;
178         if(!write_big_endian_uint32(f, 0)) /* unused second word */
179                 return false;
180
181         return true;
182 }
183
184 /* a mono one-sample 16bps stream */
185 static FLAC__bool generate_01(void)
186 {
187         FILE *f;
188         FLAC__int16 x = -32768;
189
190         if(0 == (f = fopen("test01.raw", mode)))
191                 return false;
192
193         if(!write_little_endian_int16(f, x))
194                 goto foo;
195
196         fclose(f);
197         return true;
198 foo:
199         fclose(f);
200         return false;
201 }
202
203 /* a stereo one-sample 16bps stream */
204 static FLAC__bool generate_02(void)
205 {
206         FILE *f;
207         FLAC__int16 xl = -32768, xr = 32767;
208
209         if(0 == (f = fopen("test02.raw", mode)))
210                 return false;
211
212         if(!write_little_endian_int16(f, xl))
213                 goto foo;
214         if(!write_little_endian_int16(f, xr))
215                 goto foo;
216
217         fclose(f);
218         return true;
219 foo:
220         fclose(f);
221         return false;
222 }
223
224 /* a mono five-sample 16bps stream */
225 static FLAC__bool generate_03(void)
226 {
227         FILE *f;
228         FLAC__int16 x[] = { -25, 0, 25, 50, 100 };
229         unsigned i;
230
231         if(0 == (f = fopen("test03.raw", mode)))
232                 return false;
233
234         for(i = 0; i < 5; i++)
235                 if(!write_little_endian_int16(f, x[i]))
236                         goto foo;
237
238         fclose(f);
239         return true;
240 foo:
241         fclose(f);
242         return false;
243 }
244
245 /* a stereo five-sample 16bps stream */
246 static FLAC__bool generate_04(void)
247 {
248         FILE *f;
249         FLAC__int16 x[] = { -25, 500, 0, 400, 25, 300, 50, 200, 100, 100 };
250         unsigned i;
251
252         if(0 == (f = fopen("test04.raw", mode)))
253                 return false;
254
255         for(i = 0; i < 10; i++)
256                 if(!write_little_endian_int16(f, x[i]))
257                         goto foo;
258
259         fclose(f);
260         return true;
261 foo:
262         fclose(f);
263         return false;
264 }
265
266 /* a mono full-scale deflection 8bps stream */
267 static FLAC__bool generate_fsd8(const char *fn, const int pattern[], unsigned reps)
268 {
269         FILE *f;
270         unsigned rep, p;
271
272         FLAC__ASSERT(pattern != 0);
273
274         if(0 == (f = fopen(fn, mode)))
275                 return false;
276
277         for(rep = 0; rep < reps; rep++) {
278                 for(p = 0; pattern[p]; p++) {
279                         signed char x = pattern[p] > 0? 127 : -128;
280                         if(fwrite(&x, sizeof(x), 1, f) < 1)
281                                 goto foo;
282                 }
283         }
284
285         fclose(f);
286         return true;
287 foo:
288         fclose(f);
289         return false;
290 }
291
292 /* a mono full-scale deflection 16bps stream */
293 static FLAC__bool generate_fsd16(const char *fn, const int pattern[], unsigned reps)
294 {
295         FILE *f;
296         unsigned rep, p;
297
298         FLAC__ASSERT(pattern != 0);
299
300         if(0 == (f = fopen(fn, mode)))
301                 return false;
302
303         for(rep = 0; rep < reps; rep++) {
304                 for(p = 0; pattern[p]; p++) {
305                         FLAC__int16 x = pattern[p] > 0? 32767 : -32768;
306                         if(!write_little_endian_int16(f, x))
307                                 goto foo;
308                 }
309         }
310
311         fclose(f);
312         return true;
313 foo:
314         fclose(f);
315         return false;
316 }
317
318 /* a stereo wasted-bits-per-sample 16bps stream */
319 static FLAC__bool generate_wbps16(const char *fn, unsigned samples)
320 {
321         FILE *f;
322         unsigned sample;
323
324         if(0 == (f = fopen(fn, mode)))
325                 return false;
326
327         for(sample = 0; sample < samples; sample++) {
328                 FLAC__int16 l = (sample % 2000) << 2;
329                 FLAC__int16 r = (sample % 1000) << 3;
330                 if(!write_little_endian_int16(f, l))
331                         goto foo;
332                 if(!write_little_endian_int16(f, r))
333                         goto foo;
334         }
335
336         fclose(f);
337         return true;
338 foo:
339         fclose(f);
340         return false;
341 }
342
343 /* a mono full-scale deflection 24bps stream */
344 static FLAC__bool generate_fsd24(const char *fn, const int pattern[], unsigned reps)
345 {
346         FILE *f;
347         unsigned rep, p;
348
349         FLAC__ASSERT(pattern != 0);
350
351         if(0 == (f = fopen(fn, mode)))
352                 return false;
353
354         for(rep = 0; rep < reps; rep++) {
355                 for(p = 0; pattern[p]; p++) {
356                         FLAC__int32 x = pattern[p] > 0? 8388607 : -8388608;
357                         if(!write_little_endian_int24(f, x))
358                                 goto foo;
359                 }
360         }
361
362         fclose(f);
363         return true;
364 foo:
365         fclose(f);
366         return false;
367 }
368
369 /* a mono sine-wave 8bps stream */
370 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)
371 {
372         const FLAC__int8 full_scale = 127;
373         const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
374         const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
375         FILE *f;
376         double theta1, theta2;
377         unsigned i;
378
379         if(0 == (f = fopen(fn, mode)))
380                 return false;
381
382         for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
383                 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
384                 FLAC__int8 v = (FLAC__int8)(val + 0.5);
385                 if(fwrite(&v, sizeof(v), 1, f) < 1)
386                         goto foo;
387         }
388
389         fclose(f);
390         return true;
391 foo:
392         fclose(f);
393         return false;
394 }
395
396 /* a stereo sine-wave 8bps stream */
397 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)
398 {
399         const FLAC__int8 full_scale = 127;
400         const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
401         const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
402         FILE *f;
403         double theta1, theta2;
404         unsigned i;
405
406         if(0 == (f = fopen(fn, mode)))
407                 return false;
408
409         for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
410                 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
411                 FLAC__int8 v = (FLAC__int8)(val + 0.5);
412                 if(fwrite(&v, sizeof(v), 1, f) < 1)
413                         goto foo;
414                 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
415                 v = (FLAC__int8)(val + 0.5);
416                 if(fwrite(&v, sizeof(v), 1, f) < 1)
417                         goto foo;
418         }
419
420         fclose(f);
421         return true;
422 foo:
423         fclose(f);
424         return false;
425 }
426
427 /* a mono sine-wave 16bps stream */
428 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)
429 {
430         const FLAC__int16 full_scale = 32767;
431         const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
432         const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
433         FILE *f;
434         double theta1, theta2;
435         unsigned i;
436
437         if(0 == (f = fopen(fn, mode)))
438                 return false;
439
440         for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
441                 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
442                 FLAC__int16 v = (FLAC__int16)(val + 0.5);
443                 if(!write_little_endian_int16(f, v))
444                         goto foo;
445         }
446
447         fclose(f);
448         return true;
449 foo:
450         fclose(f);
451         return false;
452 }
453
454 /* a stereo sine-wave 16bps stream */
455 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)
456 {
457         const FLAC__int16 full_scale = 32767;
458         const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
459         const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
460         FILE *f;
461         double theta1, theta2;
462         unsigned i;
463
464         if(0 == (f = fopen(fn, mode)))
465                 return false;
466
467         for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
468                 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
469                 FLAC__int16 v = (FLAC__int16)(val + 0.5);
470                 if(!write_little_endian_int16(f, v))
471                         goto foo;
472                 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
473                 v = (FLAC__int16)(val + 0.5);
474                 if(!write_little_endian_int16(f, v))
475                         goto foo;
476         }
477
478         fclose(f);
479         return true;
480 foo:
481         fclose(f);
482         return false;
483 }
484
485 /* a mono sine-wave 24bps stream */
486 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)
487 {
488         const FLAC__int32 full_scale = 0x7fffff;
489         const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
490         const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
491         FILE *f;
492         double theta1, theta2;
493         unsigned i;
494
495         if(0 == (f = fopen(fn, mode)))
496                 return false;
497
498         for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
499                 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
500                 FLAC__int32 v = (FLAC__int32)(val + 0.5);
501                 if(!write_little_endian_int24(f, v))
502                         goto foo;
503         }
504
505         fclose(f);
506         return true;
507 foo:
508         fclose(f);
509         return false;
510 }
511
512 /* a stereo sine-wave 24bps stream */
513 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)
514 {
515         const FLAC__int32 full_scale = 0x7fffff;
516         const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
517         const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
518         FILE *f;
519         double theta1, theta2;
520         unsigned i;
521
522         if(0 == (f = fopen(fn, mode)))
523                 return false;
524
525         for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
526                 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
527                 FLAC__int32 v = (FLAC__int32)(val + 0.5);
528                 if(!write_little_endian_int24(f, v))
529                         goto foo;
530                 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
531                 v = (FLAC__int32)(val + 0.5);
532                 if(!write_little_endian_int24(f, v))
533                         goto foo;
534         }
535
536         fclose(f);
537         return true;
538 foo:
539         fclose(f);
540         return false;
541 }
542
543 static FLAC__bool generate_noise(const char *fn, unsigned bytes)
544 {
545         FILE *f;
546         unsigned b;
547
548         if(0 == (f = fopen(fn, mode)))
549                 return false;
550
551         for(b = 0; b < bytes; b++) {
552 #if !defined _MSC_VER && !defined __MINGW32__
553                 FLAC__byte x = (FLAC__byte)(((unsigned)random()) & 0xff);
554 #else
555                 FLAC__byte x = (FLAC__byte)(((unsigned)rand()) & 0xff);
556 #endif
557                 if(fwrite(&x, sizeof(x), 1, f) < 1)
558                         goto foo;
559         }
560
561         fclose(f);
562         return true;
563 foo:
564         fclose(f);
565         return false;
566 }
567
568 static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples)
569 {
570         const unsigned true_size = channels * ((bps+7)/8) * samples;
571         const unsigned padded_size = (true_size + 1) & (~1u);
572         const unsigned shift = 8 - (bps%8);
573         FILE *f;
574         unsigned i;
575
576         if(0 == (f = fopen(filename, mode)))
577                 return false;
578         if(fwrite("FORM", 1, 4, f) < 4)
579                 goto foo;
580         if(!write_big_endian_uint32(f, padded_size + 46))
581                 goto foo;
582         if(fwrite("AIFFCOMM\000\000\000\022", 1, 12, f) < 12)
583                 goto foo;
584         if(!write_big_endian_uint16(f, (FLAC__uint16)channels))
585                 goto foo;
586         if(!write_big_endian_uint32(f, samples))
587                 goto foo;
588         if(!write_big_endian_uint16(f, (FLAC__uint16)bps))
589                 goto foo;
590         if(!write_sane_extended(f, sample_rate))
591                 goto foo;
592         if(fwrite("SSND", 1, 4, f) < 4)
593                 goto foo;
594         if(!write_big_endian_uint32(f, true_size + 8))
595                 goto foo;
596         if(fwrite("\000\000\000\000\000\000\000\000", 1, 8, f) < 8)
597                 goto foo;
598
599         for(i = 0; i < true_size; i++)
600                 if(fputc(GET_RANDOM_BYTE<<shift, f) == EOF)
601                         goto foo;
602         for( ; i < padded_size; i++)
603                 if(fputc(0, f) == EOF)
604                         goto foo;
605
606         fclose(f);
607         return true;
608 foo:
609         fclose(f);
610         return false;
611 }
612
613 static FLAC__bool generate_wav(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples, FLAC__bool strict)
614 {
615         const FLAC__bool waveformatextensible = strict && (channels > 2 || (bps%8));
616         /*                                                                 ^^^^^^^
617          * (bps%8) allows 24 bps which is technically supposed to be WAVEFORMATEXTENSIBLE but we
618          * write 24bps as WAVEFORMATEX since it's unambiguous and matches how flac writes it
619          */
620         const unsigned true_size = channels * ((bps+7)/8) * samples;
621         const unsigned padded_size = (true_size + 1) & (~1u);
622         const unsigned shift = (bps%8)? 8 - (bps%8) : 0;
623         FILE *f;
624         unsigned i;
625
626         if(0 == (f = fopen(filename, mode)))
627                 return false;
628         if(fwrite("RIFF", 1, 4, f) < 4)
629                 goto foo;
630         if(!write_little_endian_uint32(f, padded_size + (waveformatextensible?60:36)))
631                 goto foo;
632         if(fwrite("WAVEfmt ", 1, 8, f) < 8)
633                 goto foo;
634         if(!write_little_endian_uint32(f, waveformatextensible?40:16))
635                 goto foo;
636         if(!write_little_endian_uint16(f, (FLAC__uint16)(waveformatextensible?65534:1)))
637                 goto foo;
638         if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
639                 goto foo;
640         if(!write_little_endian_uint32(f, sample_rate))
641                 goto foo;
642         if(!write_little_endian_uint32(f, sample_rate * channels * ((bps+7)/8)))
643                 goto foo;
644         if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * ((bps+7)/8)))) /* block align */
645                 goto foo;
646         if(!write_little_endian_uint16(f, (FLAC__uint16)(bps+shift)))
647                 goto foo;
648         if(waveformatextensible) {
649                 if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
650                         goto foo;
651                 if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
652                         goto foo;
653                 if(!write_little_endian_uint32(f, 0)) /* channelMask */
654                         goto foo;
655                 /* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
656                 if(fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
657                         goto foo;
658         }
659         if(fwrite("data", 1, 4, f) < 4)
660                 goto foo;
661         if(!write_little_endian_uint32(f, true_size))
662                 goto foo;
663
664         for(i = 0; i < true_size; i++)
665                 if(fputc(GET_RANDOM_BYTE<<shift, f) == EOF)
666                         goto foo;
667         for( ; i < padded_size; i++)
668                 if(fputc(0, f) == EOF)
669                         goto foo;
670
671         fclose(f);
672         return true;
673 foo:
674         fclose(f);
675         return false;
676 }
677
678 static FLAC__bool generate_wackywavs(void)
679 {
680         FILE *f;
681         FLAC__byte wav[] = {
682                 'R', 'I', 'F', 'F',  76,   0,   0,   0,
683                 'W', 'A', 'V', 'E', 'f', 'a', 'c', 't',
684                   4,   0,   0,  0 , 'b', 'l', 'a', 'h',
685                 'p', 'a', 'd', ' ',   4,   0,   0,   0,
686                 'B', 'L', 'A', 'H', 'f', 'm', 't', ' ',
687                  16,   0,   0,   0,   1,   0,   1,   0,
688                 0x44,0xAC,  0,   0,   0,   0,   0,   0,
689                   2,   0,  16,   0, 'd', 'a', 't', 'a',
690                  16,   0,   0,   0,   0,   0,   1,   0,
691                   4,   0,   9,   0,  16,   0,  25,   0,
692                  36,   0,  49,   0, 'p', 'a', 'd', ' ',
693                   4,   0,   0,   0, 'b', 'l', 'a', 'h'
694         };
695
696         if(0 == (f = fopen("wacky1.wav", mode)))
697                 return false;
698         if(fwrite(wav, 1, 84, f) < 84)
699                 goto foo;
700         fclose(f);
701
702         wav[4] += 12;
703         if(0 == (f = fopen("wacky2.wav", mode)))
704                 return false;
705         if(fwrite(wav, 1, 96, f) < 96)
706                 goto foo;
707         fclose(f);
708
709         return true;
710 foo:
711         fclose(f);
712         return false;
713 }
714
715 int main(int argc, char *argv[])
716 {
717         FLAC__uint32 test = 1;
718         unsigned channels;
719
720         int pattern01[] = { 1, -1, 0 };
721         int pattern02[] = { 1, 1, -1, 0 };
722         int pattern03[] = { 1, -1, -1, 0 };
723         int pattern04[] = { 1, -1, 1, -1, 0 };
724         int pattern05[] = { 1, -1, -1, 1, 0 };
725         int pattern06[] = { 1, -1, 1, 1, -1, 0 };
726         int pattern07[] = { 1, -1, -1, 1, -1, 0 };
727
728         (void)argc;
729         (void)argv;
730         is_big_endian_host = (*((FLAC__byte*)(&test)))? false : true;
731
732 #if !defined _MSC_VER && !defined __MINGW32__
733         {
734                 struct timeval tv;
735
736                 if(gettimeofday(&tv, 0) < 0) {
737                         fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
738                         tv.tv_usec = 4321;
739                 }
740                 srandom(tv.tv_usec);
741         }
742 #else
743         srand(time(0));
744 #endif
745
746         if(!generate_01()) return 1;
747         if(!generate_02()) return 1;
748         if(!generate_03()) return 1;
749         if(!generate_04()) return 1;
750
751         if(!generate_fsd8("fsd8-01.raw", pattern01, 100)) return 1;
752         if(!generate_fsd8("fsd8-02.raw", pattern02, 100)) return 1;
753         if(!generate_fsd8("fsd8-03.raw", pattern03, 100)) return 1;
754         if(!generate_fsd8("fsd8-04.raw", pattern04, 100)) return 1;
755         if(!generate_fsd8("fsd8-05.raw", pattern05, 100)) return 1;
756         if(!generate_fsd8("fsd8-06.raw", pattern06, 100)) return 1;
757         if(!generate_fsd8("fsd8-07.raw", pattern07, 100)) return 1;
758
759         if(!generate_fsd16("fsd16-01.raw", pattern01, 100)) return 1;
760         if(!generate_fsd16("fsd16-02.raw", pattern02, 100)) return 1;
761         if(!generate_fsd16("fsd16-03.raw", pattern03, 100)) return 1;
762         if(!generate_fsd16("fsd16-04.raw", pattern04, 100)) return 1;
763         if(!generate_fsd16("fsd16-05.raw", pattern05, 100)) return 1;
764         if(!generate_fsd16("fsd16-06.raw", pattern06, 100)) return 1;
765         if(!generate_fsd16("fsd16-07.raw", pattern07, 100)) return 1;
766
767         if(!generate_fsd24("fsd24-01.raw", pattern01, 100)) return 1;
768         if(!generate_fsd24("fsd24-02.raw", pattern02, 100)) return 1;
769         if(!generate_fsd24("fsd24-03.raw", pattern03, 100)) return 1;
770         if(!generate_fsd24("fsd24-04.raw", pattern04, 100)) return 1;
771         if(!generate_fsd24("fsd24-05.raw", pattern05, 100)) return 1;
772         if(!generate_fsd24("fsd24-06.raw", pattern06, 100)) return 1;
773         if(!generate_fsd24("fsd24-07.raw", pattern07, 100)) return 1;
774
775         if(!generate_wbps16("wbps16-01.raw", 1000)) return 1;
776
777         if(!generate_sine8_1("sine8-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
778         if(!generate_sine8_1("sine8-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
779         if(!generate_sine8_1("sine8-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
780         if(!generate_sine8_1("sine8-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
781         if(!generate_sine8_1("sine8-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
782
783         if(!generate_sine8_2("sine8-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
784         if(!generate_sine8_2("sine8-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
785         if(!generate_sine8_2("sine8-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
786         if(!generate_sine8_2("sine8-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
787         if(!generate_sine8_2("sine8-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
788         if(!generate_sine8_2("sine8-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
789         if(!generate_sine8_2("sine8-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
790         if(!generate_sine8_2("sine8-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
791         if(!generate_sine8_2("sine8-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
792         if(!generate_sine8_2("sine8-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
793
794         if(!generate_sine16_1("sine16-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
795         if(!generate_sine16_1("sine16-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
796         if(!generate_sine16_1("sine16-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
797         if(!generate_sine16_1("sine16-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
798         if(!generate_sine16_1("sine16-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
799
800         if(!generate_sine16_2("sine16-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
801         if(!generate_sine16_2("sine16-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
802         if(!generate_sine16_2("sine16-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
803         if(!generate_sine16_2("sine16-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
804         if(!generate_sine16_2("sine16-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
805         if(!generate_sine16_2("sine16-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
806         if(!generate_sine16_2("sine16-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
807         if(!generate_sine16_2("sine16-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
808         if(!generate_sine16_2("sine16-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
809         if(!generate_sine16_2("sine16-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
810
811         if(!generate_sine24_1("sine24-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
812         if(!generate_sine24_1("sine24-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
813         if(!generate_sine24_1("sine24-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
814         if(!generate_sine24_1("sine24-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
815         if(!generate_sine24_1("sine24-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
816
817         if(!generate_sine24_2("sine24-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
818         if(!generate_sine24_2("sine24-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
819         if(!generate_sine24_2("sine24-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
820         if(!generate_sine24_2("sine24-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
821         if(!generate_sine24_2("sine24-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
822         if(!generate_sine24_2("sine24-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
823         if(!generate_sine24_2("sine24-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
824         if(!generate_sine24_2("sine24-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
825         if(!generate_sine24_2("sine24-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
826         if(!generate_sine24_2("sine24-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
827
828         /* WATCHOUT: the size of noise.raw is hardcoded into test/test_flac.sh */
829         if(!generate_noise("noise.raw", 65536 * 8 * 3)) return 1;
830         if(!generate_noise("noise8m32.raw", 32)) return 1;
831         if(!generate_wackywavs()) return 1;
832         for(channels = 1; channels <= 8; channels++) {
833                 unsigned bits_per_sample;
834                 for(bits_per_sample = 4; bits_per_sample <= 24; bits_per_sample++) {
835                         static const unsigned nsamples[] = { 1, 111, 4777 } ;
836                         unsigned samples;
837                         for(samples = 0; samples < sizeof(nsamples)/sizeof(nsamples[0]); samples++) {
838                                 char fn[64];
839
840                                 sprintf(fn, "rt-%u-%u-%u.aiff", channels, bits_per_sample, nsamples[samples]);
841                                 if(!generate_aiff(fn, 44100, channels, bits_per_sample, nsamples[samples]))
842                                         return 1;
843
844                                 sprintf(fn, "rt-%u-%u-%u.wav", channels, bits_per_sample, nsamples[samples]);
845                                 if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true))
846                                         return 1;
847
848                                 if(bits_per_sample % 8 == 0) {
849                                         sprintf(fn, "rt-%u-%u-%u.raw", channels, bits_per_sample, nsamples[samples]);
850                                         if(!generate_noise(fn, channels * bits_per_sample/8 * nsamples[samples]))
851                                                 return 1;
852                                 }
853                         }
854                 }
855         }
856
857         return 0;
858 }