1 /* test_streams - Simple test pattern generator
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
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.
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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "share/compat.h"
27 #if defined _MSC_VER || defined __MINGW32__
32 #include "FLAC/assert.h"
33 #include "FLAC/ordinals.h"
36 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
37 #define M_PI 3.14159265358979323846
40 #if !defined _MSC_VER && !defined __MINGW32__
41 #define GET_RANDOM_BYTE (((unsigned)random()) & 0xff)
43 #define GET_RANDOM_BYTE (((unsigned)rand()) & 0xff)
46 static FLAC__bool is_big_endian_host;
49 static FLAC__bool write_little_endian(FILE *f, FLAC__int32 x, size_t bytes)
52 if(fputc(x, f) == EOF)
60 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
64 fputc(x >> 8, f) != EOF
68 static FLAC__bool write_little_endian_int16(FILE *f, FLAC__int16 x)
70 return write_little_endian_uint16(f, (FLAC__uint16)x);
73 static FLAC__bool write_little_endian_uint24(FILE *f, FLAC__uint32 x)
77 fputc(x >> 8, f) != EOF &&
78 fputc(x >> 16, f) != EOF
82 static FLAC__bool write_little_endian_int24(FILE *f, FLAC__int32 x)
84 return write_little_endian_uint24(f, (FLAC__uint32)x);
87 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
91 fputc(x >> 8, f) != EOF &&
92 fputc(x >> 16, f) != EOF &&
93 fputc(x >> 24, f) != EOF
98 /* @@@ not used (yet) */
99 static FLAC__bool write_little_endian_int32(FILE *f, FLAC__int32 x)
101 return write_little_endian_uint32(f, (FLAC__uint32)x);
105 static FLAC__bool write_little_endian_uint64(FILE *f, FLAC__uint64 x)
108 fputc(x, f) != EOF &&
109 fputc(x >> 8, f) != EOF &&
110 fputc(x >> 16, f) != EOF &&
111 fputc(x >> 24, f) != EOF &&
112 fputc(x >> 32, f) != EOF &&
113 fputc(x >> 40, f) != EOF &&
114 fputc(x >> 48, f) != EOF &&
115 fputc(x >> 56, f) != EOF
119 static FLAC__bool write_big_endian(FILE *f, FLAC__int32 x, size_t bytes)
124 if(fputc(x>>24, f) == EOF)
132 static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 x)
135 fputc(x >> 8, f) != EOF &&
141 /* @@@ not used (yet) */
142 static FLAC__bool write_big_endian_int16(FILE *f, FLAC__int16 x)
144 return write_big_endian_uint16(f, (FLAC__uint16)x);
149 /* @@@ not used (yet) */
150 static FLAC__bool write_big_endian_uint24(FILE *f, FLAC__uint32 x)
153 fputc(x >> 16, f) != EOF &&
154 fputc(x >> 8, f) != EOF &&
161 /* @@@ not used (yet) */
162 static FLAC__bool write_big_endian_int24(FILE *f, FLAC__int32 x)
164 return write_big_endian_uint24(f, (FLAC__uint32)x);
168 static FLAC__bool write_big_endian_uint32(FILE *f, FLAC__uint32 x)
171 fputc(x >> 24, f) != EOF &&
172 fputc(x >> 16, f) != EOF &&
173 fputc(x >> 8, f) != EOF &&
179 /* @@@ not used (yet) */
180 static FLAC__bool write_big_endian_int32(FILE *f, FLAC__int32 x)
182 return write_big_endian_uint32(f, (FLAC__uint32)x);
186 static FLAC__bool write_sane_extended(FILE *f, unsigned val)
187 /* Write to 'f' a SANE extended representation of 'val'. Return false if
188 * the write succeeds; return true otherwise.
190 * SANE extended is an 80-bit IEEE-754 representation with sign bit, 15 bits
191 * of exponent, and 64 bits of significand (mantissa). Unlike most IEEE-754
192 * representations, it does not imply a 1 above the MSB of the significand.
198 unsigned int shift, exponent;
200 FLAC__ASSERT(val!=0U); /* handling 0 would require a special case */
202 for(shift= 0U; (val>>(31-shift))==0U; ++shift)
205 exponent= 63U-(shift+32U); /* add 32 for unused second word */
207 if(!write_big_endian_uint16(f, (FLAC__uint16)(exponent+0x3FFF)))
209 if(!write_big_endian_uint32(f, val))
211 if(!write_big_endian_uint32(f, 0)) /* unused second word */
217 /* a mono one-sample 16bps stream */
218 static FLAC__bool generate_01(void)
221 FLAC__int16 x = -32768;
223 if(0 == (f = fopen("test01.raw", "wb")))
226 if(!write_little_endian_int16(f, x))
236 /* a stereo one-sample 16bps stream */
237 static FLAC__bool generate_02(void)
240 FLAC__int16 xl = -32768, xr = 32767;
242 if(0 == (f = fopen("test02.raw", "wb")))
245 if(!write_little_endian_int16(f, xl))
247 if(!write_little_endian_int16(f, xr))
257 /* a mono five-sample 16bps stream */
258 static FLAC__bool generate_03(void)
261 FLAC__int16 x[] = { -25, 0, 25, 50, 100 };
264 if(0 == (f = fopen("test03.raw", "wb")))
267 for(i = 0; i < 5; i++)
268 if(!write_little_endian_int16(f, x[i]))
278 /* a stereo five-sample 16bps stream */
279 static FLAC__bool generate_04(void)
282 FLAC__int16 x[] = { -25, 500, 0, 400, 25, 300, 50, 200, 100, 100 };
285 if(0 == (f = fopen("test04.raw", "wb")))
288 for(i = 0; i < 10; i++)
289 if(!write_little_endian_int16(f, x[i]))
299 /* a mono full-scale deflection 8bps stream */
300 static FLAC__bool generate_fsd8(const char *fn, const int pattern[], unsigned reps)
305 FLAC__ASSERT(pattern != 0);
307 if(0 == (f = fopen(fn, "wb")))
310 for(rep = 0; rep < reps; rep++) {
311 for(p = 0; pattern[p]; p++) {
312 signed char x = pattern[p] > 0? 127 : -128;
313 if(fwrite(&x, sizeof(x), 1, f) < 1)
325 /* a mono full-scale deflection 16bps stream */
326 static FLAC__bool generate_fsd16(const char *fn, const int pattern[], unsigned reps)
331 FLAC__ASSERT(pattern != 0);
333 if(0 == (f = fopen(fn, "wb")))
336 for(rep = 0; rep < reps; rep++) {
337 for(p = 0; pattern[p]; p++) {
338 FLAC__int16 x = pattern[p] > 0? 32767 : -32768;
339 if(!write_little_endian_int16(f, x))
351 /* a stereo wasted-bits-per-sample 16bps stream */
352 static FLAC__bool generate_wbps16(const char *fn, unsigned samples)
357 if(0 == (f = fopen(fn, "wb")))
360 for(sample = 0; sample < samples; sample++) {
361 FLAC__int16 l = (sample % 2000) << 2;
362 FLAC__int16 r = (sample % 1000) << 3;
363 if(!write_little_endian_int16(f, l))
365 if(!write_little_endian_int16(f, r))
376 /* a mono full-scale deflection 24bps stream */
377 static FLAC__bool generate_fsd24(const char *fn, const int pattern[], unsigned reps)
382 FLAC__ASSERT(pattern != 0);
384 if(0 == (f = fopen(fn, "wb")))
387 for(rep = 0; rep < reps; rep++) {
388 for(p = 0; pattern[p]; p++) {
389 FLAC__int32 x = pattern[p] > 0? 8388607 : -8388608;
390 if(!write_little_endian_int24(f, x))
402 /* a mono sine-wave 8bps stream */
403 static FLAC__bool generate_sine8_1(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2)
405 const FLAC__int8 full_scale = 127;
406 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
407 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
409 double theta1, theta2;
412 if(0 == (f = fopen(fn, "wb")))
415 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
416 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
417 FLAC__int8 v = (FLAC__int8)(val + 0.5);
418 if(fwrite(&v, sizeof(v), 1, f) < 1)
429 /* a stereo sine-wave 8bps stream */
430 static FLAC__bool generate_sine8_2(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2, double fmult)
432 const FLAC__int8 full_scale = 127;
433 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
434 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
436 double theta1, theta2;
439 if(0 == (f = fopen(fn, "wb")))
442 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
443 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
444 FLAC__int8 v = (FLAC__int8)(val + 0.5);
445 if(fwrite(&v, sizeof(v), 1, f) < 1)
447 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
448 v = (FLAC__int8)(val + 0.5);
449 if(fwrite(&v, sizeof(v), 1, f) < 1)
460 /* a mono sine-wave 16bps stream */
461 static FLAC__bool generate_sine16_1(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2)
463 const FLAC__int16 full_scale = 32767;
464 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
465 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
467 double theta1, theta2;
470 if(0 == (f = fopen(fn, "wb")))
473 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
474 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
475 FLAC__int16 v = (FLAC__int16)(val + 0.5);
476 if(!write_little_endian_int16(f, v))
487 /* a stereo sine-wave 16bps stream */
488 static FLAC__bool generate_sine16_2(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2, double fmult)
490 const FLAC__int16 full_scale = 32767;
491 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
492 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
494 double theta1, theta2;
497 if(0 == (f = fopen(fn, "wb")))
500 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
501 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
502 FLAC__int16 v = (FLAC__int16)(val + 0.5);
503 if(!write_little_endian_int16(f, v))
505 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
506 v = (FLAC__int16)(val + 0.5);
507 if(!write_little_endian_int16(f, v))
518 /* a mono sine-wave 24bps stream */
519 static FLAC__bool generate_sine24_1(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2)
521 const FLAC__int32 full_scale = 0x7fffff;
522 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
523 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
525 double theta1, theta2;
528 if(0 == (f = fopen(fn, "wb")))
531 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
532 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
533 FLAC__int32 v = (FLAC__int32)(val + 0.5);
534 if(!write_little_endian_int24(f, v))
545 /* a stereo sine-wave 24bps stream */
546 static FLAC__bool generate_sine24_2(const char *fn, const double sample_rate, const unsigned samples, const double f1, const double a1, const double f2, const double a2, double fmult)
548 const FLAC__int32 full_scale = 0x7fffff;
549 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
550 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
552 double theta1, theta2;
555 if(0 == (f = fopen(fn, "wb")))
558 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
559 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
560 FLAC__int32 v = (FLAC__int32)(val + 0.5);
561 if(!write_little_endian_int24(f, v))
563 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
564 v = (FLAC__int32)(val + 0.5);
565 if(!write_little_endian_int24(f, v))
576 static FLAC__bool generate_noise(const char *fn, unsigned bytes)
581 if(0 == (f = fopen(fn, "wb")))
584 for(b = 0; b < bytes; b++) {
585 #if !defined _MSC_VER && !defined __MINGW32__
586 FLAC__byte x = (FLAC__byte)(((unsigned)random()) & 0xff);
588 FLAC__byte x = (FLAC__byte)(((unsigned)rand()) & 0xff);
590 if(fwrite(&x, sizeof(x), 1, f) < 1)
601 static FLAC__bool generate_raw(const char *filename, unsigned channels, unsigned bytes_per_sample, unsigned samples)
603 const FLAC__int32 full_scale = (1 << (bytes_per_sample*8-1)) - 1;
604 const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
605 const double delta1 = 2.0 * M_PI / ( 44100.0 / f1);
606 const double delta2 = 2.0 * M_PI / ( 44100.0 / f2);
607 double theta1, theta2;
611 if(0 == (f = fopen(filename, "wb")))
614 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
615 for(j = 0; j < channels; j++) {
616 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
617 FLAC__int32 v = (FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8);
618 if(!write_little_endian(f, v, bytes_per_sample))
630 static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples)
632 const unsigned bytes_per_sample = (bps+7)/8;
633 const unsigned true_size = channels * bytes_per_sample * samples;
634 const unsigned padded_size = (true_size + 1) & (~1u);
635 const unsigned shift = (bps%8)? 8 - (bps%8) : 0;
636 const FLAC__int32 full_scale = (1 << (bps-1)) - 1;
637 const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
638 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
639 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
640 double theta1, theta2;
644 if(0 == (f = fopen(filename, "wb")))
646 if(fwrite("FORM", 1, 4, f) < 4)
648 if(!write_big_endian_uint32(f, padded_size + 46))
650 if(fwrite("AIFFCOMM\000\000\000\022", 1, 12, f) < 12)
652 if(!write_big_endian_uint16(f, (FLAC__uint16)channels))
654 if(!write_big_endian_uint32(f, samples))
656 if(!write_big_endian_uint16(f, (FLAC__uint16)bps))
658 if(!write_sane_extended(f, sample_rate))
660 if(fwrite("SSND", 1, 4, f) < 4)
662 if(!write_big_endian_uint32(f, true_size + 8))
664 if(fwrite("\000\000\000\000\000\000\000\000", 1, 8, f) < 8)
667 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
668 for(j = 0; j < channels; j++) {
669 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
670 FLAC__int32 v = ((FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8)) << shift;
671 if(!write_big_endian(f, v, bytes_per_sample))
675 for(i = true_size; i < padded_size; i++)
676 if(fputc(0, f) == EOF)
686 /* flavor is: 0:WAVE, 1:RF64, 2:WAVE64 */
687 static FLAC__bool generate_wav(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples, FLAC__bool strict, int flavor)
689 const FLAC__bool waveformatextensible = strict && (channels > 2 || (bps%8));
691 * (bps%8) allows 24 bps which is technically supposed to be WAVEFORMATEXTENSIBLE but we
692 * write 24bps as WAVEFORMATEX since it's unambiguous and matches how flac writes it
695 const unsigned bytes_per_sample = (bps+7)/8;
696 const unsigned shift = (bps%8)? 8 - (bps%8) : 0;
697 /* this rig is not going over 4G so we're ok with 32-bit sizes here */
698 const FLAC__uint32 true_size = channels * bytes_per_sample * samples;
699 const FLAC__uint32 padded_size = flavor<2? (true_size + 1) & (~1u) : (true_size + 7) & (~7u);
700 const FLAC__int32 full_scale = (1 << (bps-1)) - 1;
701 const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
702 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
703 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
704 double theta1, theta2;
708 if(0 == (f = fopen(filename, "wb")))
710 /* RIFFxxxxWAVE or equivalent: */
713 if(fwrite("RIFF", 1, 4, f) < 4)
716 /* +8+{40,16} for fmt chunk */
717 /* +8 for data chunk header */
718 if(!write_little_endian_uint32(f, 4 + 8+(waveformatextensible?40:16) + 8 + padded_size))
720 if(fwrite("WAVE", 1, 4, f) < 4)
724 if(fwrite("RF64", 1, 4, f) < 4)
726 if(!write_little_endian_uint32(f, 0xffffffff))
728 if(fwrite("WAVE", 1, 4, f) < 4)
732 /* RIFF GUID 66666972-912E-11CF-A5D6-28DB04C10000 */
733 if(fwrite("\x72\x69\x66\x66\x2E\x91\xCF\x11\xD6\xA5\x28\xDB\x04\xC1\x00\x00", 1, 16, f) < 16)
735 /* +(16+8) for RIFF GUID + size */
736 /* +16 for WAVE GUID */
737 /* +16+8+{40,16} for fmt chunk */
738 /* +16+8 for data chunk header */
739 if(!write_little_endian_uint64(f, (16+8) + 16 + 16+8+(waveformatextensible?40:16) + (16+8) + padded_size))
741 /* WAVE GUID 65766177-ACF3-11D3-8CD1-00C04F8EDB8A */
742 if(fwrite("\x77\x61\x76\x65\xF3\xAC\xD3\x11\xD1\x8C\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) < 16)
748 if(flavor == 1) { /* rf64 */
749 if(fwrite("ds64", 1, 4, f) < 4)
751 if(!write_little_endian_uint32(f, 28)) /* ds64 chunk size */
753 if(!write_little_endian_uint64(f, 36 + padded_size + (waveformatextensible?60:36)))
755 if(!write_little_endian_uint64(f, true_size))
757 if(!write_little_endian_uint64(f, samples))
759 if(!write_little_endian_uint32(f, 0)) /* table size */
764 if(fwrite("fmt ", 1, 4, f) < 4)
767 if(!write_little_endian_uint32(f, waveformatextensible?40:16))
771 /* fmt GUID 20746D66-ACF3-11D3-8CD1-00C04F8EDB8A */
772 if(fwrite("\x66\x6D\x74\x20\xF3\xAC\xD3\x11\xD1\x8C\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) < 16)
774 /* chunk size (+16+8 for GUID and size fields) */
775 if(!write_little_endian_uint64(f, 16+8+(waveformatextensible?40:16)))
778 if(!write_little_endian_uint16(f, (FLAC__uint16)(waveformatextensible?65534:1)))
780 if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
782 if(!write_little_endian_uint32(f, sample_rate))
784 if(!write_little_endian_uint32(f, sample_rate * channels * bytes_per_sample))
786 if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * bytes_per_sample))) /* block align */
788 if(!write_little_endian_uint16(f, (FLAC__uint16)(bps+shift)))
790 if(waveformatextensible) {
791 if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
793 if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
795 if(!write_little_endian_uint32(f, 0)) /* channelMask */
797 /* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
798 if(fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
803 if(fwrite("data", 1, 4, f) < 4)
805 if(!write_little_endian_uint32(f, flavor==1? 0xffffffff : true_size))
809 /* data GUID 61746164-ACF3-11D3-8CD1-00C04F8EDB8A */
810 if(fwrite("\x64\x61\x74\x61\xF3\xAC\xD3\x11\xD1\x8C\x00\xC0\x4F\x8E\xDB\x8A", 1, 16, f) != 16)
812 /* +16+8 for GUID and size fields */
813 if(!write_little_endian_uint64(f, 16+8 + true_size))
817 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
818 for(j = 0; j < channels; j++) {
819 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
820 FLAC__int32 v = ((FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8)) << shift;
821 if(!write_little_endian(f, v, bytes_per_sample))
825 for(i = true_size; i < padded_size; i++)
826 if(fputc(0, f) == EOF)
836 static FLAC__bool generate_wackywavs(void)
840 'R', 'I', 'F', 'F', 76, 0, 0, 0,
841 'W', 'A', 'V', 'E', 'j', 'u', 'n', 'k',
842 4, 0, 0, 0 , 'b', 'l', 'a', 'h',
843 'p', 'a', 'd', ' ', 4, 0, 0, 0,
844 'B', 'L', 'A', 'H', 'f', 'm', 't', ' ',
845 16, 0, 0, 0, 1, 0, 1, 0,
846 0x44,0xAC, 0, 0,0x88,0x58,0x01, 0,
847 2, 0, 16, 0, 'd', 'a', 't', 'a',
848 16, 0, 0, 0, 0, 0, 1, 0,
849 4, 0, 9, 0, 16, 0, 25, 0,
850 36, 0, 49, 0, 'p', 'a', 'd', ' ',
851 4, 0, 0, 0, 'b', 'l', 'a', 'h'
854 if(0 == (f = fopen("wacky1.wav", "wb")))
856 if(fwrite(wav, 1, 84, f) < 84)
861 if(0 == (f = fopen("wacky2.wav", "wb")))
863 if(fwrite(wav, 1, 96, f) < 96)
873 static FLAC__bool generate_noisy_sine(void)
877 'R', 'I', 'F', 'F', 76, 0, 0, 0,
878 'W', 'A', 'V', 'E', 'f', 'm', 't', ' ',
879 16, 0, 0, 0, 1, 0, 1, 0,
880 0x44,0xAC, 0, 0,0x88,0x58,0x01, 0,
881 2, 0, 16, 0, 'd', 'a', 't', 'a',
884 int32_t randstate = 0x1243456;
885 double sample, last_val = 0.0;
888 if(0 == (f = fopen("noisy-sine.wav", "wb")))
890 if(fwrite(wav, 1, sizeof (wav), f) < sizeof (wav))
893 for (k = 0 ; k < 5 * 44100 ; k++) {
894 /* Obvioulsy not a crypto quality RNG. */
895 randstate = 11117 * randstate + 211231;
896 randstate = 11117 * randstate + 211231;
897 randstate = 11117 * randstate + 211231;
899 sample = randstate / (0x7fffffff * 1.000001);
900 sample = 0.2 * sample - 0.9 * last_val;
904 sample += sin (2.0 * k * M_PI * 1.0 / 32.0);
906 #if !defined _MSC_VER
907 write_little_endian_int16(f, lrintf(sample * 32700.0));
909 write_little_endian_int16(f, (FLAC__int16)(sample * 32700.0));
921 static FLAC__bool generate_wackywav64s(void)
925 0x72,0x69,0x66,0x66,0x2E,0x91,0xCF,0x11, /* RIFF GUID */
926 0xD6,0xA5,0x28,0xDB,0x04,0xC1,0x00,0x00,
927 152, 0, 0, 0, 0, 0, 0, 0,
928 0x77,0x61,0x76,0x65,0xF3,0xAC,0xD3,0x11, /* WAVE GUID */
929 0xD1,0x8C,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
930 0x6A,0x75,0x6E,0x6B,0xF3,0xAC,0xD3,0x11, /* junk GUID */
931 0xD1,0x8C,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
932 32, 0, 0, 0 , 0, 0, 0, 0,
933 'b', 'l', 'a', 'h', 'b', 'l', 'a', 'h',
934 0x66,0x6D,0x74,0x20,0xF3,0xAC,0xD3,0x11, /* fmt GUID */
935 0xD1,0x8C,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
936 40, 0, 0, 0 , 0, 0, 0, 0,
937 1, 0, 1, 0,0x44,0xAC, 0, 0,
938 0x88,0x58,0x01, 0, 2, 0, 16, 0,
939 0x64,0x61,0x74,0x61,0xF3,0xAC,0xD3,0x11, /* data GUID */
940 0xD1,0x8C,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
941 40, 0, 0, 0 , 0, 0, 0, 0,
942 0, 0, 1, 0, 4, 0, 9, 0,
943 16, 0, 25, 0, 36, 0, 49, 0,
944 0x6A,0x75,0x6E,0x6B,0xF3,0xAC,0xD3,0x11, /* junk GUID */
945 0xD1,0x8C,0x00,0xC0,0x4F,0x8E,0xDB,0x8A,
946 32, 0, 0, 0 , 0, 0, 0, 0,
947 'b', 'l', 'a', 'h', 'b', 'l', 'a', 'h'
950 if(0 == (f = fopen("wacky1.w64", "wb")))
952 if(fwrite(wav, 1, wav[16], f) < wav[16])
957 if(0 == (f = fopen("wacky2.w64", "wb")))
959 if(fwrite(wav, 1, wav[16], f) < wav[16])
969 static FLAC__bool generate_wackyrf64s(void)
973 'R', 'F', '6', '4', 255, 255, 255, 255,
974 'W', 'A', 'V', 'E', 'd', 's', '6', '4',
975 28, 0, 0, 0, 112, 0, 0, 0,
976 0, 0, 0, 0, 16, 0, 0, 0,
977 0, 0, 0, 0, 8, 0, 0, 0,
978 0, 0, 0, 0, 0, 0, 0, 0,
980 4, 0, 0, 0, 'b', 'l', 'a', 'h',
981 'p', 'a', 'd', ' ', 4, 0, 0, 0,
982 'B', 'L', 'A', 'H', 'f', 'm', 't', ' ',
983 16, 0, 0, 0, 1, 0, 1, 0,
984 0x44,0xAC, 0, 0,0x88,0x58,0x01, 0,
985 2, 0, 16, 0, 'd', 'a', 't', 'a',
986 255, 255, 255, 255, 0, 0, 1, 0,
987 4, 0, 9, 0, 16, 0, 25, 0,
988 36, 0, 49, 0, 'p', 'a', 'd', ' ',
989 4, 0, 0, 0, 'b', 'l', 'a', 'h'
992 if(0 == (f = fopen("wacky1.rf64", "wb")))
994 if(fwrite(wav, 1, 120, f) < 120)
999 if(0 == (f = fopen("wacky2.rf64", "wb")))
1001 if(fwrite(wav, 1, 132, f) < 132)
1011 int main(int argc, char *argv[])
1013 FLAC__uint32 test = 1;
1016 int pattern01[] = { 1, -1, 0 };
1017 int pattern02[] = { 1, 1, -1, 0 };
1018 int pattern03[] = { 1, -1, -1, 0 };
1019 int pattern04[] = { 1, -1, 1, -1, 0 };
1020 int pattern05[] = { 1, -1, -1, 1, 0 };
1021 int pattern06[] = { 1, -1, 1, 1, -1, 0 };
1022 int pattern07[] = { 1, -1, -1, 1, -1, 0 };
1026 is_big_endian_host = (*((FLAC__byte*)(&test)))? false : true;
1028 #if !defined _MSC_VER && !defined __MINGW32__
1032 if(gettimeofday(&tv, 0) < 0) {
1033 fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
1036 srandom(tv.tv_usec);
1039 srand((unsigned)time(0));
1042 if(!generate_01()) return 1;
1043 if(!generate_02()) return 1;
1044 if(!generate_03()) return 1;
1045 if(!generate_04()) return 1;
1047 if(!generate_fsd8("fsd8-01.raw", pattern01, 100)) return 1;
1048 if(!generate_fsd8("fsd8-02.raw", pattern02, 100)) return 1;
1049 if(!generate_fsd8("fsd8-03.raw", pattern03, 100)) return 1;
1050 if(!generate_fsd8("fsd8-04.raw", pattern04, 100)) return 1;
1051 if(!generate_fsd8("fsd8-05.raw", pattern05, 100)) return 1;
1052 if(!generate_fsd8("fsd8-06.raw", pattern06, 100)) return 1;
1053 if(!generate_fsd8("fsd8-07.raw", pattern07, 100)) return 1;
1055 if(!generate_fsd16("fsd16-01.raw", pattern01, 100)) return 1;
1056 if(!generate_fsd16("fsd16-02.raw", pattern02, 100)) return 1;
1057 if(!generate_fsd16("fsd16-03.raw", pattern03, 100)) return 1;
1058 if(!generate_fsd16("fsd16-04.raw", pattern04, 100)) return 1;
1059 if(!generate_fsd16("fsd16-05.raw", pattern05, 100)) return 1;
1060 if(!generate_fsd16("fsd16-06.raw", pattern06, 100)) return 1;
1061 if(!generate_fsd16("fsd16-07.raw", pattern07, 100)) return 1;
1063 if(!generate_fsd24("fsd24-01.raw", pattern01, 100)) return 1;
1064 if(!generate_fsd24("fsd24-02.raw", pattern02, 100)) return 1;
1065 if(!generate_fsd24("fsd24-03.raw", pattern03, 100)) return 1;
1066 if(!generate_fsd24("fsd24-04.raw", pattern04, 100)) return 1;
1067 if(!generate_fsd24("fsd24-05.raw", pattern05, 100)) return 1;
1068 if(!generate_fsd24("fsd24-06.raw", pattern06, 100)) return 1;
1069 if(!generate_fsd24("fsd24-07.raw", pattern07, 100)) return 1;
1071 if(!generate_wbps16("wbps16-01.raw", 1000)) return 1;
1073 if(!generate_sine8_1("sine8-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
1074 if(!generate_sine8_1("sine8-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
1075 if(!generate_sine8_1("sine8-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
1076 if(!generate_sine8_1("sine8-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
1077 if(!generate_sine8_1("sine8-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
1079 if(!generate_sine8_2("sine8-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
1080 if(!generate_sine8_2("sine8-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
1081 if(!generate_sine8_2("sine8-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
1082 if(!generate_sine8_2("sine8-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
1083 if(!generate_sine8_2("sine8-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
1084 if(!generate_sine8_2("sine8-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
1085 if(!generate_sine8_2("sine8-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
1086 if(!generate_sine8_2("sine8-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
1087 if(!generate_sine8_2("sine8-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
1088 if(!generate_sine8_2("sine8-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
1090 if(!generate_sine16_1("sine16-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
1091 if(!generate_sine16_1("sine16-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
1092 if(!generate_sine16_1("sine16-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
1093 if(!generate_sine16_1("sine16-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
1094 if(!generate_sine16_1("sine16-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
1096 if(!generate_sine16_2("sine16-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
1097 if(!generate_sine16_2("sine16-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
1098 if(!generate_sine16_2("sine16-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
1099 if(!generate_sine16_2("sine16-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
1100 if(!generate_sine16_2("sine16-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
1101 if(!generate_sine16_2("sine16-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
1102 if(!generate_sine16_2("sine16-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
1103 if(!generate_sine16_2("sine16-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
1104 if(!generate_sine16_2("sine16-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
1105 if(!generate_sine16_2("sine16-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
1107 if(!generate_sine24_1("sine24-00.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49)) return 1;
1108 if(!generate_sine24_1("sine24-01.raw", 96000.0, 200000, 441.0, 0.61, 661.5, 0.37)) return 1;
1109 if(!generate_sine24_1("sine24-02.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49)) return 1;
1110 if(!generate_sine24_1("sine24-03.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49)) return 1;
1111 if(!generate_sine24_1("sine24-04.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
1113 if(!generate_sine24_2("sine24-10.raw", 48000.0, 200000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
1114 if(!generate_sine24_2("sine24-11.raw", 48000.0, 200000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
1115 if(!generate_sine24_2("sine24-12.raw", 96000.0, 200000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
1116 if(!generate_sine24_2("sine24-13.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
1117 if(!generate_sine24_2("sine24-14.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
1118 if(!generate_sine24_2("sine24-15.raw", 44100.0, 200000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
1119 if(!generate_sine24_2("sine24-16.raw", 44100.0, 200000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
1120 if(!generate_sine24_2("sine24-17.raw", 44100.0, 200000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
1121 if(!generate_sine24_2("sine24-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
1122 if(!generate_sine24_2("sine24-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
1124 /* WATCHOUT: the size of noise.raw is hardcoded into test/test_flac.sh */
1125 if(!generate_noise("noise.raw", 65536 * 8 * 3)) return 1;
1126 if(!generate_noise("noise8m32.raw", 32)) return 1;
1127 if(!generate_wackywavs()) return 1;
1128 if(!generate_wackywav64s()) return 1;
1129 if(!generate_wackyrf64s()) return 1;
1130 if(!generate_noisy_sine()) return 1;
1131 for(channels = 1; channels <= 8; channels *= 2) {
1132 unsigned bits_per_sample;
1133 for(bits_per_sample = 8; bits_per_sample <= 24; bits_per_sample += 4) {
1134 static const unsigned nsamples[] = { 1, 111, 4777 } ;
1136 for(samples = 0; samples < sizeof(nsamples)/sizeof(nsamples[0]); samples++) {
1139 sprintf(fn, "rt-%u-%u-%u.aiff", channels, bits_per_sample, nsamples[samples]);
1140 if(!generate_aiff(fn, 44100, channels, bits_per_sample, nsamples[samples]))
1143 sprintf(fn, "rt-%u-%u-%u.wav", channels, bits_per_sample, nsamples[samples]);
1144 if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/0))
1147 sprintf(fn, "rt-%u-%u-%u.rf64", channels, bits_per_sample, nsamples[samples]);
1148 if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/1))
1151 sprintf(fn, "rt-%u-%u-%u.w64", channels, bits_per_sample, nsamples[samples]);
1152 if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true, /*flavor=*/2))
1155 if(bits_per_sample % 8 == 0) {
1156 sprintf(fn, "rt-%u-%u-%u.raw", channels, bits_per_sample, nsamples[samples]);
1157 if(!generate_raw(fn, channels, bits_per_sample/8, nsamples[samples]))