1 /* test_streams - Simple test pattern generator
2 * Copyright (C) 2000,2001 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.
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 static void swap16(FLAC__int16 *i)
43 unsigned char *x = (unsigned char *)i, b;
44 if(!is_big_endian_host) {
51 static void swap24(FLAC__byte *x)
53 if(is_big_endian_host) {
65 /* a mono one-sample 16bps stream */
66 static FLAC__bool generate_01()
69 FLAC__int16 x = -32768;
71 if(0 == (f = fopen("test01.raw", mode)))
75 if(fwrite(&x, sizeof(x), 1, f) < 1)
85 /* a stereo one-sample 16bps stream */
86 static FLAC__bool generate_02()
89 FLAC__int16 xl = -32768, xr = 32767;
91 if(0 == (f = fopen("test02.raw", mode)))
97 if(fwrite(&xl, sizeof(xl), 1, f) < 1)
99 if(fwrite(&xr, sizeof(xr), 1, f) < 1)
109 /* a mono five-sample 16bps stream */
110 static FLAC__bool generate_03()
113 FLAC__int16 x[] = { -25, 0, 25, 50, 100 };
116 if(0 == (f = fopen("test03.raw", mode)))
119 for(i = 0; i < 5; i++)
122 if(fwrite(&x, sizeof(FLAC__int16), 5, f) < 5)
132 /* a stereo five-sample 16bps stream */
133 static FLAC__bool generate_04()
136 FLAC__int16 x[] = { -25, 500, 0, 400, 25, 300, 50, 200, 100, 100 };
139 if(0 == (f = fopen("test04.raw", mode)))
142 for(i = 0; i < 10; i++)
145 if(fwrite(&x, sizeof(FLAC__int16), 10, f) < 10)
155 /* a mono full-scale deflection 8bps stream */
156 static FLAC__bool generate_fsd8(const char *fn, const int pattern[], unsigned reps)
161 FLAC__ASSERT(pattern != 0);
163 if(0 == (f = fopen(fn, mode)))
166 for(rep = 0; rep < reps; rep++) {
167 for(p = 0; pattern[p]; p++) {
168 signed char x = pattern[p] > 0? 127 : -128;
169 if(fwrite(&x, sizeof(x), 1, f) < 1)
181 /* a mono full-scale deflection 16bps stream */
182 static FLAC__bool generate_fsd16(const char *fn, const int pattern[], unsigned reps)
187 FLAC__ASSERT(pattern != 0);
189 if(0 == (f = fopen(fn, mode)))
192 for(rep = 0; rep < reps; rep++) {
193 for(p = 0; pattern[p]; p++) {
194 FLAC__int16 x = pattern[p] > 0? 32767 : -32768;
196 if(fwrite(&x, sizeof(x), 1, f) < 1)
208 /* a stereo wasted-bits-per-sample 16bps stream */
209 static FLAC__bool generate_wbps16(const char *fn, unsigned samples)
214 if(0 == (f = fopen(fn, mode)))
217 for(sample = 0; sample < samples; sample++) {
218 FLAC__int16 l = (sample % 2000) << 2;
219 FLAC__int16 r = (sample % 1000) << 3;
222 if(fwrite(&l, sizeof(l), 1, f) < 1)
224 if(fwrite(&r, sizeof(r), 1, f) < 1)
235 /* a mono full-scale deflection 24bps stream */
236 static FLAC__bool generate_fsd24(const char *fn, const int pattern[], unsigned reps)
241 FLAC__ASSERT(pattern != 0);
243 if(0 == (f = fopen(fn, mode)))
246 for(rep = 0; rep < reps; rep++) {
247 for(p = 0; pattern[p]; p++) {
248 FLAC__int32 x = pattern[p] > 0? 8388607 : -8388608;
249 FLAC__byte *b = (FLAC__byte*)(&x);
251 if(fwrite(b, 3, 1, f) < 1)
263 /* a mono sine-wave 16bps stream */
264 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)
266 const FLAC__int16 full_scale = 32767;
267 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
268 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
270 double theta1, theta2;
273 if(0 == (f = fopen(fn, mode)))
276 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
277 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
278 FLAC__int16 v = (FLAC__int16)(val + 0.5);
280 if(fwrite(&v, sizeof(v), 1, f) < 1)
291 /* a stereo sine-wave 16bps stream */
292 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)
294 const FLAC__int16 full_scale = 32767;
295 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
296 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
298 double theta1, theta2;
301 if(0 == (f = fopen(fn, mode)))
304 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
305 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
306 FLAC__int16 v = (FLAC__int16)(val + 0.5);
308 if(fwrite(&v, sizeof(v), 1, f) < 1)
310 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
311 v = (FLAC__int16)(val + 0.5);
313 if(fwrite(&v, sizeof(v), 1, f) < 1)
324 /* a mono sine-wave 24bps stream */
325 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)
327 const FLAC__int32 full_scale = 0x7fffff;
328 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
329 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
331 double theta1, theta2;
334 if(0 == (f = fopen(fn, mode)))
337 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
338 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
339 FLAC__int32 v = (FLAC__int32)(val + 0.5);
340 FLAC__byte *b = (FLAC__byte*)(&v);
342 if(fwrite(b, 3, 1, f) < 1)
353 /* a stereo sine-wave 24bps stream */
354 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)
356 const FLAC__int32 full_scale = 0x7fffff;
357 const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
358 const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
360 double theta1, theta2;
363 if(0 == (f = fopen(fn, mode)))
366 for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
367 double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
368 FLAC__int32 v = (FLAC__int32)(val + 0.5);
369 FLAC__byte *b = (FLAC__byte*)(&v);
371 if(fwrite(b, 3, 1, f) < 1)
373 val = -(a1*sin(theta1*fmult) + a2*sin(theta2*fmult))*(double)full_scale;
374 v = (FLAC__int32)(val + 0.5);
376 if(fwrite(b, 3, 1, f) < 1)
387 static FLAC__bool generate_noise(const char *fn, unsigned bytes)
394 if(gettimeofday(&tv, 0) < 0) {
395 fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
400 /* who has the patience to figure out how to do RNG with VC++? */
403 if(0 == (f = fopen(fn, mode)))
406 for(b = 0; b < bytes; b++) {
408 FLAC__byte x = (FLAC__byte)(((unsigned)random()) & 0xff);
410 FLAC__byte x = (FLAC__byte)((((unsigned)generate_noise) >> 8) ^ (b * 17)); /* fake it */
412 if(fwrite(&x, sizeof(x), 1, f) < 1)
423 static FLAC__bool generate_wackywavs()
427 'R', 'I', 'F', 'F', 76, 0, 0, 0,
428 'W', 'A', 'V', 'E', 'f', 'a', 'c', 't',
429 4, 0, 0, 0 , 'b', 'l', 'a', 'h',
430 'p', 'a', 'd', ' ', 4, 0, 0, 0,
431 'B', 'L', 'A', 'H', 'f', 'm', 't', ' ',
432 16, 0, 0, 0, 1, 0, 1, 0,
433 0x44,0xAC, 0, 0, 0, 0, 0, 0,
434 2, 0, 16, 0, 'd', 'a', 't', 'a',
435 16, 0, 0, 0, 0, 0, 1, 0,
436 4, 0, 9, 0, 16, 0, 25, 0,
437 36, 0, 49, 0, 'p', 'a', 'd', ' ',
438 4, 0, 0, 0, 'b', 'l', 'a', 'h'
441 if(0 == (f = fopen("wacky1.wav", mode)))
443 if(fwrite(wav, 1, 84, f) < 84)
448 if(0 == (f = fopen("wacky2.wav", mode)))
450 if(fwrite(wav, 1, 96, f) < 96)
460 int main(int argc, char *argv[])
462 FLAC__uint32 test = 1;
464 int pattern01[] = { 1, -1, 0 };
465 int pattern02[] = { 1, 1, -1, 0 };
466 int pattern03[] = { 1, -1, -1, 0 };
467 int pattern04[] = { 1, -1, 1, -1, 0 };
468 int pattern05[] = { 1, -1, -1, 1, 0 };
469 int pattern06[] = { 1, -1, 1, 1, -1, 0 };
470 int pattern07[] = { 1, -1, -1, 1, -1, 0 };
474 is_big_endian_host = (*((FLAC__byte*)(&test)))? false : true;
476 if(!generate_01()) return 1;
477 if(!generate_02()) return 1;
478 if(!generate_03()) return 1;
479 if(!generate_04()) return 1;
481 if(!generate_fsd8("fsd8-01.raw", pattern01, 100)) return 1;
482 if(!generate_fsd8("fsd8-02.raw", pattern02, 100)) return 1;
483 if(!generate_fsd8("fsd8-03.raw", pattern03, 100)) return 1;
484 if(!generate_fsd8("fsd8-04.raw", pattern04, 100)) return 1;
485 if(!generate_fsd8("fsd8-05.raw", pattern05, 100)) return 1;
486 if(!generate_fsd8("fsd8-06.raw", pattern06, 100)) return 1;
487 if(!generate_fsd8("fsd8-07.raw", pattern07, 100)) return 1;
489 if(!generate_fsd16("fsd16-01.raw", pattern01, 100)) return 1;
490 if(!generate_fsd16("fsd16-02.raw", pattern02, 100)) return 1;
491 if(!generate_fsd16("fsd16-03.raw", pattern03, 100)) return 1;
492 if(!generate_fsd16("fsd16-04.raw", pattern04, 100)) return 1;
493 if(!generate_fsd16("fsd16-05.raw", pattern05, 100)) return 1;
494 if(!generate_fsd16("fsd16-06.raw", pattern06, 100)) return 1;
495 if(!generate_fsd16("fsd16-07.raw", pattern07, 100)) return 1;
497 if(!generate_fsd24("fsd24-01.raw", pattern01, 100)) return 1;
498 if(!generate_fsd24("fsd24-02.raw", pattern02, 100)) return 1;
499 if(!generate_fsd24("fsd24-03.raw", pattern03, 100)) return 1;
500 if(!generate_fsd24("fsd24-04.raw", pattern04, 100)) return 1;
501 if(!generate_fsd24("fsd24-05.raw", pattern05, 100)) return 1;
502 if(!generate_fsd24("fsd24-06.raw", pattern06, 100)) return 1;
503 if(!generate_fsd24("fsd24-07.raw", pattern07, 100)) return 1;
505 if(!generate_wbps16("wbps16-01.raw", 1000)) return 1;
507 if(!generate_sine16_1("sine16-00.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49)) return 1;
508 if(!generate_sine16_1("sine16-01.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37)) return 1;
509 if(!generate_sine16_1("sine16-02.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49)) return 1;
510 if(!generate_sine16_1("sine16-03.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49)) return 1;
511 if(!generate_sine16_1("sine16-04.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
513 if(!generate_sine16_2("sine16-10.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
514 if(!generate_sine16_2("sine16-11.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
515 if(!generate_sine16_2("sine16-12.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
516 if(!generate_sine16_2("sine16-13.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
517 if(!generate_sine16_2("sine16-14.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
518 if(!generate_sine16_2("sine16-15.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
519 if(!generate_sine16_2("sine16-16.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
520 if(!generate_sine16_2("sine16-17.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
521 if(!generate_sine16_2("sine16-18.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
522 if(!generate_sine16_2("sine16-19.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
524 if(!generate_sine24_1("sine24-00.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49)) return 1;
525 if(!generate_sine24_1("sine24-01.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37)) return 1;
526 if(!generate_sine24_1("sine24-02.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49)) return 1;
527 if(!generate_sine24_1("sine24-03.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49)) return 1;
528 if(!generate_sine24_1("sine24-04.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29)) return 1;
530 if(!generate_sine24_2("sine24-10.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49, 1.0)) return 1;
531 if(!generate_sine24_2("sine24-11.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37, 1.0)) return 1;
532 if(!generate_sine24_2("sine24-12.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49, 1.0)) return 1;
533 if(!generate_sine24_2("sine24-13.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49, 1.0)) return 1;
534 if(!generate_sine24_2("sine24-14.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29, 1.0)) return 1;
535 if(!generate_sine24_2("sine24-15.raw", 44100.0, 80000, 441.0, 0.50, 441.0, 0.49, 0.5)) return 1;
536 if(!generate_sine24_2("sine24-16.raw", 44100.0, 80000, 441.0, 0.61, 661.5, 0.37, 2.0)) return 1;
537 if(!generate_sine24_2("sine24-17.raw", 44100.0, 80000, 441.0, 0.50, 882.0, 0.49, 0.7)) return 1;
538 if(!generate_sine24_2("sine24-18.raw", 44100.0, 80000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
539 if(!generate_sine24_2("sine24-19.raw", 44100.0, 50000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
541 if(!generate_noise("noise.raw", 65536 * 8 * 3)) return 1;
542 if(!generate_wackywavs()) return 1;