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