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