test/testresample.c
changeset 3017 3272431eeee2
child 3018 d706d3170d7d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testresample.c	Sun Jan 11 04:05:28 2009 +0000
     1.3 @@ -0,0 +1,112 @@
     1.4 +#include <stdio.h>
     1.5 +#include "SDL.h"
     1.6 +
     1.7 +int main(int argc, char **argv)
     1.8 +{
     1.9 +    SDL_AudioSpec spec;
    1.10 +    SDL_AudioCVT cvt;
    1.11 +    Uint32 len = 0;
    1.12 +    Uint8 *data = NULL;
    1.13 +    int cvtfreq = 0;
    1.14 +    int bitsize = 0;
    1.15 +    int blockalign = 0;
    1.16 +    int avgbytes = 0;
    1.17 +    SDL_RWops *io = NULL;
    1.18 +
    1.19 +    if (argc != 4)
    1.20 +    {
    1.21 +        fprintf(stderr, "USAGE: %s in.wav out.wav newfreq\n", argv[0]);
    1.22 +        return 1;
    1.23 +    }
    1.24 +
    1.25 +    cvtfreq = atoi(argv[3]);
    1.26 +
    1.27 +    if (SDL_Init(SDL_INIT_AUDIO) == -1)
    1.28 +    {
    1.29 +        fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
    1.30 +        return 2;
    1.31 +    }
    1.32 +
    1.33 +    if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL)
    1.34 +    {
    1.35 +        fprintf(stderr, "failed to load %s: %s\n", argv[1], SDL_GetError());
    1.36 +        SDL_Quit();
    1.37 +        return 3;
    1.38 +    }
    1.39 +
    1.40 +    if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
    1.41 +                          spec.format, spec.channels, cvtfreq) == -1)
    1.42 +    {
    1.43 +        fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
    1.44 +        SDL_FreeWAV(data);
    1.45 +        SDL_Quit();
    1.46 +        return 4;
    1.47 +    }
    1.48 +
    1.49 +    cvt.len = len;
    1.50 +    cvt.buf = (Uint8 *) malloc(len * cvt.len_mult);
    1.51 +    if (cvt.buf == NULL)
    1.52 +    {
    1.53 +        fprintf(stderr, "Out of memory.\n");
    1.54 +        SDL_FreeWAV(data);
    1.55 +        SDL_Quit();
    1.56 +        return 5;
    1.57 +    }
    1.58 +    memcpy(cvt.buf, data, len);
    1.59 +
    1.60 +    if (SDL_ConvertAudio(&cvt) == -1)
    1.61 +    {
    1.62 +        fprintf(stderr, "Conversion failed: %s\n", SDL_GetError());
    1.63 +        free(cvt.buf);
    1.64 +        SDL_FreeWAV(data);
    1.65 +        SDL_Quit();
    1.66 +        return 6;
    1.67 +    }
    1.68 +
    1.69 +    /* write out a WAV header... */
    1.70 +    io = SDL_RWFromFile(argv[2], "wb");
    1.71 +    if (io == NULL)
    1.72 +    {
    1.73 +        fprintf(stderr, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
    1.74 +        free(cvt.buf);
    1.75 +        SDL_FreeWAV(data);
    1.76 +        SDL_Quit();
    1.77 +        return 7;
    1.78 +    }
    1.79 +
    1.80 +    bitsize = SDL_AUDIO_BITSIZE(spec.format);
    1.81 +    blockalign = (bitsize / 8) * spec.channels;
    1.82 +    avgbytes = cvtfreq * blockalign;
    1.83 +
    1.84 +    SDL_WriteLE32(io, 0x46464952);  /* RIFF */
    1.85 +    SDL_WriteLE32(io, len * cvt.len_mult + 36);
    1.86 +    SDL_WriteLE32(io, 0x45564157);  /* WAVE */
    1.87 +    SDL_WriteLE32(io, 0x20746D66);  /* fmt */
    1.88 +    SDL_WriteLE32(io, 16);          /* chunk size */
    1.89 +    SDL_WriteLE16(io, 1);           /* uncompressed */
    1.90 +    SDL_WriteLE16(io, spec.channels);  /* channels */
    1.91 +    SDL_WriteLE32(io, cvtfreq);  /* sample rate */
    1.92 +    SDL_WriteLE32(io, avgbytes);  /* average bytes per second */
    1.93 +    SDL_WriteLE16(io, blockalign);  /* block align */
    1.94 +    SDL_WriteLE16(io, bitsize);  /* significant bits per sample */
    1.95 +    SDL_WriteLE32(io, 0x61746164);  /* data */
    1.96 +    SDL_WriteLE32(io, len * cvt.len_mult);  /* size */
    1.97 +    SDL_RWwrite(io, cvt.buf, len * cvt.len_mult, 1);
    1.98 +
    1.99 +    if (SDL_RWclose(io) == -1)
   1.100 +    {
   1.101 +        fprintf(stderr, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
   1.102 +        free(cvt.buf);
   1.103 +        SDL_FreeWAV(data);
   1.104 +        SDL_Quit();
   1.105 +        return 8;
   1.106 +    } // if
   1.107 +
   1.108 +    free(cvt.buf);
   1.109 +    SDL_FreeWAV(data);
   1.110 +    SDL_Quit();
   1.111 +    return 0;
   1.112 +} // main
   1.113 +
   1.114 +// end of resample_test.c ...
   1.115 +