test/testresample.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 14 Jan 2009 04:25:32 +0000
changeset 3040 62d4992e5a92
parent 3018 d706d3170d7d
child 5535 96594ac5fd1a
permissions -rw-r--r--
indent
     1 #include <stdio.h>
     2 #include "SDL.h"
     3 
     4 int
     5 main(int argc, char **argv)
     6 {
     7     SDL_AudioSpec spec;
     8     SDL_AudioCVT cvt;
     9     Uint32 len = 0;
    10     Uint8 *data = NULL;
    11     int cvtfreq = 0;
    12     int bitsize = 0;
    13     int blockalign = 0;
    14     int avgbytes = 0;
    15     SDL_RWops *io = NULL;
    16 
    17     if (argc != 4) {
    18         fprintf(stderr, "USAGE: %s in.wav out.wav newfreq\n", argv[0]);
    19         return 1;
    20     }
    21 
    22     cvtfreq = atoi(argv[3]);
    23 
    24     if (SDL_Init(SDL_INIT_AUDIO) == -1) {
    25         fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
    26         return 2;
    27     }
    28 
    29     if (SDL_LoadWAV(argv[1], &spec, &data, &len) == NULL) {
    30         fprintf(stderr, "failed to load %s: %s\n", argv[1], SDL_GetError());
    31         SDL_Quit();
    32         return 3;
    33     }
    34 
    35     if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
    36                           spec.format, spec.channels, cvtfreq) == -1) {
    37         fprintf(stderr, "failed to build CVT: %s\n", SDL_GetError());
    38         SDL_FreeWAV(data);
    39         SDL_Quit();
    40         return 4;
    41     }
    42 
    43     cvt.len = len;
    44     cvt.buf = (Uint8 *) malloc(len * cvt.len_mult);
    45     if (cvt.buf == NULL) {
    46         fprintf(stderr, "Out of memory.\n");
    47         SDL_FreeWAV(data);
    48         SDL_Quit();
    49         return 5;
    50     }
    51     memcpy(cvt.buf, data, len);
    52 
    53     if (SDL_ConvertAudio(&cvt) == -1) {
    54         fprintf(stderr, "Conversion failed: %s\n", SDL_GetError());
    55         free(cvt.buf);
    56         SDL_FreeWAV(data);
    57         SDL_Quit();
    58         return 6;
    59     }
    60 
    61     /* write out a WAV header... */
    62     io = SDL_RWFromFile(argv[2], "wb");
    63     if (io == NULL) {
    64         fprintf(stderr, "fopen('%s') failed: %s\n", argv[2], SDL_GetError());
    65         free(cvt.buf);
    66         SDL_FreeWAV(data);
    67         SDL_Quit();
    68         return 7;
    69     }
    70 
    71     bitsize = SDL_AUDIO_BITSIZE(spec.format);
    72     blockalign = (bitsize / 8) * spec.channels;
    73     avgbytes = cvtfreq * blockalign;
    74 
    75     SDL_WriteLE32(io, 0x46464952);      /* RIFF */
    76     SDL_WriteLE32(io, len * cvt.len_mult + 36);
    77     SDL_WriteLE32(io, 0x45564157);      /* WAVE */
    78     SDL_WriteLE32(io, 0x20746D66);      /* fmt */
    79     SDL_WriteLE32(io, 16);      /* chunk size */
    80     SDL_WriteLE16(io, 1);       /* uncompressed */
    81     SDL_WriteLE16(io, spec.channels);   /* channels */
    82     SDL_WriteLE32(io, cvtfreq); /* sample rate */
    83     SDL_WriteLE32(io, avgbytes);        /* average bytes per second */
    84     SDL_WriteLE16(io, blockalign);      /* block align */
    85     SDL_WriteLE16(io, bitsize); /* significant bits per sample */
    86     SDL_WriteLE32(io, 0x61746164);      /* data */
    87     SDL_WriteLE32(io, cvt.len_cvt);     /* size */
    88     SDL_RWwrite(io, cvt.buf, cvt.len_cvt, 1);
    89 
    90     if (SDL_RWclose(io) == -1) {
    91         fprintf(stderr, "fclose('%s') failed: %s\n", argv[2], SDL_GetError());
    92         free(cvt.buf);
    93         SDL_FreeWAV(data);
    94         SDL_Quit();
    95         return 8;
    96     }                           // if
    97 
    98     free(cvt.buf);
    99     SDL_FreeWAV(data);
   100     SDL_Quit();
   101     return 0;
   102 }                               // main
   103 
   104 // end of resample_test.c ...