~ubuntu-branches/ubuntu/raring/flac/raring

« back to all changes in this revision

Viewing changes to src/test_streams/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Kwan
  • Date: 2007-05-29 22:56:36 UTC
  • mto: (8.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20070529225636-p8lkii0r0kp50pns
Tags: upstream-1.1.4
ImportĀ upstreamĀ versionĀ 1.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
3
3
 *
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
16
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
17
 */
18
18
 
 
19
#if HAVE_CONFIG_H
 
20
#  include <config.h>
 
21
#endif
 
22
 
19
23
#include <math.h>
20
24
#include <stdio.h>
21
25
#include <stdlib.h>
32
36
#define M_PI 3.14159265358979323846
33
37
#endif
34
38
 
35
 
#ifdef _WIN32
 
39
#if defined _WIN32 || defined __EMX__
36
40
        static const char *mode = "wb";
37
41
#else
38
42
        static const char *mode = "w";
39
43
#endif
40
44
 
 
45
#if !defined _MSC_VER && !defined __MINGW32__
 
46
#define GET_RANDOM_BYTE (((unsigned)random()) & 0xff)
 
47
#else
 
48
#define GET_RANDOM_BYTE (((unsigned)rand()) & 0xff)
 
49
#endif
 
50
 
41
51
static FLAC__bool is_big_endian_host;
42
52
 
43
53
 
 
54
static FLAC__bool write_little_endian(FILE *f, FLAC__int32 x, size_t bytes)
 
55
{
 
56
        while(bytes) {
 
57
                if(fputc(x, f) == EOF)
 
58
                        return false;
 
59
                x >>= 8;
 
60
                bytes--;
 
61
        }
 
62
        return true;
 
63
}
 
64
 
44
65
static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
45
66
{
46
67
        return
86
107
}
87
108
#endif
88
109
 
 
110
static FLAC__bool write_big_endian(FILE *f, FLAC__int32 x, size_t bytes)
 
111
{
 
112
        if(bytes < 4)
 
113
                x <<= 8*(4-bytes);
 
114
        while(bytes) {
 
115
                if(fputc(x>>24, f) == EOF)
 
116
                        return false;
 
117
                x <<= 8;
 
118
                bytes--;
 
119
        }
 
120
        return true;
 
121
}
 
122
 
89
123
static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 x)
90
124
{
91
125
        return
172
206
}
173
207
 
174
208
/* a mono one-sample 16bps stream */
175
 
static FLAC__bool generate_01()
 
209
static FLAC__bool generate_01(void)
176
210
{
177
211
        FILE *f;
178
212
        FLAC__int16 x = -32768;
191
225
}
192
226
 
193
227
/* a stereo one-sample 16bps stream */
194
 
static FLAC__bool generate_02()
 
228
static FLAC__bool generate_02(void)
195
229
{
196
230
        FILE *f;
197
231
        FLAC__int16 xl = -32768, xr = 32767;
212
246
}
213
247
 
214
248
/* a mono five-sample 16bps stream */
215
 
static FLAC__bool generate_03()
 
249
static FLAC__bool generate_03(void)
216
250
{
217
251
        FILE *f;
218
252
        FLAC__int16 x[] = { -25, 0, 25, 50, 100 };
233
267
}
234
268
 
235
269
/* a stereo five-sample 16bps stream */
236
 
static FLAC__bool generate_04()
 
270
static FLAC__bool generate_04(void)
237
271
{
238
272
        FILE *f;
239
273
        FLAC__int16 x[] = { -25, 500, 0, 400, 25, 300, 50, 200, 100, 100 };
534
568
{
535
569
        FILE *f;
536
570
        unsigned b;
537
 
#if !defined _MSC_VER && !defined __MINGW32__
538
 
        struct timeval tv;
539
 
 
540
 
        if(gettimeofday(&tv, 0) < 0) {
541
 
                fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
542
 
                tv.tv_usec = 4321;
543
 
        }
544
 
        srandom(tv.tv_usec);
545
 
#else
546
 
        srand(time(0));
547
 
#endif
548
571
 
549
572
        if(0 == (f = fopen(fn, mode)))
550
573
                return false;
566
589
        return false;
567
590
}
568
591
 
569
 
static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsigned channels, unsigned bytes_per_sample, unsigned samples)
570
 
{
 
592
static FLAC__bool generate_raw(const char *filename, unsigned channels, unsigned bytes_per_sample, unsigned samples)
 
593
{
 
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;
 
599
        FILE *f;
 
600
        unsigned i, j;
 
601
 
 
602
        if(0 == (f = fopen(filename, mode)))
 
603
                return false;
 
604
 
 
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))
 
610
                                goto foo;
 
611
                }
 
612
        }
 
613
 
 
614
        fclose(f);
 
615
        return true;
 
616
foo:
 
617
        fclose(f);
 
618
        return false;
 
619
}
 
620
 
 
621
static FLAC__bool generate_aiff(const char *filename, unsigned sample_rate, unsigned channels, unsigned bps, unsigned samples)
 
622
{
 
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;
573
632
        FILE *f;
574
 
        unsigned i;
 
633
        unsigned i, j;
575
634
 
576
635
        if(0 == (f = fopen(filename, mode)))
577
636
                return false;
585
644
                goto foo;
586
645
        if(!write_big_endian_uint32(f, samples))
587
646
                goto foo;
588
 
        if(!write_big_endian_uint16(f, (FLAC__uint16)(8 * bytes_per_sample)))
 
647
        if(!write_big_endian_uint16(f, (FLAC__uint16)bps))
589
648
                goto foo;
590
649
        if(!write_sane_extended(f, sample_rate))
591
650
                goto foo;
596
655
        if(fwrite("\000\000\000\000\000\000\000\000", 1, 8, f) < 8)
597
656
                goto foo;
598
657
 
599
 
        for(i = 0; i < true_size; i++)
600
 
                if(fputc(i, f) == EOF)
601
 
                        goto foo;
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))
 
663
                                goto foo;
 
664
                }
 
665
        }
 
666
        for(i = true_size; i < padded_size; i++)
603
667
                if(fputc(0, f) == EOF)
604
668
                        goto foo;
605
669
 
610
674
        return false;
611
675
}
612
676
 
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)
614
678
{
 
679
        const FLAC__bool waveformatextensible = strict && (channels > 2 || (bps%8));
 
680
        /*                                                                 ^^^^^^^
 
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
 
683
         */
 
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;
617
693
        FILE *f;
618
 
        unsigned i;
 
694
        unsigned i, j;
619
695
 
620
696
        if(0 == (f = fopen(filename, mode)))
621
697
                return false;
622
698
        if(fwrite("RIFF", 1, 4, f) < 4)
623
699
                goto foo;
624
 
        if(!write_little_endian_uint32(f, padded_size + 36))
625
 
                goto foo;
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)))
 
701
                goto foo;
 
702
        if(fwrite("WAVEfmt ", 1, 8, f) < 8)
 
703
                goto foo;
 
704
        if(!write_little_endian_uint32(f, waveformatextensible?40:16))
 
705
                goto foo;
 
706
        if(!write_little_endian_uint16(f, (FLAC__uint16)(waveformatextensible?65534:1)))
627
707
                goto foo;
628
708
        if(!write_little_endian_uint16(f, (FLAC__uint16)channels))
629
709
                goto foo;
633
713
                goto foo;
634
714
        if(!write_little_endian_uint16(f, (FLAC__uint16)(channels * bytes_per_sample))) /* block align */
635
715
                goto foo;
636
 
        if(!write_little_endian_uint16(f, (FLAC__uint16)(8 * bytes_per_sample)))
 
716
        if(!write_little_endian_uint16(f, (FLAC__uint16)(bps+shift)))
637
717
                goto foo;
 
718
        if(waveformatextensible) {
 
719
                if(!write_little_endian_uint16(f, (FLAC__uint16)22)) /* cbSize */
 
720
                        goto foo;
 
721
                if(!write_little_endian_uint16(f, (FLAC__uint16)bps)) /* validBitsPerSample */
 
722
                        goto foo;
 
723
                if(!write_little_endian_uint32(f, 0)) /* channelMask */
 
724
                        goto foo;
 
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)
 
727
                        goto foo;
 
728
        }
638
729
        if(fwrite("data", 1, 4, f) < 4)
639
730
                goto foo;
640
731
        if(!write_little_endian_uint32(f, true_size))
641
732
                goto foo;
642
733
 
643
 
        for(i = 0; i < true_size; i++)
644
 
                if(fputc(i, f) == EOF)
645
 
                        goto foo;
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))
 
739
                                goto foo;
 
740
                }
 
741
        }
 
742
        for(i = true_size; i < padded_size; i++)
647
743
                if(fputc(0, f) == EOF)
648
744
                        goto foo;
649
745
 
654
750
        return false;
655
751
}
656
752
 
657
 
static FLAC__bool generate_wackywavs()
 
753
static FLAC__bool generate_wackywavs(void)
658
754
{
659
755
        FILE *f;
660
756
        FLAC__byte wav[] = {
708
804
        (void)argv;
709
805
        is_big_endian_host = (*((FLAC__byte*)(&test)))? false : true;
710
806
 
 
807
#if !defined _MSC_VER && !defined __MINGW32__
 
808
        {
 
809
                struct timeval tv;
 
810
 
 
811
                if(gettimeofday(&tv, 0) < 0) {
 
812
                        fprintf(stderr, "WARNING: couldn't seed RNG with time\n");
 
813
                        tv.tv_usec = 4321;
 
814
                }
 
815
                srandom(tv.tv_usec);
 
816
        }
 
817
#else
 
818
        srand(time(0));
 
819
#endif
 
820
 
711
821
        if(!generate_01()) return 1;
712
822
        if(!generate_02()) return 1;
713
823
        if(!generate_03()) return 1;
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;
792
902
 
 
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++) {
802
913
                                char fn[64];
803
914
 
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]))
806
 
                                        return 1;
807
 
 
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]))
810
 
                                        return 1;
811
 
 
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]))
814
 
                                        return 1;
 
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]))
 
917
                                        return 1;
 
918
 
 
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))
 
921
                                        return 1;
 
922
 
 
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]))
 
926
                                                return 1;
 
927
                                }
815
928
                        }
816
929
                }
817
930
        }