1
1
/* test_streams - Simple test pattern generator
2
* Copyright (C) 2000,2001,2002,2003,2004,2005 Josh Coalson
2
* Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
4
4
* This program is free software; you can redistribute it and/or
5
5
* modify it under the terms of the GNU General Public License
32
36
#define M_PI 3.14159265358979323846
39
#if defined _WIN32 || defined __EMX__
36
40
static const char *mode = "wb";
38
42
static const char *mode = "w";
45
#if !defined _MSC_VER && !defined __MINGW32__
46
#define GET_RANDOM_BYTE (((unsigned)random()) & 0xff)
48
#define GET_RANDOM_BYTE (((unsigned)rand()) & 0xff)
41
51
static FLAC__bool is_big_endian_host;
54
static FLAC__bool write_little_endian(FILE *f, FLAC__int32 x, size_t bytes)
57
if(fputc(x, f) == EOF)
44
65
static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
569
static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsigned channels, unsigned bytes_per_sample, unsigned samples)
592
static FLAC__bool generate_raw(const char *filename, unsigned channels, unsigned bytes_per_sample, unsigned samples)
594
const FLAC__int32 full_scale = (1 << (bytes_per_sample*8-1)) - 1;
595
const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
596
const double delta1 = 2.0 * M_PI / ( 44100.0 / f1);
597
const double delta2 = 2.0 * M_PI / ( 44100.0 / f2);
598
double theta1, theta2;
602
if(0 == (f = fopen(filename, mode)))
605
for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
606
for(j = 0; j < channels; j++) {
607
double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
608
FLAC__int32 v = (FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8);
609
if(!write_little_endian(f, v, bytes_per_sample))
621
static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples)
623
const unsigned bytes_per_sample = (bps+7)/8;
571
624
const unsigned true_size = channels * bytes_per_sample * samples;
572
625
const unsigned padded_size = (true_size + 1) & (~1u);
626
const unsigned shift = (bps%8)? 8 - (bps%8) : 0;
627
const FLAC__int32 full_scale = (1 << (bps-1)) - 1;
628
const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
629
const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
630
const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
631
double theta1, theta2;
576
635
if(0 == (f = fopen(filename, mode)))
596
655
if(fwrite("\000\000\000\000\000\000\000\000", 1, 8, f) < 8)
599
for(i = 0; i < true_size; i++)
600
if(fputc(i, f) == EOF)
602
for( ; i < padded_size; i++)
658
for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
659
for(j = 0; j < channels; j++) {
660
double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
661
FLAC__int32 v = ((FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8)) << shift;
662
if(!write_big_endian(f, v, bytes_per_sample))
666
for(i = true_size; i < padded_size; i++)
603
667
if(fputc(0, f) == EOF)
613
static FLAC__bool generate_wav(const char *filename, unsigned sample_rate, unsigned channels, unsigned bytes_per_sample, unsigned samples)
677
static FLAC__bool generate_wav(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples, FLAC__bool strict)
679
const FLAC__bool waveformatextensible = strict && (channels > 2 || (bps%8));
681
* (bps%8) allows 24 bps which is technically supposed to be WAVEFORMATEXTENSIBLE but we
682
* write 24bps as WAVEFORMATEX since it's unambiguous and matches how flac writes it
684
const unsigned bytes_per_sample = (bps+7)/8;
615
685
const unsigned true_size = channels * bytes_per_sample * samples;
616
686
const unsigned padded_size = (true_size + 1) & (~1u);
687
const unsigned shift = (bps%8)? 8 - (bps%8) : 0;
688
const FLAC__int32 full_scale = (1 << (bps-1)) - 1;
689
const double f1 = 441.0, a1 = 0.61, f2 = 661.5, a2 = 0.37;
690
const double delta1 = 2.0 * M_PI / ( sample_rate / f1);
691
const double delta2 = 2.0 * M_PI / ( sample_rate / f2);
692
double theta1, theta2;
620
696
if(0 == (f = fopen(filename, mode)))
622
698
if(fwrite("RIFF", 1, 4, f) < 4)
624
if(!write_little_endian_uint32(f, padded_size + 36))
626
if(fwrite("WAVEfmt \020\000\000\000\001\000", 1, 14, f) < 14)
700
if(!write_little_endian_uint32(f, padded_size + (waveformatextensible?60:36)))
702
if(fwrite("WAVEfmt ", 1, 8, f) < 8)
704
if(!write_little_endian_uint32(f, waveformatextensible?40:16))
706
if(!write_little_endian_uint16(f, (FLAC__uint16)(waveformatextensible?65534:1)))
628
708
if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
634
714
if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * bytes_per_sample))) /* block align */
636
if(!write_little_endian_uint16(f, (FLAC__uint16)(8 * bytes_per_sample)))
716
if(!write_little_endian_uint16(f, (FLAC__uint16)(bps+shift)))
718
if(waveformatextensible) {
719
if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
721
if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
723
if(!write_little_endian_uint32(f, 0)) /* channelMask */
725
/* GUID = {0x00000001, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}} */
726
if(fwrite("\x01\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xaa\x00\x38\x9b\x71", 1, 16, f) != 16)
638
729
if(fwrite("data", 1, 4, f) < 4)
640
731
if(!write_little_endian_uint32(f, true_size))
643
for(i = 0; i < true_size; i++)
644
if(fputc(i, f) == EOF)
646
for( ; i < padded_size; i++)
734
for(i = 0, theta1 = theta2 = 0.0; i < samples; i++, theta1 += delta1, theta2 += delta2) {
735
for(j = 0; j < channels; j++) {
736
double val = (a1*sin(theta1) + a2*sin(theta2))*(double)full_scale;
737
FLAC__int32 v = ((FLAC__int32)(val + 0.5) + ((GET_RANDOM_BYTE>>4)-8)) << shift;
738
if(!write_little_endian(f, v, bytes_per_sample))
742
for(i = true_size; i < padded_size; i++)
647
743
if(fputc(0, f) == EOF)
790
900
if(!generate_sine24_2("sine24-18.raw", 44100.0, 200000, 441.0, 0.50, 4410.0, 0.49, 1.3)) return 1;
791
901
if(!generate_sine24_2("sine24-19.raw", 44100.0, 200000, 8820.0, 0.70, 4410.0, 0.29, 0.1)) return 1;
903
/* WATCHOUT: the size of noise.raw is hardcoded into test/test_flac.sh */
793
904
if(!generate_noise("noise.raw", 65536 * 8 * 3)) return 1;
794
905
if(!generate_noise("noise8m32.raw", 32)) return 1;
795
906
if(!generate_wackywavs()) return 1;
796
907
for(channels = 1; channels <= 8; channels++) {
797
unsigned bytes_per_sample;
798
for(bytes_per_sample = 1; bytes_per_sample <= 3; bytes_per_sample++) {
799
static const unsigned nsamples[] = { 1, 111, 5555 } ;
908
unsigned bits_per_sample;
909
for(bits_per_sample = 4; bits_per_sample <= 24; bits_per_sample++) {
910
static const unsigned nsamples[] = { 1, 111, 4777 } ;
800
911
unsigned samples;
801
912
for(samples = 0; samples < sizeof(nsamples)/sizeof(nsamples[0]); samples++) {
804
sprintf(fn, "rt-%u-%u-%u.aiff", channels, bytes_per_sample, nsamples[samples]);
805
if(!generate_aiff(fn, 44100, channels, bytes_per_sample, nsamples[samples]))
808
sprintf(fn, "rt-%u-%u-%u.raw", channels, bytes_per_sample, nsamples[samples]);
809
if(!generate_noise(fn, channels * bytes_per_sample * nsamples[samples]))
812
sprintf(fn, "rt-%u-%u-%u.wav", channels, bytes_per_sample, nsamples[samples]);
813
if(!generate_wav(fn, 44100, channels, bytes_per_sample, nsamples[samples]))
915
sprintf(fn, "rt-%u-%u-%u.aiff", channels, bits_per_sample, nsamples[samples]);
916
if(!generate_aiff(fn, 44100, channels, bits_per_sample, nsamples[samples]))
919
sprintf(fn, "rt-%u-%u-%u.wav", channels, bits_per_sample, nsamples[samples]);
920
if(!generate_wav(fn, 44100, channels, bits_per_sample, nsamples[samples], /*strict=*/true))
923
if(bits_per_sample % 8 == 0) {
924
sprintf(fn, "rt-%u-%u-%u.raw", channels, bits_per_sample, nsamples[samples]);
925
if(!generate_raw(fn, channels, bits_per_sample/8, nsamples[samples]))