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