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