Added testresample.c
authorRyan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 3016 6be275627762
child 3018 d706d3170d7d
Added testresample.c
test/Makefile.in
test/testresample.c
--- a/test/Makefile.in	Sun Jan 11 02:18:18 2009 +0000
+++ b/test/Makefile.in	Sun Jan 11 04:05:28 2009 +0000
@@ -7,7 +7,7 @@
 CFLAGS  = @CFLAGS@
 LIBS	= @LIBS@
 
-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)
+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)
 
 all: Makefile $(TARGETS)
 
@@ -23,6 +23,9 @@
 loopwave$(EXE): $(srcdir)/loopwave.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
+testresample$(EXE): $(srcdir)/testresample.c
+	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
+
 testaudioinfo$(EXE): $(srcdir)/testaudioinfo.c
 	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/testresample.c	Sun Jan 11 04:05:28 2009 +0000
@@ -0,0 +1,112 @@
+#include <stdio.h>
+#include "SDL.h"
+
+int main(int argc, char **argv)
+{
+    SDL_AudioSpec spec;
+    SDL_AudioCVT cvt;
+    Uint32 len = 0;
+    Uint8 *data = NULL;
+    int cvtfreq = 0;
+    int bitsize = 0;
+    int blockalign = 0;
+    int avgbytes = 0;
+    SDL_RWops *io = NULL;
+
+    if (argc != 4)
+    {
+        fprintf(stderr, "USAGE: %s in.wav out.wav newfreq\n", argv[0]);
+        return 1;
+    }
+
+    cvtfreq = atoi(argv[3]);
+
+    if (SDL_Init(SDL_INIT_AUDIO) == -1)
+    {
+        fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
+        return 2;
+    }
+
+    if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL)
+    {
+        fprintf(stderr, "failed to load %s: %s\n", argv[1], SDL_GetError());
+        SDL_Quit();
+        return 3;
+    }
+
+    if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
+                          spec.format, spec.channels, cvtfreq) == -1)
+    {
+        fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
+        SDL_FreeWAV(data);
+        SDL_Quit();
+        return 4;
+    }
+
+    cvt.len = len;
+    cvt.buf = (Uint8 *) malloc(len * cvt.len_mult);
+    if (cvt.buf == NULL)
+    {
+        fprintf(stderr, "Out of memory.\n");
+        SDL_FreeWAV(data);
+        SDL_Quit();
+        return 5;
+    }
+    memcpy(cvt.buf, data, len);
+
+    if (SDL_ConvertAudio(&cvt) == -1)
+    {
+        fprintf(stderr, "Conversion failed: %s\n", SDL_GetError());
+        free(cvt.buf);
+        SDL_FreeWAV(data);
+        SDL_Quit();
+        return 6;
+    }
+
+    /* write out a WAV header... */
+    io = SDL_RWFromFile(argv[2], "wb");
+    if (io == NULL)
+    {
+        fprintf(stderr, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
+        free(cvt.buf);
+        SDL_FreeWAV(data);
+        SDL_Quit();
+        return 7;
+    }
+
+    bitsize = SDL_AUDIO_BITSIZE(spec.format);
+    blockalign = (bitsize / 8) * spec.channels;
+    avgbytes = cvtfreq * blockalign;
+
+    SDL_WriteLE32(io, 0x46464952);  /* RIFF */
+    SDL_WriteLE32(io, len * cvt.len_mult + 36);
+    SDL_WriteLE32(io, 0x45564157);  /* WAVE */
+    SDL_WriteLE32(io, 0x20746D66);  /* fmt */
+    SDL_WriteLE32(io, 16);          /* chunk size */
+    SDL_WriteLE16(io, 1);           /* uncompressed */
+    SDL_WriteLE16(io, spec.channels);  /* channels */
+    SDL_WriteLE32(io, cvtfreq);  /* sample rate */
+    SDL_WriteLE32(io, avgbytes);  /* average bytes per second */
+    SDL_WriteLE16(io, blockalign);  /* block align */
+    SDL_WriteLE16(io, bitsize);  /* significant bits per sample */
+    SDL_WriteLE32(io, 0x61746164);  /* data */
+    SDL_WriteLE32(io, len * cvt.len_mult);  /* size */
+    SDL_RWwrite(io, cvt.buf, len * cvt.len_mult, 1);
+
+    if (SDL_RWclose(io) == -1)
+    {
+        fprintf(stderr, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
+        free(cvt.buf);
+        SDL_FreeWAV(data);
+        SDL_Quit();
+        return 8;
+    } // if
+
+    free(cvt.buf);
+    SDL_FreeWAV(data);
+    SDL_Quit();
+    return 0;
+} // main
+
+// end of resample_test.c ...
+