1 /* test_streams - Simple test pattern generator
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "share/compat.h"
28 #if defined _MSC_VER || defined __MINGW32__
33 #include "FLAC/assert.h"
34 #include "FLAC/ordinals.h"
35 #include "share/compat.h"
38 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
39 #define M_PI 3.14159265358979323846
42 #if !defined _MSC_VER && !defined __MINGW32__
43 #define GET_RANDOM_BYTE (((unsigned)random()) & 0xff)
45 #define GET_RANDOM_BYTE (((unsigned)rand()) & 0xff)
48 static FLAC__bool is_big_endian_host;
51 static FLAC__bool write_little_endian(FILE *f, FLAC__int32 x, size_t bytes)
54 if(fputc(x, f) == EOF)
62 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
66 fputc(x >> 8, f) != EOF
70 static FLAC__bool write_little_endian_int16(FILE *f, FLAC__int16 x)
72 return write_little_endian_uint16(f, (FLAC__uint16)x);
75 static FLAC__bool write_little_endian_uint24(FILE *f, FLAC__uint32 x)
79 fputc(x >> 8, f) != EOF &&
80 fputc(x >> 16, f) != EOF
84 static FLAC__bool write_little_endian_int24(FILE *f, FLAC__int32 x)
86 return write_little_endian_uint24(f, (FLAC__uint32)x);
89 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
93 fputc(x >> 8, f) != EOF &&
94 fputc(x >> 16, f) != EOF &&
95 fputc(x >> 24, f) != EOF
100 /* @@@ not used (yet) */
101 static FLAC__bool write_little_endian_int32(FILE *f, FLAC__int32 x)
103 return write_little_endian_uint32(f, (FLAC__uint32)x);
107 static FLAC__bool write_little_endian_uint64(FILE *f, FLAC__uint64 x)
110 fputc(x, f) != EOF &&
111 fputc(x >> 8, f) != EOF &&
112 fputc(x >> 16, f) != EOF &&
113 fputc(x >> 24, f) != EOF &&
114 fputc(x >> 32, f) != EOF &&
115 fputc(x >> 40, f) != EOF &&
116 fputc(x >> 48, f) != EOF &&
117 fputc(x >> 56, f) != EOF
121 static FLAC__bool write_big_endian(FILE *f, FLAC__int32 x, size_t bytes)
126 if(fputc(x>>24, f) == EOF)
134 static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 x)
137 fputc(x >> 8, f) != EOF &&
143 /* @@@ not used (yet) */
144 static FLAC__bool write_big_endian_int16(FILE *f, FLAC__int16 x)
146 return write_big_endian_uint16(f, (FLAC__uint16)x);
151 /* @@@ not used (yet) */
152 static FLAC__bool write_big_endian_uint24(FILE *f, FLAC__uint32 x)
155 fputc(x >> 16, f) != EOF &&
156 fputc(x >> 8, f) != EOF &&
163 /* @@@ not used (yet) */
164 static FLAC__bool write_big_endian_int24(FILE *f, FLAC__int32 x)
166 return write_big_endian_uint24(f, (FLAC__uint32)x);
170 static FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 x)
173 fputc(x >> 24, f) != EOF &&
174 fputc(x >> 16, f) != EOF &&
175 fputc(x >> 8, f) != EOF &&
181 /* @@@ not used (yet) */
182 static FLAC__bool write_big_endian_int32(FILE *f, FLAC__int32 x)
184 return write_big_endian_uint32(f, (FLAC__uint32)x);
188 static FLAC__bool write_sane_extended(FILE *f, unsigned val)
189 /* Write to 'f' a SANE extended representation of 'val'. Return false if
190 * the write succeeds; return true otherwise.
192 * SANE extended is an 80-bit IEEE-754 representation with sign bit, 15 bits
193 * of exponent, and 64 bits of significand (mantissa). Unlike most IEEE-754
194 * representations, it does not imply a 1 above the MSB of the significand.
200 unsigned int shift, exponent;
202 FLAC__ASSERT(val!=0U); /* handling 0 would require a special case */
204 for(shift= 0U; (val>>(31-shift))==0U; ++shift)
207 exponent= 63U-(shift+32U); /* add 32 for unused second word */
209 if(!write_big_endian_uint16(f, (FLAC__uint16)(exponent+0x3FFF)))
211 if(!write_big_endian_uint32(f, val))
213 if(!write_big_endian_uint32(f, 0)) /* unused second word */
219 /* a mono one-sample 16bps stream */
220 static FLAC__bool generate_01(void)
223 FLAC__int16 x = -32768;
225 if(0 == (f = flac_fopen("test01.raw", "wb")))
228 if(!write_little_endian_int16(f, x))
238 /* a stereo one-sample 16bps stream */
239 static FLAC__bool generate_02(void)
242 FLAC__int16 xl = -32768, xr = 32767;
244 if(0 == (f = flac_fopen("test02.raw", "wb")))
247 if(!write_little_endian_int16(f, xl))
249 if(!write_little_endian_int16(f, xr))
259 /* a mono five-sample 16bps stream */
260 static FLAC__bool generate_03(void)
263 FLAC__int16 x[] = { -25, 0, 25, 50, 100 };
266 if(0 == (f = flac_fopen("test03.raw", "wb")))
269 for(i = 0; i < 5; i++)
270 if(!write_little_endian_int16(f, x[i]))
280 /* a stereo five-sample 16bps stream */
281 static FLAC__bool generate_04(void)
284 FLAC__int16 x[] = { -25, 500, 0, 400, 25, 300, 50, 200, 100, 100 };
287 if(0 == (f = flac_fopen("test04.raw", "wb")))
290 for(i = 0; i < 10; i++)
291 if(!write_little_endian_int16(f, x[i]))
301 /* a mono full-scale deflection 8bps stream */
302 static FLAC__bool generate_fsd8(const char *fn, const int pattern[], unsigned reps)
307 FLAC__ASSERT(pattern != 0);
309 if(0 == (f = flac_fopen(fn, "wb")))
312 for(rep = 0; rep < reps; rep++) {
313 for(p = 0; pattern[p]; p++) {
314 signed char x = pattern[p] > 0? 127 : -128;
315 if(fwrite(&x, sizeof(x), 1, f) < 1)
327 /* a mono full-scale deflection 16bps stream */
328 static FLAC__bool generate_fsd16(const char *fn, const int pattern[], unsigned reps)
333 FLAC__ASSERT(pattern != 0);
335 if(0 == (f = flac_fopen(fn, "wb")))
338 for(rep = 0; rep < reps; rep++) {
339 for(p = 0; pattern[p]; p++) {
340 FLAC__int16 x = pattern[p] > 0? 32767 : -32768;
341 if(!write_little_endian_int16(f, x))
353 /* a stereo wasted-bits-per-sample 16bps stream */
354 static FLAC__bool generate_wbps16(const char *fn, unsigned samples)
359 if(0 == (f = flac_fopen(fn, "wb")))
362 for(sample = 0; sample < samples; sample++) {
363 FLAC__int16 l = (sample % 2000) << 2;
364 FLAC__int16 r = (sample % 1000) << 3;
365 if(!write_little_endian_int16(f, l))
367 if(!write_little_endian_int16(f, r))
378 /* a mono full-scale deflection 24bps stream */
379 static FLAC__bool generate_fsd24(const char *fn, const int pattern[], unsigned reps)
384 FLAC__ASSERT(pattern != 0);
386 if(0 == (f = flac_fopen(fn, "wb")))
389 for(rep = 0; rep < reps; rep++) {
390 for(p = 0; pattern[p]; p++) {
391 FLAC__int32 x = pattern[p] > 0? 8388607 : -8388608;
392 if(!write_little_endian_int24(f, x))
404 /* a mono sine-wave 8bps stream */
405 static FLAC__bool generate_sine8_1(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2)
407 const FLAC__int8 full_scale = 127;
408 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
409 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
411 double theta1, theta2;
414 if(0 == (f = flac_fopen(fn, "wb")))
417 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
418 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
419 FLAC__int8 v = (FLAC__int8)(val + 0.5);
420 if(fwrite(&v, sizeof(v), 1, f) < 1)
431 /* a stereo sine-wave 8bps stream */
432 static FLAC__bool generate_sine8_2(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2, double fmult)
434 const FLAC__int8 full_scale = 127;
435 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
436 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
438 double theta1, theta2;
441 if(0 == (f = flac_fopen(fn, "wb")))
444 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
445 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
446 FLAC__int8 v = (FLAC__int8)(val + 0.5);
447 if(fwrite(&v, sizeof(v), 1, f) < 1)
449 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
450 v = (FLAC__int8)(val + 0.5);
451 if(fwrite(&v, sizeof(v), 1, f) < 1)
462 /* a mono sine-wave 16bps stream */
463 static FLAC__bool generate_sine16_1(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2)
465 const FLAC__int16 full_scale = 32767;
466 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
467 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
469 double theta1, theta2;
472 if(0 == (f = flac_fopen(fn, "wb")))
475 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
476 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
477 FLAC__int16 v = (FLAC__int16)(val + 0.5);
478 if(!write_little_endian_int16(f, v))
489 /* a stereo sine-wave 16bps stream */
490 static FLAC__bool generate_sine16_2(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2, double fmult)
492 const FLAC__int16 full_scale = 32767;
493 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
494 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
496 double theta1, theta2;
499 if(0 == (f = flac_fopen(fn, "wb")))
502 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
503 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
504 FLAC__int16 v = (FLAC__int16)(val + 0.5);
505 if(!write_little_endian_int16(f, v))
507 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
508 v = (FLAC__int16)(val + 0.5);
509 if(!write_little_endian_int16(f, v))
520 /* a mono sine-wave 24bps stream */
521 static FLAC__bool generate_sine24_1(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2)
523 const FLAC__int32 full_scale = 0x7fffff;
524 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
525 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
527 double theta1, theta2;
530 if(0 == (f = flac_fopen(fn, "wb")))
533 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
534 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
535 FLAC__int32 v = (FLAC__int32)(val + 0.5);
536 if(!write_little_endian_int24(f, v))
547 /* a stereo sine-wave 24bps stream */
548 static FLAC__bool generate_sine24_2(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2, double fmult)
550 const FLAC__int32 full_scale = 0x7fffff;
551 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
552 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
554 double theta1, theta2;
557 if(0 == (f = flac_fopen(fn, "wb")))
560 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
561 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
562 FLAC__int32 v = (FLAC__int32)(val + 0.5);
563 if(!write_little_endian_int24(f, v))
565 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
566 v = (FLAC__int32)(val + 0.5);
567 if(!write_little_endian_int24(f, v))
578 static FLAC__bool generate_noise(const char *fn, unsigned bytes)
583 if(0 == (f = flac_fopen(fn, "wb")))
586 for(b = 0; b < bytes; b++) {
587 #if !defined _MSC_VER && !defined __MINGW32__
588 FLAC__byte x = (FLAC__byte)(((unsigned)random()) & 0xff);
590 FLAC__byte x = (FLAC__byte)(((unsigned)rand()) & 0xff);
592 if(fwrite(&x, sizeof(x), 1, f) < 1)
603 static FLAC__bool generate_raw(const char *filename, unsigned channels, unsigned bytes_per_sample, unsigned samples)
605 const FLAC__int32 full_scale = (1 << (bytes_per_sample*8-1)) - 1;
606 const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
607 const double delta1 = 2.0 * M_PI / ( 44100.0 / f1);
608 const double delta2 = 2.0 * M_PI / ( 44100.0 / f2);
609 double theta1, theta2;
613 if(0 == (f = flac_fopen(filename, "wb")))
616 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
617 for(j = 0; j < channels; j++) {
618 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
619 FLAC__int32 v = (FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8);
620 if(!write_little_endian(f, v, bytes_per_sample))
632 static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples)
634 const unsigned bytes_per_sample = (bps+7)/8;
635 const unsigned true_size = channels * bytes_per_sample * samples;
636 const unsigned padded_size = (true_size + 1) & (~1u);
637 const unsigned shift = (bps%8)? 8 - (bps%8) : 0;
638 const FLAC__int32 full_scale = (1 << (bps-1)) - 1;
639 const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
640 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
641 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
642 double theta1, theta2;
646 if(0 == (f = flac_fopen(filename, "wb")))
648 if(fwrite("FORM", 1, 4, f) < 4)
650 if(!write_big_endian_uint32(f, padded_size + 46))
652 if(fwrite("AIFFCOMM\000\000\000\022", 1, 12, f) < 12)
654 if(!write_big_endian_uint16(f, (FLAC__uint16)channels))
656 if(!write_big_endian_uint32(f, samples))
658 if(!write_big_endian_uint16(f, (FLAC__uint16)bps))
660 if(!write_sane_extended(f, sample_rate))
662 if(fwrite("SSND", 1, 4, f) < 4)
664 if(!write_big_endian_uint32(f, true_size + 8))
666 if(fwrite("\000\000\000\000\000\000\000\000", 1, 8, f) < 8)
669 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
670 for(j = 0; j < channels; j++) {
671 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
672 FLAC__int32 v = ((FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8)) << shift;
673 if(!write_big_endian(f, v, bytes_per_sample))
677 for(i = true_size; i < padded_size; i++)
678 if(fputc(0, f) == EOF)
688 /* flavor is: 0:WAVE, 1:RF64, 2:WAVE64 */
689 static FLAC__bool generate_wav(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples, FLAC__bool strict, int flavor)
691 const FLAC__bool waveformatextensible = strict && (channels > 2 || (bps%8));
693 * (bps%8) allows 24 bps which is technically supposed to be WAVEFORMATEXTENSIBLE but we
694 * write 24bps as WAVEFORMATEX since it's unambiguous and matches how flac writes it
697 const unsigned bytes_per_sample = (bps+7)/8;
698 const unsigned shift = (bps%8)? 8 - (bps%8) : 0;
699 /* this rig is not going over 4G so we're ok with 32-bit sizes here */
700 const FLAC__uint32 true_size = channels * bytes_per_sample * samples;
701 const FLAC__uint32 padded_size = flavor<2? (true_size + 1) & (~1u) : (true_size + 7) & (~7u);
702 const FLAC__int32 full_scale = (1 << (bps-1)) - 1;
703 const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
704 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
705 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
706 double theta1, theta2;
710 if(0 == (f = flac_fopen(filename, "wb")))
712 /* RIFFxxxxWAVE or equivalent: */
715 if(fwrite("RIFF", 1, 4, f) < 4)
718 /* +8+{40,16} for fmt chunk */
719 /* +8 for data chunk header */
720 if(!write_little_endian_uint32(f, 4 + 8+(waveformatextensible?40:16) + 8 + padded_size))
722 if(fwrite("WAVE", 1, 4, f) < 4)
726 if(fwrite("RF64", 1, 4, f) < 4)
728 if(!write_little_endian_uint32(f, 0xffffffff))
730 if(fwrite("WAVE", 1, 4, f) < 4)
734 /* RIFF GUID 66666972-912E-11CF-A5D6-28DB04C10000 */
735 if(fwrite("\x72\x69\x66\x66\x2E\x91\xCF\x11\xA5\xD6\x28\xDB\x04\xC1\x00\x00", 1, 16, f) < 16)
737 /* +(16+8) for RIFF GUID + size */
738 /* +16 for WAVE GUID */
739 /* +16+8+{40,16} for fmt chunk */
740 /* +16+8 for data chunk header */
741 if(!write_little_endian_uint64(f, (16+8) + 16 + 16+8+(waveformatextensible?40:16) + (16+8) + padded_size))
743 /* WAVE GUID 65766177-ACF3-11D3-8CD1-00C04F8EDB8A */
744 if(fwrite("\x77\x61\x76\x65\xF3\xAC\xD3\x11\x8C\xD1\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) < 16)
750 if(flavor == 1) { /* rf64 */
751 if(fwrite("ds64", 1, 4, f) < 4)
753 if(!write_little_endian_uint32(f, 28)) /* ds64 chunk size */
755 if(!write_little_endian_uint64(f, 36 + padded_size + (waveformatextensible?60:36)))
757 if(!write_little_endian_uint64(f, true_size))
759 if(!write_little_endian_uint64(f, samples))
761 if(!write_little_endian_uint32(f, 0)) /* table size */
766 if(fwrite("fmt ", 1, 4, f) < 4)
769 if(!write_little_endian_uint32(f, waveformatextensible?40:16))
773 /* fmt GUID 20746D66-ACF3-11D3-8CD1-00C04F8EDB8A */
774 if(fwrite("\x66\x6D\x74\x20\xF3\xAC\xD3\x11\x8C\xD1\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) < 16)
776 /* chunk size (+16+8 for GUID and size fields) */
777 if(!write_little_endian_uint64(f, 16+8+(waveformatextensible?40:16)))
780 if(!write_little_endian_uint16(f, (FLAC__uint16)(waveformatextensible?65534:1)))
782 if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
784 if(!write_little_endian_uint32(f, sample_rate))
786 if(!write_little_endian_uint32(f, sample_rate * channels * bytes_per_sample))
788 if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * bytes_per_sample))) /* block align */
790 if(!write_little_endian_uint16(f, (FLAC__uint16)(bps+shift)))
792 if(waveformatextensible) {
793 if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
795 if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
797 if(!write_little_endian_uint32(f, 0)) /* channelMask */
799 /* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
800 if(fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
805 if(fwrite("data", 1, 4, f) < 4)
807 if(!write_little_endian_uint32(f, flavor==1? 0xffffffff : true_size))
811 /* data GUID 61746164-ACF3-11D3-8CD1-00C04F8EDB8A */
812 if(fwrite("\x64\x61\x74\x61\xF3\xAC\xD3\x11\x8C\xD1\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) != 16)
814 /* +16+8 for GUID and size fields */
815 if(!write_little_endian_uint64(f, 16+8 + true_size))
819 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
820 for(j = 0; j < channels; j++) {
821 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
822 FLAC__int32 v = ((FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8)) << shift;
823 if(!write_little_endian(f, v, bytes_per_sample))
827 for(i = true_size; i < padded_size; i++)
828 if(fputc(0, f) == EOF)
838 static FLAC__bool generate_wackywavs(void)
842 'R', 'I', 'F', 'F', 76, 0, 0, 0,
843 'W', 'A', 'V', 'E', 'j', 'u', 'n', 'k',
844 4, 0, 0, 0 , 'b', 'l', 'a', 'h',
845 'p', 'a', 'd', ' ', 4, 0, 0, 0,
846 'B', 'L', 'A', 'H', 'f', 'm', 't', ' ',
847 16, 0, 0, 0, 1, 0, 1, 0,
848 0x44,0xAC, 0, 0,0x88,0x58,0x01, 0,
849 2, 0, 16, 0, 'd', 'a', 't', 'a',
850 16, 0, 0, 0, 0, 0, 1, 0,
851 4, 0, 9, 0, 16, 0, 25, 0,
852 36, 0, 49, 0, 'p', 'a', 'd', ' ',
853 4, 0, 0, 0, 'b', 'l', 'a', 'h'
856 if(0 == (f = flac_fopen("wacky1.wav", "wb")))
858 if(fwrite(wav, 1, 84, f) < 84)
863 if(0 == (f = flac_fopen("wacky2.wav", "wb")))
865 if(fwrite(wav, 1, 96, f) < 96)
875 static FLAC__bool generate_noisy_sine(void)
879 'R', 'I', 'F', 'F', 76, 0, 0, 0,
880 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ',
881 16, 0, 0, 0, 1, 0, 1, 0,
882 0x44,0xAC, 0, 0,0x88,0x58,0x01, 0,
883 2, 0, 16, 0, 'd', 'a', 't', 'a',
886 int32_t randstate = 0x1243456;
887 double sample, last_val = 0.0;
890 if(0 == (f = flac_fopen("noisy-sine.wav", "wb")))
892 if(fwrite(wav, 1, sizeof (wav), f) < sizeof (wav))
895 for (k = 0 ; k < 5 * 44100 ; k++) {
896 /* Obvioulsy not a crypto quality RNG. */
897 randstate = 11117 * randstate + 211231;
898 randstate = 11117 * randstate + 211231;
899 randstate = 11117 * randstate + 211231;
901 sample = randstate / (0x7fffffff * 1.000001);
902 sample = 0.2 * sample - 0.9 * last_val;
906 sample += sin (2.0 * k * M_PI * 1.0 / 32.0);
908 #if !defined _MSC_VER
909 write_little_endian_int16(f, lrintf(sample * 32700.0));
911 write_little_endian_int16(f, (FLAC__int16)(sample * 32700.0));
923 static FLAC__bool generate_wackywav64s(void)
927 0x72,0x69,0x66,0x66,0x2E,0x91,0xCF,0x11, /* RIFF GUID */
928 0xA5,0xD6,0x28,0xDB,0x04,0xC1,0x00,0x00,
929 152, 0, 0, 0, 0, 0, 0, 0,
930 0x77,0x61,0x76,0x65,0xF3,0xAC,0xD3,0x11, /* WAVE GUID */
931 0x8C,0xD1,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
932 0x6A,0x75,0x6E,0x6B,0xF3,0xAC,0xD3,0x11, /* junk GUID */
933 0x8C,0xD1,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
934 32, 0, 0, 0 , 0, 0, 0, 0,
935 'b', 'l', 'a', 'h', 'b', 'l', 'a', 'h',
936 0x66,0x6D,0x74,0x20,0xF3,0xAC,0xD3,0x11, /* fmt GUID */
937 0x8C,0xD1,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
938 40, 0, 0, 0 , 0, 0, 0, 0,
939 1, 0, 1, 0,0x44,0xAC, 0, 0,
940 0x88,0x58,0x01, 0, 2, 0, 16, 0,
941 0x64,0x61,0x74,0x61,0xF3,0xAC,0xD3,0x11, /* data GUID */
942 0x8C,0xD1,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
943 40, 0, 0, 0 , 0, 0, 0, 0,
944 0, 0, 1, 0, 4, 0, 9, 0,
945 16, 0, 25, 0, 36, 0, 49, 0,
946 0x6A,0x75,0x6E,0x6B,0xF3,0xAC,0xD3,0x11, /* junk GUID */
947 0x8C,0xD1,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
948 32, 0, 0, 0 , 0, 0, 0, 0,
949 'b', 'l', 'a', 'h', 'b', 'l', 'a', 'h'
952 if(0 == (f = flac_fopen("wacky1.w64", "wb")))
954 if(fwrite(wav, 1, wav[16], f) < wav[16])
959 if(0 == (f = flac_fopen("wacky2.w64", "wb")))
961 if(fwrite(wav, 1, wav[16], f) < wav[16])
971 static FLAC__bool generate_wackyrf64s(void)
975 'R', 'F', '6', '4', 255, 255, 255, 255,
976 'W', 'A', 'V', 'E', 'd', 's', '6', '4',
977 28, 0, 0, 0, 112, 0, 0, 0,
978 0, 0, 0, 0, 16, 0, 0, 0,
979 0, 0, 0, 0, 8, 0, 0, 0,
980 0, 0, 0, 0, 0, 0, 0, 0,
982 4, 0, 0, 0, 'b', 'l', 'a', 'h',
983 'p', 'a', 'd', ' ', 4, 0, 0, 0,
984 'B', 'L', 'A', 'H', 'f', 'm', 't', ' ',
985 16, 0, 0, 0, 1, 0, 1, 0,
986 0x44,0xAC, 0, 0,0x88,0x58,0x01, 0,
987 2, 0, 16, 0, 'd', 'a', 't', 'a',
988 255, 255, 255, 255, 0, 0, 1, 0,
989 4, 0, 9, 0, 16, 0, 25, 0,
990 36, 0, 49, 0, 'p', 'a', 'd', ' ',
991 4, 0, 0, 0, 'b', 'l', 'a', 'h'
994 if(0 == (f = flac_fopen("wacky1.rf64", "wb")))
996 if(fwrite(wav, 1, 120, f) < 120)
1001 if(0 == (f = flac_fopen("wacky2.rf64", "wb")))
1003 if(fwrite(wav, 1, 132, f) < 132)
1013 int main(int argc, char *argv[])
1015 FLAC__uint32 test = 1;
1018 int pattern01[] = { 1, -1, 0 };
1019 int pattern02[] = { 1, 1, -1, 0 };
1020 int pattern03[] = { 1, -1, -1, 0 };
1021 int pattern04[] = { 1, -1, 1, -1, 0 };
1022 int pattern05[] = { 1, -1, -1, 1, 0 };
1023 int pattern06[] = { 1, -1, 1, 1, -1, 0 };
1024 int pattern07[] = { 1, -1, -1, 1, -1, 0 };
1028 is_big_endian_host = (*((FLAC__byte*)(&test)))? false : true;
1030 #if !defined _MSC_VER && !defined __MINGW32__
1034 if(gettimeofday(&tv, 0) < 0) {
1035 fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
1038 srandom(tv.tv_usec);
1041 srand((unsigned)time(0));
1044 if(!generate_01()) return 1;
1045 if(!generate_02()) return 1;
1046 if(!generate_03()) return 1;
1047 if(!generate_04()) return 1;
1049 if(!generate_fsd8("fsd8-01.raw", pattern01, 100)) return 1;
1050 if(!generate_fsd8("fsd8-02.raw", pattern02, 100)) return 1;
1051 if(!generate_fsd8("fsd8-03.raw", pattern03, 100)) return 1;
1052 if(!generate_fsd8("fsd8-04.raw", pattern04, 100)) return 1;
1053 if(!generate_fsd8("fsd8-05.raw", pattern05, 100)) return 1;
1054 if(!generate_fsd8("fsd8-06.raw", pattern06, 100)) return 1;
1055 if(!generate_fsd8("fsd8-07.raw", pattern07, 100)) return 1;
1057 if(!generate_fsd16("fsd16-01.raw", pattern01, 100)) return 1;
1058 if(!generate_fsd16("fsd16-02.raw", pattern02, 100)) return 1;
1059 if(!generate_fsd16("fsd16-03.raw", pattern03, 100)) return 1;
1060 if(!generate_fsd16("fsd16-04.raw", pattern04, 100)) return 1;
1061 if(!generate_fsd16("fsd16-05.raw", pattern05, 100)) return 1;
1062 if(!generate_fsd16("fsd16-06.raw", pattern06, 100)) return 1;
1063 if(!generate_fsd16("fsd16-07.raw", pattern07, 100)) return 1;
1065 if(!generate_fsd24("fsd24-01.raw", pattern01, 100)) return 1;
1066 if(!generate_fsd24("fsd24-02.raw", pattern02, 100)) return 1;
1067 if(!generate_fsd24("fsd24-03.raw", pattern03, 100)) return 1;
1068 if(!generate_fsd24("fsd24-04.raw", pattern04, 100)) return 1;
1069 if(!generate_fsd24("fsd24-05.raw", pattern05, 100)) return 1;
1070 if(!generate_fsd24("fsd24-06.raw", pattern06, 100)) return 1;
1071 if(!generate_fsd24("fsd24-07.raw", pattern07, 100)) return 1;
1073 if(!generate_wbps16("wbps16-01.raw", 1000)) return 1;
1075 if(!generate_sine8_1("sine8-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
1076 if(!generate_sine8_1("sine8-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
1077 if(!generate_sine8_1("sine8-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
1078 if(!generate_sine8_1("sine8-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
1079 if(!generate_sine8_1("sine8-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
1081 if(!generate_sine8_2("sine8-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
1082 if(!generate_sine8_2("sine8-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
1083 if(!generate_sine8_2("sine8-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
1084 if(!generate_sine8_2("sine8-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
1085 if(!generate_sine8_2("sine8-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
1086 if(!generate_sine8_2("sine8-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
1087 if(!generate_sine8_2("sine8-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
1088 if(!generate_sine8_2("sine8-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
1089 if(!generate_sine8_2("sine8-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
1090 if(!generate_sine8_2("sine8-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
1092 if(!generate_sine16_1("sine16-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
1093 if(!generate_sine16_1("sine16-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
1094 if(!generate_sine16_1("sine16-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
1095 if(!generate_sine16_1("sine16-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
1096 if(!generate_sine16_1("sine16-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
1098 if(!generate_sine16_2("sine16-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
1099 if(!generate_sine16_2("sine16-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
1100 if(!generate_sine16_2("sine16-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
1101 if(!generate_sine16_2("sine16-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
1102 if(!generate_sine16_2("sine16-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
1103 if(!generate_sine16_2("sine16-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
1104 if(!generate_sine16_2("sine16-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
1105 if(!generate_sine16_2("sine16-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
1106 if(!generate_sine16_2("sine16-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
1107 if(!generate_sine16_2("sine16-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
1109 if(!generate_sine24_1("sine24-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
1110 if(!generate_sine24_1("sine24-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
1111 if(!generate_sine24_1("sine24-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
1112 if(!generate_sine24_1("sine24-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
1113 if(!generate_sine24_1("sine24-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
1115 if(!generate_sine24_2("sine24-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
1116 if(!generate_sine24_2("sine24-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
1117 if(!generate_sine24_2("sine24-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
1118 if(!generate_sine24_2("sine24-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
1119 if(!generate_sine24_2("sine24-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
1120 if(!generate_sine24_2("sine24-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
1121 if(!generate_sine24_2("sine24-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
1122 if(!generate_sine24_2("sine24-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
1123 if(!generate_sine24_2("sine24-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
1124 if(!generate_sine24_2("sine24-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
1126 /* WATCHOUT: the size of noise.raw is hardcoded into test/test_flac.sh */
1127 if(!generate_noise("noise.raw", 65536 * 8 * 3)) return 1;
1128 if(!generate_noise("noise8m32.raw", 32)) return 1;
1129 if(!generate_wackywavs()) return 1;
1130 if(!generate_wackywav64s()) return 1;
1131 if(!generate_wackyrf64s()) return 1;
1132 if(!generate_noisy_sine()) return 1;
1133 for(channels = 1; channels <= 8; channels *= 2) {
1134 unsigned bits_per_sample;
1135 for(bits_per_sample = 8; bits_per_sample <= 24; bits_per_sample += 4) {
1136 static const unsigned nsamples[] = { 1, 111, 4777 } ;
1138 for(samples = 0; samples < sizeof(nsamples)/sizeof(nsamples[0]); samples++) {
1141 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.aiff", channels, bits_per_sample, nsamples[samples]);
1142 if(!generate_aiff(fn, 44100, channels, bits_per_sample, nsamples[samples]))
1145 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.wav", channels, bits_per_sample, nsamples[samples]);
1146 if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/0))
1149 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.rf64", channels, bits_per_sample, nsamples[samples]);
1150 if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/1))
1153 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.w64", channels, bits_per_sample, nsamples[samples]);
1154 if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/2))
1157 if(bits_per_sample % 8 == 0) {
1158 flac_snprintf(fn, sizeof (fn), "rt-%u-%u-%u.raw", channels, bits_per_sample, nsamples[samples]);
1159 if(!generate_raw(fn, channels, bits_per_sample/8, nsamples[samples]))