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