Added testresample.c
authorRyan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 30173272431eeee2
parent 3016 6be275627762
child 3018 d706d3170d7d
Added testresample.c
test/Makefile.in
test/testresample.c
     1.1 --- a/test/Makefile.in	Sun Jan 11 02:18:18 2009 +0000
     1.2 +++ b/test/Makefile.in	Sun Jan 11 04:05:28 2009 +0000
     1.3 @@ -7,7 +7,7 @@
     1.4  CFLAGS  = @CFLAGS@
     1.5  LIBS	= @LIBS@
     1.6  
     1.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testintersections$(EXE) testdraw2$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE)
     1.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testresample$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testintersections$(EXE) testdraw2$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE)
     1.9  
    1.10  all: Makefile $(TARGETS)
    1.11  
    1.12 @@ -23,6 +23,9 @@
    1.13  loopwave$(EXE): $(srcdir)/loopwave.c
    1.14  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.15  
    1.16 +testresample$(EXE): $(srcdir)/testresample.c
    1.17 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.18 +
    1.19  testaudioinfo$(EXE): $(srcdir)/testaudioinfo.c
    1.20  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.21  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/testresample.c	Sun Jan 11 04:05:28 2009 +0000
     2.3 @@ -0,0 +1,112 @@
     2.4 +#include <stdio.h>
     2.5 +#include "SDL.h"
     2.6 +
     2.7 +int main(int argc, char **argv)
     2.8 +{
     2.9 +    SDL_AudioSpec spec;
    2.10 +    SDL_AudioCVT cvt;
    2.11 +    Uint32 len = 0;
    2.12 +    Uint8 *data = NULL;
    2.13 +    int cvtfreq = 0;
    2.14 +    int bitsize = 0;
    2.15 +    int blockalign = 0;
    2.16 +    int avgbytes = 0;
    2.17 +    SDL_RWops *io = NULL;
    2.18 +
    2.19 +    if (argc != 4)
    2.20 +    {
    2.21 +        fprintf(stderr, "USAGE: %s in.wav out.wav newfreq\n", argv[0]);
    2.22 +        return 1;
    2.23 +    }
    2.24 +
    2.25 +    cvtfreq = atoi(argv[3]);
    2.26 +
    2.27 +    if (SDL_Init(SDL_INIT_AUDIO) == -1)
    2.28 +    {
    2.29 +        fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
    2.30 +        return 2;
    2.31 +    }
    2.32 +
    2.33 +    if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL)
    2.34 +    {
    2.35 +        fprintf(stderr, "failed to load %s: %s\n", argv[1], SDL_GetError());
    2.36 +        SDL_Quit();
    2.37 +        return 3;
    2.38 +    }
    2.39 +
    2.40 +    if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
    2.41 +                          spec.format, spec.channels, cvtfreq) == -1)
    2.42 +    {
    2.43 +        fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
    2.44 +        SDL_FreeWAV(data);
    2.45 +        SDL_Quit();
    2.46 +        return 4;
    2.47 +    }
    2.48 +
    2.49 +    cvt.len = len;
    2.50 +    cvt.buf = (Uint8 *) malloc(len * cvt.len_mult);
    2.51 +    if (cvt.buf == NULL)
    2.52 +    {
    2.53 +        fprintf(stderr, "Out of memory.\n");
    2.54 +        SDL_FreeWAV(data);
    2.55 +        SDL_Quit();
    2.56 +        return 5;
    2.57 +    }
    2.58 +    memcpy(cvt.buf, data, len);
    2.59 +
    2.60 +    if (SDL_ConvertAudio(&cvt) == -1)
    2.61 +    {
    2.62 +        fprintf(stderr, "Conversion failed: %s\n", SDL_GetError());
    2.63 +        free(cvt.buf);
    2.64 +        SDL_FreeWAV(data);
    2.65 +        SDL_Quit();
    2.66 +        return 6;
    2.67 +    }
    2.68 +
    2.69 +    /* write out a WAV header... */
    2.70 +    io = SDL_RWFromFile(argv[2], "wb");
    2.71 +    if (io == NULL)
    2.72 +    {
    2.73 +        fprintf(stderr, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
    2.74 +        free(cvt.buf);
    2.75 +        SDL_FreeWAV(data);
    2.76 +        SDL_Quit();
    2.77 +        return 7;
    2.78 +    }
    2.79 +
    2.80 +    bitsize = SDL_AUDIO_BITSIZE(spec.format);
    2.81 +    blockalign = (bitsize / 8) * spec.channels;
    2.82 +    avgbytes = cvtfreq * blockalign;
    2.83 +
    2.84 +    SDL_WriteLE32(io, 0x46464952);  /* RIFF */
    2.85 +    SDL_WriteLE32(io, len * cvt.len_mult + 36);
    2.86 +    SDL_WriteLE32(io, 0x45564157);  /* WAVE */
    2.87 +    SDL_WriteLE32(io, 0x20746D66);  /* fmt */
    2.88 +    SDL_WriteLE32(io, 16);          /* chunk size */
    2.89 +    SDL_WriteLE16(io, 1);           /* uncompressed */
    2.90 +    SDL_WriteLE16(io, spec.channels);  /* channels */
    2.91 +    SDL_WriteLE32(io, cvtfreq);  /* sample rate */
    2.92 +    SDL_WriteLE32(io, avgbytes);  /* average bytes per second */
    2.93 +    SDL_WriteLE16(io, blockalign);  /* block align */
    2.94 +    SDL_WriteLE16(io, bitsize);  /* significant bits per sample */
    2.95 +    SDL_WriteLE32(io, 0x61746164);  /* data */
    2.96 +    SDL_WriteLE32(io, len * cvt.len_mult);  /* size */
    2.97 +    SDL_RWwrite(io, cvt.buf, len * cvt.len_mult, 1);
    2.98 +
    2.99 +    if (SDL_RWclose(io) == -1)
   2.100 +    {
   2.101 +        fprintf(stderr, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
   2.102 +        free(cvt.buf);
   2.103 +        SDL_FreeWAV(data);
   2.104 +        SDL_Quit();
   2.105 +        return 8;
   2.106 +    } // if
   2.107 +
   2.108 +    free(cvt.buf);
   2.109 +    SDL_FreeWAV(data);
   2.110 +    SDL_Quit();
   2.111 +    return 0;
   2.112 +} // main
   2.113 +
   2.114 +// end of resample_test.c ...
   2.115 +