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