1 /* test_streams - Simple test pattern generator
2 * Copyright (C) 2000,2001,2002 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
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.
22 #if !defined _MSC_VER && !defined __MINGW32__
25 #include "FLAC/assert.h"
26 #include "FLAC/ordinals.h"
29 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
30 #define M_PI 3.14159265358979323846
34 static const char *mode = "wb";
36 static const char *mode = "w";
39 static FLAC__bool is_big_endian_host;
41 /* some flavors of UNIX #define this */
45 static void swap16(FLAC__int16 *i)
47 unsigned char *x = (unsigned char *)i, b;
48 if(!is_big_endian_host) {
55 static void swap24(FLAC__byte *x)
57 if(is_big_endian_host) {
69 /* a mono one-sample 16bps stream */
70 static FLAC__bool generate_01()
73 FLAC__int16 x = -32768;
75 if(0 == (f = fopen("test01.raw", mode)))
79 if(fwrite(&x, sizeof(x), 1, f) < 1)
89 /* a stereo one-sample 16bps stream */
90 static FLAC__bool generate_02()
93 FLAC__int16 xl = -32768, xr = 32767;
95 if(0 == (f = fopen("test02.raw", mode)))
101 if(fwrite(&xl, sizeof(xl), 1, f) < 1)
103 if(fwrite(&xr, sizeof(xr), 1, f) < 1)
113 /* a mono five-sample 16bps stream */
114 static FLAC__bool generate_03()
117 FLAC__int16 x[] = { -25, 0, 25, 50, 100 };
120 if(0 == (f = fopen("test03.raw", mode)))
123 for(i = 0; i < 5; i++)
126 if(fwrite(&x, sizeof(FLAC__int16), 5, f) < 5)
136 /* a stereo five-sample 16bps stream */
137 static FLAC__bool generate_04()
140 FLAC__int16 x[] = { -25, 500, 0, 400, 25, 300, 50, 200, 100, 100 };
143 if(0 == (f = fopen("test04.raw", mode)))
146 for(i = 0; i < 10; i++)
149 if(fwrite(&x, sizeof(FLAC__int16), 10, f) < 10)
159 /* a mono full-scale deflection 8bps stream */
160 static FLAC__bool generate_fsd8(const char *fn, const int pattern[], unsigned reps)
165 FLAC__ASSERT(pattern != 0);
167 if(0 == (f = fopen(fn, mode)))
170 for(rep = 0; rep < reps; rep++) {
171 for(p = 0; pattern[p]; p++) {
172 signed char x = pattern[p] > 0? 127 : -128;
173 if(fwrite(&x, sizeof(x), 1, f) < 1)
185 /* a mono full-scale deflection 16bps stream */
186 static FLAC__bool generate_fsd16(const char *fn, const int pattern[], unsigned reps)
191 FLAC__ASSERT(pattern != 0);
193 if(0 == (f = fopen(fn, mode)))
196 for(rep = 0; rep < reps; rep++) {
197 for(p = 0; pattern[p]; p++) {
198 FLAC__int16 x = pattern[p] > 0? 32767 : -32768;
200 if(fwrite(&x, sizeof(x), 1, f) < 1)
212 /* a stereo wasted-bits-per-sample 16bps stream */
213 static FLAC__bool generate_wbps16(const char *fn, unsigned samples)
218 if(0 == (f = fopen(fn, mode)))
221 for(sample = 0; sample < samples; sample++) {
222 FLAC__int16 l = (sample % 2000) << 2;
223 FLAC__int16 r = (sample % 1000) << 3;
226 if(fwrite(&l, sizeof(l), 1, f) < 1)
228 if(fwrite(&r, sizeof(r), 1, f) < 1)
239 /* a mono full-scale deflection 24bps stream */
240 static FLAC__bool generate_fsd24(const char *fn, const int pattern[], unsigned reps)
245 FLAC__ASSERT(pattern != 0);
247 if(0 == (f = fopen(fn, mode)))
250 for(rep = 0; rep < reps; rep++) {
251 for(p = 0; pattern[p]; p++) {
252 FLAC__int32 x = pattern[p] > 0? 8388607 : -8388608;
253 FLAC__byte *b = (FLAC__byte*)(&x);
255 if(fwrite(b, 3, 1, f) < 1)
267 /* a mono sine-wave 16bps stream */
268 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)
270 const FLAC__int16 full_scale = 32767;
271 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
272 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
274 double theta1, theta2;
277 if(0 == (f = fopen(fn, mode)))
280 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
281 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
282 FLAC__int16 v = (FLAC__int16)(val + 0.5);
284 if(fwrite(&v, sizeof(v), 1, f) < 1)
295 /* a stereo sine-wave 16bps stream */
296 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)
298 const FLAC__int16 full_scale = 32767;
299 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
300 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
302 double theta1, theta2;
305 if(0 == (f = fopen(fn, mode)))
308 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
309 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
310 FLAC__int16 v = (FLAC__int16)(val + 0.5);
312 if(fwrite(&v, sizeof(v), 1, f) < 1)
314 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
315 v = (FLAC__int16)(val + 0.5);
317 if(fwrite(&v, sizeof(v), 1, f) < 1)
328 /* a mono sine-wave 24bps stream */
329 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)
331 const FLAC__int32 full_scale = 0x7fffff;
332 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
333 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
335 double theta1, theta2;
338 if(0 == (f = fopen(fn, mode)))
341 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
342 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
343 FLAC__int32 v = (FLAC__int32)(val + 0.5);
344 FLAC__byte *b = (FLAC__byte*)(&v);
346 if(fwrite(b, 3, 1, f) < 1)
357 /* a stereo sine-wave 24bps stream */
358 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)
360 const FLAC__int32 full_scale = 0x7fffff;
361 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
362 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
364 double theta1, theta2;
367 if(0 == (f = fopen(fn, mode)))
370 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
371 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
372 FLAC__int32 v = (FLAC__int32)(val + 0.5);
373 FLAC__byte *b = (FLAC__byte*)(&v);
375 if(fwrite(b, 3, 1, f) < 1)
377 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
378 v = (FLAC__int32)(val + 0.5);
380 if(fwrite(b, 3, 1, f) < 1)
391 static FLAC__bool generate_noise(const char *fn, unsigned bytes)
395 #if !defined _MSC_VER && !defined __MINGW32__
398 if(gettimeofday(&tv, 0) < 0) {
399 fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
404 /* who has the patience to figure out how to do RNG with VC++? */
407 if(0 == (f = fopen(fn, mode)))
410 for(b = 0; b < bytes; b++) {
411 #if !defined _MSC_VER && !defined __MINGW32__
412 FLAC__byte x = (FLAC__byte)(((unsigned)random()) & 0xff);
414 FLAC__byte x = (FLAC__byte)((((unsigned)generate_noise) >> 8) ^ (b * 17)); /* fake it */
416 if(fwrite(&x, sizeof(x), 1, f) < 1)
427 static FLAC__bool generate_wackywavs()
431 'R', 'I', 'F', 'F', 76, 0, 0, 0,
432 'W', 'A', 'V', 'E', 'f', 'a', 'c', 't',
433 4, 0, 0, 0 , 'b', 'l', 'a', 'h',
434 'p', 'a', 'd', ' ', 4, 0, 0, 0,
435 'B', 'L', 'A', 'H', 'f', 'm', 't', ' ',
436 16, 0, 0, 0, 1, 0, 1, 0,
437 0x44,0xAC, 0, 0, 0, 0, 0, 0,
438 2, 0, 16, 0, 'd', 'a', 't', 'a',
439 16, 0, 0, 0, 0, 0, 1, 0,
440 4, 0, 9, 0, 16, 0, 25, 0,
441 36, 0, 49, 0, 'p', 'a', 'd', ' ',
442 4, 0, 0, 0, 'b', 'l', 'a', 'h'
445 if(0 == (f = fopen("wacky1.wav", mode)))
447 if(fwrite(wav, 1, 84, f) < 84)
452 if(0 == (f = fopen("wacky2.wav", mode)))
454 if(fwrite(wav, 1, 96, f) < 96)
464 int main(int argc, char *argv[])
466 FLAC__uint32 test = 1;
468 int pattern01[] = { 1, -1, 0 };
469 int pattern02[] = { 1, 1, -1, 0 };
470 int pattern03[] = { 1, -1, -1, 0 };
471 int pattern04[] = { 1, -1, 1, -1, 0 };
472 int pattern05[] = { 1, -1, -1, 1, 0 };
473 int pattern06[] = { 1, -1, 1, 1, -1, 0 };
474 int pattern07[] = { 1, -1, -1, 1, -1, 0 };
478 is_big_endian_host = (*((FLAC__byte*)(&test)))? false : true;
480 if(!generate_01()) return 1;
481 if(!generate_02()) return 1;
482 if(!generate_03()) return 1;
483 if(!generate_04()) return 1;
485 if(!generate_fsd8("fsd8-01.raw", pattern01, 100)) return 1;
486 if(!generate_fsd8("fsd8-02.raw", pattern02, 100)) return 1;
487 if(!generate_fsd8("fsd8-03.raw", pattern03, 100)) return 1;
488 if(!generate_fsd8("fsd8-04.raw", pattern04, 100)) return 1;
489 if(!generate_fsd8("fsd8-05.raw", pattern05, 100)) return 1;
490 if(!generate_fsd8("fsd8-06.raw", pattern06, 100)) return 1;
491 if(!generate_fsd8("fsd8-07.raw", pattern07, 100)) return 1;
493 if(!generate_fsd16("fsd16-01.raw", pattern01, 100)) return 1;
494 if(!generate_fsd16("fsd16-02.raw", pattern02, 100)) return 1;
495 if(!generate_fsd16("fsd16-03.raw", pattern03, 100)) return 1;
496 if(!generate_fsd16("fsd16-04.raw", pattern04, 100)) return 1;
497 if(!generate_fsd16("fsd16-05.raw", pattern05, 100)) return 1;
498 if(!generate_fsd16("fsd16-06.raw", pattern06, 100)) return 1;
499 if(!generate_fsd16("fsd16-07.raw", pattern07, 100)) return 1;
501 if(!generate_fsd24("fsd24-01.raw", pattern01, 100)) return 1;
502 if(!generate_fsd24("fsd24-02.raw", pattern02, 100)) return 1;
503 if(!generate_fsd24("fsd24-03.raw", pattern03, 100)) return 1;
504 if(!generate_fsd24("fsd24-04.raw", pattern04, 100)) return 1;
505 if(!generate_fsd24("fsd24-05.raw", pattern05, 100)) return 1;
506 if(!generate_fsd24("fsd24-06.raw", pattern06, 100)) return 1;
507 if(!generate_fsd24("fsd24-07.raw", pattern07, 100)) return 1;
509 if(!generate_wbps16("wbps16-01.raw", 1000)) return 1;
511 if(!generate_sine16_1("sine16-00.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49)) return 1;
512 if(!generate_sine16_1("sine16-01.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37)) return 1;
513 if(!generate_sine16_1("sine16-02.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49)) return 1;
514 if(!generate_sine16_1("sine16-03.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49)) return 1;
515 if(!generate_sine16_1("sine16-04.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
517 if(!generate_sine16_2("sine16-10.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
518 if(!generate_sine16_2("sine16-11.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
519 if(!generate_sine16_2("sine16-12.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
520 if(!generate_sine16_2("sine16-13.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
521 if(!generate_sine16_2("sine16-14.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
522 if(!generate_sine16_2("sine16-15.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
523 if(!generate_sine16_2("sine16-16.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
524 if(!generate_sine16_2("sine16-17.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
525 if(!generate_sine16_2("sine16-18.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
526 if(!generate_sine16_2("sine16-19.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
528 if(!generate_sine24_1("sine24-00.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49)) return 1;
529 if(!generate_sine24_1("sine24-01.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37)) return 1;
530 if(!generate_sine24_1("sine24-02.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49)) return 1;
531 if(!generate_sine24_1("sine24-03.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49)) return 1;
532 if(!generate_sine24_1("sine24-04.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
534 if(!generate_sine24_2("sine24-10.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
535 if(!generate_sine24_2("sine24-11.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
536 if(!generate_sine24_2("sine24-12.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
537 if(!generate_sine24_2("sine24-13.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
538 if(!generate_sine24_2("sine24-14.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
539 if(!generate_sine24_2("sine24-15.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
540 if(!generate_sine24_2("sine24-16.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
541 if(!generate_sine24_2("sine24-17.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
542 if(!generate_sine24_2("sine24-18.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
543 if(!generate_sine24_2("sine24-19.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
545 if(!generate_noise("noise.raw", 65536 * 8 * 3)) return 1;
546 if(!generate_wackywavs()) return 1;